编译原理第一次作业

来源:互联网 发布:淘宝里怎么店铺收藏 编辑:程序博客网 时间: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
原创粉丝点击