⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 source.c

📁 用c语言实现的语法语义分析程序 实现了四元式得生成功能齐全
💻 C
字号:
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <alloc.h>

struct stake1
{ char *s[15];
  int top;
} chstake;/*运算元素栈*/

struct stake2
{ char ch[20];
  int sjb[20];
  int top;
} ysstake;/*运算符号栈*/

struct lgstake1
{ char ch[10];
  int top;
}logfh;/*逻辑运算符号栈*/

struct lgstake2
{ int ysbh[10];
  int top;
}logys;/*逻辑运算元素*/

struct syshi/*四元式队列*/
{ char *s[4];
  int bhao;
  struct syshi *next;
}yshead,*ystail;/*头节点和尾指针*/

int sysnum,bh,logopr;/*四元式个数和临时变量个数*/
int truelink,falselink,tchuko;

void pushys(char *stemp) /*运算元素进栈*/
{ int ln;
  ln=strlen(stemp);
  chstake.s[chstake.top]=(char *)malloc((ln+1)*sizeof(char));
  strcpy(chstake.s[chstake.top],stemp);
  chstake.top++;
}

void pushsf(char ch1,int jb)/*运算符进栈*/
{ ysstake.ch[ysstake.top]=ch1;
  ysstake.sjb[ysstake.top]=jb;
  ysstake.top++;
}

char *popys() /*运算元素出栈*/
{ char *tp;
  chstake.top--;
  tp=chstake.s[chstake.top];
  chstake.s[chstake.top]=NULL;
  return(tp);
}

char popsf() /*运算符出栈*/
{ char ctemp;
  ysstake.top--;
  ctemp=ysstake.ch[ysstake.top];
  return(ctemp);
}

void back(int t1,int t2)
{ struct syshi *temp3;
  int bh3,t0;
  bh3=t1;
  while(bh3!=0)
  { temp3=yshead.next;
    while(temp3->bhao!=bh3) temp3=temp3->next;
    sscanf(temp3->s[0],"%d",&bh3);
    sprintf(temp3->s[0],"%d",t2);
  }
}

void batch()
{ struct syshi *temp1,*temp2,*temp3;
  int bh1,bh2,bh3,i=0;
  char ch1;
  while(i<logfh.top)
  { bh1=logys.ysbh[i];
    temp1=yshead.next;
    while(temp1->bhao!=bh1) temp1=temp1->next;
    ch1=logfh.ch[i];
    if(ch1=='&')
    { bh3=truelink;
      truelink=0;
      back(bh3,bh1+2);
      sprintf(temp1->s[0],"%d",bh1+2);
      temp1=temp1->next;
      sprintf(temp1->s[0],"%d",falselink);
      falselink=bh1+1;
    }
    if(ch1=='|')
    { sprintf(temp1->s[0],"%d",truelink);
      truelink=bh1;
      temp1=temp1->next;
      bh3=falselink;
      falselink=0;
      back(bh3,bh1+2);
      sprintf(temp1->s[0],"%d",bh1+2);
    }
    i++;
  }
  temp2=yshead.next;
  bh2=logys.ysbh[logys.top-1];
  while(temp2->bhao!=bh2) temp2=temp2->next;
  sprintf(temp2->s[0],"%d",truelink);truelink=bh2;
  temp2=temp2->next;
  sprintf(temp2->s[0],"%d",falselink);falselink=bh2+1;
  logys.top=0;
}

