编译原理第一次作业

本文深入探讨了AXL分析器的核心组件,包括错误标识符、中间表达式、YYLVAL结构以及表达式的解析过程。通过详细解释如何使用Lex库进行表达式输入、解析与优化,为开发者提供了一套高效处理复杂表达式的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 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();		
	}
	else
		advance();
	return temp;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值