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

时间:2022-06-01 21:09:20

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

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

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

 statements -> expression SEMI
| expression SEMI statements expression -> PLUS expression expression
| MINUS expression expression
| TIMES expression expression
| DIVISION expression expression
| NUM_OR_ID

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

最后测试效果如下:

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

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

 //retinf.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h> #include "lex.h" char err_id[] = "error";
char * midexp;
extern char * yytext; struct YYLVAL {
int last_op; /* last operation of expression
for elimination of redundant parentheses */ char * val; /* 记录表达式中间临时变量 */
char * expr; /* 记录表达式后缀式 */
}; typedef struct YYLVAL Yylval; Yylval * expression(void); char *newname(void); /* 在name.c中定义 */ extern void freename(char *name); void statements(void) {
Yylval *temp;
printf("Please input an infix expression and ending with \";\"\n");
while (!match(EOI)) { temp = expression(); printf("The Expression Is %s\n", temp->expr);
freename(temp->val); free(temp->expr);
free(temp);
if (match(SEMI)) {
printf("Please input an infix expression and ending with \";\"\n");
advance(); }
else {
fprintf(stderr, "%d: Inserting missing semicolon\n", yylineno);
}
}
} Yylval * expression(void) {
Yylval *tempToReturn;
tempToReturn = (Yylval *)malloc(sizeof(Yylval)); Yylval *temp0, *temp1;
while (match(PLUS) || match(MINUS) || match(TIMES) || match(DIVISION)) { char op = yytext[];
advance();
temp0 = expression(); temp1 = expression(); bool tempToReturnIsPro = op == '*' || op == '/';
bool temp0IsLower = temp0->last_op == PLUS || temp0->last_op == MINUS;
bool temp1IsLower = temp1->last_op == PLUS || temp1->last_op == MINUS; if (tempToReturnIsPro) {
if (temp0IsLower && temp1IsLower) {
tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + );
sprintf(tempToReturn->expr, "( %s ) %c ( %s )",
temp0->expr, op, temp1->expr);
}
else if (temp0IsLower) {
tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + );
sprintf(tempToReturn->expr, "( %s ) %c %s",
temp0->expr, op, temp1->expr);
}
else if (temp1IsLower) {
tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + );
sprintf(tempToReturn->expr, "%s %c ( %s )",
temp0->expr, op, temp1->expr);
}
else {
tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + );
sprintf(tempToReturn->expr, "%s %c %s",
temp0->expr, op, temp1->expr);
}
}
else {
tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + );
sprintf(tempToReturn->expr, "%s %c %s",
temp0->expr, op, temp1->expr);
}
switch (op)
{
case '+':tempToReturn->last_op = PLUS; break;
case '-':tempToReturn->last_op = MINUS; break;
case '*':tempToReturn->last_op = TIMES; break;
case '/':tempToReturn->last_op = DIVISION; break;
default:
break;
} printf(" %s %c= %s\n", temp0->val, op, temp1->val);
freename(temp1->val);
tempToReturn->val = temp0->val;
return tempToReturn; } if (match(NUM_OR_ID)) {
printf(" %s = %0.*s\n", tempToReturn->val = newname(), yyleng, yytext);
tempToReturn->expr = (char*)malloc(yyleng + );
strncpy(tempToReturn->expr, yytext, yyleng);
advance();
tempToReturn->last_op = TIMES;
return tempToReturn;
}
else if (match(SEMI)){
printf("");
return;
}
else {
tempToReturn->val = newname();
advance();
fprintf(stderr, "%d: Number or identifier expected\n", yylineno);
return;
}
}

另:

 /*main.c XL分析器 */

 main()
{
statements();
}
 /*lex.c     XL分析器 */

 #include "lex.h"
#include <stdio.h>
#include <ctype.h> char *yytext = ""; /* 当前词形,注意由于是直接指向
行缓冲区input_buffer,因此不是以'\0'结尾,
因此使用时要小心, 设初值为0, 表示缓冲区为空,
需要重新读行 */
int yyleng = ; /* 词形的长度 */
int yylineno = ; /* 输入的行号 */ lex()
{
static char input_buffer[];
char *current; current = yytext + yyleng; /* 跳过以读过的词形 */ while () { /* 读下一个词形 */
while (!*current) {
/* 如果当前缓冲区已读完,重新从键盘读入新的一行.
并且跳过空格
*/ current = input_buffer;
/* 如果读行有误,返回 EOI */
if (!fgets(input_buffer, , stdin)) {
*current = '\0';
return EOI;
} ++yylineno; while (isspace(*current))
++current;
} for (; *current; ++current) {
/* Get the next token */ yytext = current;
yyleng = ; /* 返回不同的词汇代码 */
switch (*current) {
case ';': return SEMI;
case '+': return PLUS;
case '-': return MINUS;
case '/': return DIVISION;
case '*': return TIMES;
case '(': return LP;
case ')': return RP; case '\n':
case '\t':
case ' ': break; default:
if (!isalnum(*current))
fprintf(stderr, "Ignoring illegal input <%c>\n", *current);
else {
while (isalnum(*current))
++current; yyleng = current - yytext;
return NUM_OR_ID;
} break;
}
}
}
} static int Lookahead = -; /* 向前查看的词汇,设初值为-1
表示第一次调用match函数时
必须要读取一个词汇 */ int match(int token)
{
/* 判断token是否和当前向前查看的词汇相同. */ if (Lookahead == -)
Lookahead = lex(); return token == Lookahead;
} void advance()
{
/* 向前都一个词汇 */
Lookahead = lex();
}
 /* lex.h      XL分析器*/
#define EOI 0 /* end of input */
#define SEMI 1 /* ; */
#define PLUS 2 /* + */
#define TIMES 3 /* * */
#define LP 4 /* ( */
#define RP 5 /* ) */
#define NUM_OR_ID 6 /* decimal number or identifier */
#define MINUS 7
#define DIVISION 8
extern char *yytext; /* in lex.c */
extern int yyleng;
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 complex\n", yylineno);
exit();
} return(*Namep++);
} freename(s)
char *s;
{
if (Namep > Names)
*--Namep = s;
else
fprintf(stderr, "%d: (Internal error) Name stack underflow\n",
yylineno);
}

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

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

 gcc -c lex.c

 gcc -c retinf.c

 gcc -c name.c

 gcc -c main.c

 gcc -o namebalabala lex.o retinf.o name.o main.o
//gcc -o namebalabala *.o ./namebalabala

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

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

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

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

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

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