编译原理第一次作业
来源:互联网 发布:淘宝里怎么店铺收藏 编辑:程序博客网 时间:2024/05/01 01:15
/* 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 *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 infix expression and ending with \";\"\n"); while( !match(EOI) ){ temp = expression(); printf("the affix expression is %s\n", temp -> expr); freename(temp -> val); free(temp -> expr); free(temp); if( match( SEMI ) ){ advance(); printf("Please input an infix expression and ending with \";\"\n"); } else fprintf( stderr, "%d: Inserting missing semicolon\n", yylineno ); }}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(){ /* 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;int type=0;if(match(TIMES)||match(DIVISION)) type = 1; char op=yytext[0]; advance(); Yylval* temp1=expression(); //char *h1=temp->expr; //int t1=temp->last_op; //char* d1=temp->val; Yylval* temp2=expression(); //char *h2=temp->expr; //int t2=temp->last_op; //char* d2=temp->val; printf("%s %c = %s\n",temp1->val,op,temp2->val);//printf("free %s\n",d2);freename(temp2->val); //release unused register//char *name = newname();//printf("new name:%s\n",name); temp->val=temp1->val;//update the register's number //i++;//stat[i]=d2;//release useless name if(type ==0){temp->last_op=1; // set the priority of plus and minus to be 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;// set the priority of times and division to be 2 if(temp1->last_op==1||temp2->last_op==1){ if(temp1->last_op!=1){ 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){ 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{ 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{ temp->expr=(char *)malloc(strlen(temp1->expr)+1+strlen(temp2->expr)); sprintf(temp->expr, "%s %c %s", temp1->expr, op, temp2->expr); }} }else if(match(NUM_OR_ID)){ //for the last case; char *name; name = (char *) malloc(yyleng + 1); strncpy(name, yytext, yyleng);char *name1 = newname(); 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();}elseadvance();return temp;}
0 0
- 编译原理第一次作业
- 编译原理第一次作业
- 武汉大学编译原理第一次作业
- 数据库原理第一次作业
- 计算机组成原理第一次作业
- 编译原理作业小结
- 编译原理作业有感
- RFID原理与技术课程第一次作业
- 武汉大学编译原理第二次作业
- 武汉大学编译原理第三次作业
- 编译原理作业1,2
- 编译原理作业3,4
- 西瓜的编译原理作业
- 编译原理作业1、2
- 【编译原理】第三章作业
- 【编译原理】第四章作业
- 编译原理作业3、4
- 第一次作业
- 海明码学习小结
- EditText的drawableleft跟随其Gravity
- 【Android实战项目】Odoo 邮箱客户端的经历
- Android插件化学习
- [leetcode] 187 Repeated DNA Sequences
- 编译原理第一次作业
- Ubuntu实现网络链接设置
- 关于引入第三方jar包引发的java.lang.NoClassDefFoundError解决
- 活用maven使web.xml可以用maven变量
- Python读取中文txt乱码问题
- POJ 2773 Happy 2006(二分+容斥)
- 《机器学习实战》学习笔记:基于朴素贝叶斯的分类方法
- 行列式的理解
- Android工程导出jar包,同时导出javadoc