c/c++语言开发共享编译原理作业(第一次)-完成retinf.c(阉割版)

首先,作业要求概括如下: 根据前缀表达式文法,实现statements() 和expression() 两个函数。 并且要求使得语义分析在完成分析前缀表达式并输出中间代码的同时,也能够将前缀表达式翻译为中缀表达式, 且要求翻译后的中缀表达式中尽可能少用括号。 举例如下: 输入"+ a * b c;" …

首先,作业要求概括如下:

根据前缀表达式文法,实现statements() 和expression() 两个函数。

并且要求使得语义分析在完成分析前缀表达式并输出中间代码的同时,也能够将前缀表达式翻译为中缀表达式, 且要求翻译后的中缀表达式中尽可能少用括号

 

1 statements -> expression semi  2                 |  expression semi statements  3      4 expression -> plus expression expression  5                |  minus expression expression  6                |  times expression expression  7                |  division expression expression  8                |  num_or_id    

 

举例如下: 输入”+ a * b c;“时,应输出中缀式为” a + b * c“, 而不是”a + (b * c)“或”(a) + (b * c)“等。

最后测试效果如下:

 

编译原理作业(第一次)-完成retinf.c(阉割版)

 

其中未实现河流命名算法故为阉割版。为方便读者阅读并理解后自行更改,这里只提供初版(low版),其代码如下(ddl后会更新):

 

  1 //retinf.c    2 #include <stdio.h>    3 #include <string.h>    4 #include <stdlib.h>    5 #include <stdbool.h>    6     7 #include "lex.h"    8     9 char err_id[] = "error";   10 char * midexp;   11 extern char * yytext;   12    13 struct yylval {   14     int last_op;  /* last operation of expression   15          for elimination of redundant parentheses */   16    17     char * val;  /* 记录表达式中间临时变量 */   18     char * expr; /* 记录表达式后缀式 */   19 };   20    21 typedef struct yylval yylval;   22    23 yylval * expression(void);   24    25 char *newname(void); /* 在name.c中定义 */   26    27 extern void freename(char *name);   28    29 void statements(void) {   30     yylval *temp;   31     printf("please input an infix expression and ending with ";"n");   32     while (!match(eoi)) {   33    34         temp = expression();   35    36         printf("the expression is %sn", temp->expr);   37         freename(temp->val);   38    39         free(temp->expr);   40         free(temp);   41         if (match(semi)) {   42             printf("please input an infix expression and ending with ";"n");   43             advance();   44    45         }   46         else {   47             fprintf(stderr, "%d: inserting missing semicolonn", yylineno);   48         }   49     }   50 }   51    52 yylval * expression(void) {   53     yylval *temptoreturn;   54     temptoreturn = (yylval *)malloc(sizeof(yylval));   55    56     yylval *temp0, *temp1;   57     while (match(plus) || match(minus) || match(times) || match(division)) {   58    59         char op = yytext[0];   60         advance();   61         temp0 = expression();   62    63         temp1 = expression();   64            65         bool temptoreturnispro = op == '*' || op == '/';   66         bool temp0islower = temp0->last_op == plus || temp0->last_op == minus;   67         bool temp1islower = temp1->last_op == plus || temp1->last_op == minus;   68    69    70         if (temptoreturnispro) {   71             if (temp0islower && temp1islower) {   72                 temptoreturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 12);   73                 sprintf(temptoreturn->expr, "( %s ) %c ( %s )",    74                     temp0->expr, op, temp1->expr);   75             }   76             else if (temp0islower) {   77                 temptoreturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 8);   78                 sprintf(temptoreturn->expr, "( %s ) %c %s",   79                     temp0->expr, op, temp1->expr);   80             }   81             else if (temp1islower) {   82                 temptoreturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 8);   83                 sprintf(temptoreturn->expr, "%s %c ( %s )",   84                     temp0->expr, op, temp1->expr);   85             }   86             else {   87                 temptoreturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 4);   88                 sprintf(temptoreturn->expr,  "%s %c %s",   89                     temp0->expr, op, temp1->expr);   90             }   91         }   92         else {   93             temptoreturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 4);   94             sprintf(temptoreturn->expr, "%s %c %s",   95                 temp0->expr, op, temp1->expr);   96         }   97         switch (op)   98         {   99         case '+':temptoreturn->last_op = plus; break;  100         case '-':temptoreturn->last_op = minus; break;  101         case '*':temptoreturn->last_op = times; break;  102         case '/':temptoreturn->last_op = division; break;  103         default:  104             break;  105         }  106   107         printf("    %s %c= %sn", temp0->val, op, temp1->val);  108         freename(temp1->val);  109         temptoreturn->val = temp0->val;  110         return temptoreturn;  111   112     }  113   114     if (match(num_or_id)) {  115         printf("    %s = %0.*sn", temptoreturn->val = newname(), yyleng, yytext);  116         temptoreturn->expr = (char*)malloc(yyleng + 1);  117         strncpy(temptoreturn->expr, yytext, yyleng);  118         advance();  119         temptoreturn->last_op = times;  120         return temptoreturn;  121     }  122     else if (match(semi)){  123         printf("111");  124         return;      125     }  126     else {  127         temptoreturn->val = newname();  128         advance();  129         fprintf(stderr, "%d: number or identifier expectedn", yylineno);  130         return;  131     }  132 }

 

