语法制导翻译.cpp,

  1.   //  语法制导翻译器设计示范程序         
  2.           //           -----  算数表达式四元式翻译(递归子程序法)   
  3.           //  要求:1. 读懂该程序,并上机调试成功;   
  4.           //        2. 运行该程序,输入源表达式(字母:表示变量,数字:表示常数。)   
  5.           //        3. 反复运行,考查输出的各种四元式的正确性。   
  6.           //        4. 实验报告内容:表达式的属性翻译文法;递归子程序框图;   
  7.           //                         运行结果的记录(输出的四元式不得少于10条!)   
  8.    
  9. #include "string.h"   
  10. #include "stdio.h"   
  11.    
  12. char w;                                         //当前单词(w)               
  13. int j=1;                                        //临时变量序号   
  14.      
  15. struct TOKEN                                    //单词的 TOKEN 结构    
  16. {      
  17.     char t;    
  18.      int i;   
  19. } ;                                      
  20. struct TOKEN word, sem[10];                     //结构单词(word),语义栈(sem)   
  21.    int i_sem;   
  22.    
  23. struct QT                                       //四元式结构   
  24. {    
  25.     char w;   
  26.     struct TOKEN word1;    
  27.     struct TOKEN word2;    
  28.     struct TOKEN temp;    
  29. };   
  30.    
  31. char exp[50];                                   //原算术表达式区    
  32.  int i=0;                                            //原单词序号    
  33.    
  34. struct QT qt[30];                               //四元式区   
  35.  int q=0;                                            //四元式序号   
  36.    
  37. int E();   
  38. int T();   
  39. int F();   
  40.    
  41. void next();                                     //读下一个单词   
  42. void newt();                                     //申请一个临时变量函数   
  43. void quat(char);                                 //生成四元式函数   
  44.    
  45. int main()                                       //主函数   
  46. {   
  47.     printf("please input your expression:    ");   
  48.     scanf("    %s",exp);                         // 输入表达式   
  49.     next();                                      // next(w)                                        
  50.     E();         
  51.     if (w=='\0')   
  52.     {   
  53.         printf("\n");   
  54.         for (i=0;i<q;i++)                        //输出四元式序列   
  55.         {   
  56.             printf("  (%d) ",i+1);   
  57.             printf(" ( %c",qt[i].w);   
  58.             printf(" , %c.%d",qt[i].word1.t,qt[i].word1.i);   
  59.             printf(" , %c.%d",qt[i].word2.t,qt[i].word2.i);   
  60.             printf(" , %c.%d )\n",qt[i].temp.t,qt[i].temp.i);   
  61.         }   
  62.     }   
  63.     else    printf("err");   
  64.     printf("\nHello World!\n");   
  65.     return 0;   
  66. }   
  67. int E()   
  68. {      
  69.     char w1;                                      //算符(+\-)暂存变量      
  70.     T();   
  71.     while ( w=='+' || w=='-')   
  72.     {   
  73.         w1=w;                                     //暂存运算符(+|-)    
  74.         next();                                   //next(w)   
  75.         T();   
  76.         quat(w1);   
  77.     }   
  78.     return 1;   
  79. }   
  80. int T()   
  81. {      
  82.     char w2;                                      //算符(*|/)暂存变量      
  83.     F();   
  84.     while ( w=='*' || w=='/')   
  85.     {   
  86.         w2=w;                                     //暂存运算符(*|/)   
  87.         next();                                   //next(w)   
  88.         F();   
  89.         quat(w2);   
  90.     }   
  91.     return 1;   
  92. }   
  93. int F()   
  94. {      
  95.     if ( w=='(')   
  96.     {   
  97.         next();                                          //next(w)   
  98.         E();   
  99.         if ( w!=')')   
  100.         {   
  101.             printf("err");   
  102.             return 0;   
  103.         }   
  104.     }   
  105.     else if ((w>='a' && w<='z')||(w>='0' && w<='9'))   
  106.             {   
  107.                 word.t=w; word.i=0 ;   
  108.                 sem[++i_sem]=word;                       //push(SEM,w)   
  109.             }   
  110.          else    
  111.             {   
  112.                 printf("err");   
  113.                 return 0;   
  114.             }   
  115.     next();                                              //next(w)   
  116.     return 1;   
  117. }   
  118.    
  119. void next()    
  120. {    
  121.     w=exp[i];    
  122.     i++;   
  123.  }   
  124.    
  125. void newt()   
  126.  {   
  127.     word.t='t';   
  128.     word.i=j++;   
  129.  }   
  130.    
  131. void quat(char ww)   
  132. {   
  133.     newt();   
  134.     qt[q].w=ww;                          //QT[q]:=(ww,SEM[s-1],SEM[s],temp);   
  135.     qt[q].word1=sem[i_sem-1];   
  136.     qt[q].word2=sem[i_sem];   
  137.     qt[q].temp=word;   
  138.     i_sem--;                              //pop(SEM);   
  139.     i_sem--;                              //pop(SEM);   
  140.     sem[++i_sem]=word;                    //push(SEM,temp);   
  141.     q++;   
  142. }   
  143.    
  144.    
posted @ 2017-05-30 18:19  天涯海角路  阅读(177)  评论(0)    收藏  举报