武汉大学编译原理第一次作业
来源:互联网 发布:python3网络爬虫实战 编辑:程序博客网 时间:2024/06/10 17:26
/** 计科3 halfopen*第一次作业,老师不要求做思考题*问题二:XL语言分析器的结合次序和优先级, 用retsuff.exe对输入表达式: "1 + 2 + 3;", 先进行 1 + 2 的运算, 还是先进行 2 + 3 的运算. 输入"1+2*3;",先算"2*3",还是先算"1+2".答:Please input an infix expression and ending with ";"1+2*3; t0 = 1 t1 = 2 t2 = 3 t1 *= t2 t0 += t1the affix expression is + 1 * 2 3输出结果如上,所以是先算2*3**//* retinf.c AXL分析器 */#include <stdio.h>#include <string.h>#include <stdlib.h>#include "lex.h"char err_id[] = "error";char * midexp;extern char * yytext;struct YYLVAL { char * val; /* 记录表达式中间临时变量 */ char * expr; /* 记录表达式后缀式 */ int last_op; /* last operation of expression for elimination of redundant parentheses */};typedef struct YYLVAL Yylval;Yylval *factor ( void );Yylval *term ( void );Yylval *expression ( void );char *newname( void ); /* 在name.c中定义 */char *getVar(void);extern void freename( char *name );void statements ( void ){ /* statements -> expression SEMI | expression SEMI statements */ Yylval *temp; printf("Please input an affix expression and ending with \";\"\n"); while( !match(EOI) ){ temp = expression(); printf("the infix expression is %s\n", temp -> expr); freename(temp -> val); free(temp -> expr); free(temp); if( match( SEMI ) ){ advance(); printf("Please input an affix expression and ending with \";\"\n"); } else fprintf( stderr, "%d: Inserting missing semicolon\n", yylineno ); }}/**test */void print_var(Yylval *t){char op = yytext[0];printf("expr:%s val:%s last_op:%c\n",t->expr,t->val, op);}Yylval *expression(void){ /* expression -> PLUS expression expression | MINUS expression expression | TIMES expression expression | DIVISION expression expression | NUM_OR_ID */Yylval* temp;temp=(Yylval *) malloc(sizeof(Yylval));if(match(PLUS)||match(MINUS) || match(TIMES)|| match(DIVISION) ){ //for the first 4 cases;temp = term(); }else if(match(NUM_OR_ID)){ //for the last case; temp = factor();}elseadvance();return temp;}Yylval *term(void){Yylval* temp;temp=(Yylval *) malloc(sizeof(Yylval));int type=0;if(match(TIMES)||match(DIVISION)) type = 1;char op=yytext[0]; advance(); Yylval* temp1=expression(); Yylval* temp2=expression(); printf("%s %c = %s\n",temp1->val,op,temp2->val);freename(temp2->val); //release unused register temp->val=temp1->val;//update the register's numberif(type ==0){ temp->last_op=1; temp->expr=(char *)malloc(strlen(temp1->expr)+1+strlen(temp2->expr)); sprintf( temp->expr, "%s %c %s",temp1->expr, op, temp2->expr);}else{temp->last_op=2; if(temp1->last_op==1||temp2->last_op==1){ if(temp1->last_op!=1){ // a*(b+c) temp->expr=(char *)malloc(strlen(temp1->expr)+3+strlen(temp2->expr)); sprintf(temp->expr,"%s %c %c %s %c",temp1->expr,op,'(',temp2->expr,')');}else if(temp2->last_op!=1){ // (a+c)*b temp->expr=(char *)malloc(strlen(temp1->expr)+3+strlen(temp2->expr)); sprintf(temp->expr,"%c %s %c %c %s",'(',temp1->expr,')',op,temp2->expr); }else{//(a+b)*(c+d) temp->expr=(char *)malloc(strlen(temp1->expr)+5+strlen(temp2->expr)); sprintf(temp->expr,"%c %s %c %c %c %s %c",'(',temp1->expr,')',op,'(',temp2->expr,')'); }}else{ //a+b+c temp->expr=(char *)malloc(strlen(temp1->expr)+1+strlen(temp2->expr)); sprintf(temp->expr, "%s %c %s", temp1->expr, op, temp2->expr); }}return temp; }Yylval* factor(void){char *name;char *name1 = newname(); Yylval* temp;temp=(Yylval *) malloc(sizeof(Yylval));name = (char *) malloc(yyleng + 1);strncpy(name, yytext, yyleng);printf("%s = %s\n",name1,name);temp->expr=(char *)malloc(strlen(name));sprintf(temp->expr,"%s",name);temp->last_op=2;temp->val=name1;advance();return temp;}
0 0
- 武汉大学编译原理第一次作业
- 武汉大学编译原理第二次作业
- 武汉大学编译原理第三次作业
- 武汉大学编译原理第五次作业
- 武汉大学编译原理第4次作业
- 编译原理第一次作业
- 编译原理第一次作业
- 数据库原理第一次作业
- 计算机组成原理第一次作业
- 编译原理作业小结
- 编译原理作业有感
- 2013武汉大学图形学大作业
- 武汉大学2013linux大作业
- RFID原理与技术课程第一次作业
- 编译原理作业1,2
- 编译原理作业3,4
- 西瓜的编译原理作业
- 编译原理作业1、2
- 程序员面试海量数据处理
- 徐小明:周三操作策略
- Android解决ListView中使用EditText所遇到的一些冲突
- redis数据类型初探实操
- CocoaPods安装和使用
- 武汉大学编译原理第一次作业
- SpringMVC 找不到注解实例
- mfc中指定文件打不开的问题
- Asp.Net中创建和使用Ado.net
- 执行ant命令时出现java.lang.OutOfMemoryError: Java heap space
- Java基础之反射
- BITS_TO_LONGS宏的作用
- 分治法 时间为logn的求x^n的算法
- NHibernate从入门到精通系列(6)——基本映射