void logysh()
{ struct syshi *temp;
  temp=ystail;
  temp->next=(struct syshi *)malloc(sizeof(struct syshi));
  temp=temp->next;
  temp->bhao=100+bh;
  logys.ysbh[logys.top++]=100+bh;
  bh++;
  temp->s[0]=(char *)malloc(8*sizeof(char));
  strcpy(temp->s[0],"----");
  temp->s[1]=(char *)malloc(2*sizeof(char));
  strcpy(temp->s[1]," ");
  temp->s[2]=(char *)malloc(8*sizeof(char));
  sprintf(temp->s[2],"if %s goto",popys());
  temp->s[3]=(char *)malloc(2*sizeof(char));
  strcpy(temp->s[3]," ");
  temp->next=(struct syshi *)malloc(sizeof(struct syshi));
  temp=temp->next;
  temp->bhao=100+bh;bh++;
  temp->s[0]=(char *)malloc(8*sizeof(char));
  strcpy(temp->s[0],"----");
  temp->s[1]=(char *)malloc(2*sizeof(char));
  strcpy(temp->s[1]," ");
  temp->s[2]=(char *)malloc(8*sizeof(char));
  strcpy(temp->s[2],"goto");
  temp->s[3]=(char *)malloc(2*sizeof(char));
  strcpy(temp->s[3]," ");
  temp->next=NULL;
  ystail=temp;
}

void yunsuan()/*产生四元式*/
{ struct syshi *temp;
  int i,j;
  char ch,ctmp[2];
  ch=popsf();
  temp=(struct syshi *)malloc(sizeof(struct syshi));
  temp->s[1]=popys();
  temp->s[0]=popys();
  temp->s[2]=(char *)malloc(8*sizeof(char));
  temp->s[2][0]=ch;temp->s[2][1]='\0';
  temp->s[3]=(char *)malloc(8*sizeof(char));
  sprintf(temp->s[3],"t%d",sysnum++);
  temp->bhao=100+bh;
  bh++;
  ystail->next=temp;
  pushys(temp->s[3]);
  temp->next=NULL;
  ystail=temp;
  if(ch=='<'||ch=='>')
  logysh();
}

int fys(char ch) /*判断运算符级别*/
{ int jb;
  switch(ch)
  { case '^': jb=5; break;
    case '*': jb=4; break;
    case '/': jb=4; break;
    case '+': jb=3; break;
    case '-': jb=3; break;
    case '<': jb=2; break;
    case '>': jb=2; break;
    case '=': jb=1; break;
    default: jb=0;
  }
  return(jb);
}

int count;

int cffx(char *s1)
{ int len=0,length,i,judge=1,logic=0;
  int flag=0,sfjb,qt=0;/*qt代表括号嵌套层数*/
  char ch,temp[20];
  length=strlen(s1);
  ch=s1[0];
  if(ch=='-')
  { pushys(" ");
    pushsf('-',3);
    ch=s1[++len];
  }
  while(len<length&&(judge==1))
  { if(isalpha(ch))/*是字母*/
    { if(flag==1) judge=-1;
      else
      { i=0;
        while((isalpha(ch)||isdigit(ch))&&(len<length))
        { temp[i]=ch;
          ch=s1[++len];
          i++;
        }
        temp[i]='\0'; flag=1;
        pushys(temp);
      }
    }/*取表达式中的字符变量*/
    else if(isdigit(ch))/*是数字*/
    { if(flag==1) judge=-1;
      else
      { i=0;
        while(isdigit(ch)&&(len<length))
        { temp[i]=ch;
          ch=s1[++len];
          i++;
        }
        if(ch=='.')
        { temp[i++]=ch;ch=s1[++len];
          while(isdigit(ch)&&(len<length))
          { temp[i]=ch;
            ch=s1[++len];
            i++;
          }
        }/*是小数取小数部分*/
        temp[i]='\0';
        flag=1;
        pushys(temp);
      }
    }/*取表达式中的数字常量*/
    else if(ch=='(')
    { if(flag==1) judge=-1;
      else
      { pushsf('#',-100);/*符号栈进栈一个间隔*/
        i=0; ch=s1[++len];
        while((ch!=')'&&len<length)||qt>0)
        { if(ch==')') qt--;
          if(ch=='(')
          { qt++;
            count++;
          }
          temp[i]=ch;
          ch=s1[++len];
          i++;
        }
        if(ch!=')') judge=-2;/*括号不匹配*/
        else
        { temp[i]='\0';
          judge=cffx(temp);
          ch=s1[++len];
          flag=1;
          popsf();
        }
      }
    }/*处理表达式中的括号 */
    else if(ch=='&'||ch=='|')
    { if(flag==0)
      judge=-3;
      else
      { flag=0;
        logic=1;
        logfh.ch[logfh.top]=ch;
        logfh.top++;
        ch=s1[++len];
        logopr++;
      }
    }
    else
    { if(flag==0) judge=-3;/*运算符相连错误*/
      else
      { flag=0;
        sfjb=fys(ch);
        if(sfjb==2&&logopr>0) logopr--;
        if(sfjb==0) judge=-4;/*出现非法运算符    */
        else
        { while(sfjb<=ysstake.sjb[ysstake.top-1])/*该运算符优先级小 */
          yunsuan();/*运算产生四元式*/
          pushsf(ch,sfjb);
          ch=s1[++len];
        }
      }
    }/*运算符处理 */
  }
  while(judge==1&&(ysstake.top!=1)&&(ysstake.ch[ysstake.top-1]!='#'))
  yunsuan();
  if(logopr!=0&&(count==0))
  { logysh();
    logopr--;
  }
  if(logic&&judge==1)
  { batch();
    printf("FALSE :%4d TRUE :%4d\n",falselink,truelink);
  }
  if(count>0) count--;
  return(judge);
}