另:

 

1 /*main.c xl分析器 */  2   3 main()  4 {  5     statements();  6 }

 

 1 /*lex.c     xl分析器 */   2    3    4 #include "lex.h"   5 #include <stdio.h>   6 #include <ctype.h>   7    8 char       *yytext = "";  /* 当前词形,注意由于是直接指向   9                    行缓冲区input_buffer,因此不是以''结尾,  10                    因此使用时要小心, 设初值为0, 表示缓冲区为空,  11                    需要重新读行 */  12 int        yyleng = 0;   /* 词形的长度     */  13 int        yylineno = 0;   /* 输入的行号    */  14   15 lex()  16 {  17     static char input_buffer[128];  18     char        *current;  19   20     current = yytext + yyleng;      /* 跳过以读过的词形 */  21   22     while (1) {                  /* 读下一个词形     */  23         while (!*current) {  24             /* 如果当前缓冲区已读完,重新从键盘读入新的一行.  25            并且跳过空格  26             */  27   28             current = input_buffer;  29             /* 如果读行有误,返回 eoi */  30             if (!fgets(input_buffer, 127, stdin)) {  31                 *current = '';  32                 return eoi;  33             }  34   35             ++yylineno;  36   37             while (isspace(*current))  38                 ++current;  39         }  40   41         for (; *current; ++current) {  42             /* get the next token */  43   44             yytext = current;  45             yyleng = 1;  46   47             /* 返回不同的词汇代码 */  48             switch (*current) {  49             case ';': return semi;  50             case '+': return plus;  51             case '-': return minus;  52             case '/': return division;  53             case '*': return times;  54             case '(': return lp;  55             case ')': return rp;  56   57             case 'n':  58             case 't':  59             case ' ': break;  60   61             default:  62                 if (!isalnum(*current))  63                     fprintf(stderr, "ignoring illegal input <%c>n", *current);  64                 else {  65                     while (isalnum(*current))  66                         ++current;  67   68                     yyleng = current - yytext;  69                     return num_or_id;  70                 }  71   72                 break;  73             }  74         }  75     }  76 }  77   78 static int lookahead = -1;      /* 向前查看的词汇,设初值为-1  79                    表示第一次调用match函数时  80                    必须要读取一个词汇 */  81   82 int match(int token)  83 {  84     /* 判断token是否和当前向前查看的词汇相同. */  85   86     if (lookahead == -1)  87         lookahead = lex();  88   89     return token == lookahead;  90 }  91   92 void advance()  93 {  94     /* 向前都一个词汇 */  95     lookahead = lex();  96 }

 

 1 /* lex.h      xl分析器*/   2 #define eoi           0        /*  end of input                */   3 #define semi          1        /*       ;                      */   4 #define plus          2        /*       +                      */   5 #define times         3        /*       *                      */   6 #define lp            4        /*       (                      */   7 #define rp            5        /*       )                      */   8 #define num_or_id     6        /* decimal number or identifier */   9 #define minus         7  10 #define division      8  11 extern char *yytext;        /* in lex.c                     */  12 extern int  yyleng;  13 extern int  yylineno;

 

/*name.c xl分析器 */    #include <stdio.h>    char  *names[] = { "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",             "t8", "t9", "t10", "t11", "t12", "t13", "t14", "t15" };    char  **namep = names;    extern int yylineno;    char  *newname()  {      if (namep >= &names[sizeof(names) / sizeof(*names)]) {          fprintf(stderr, "%d: expression too complexn", yylineno);          exit(1);      }        return(*namep++);  }    freename(s)  char    *s;  {      if (namep > names)          *--namep = s;      else          fprintf(stderr, "%d: (internal error) name stack underflown",              yylineno);  }

 

具体生成方法不一,使用makefile方式最好。

这里只提供基本的gcc 方式(部分linux中没有自带,自行安装)。

 

 1 gcc -c lex.c   2    3 gcc -c retinf.c   4    5 gcc -c name.c   6    7 gcc -c main.c   8    9 gcc -o namebalabala lex.o retinf.o name.o main.o  10 //gcc -o namebalabala *.o  11   12 ./namebalabala

 

注意,如果非要在win下vs中直接运行此程序的话,你会发现:

编译原理作业(第一次)-完成retinf.c(阉割版)

 

编译原理作业(第一次)-完成retinf.c(阉割版)

 

很正常,这是因为执行的标准不一样。为了防止溢出,微软要求用sprintf_s()strncpy_s() 函数(其中_s代表safe)代替sprintf()strncpy()

也就多了一个目标串长度的参数,百度一下就好了。

 

但实际过程中应该还是会有一些问题的,特别是地址方面的。这个时候就自己debug吧~

 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/604919.html

(0)
上一篇 2021年5月13日
下一篇 2021年5月13日

精彩推荐