void display()
{ struct syshi *temp;
  temp=yshead.next;
  while(temp)
  { printf("%-4d %-6s %-6s %-6s %-6s\n",
    temp->bhao,temp->s[2],temp->s[0],temp->s[1],temp->s[3]);
    temp=temp->next;
  }
}

void htian()
{ struct syshi *temp;
  int tbh,bh1;
  tbh=truelink;
  while(tbh!=0)
  { temp=yshead.next;
    while(temp->bhao!=tbh) temp=temp->next;
    sscanf(temp->s[0],"%d", &tbh);
    sprintf(temp->s[0],"%d",tchuko+100);
  }
  tbh=falselink;
  while(tbh!=0)
  { temp=yshead.next;
    while(temp->bhao!=tbh) temp=temp->next;
    sscanf(temp->s[0],"%d", &tbh);
    sprintf(temp->s[0],"%d",bh+100);
   }
}

void inigtial()
{ int i;
  sysnum=0;bh=0;
  truelink=0;falselink=0;
  chstake.top=0;
  for(i=0;i<15;i++)
    chstake.s[i]=NULL;
  ysstake.top=1;
  ysstake.sjb[0]=0;
  for(i=0;i<3;i++)
    yshead.s[i]=NULL;
  yshead.next=NULL;
  ystail=&yshead;
  logfh.top=0;
  logys.top=0;
  logopr=0;
  count=0;
}

void main(){
 int i,test,control;
 char ss[20],ss2[20];
 clrscr();
 printf("CHOOSE (0)EXIT (1)TRANSLATE:");
 scanf("%d",&control);
 while(control){
   inigtial();
   printf("Input The Arithmetic Formula:");
   scanf("%s",ss);
   if(ss[0]=='i'&&ss[1]=='f'&&ss[2]=='('){
      count--;
      test=cffx(&ss[2]);
      tchuko=bh;
      scanf("%s",ss2);
      test=cffx(ss2);
   }
   else{
      test=cffx(ss);
   }
   if(test>0){
      if(logys.top!=0){
         batch();
         printf("FALSE :%4d TRUE :%4d\n",falselink,truelink);
      }
      display();
      if(truelink!=0&&ss[0]=='i'){
         printf("*****press any key to backfill*****\n");
         getch();
         htian();
         display();
      }
   }
   else{
      printf("error expression!!");
      switch(test){
         case -1: printf("error const or var!");break;
         case -2: printf("error blacket!");break;
         case -3: printf("error functor!");break;
         case -4: printf("error char!");break;
      }
   }
   getch();
   clrscr();
   printf("CHOOSE (0)EXIT (1)TRANSLATE");
   scanf("%d",&control);
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -