⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 y.tab.c

📁 简单实现C--语言的编译器
💻 C
📖 第 1 页 / 共 5 页
字号:
	}
	}
break;
case 9:
#line 366 "c--.y"
{  
	struct table * tp = table_stack[table_len-1];
	symbol *p;
	int width;

	trace(" var_declar =>  type_spec * ID ;\n");  
	if((p = lookup(yyvsp[-1]._ident,tp))!=0){      /*had exist in this block*/
		fprintf(errfile,"line %d : %s define repeatedly\n",nline,yyvsp[-1]._ident); 
		is_error++; 
		return 0;
	}
	else{
		p = insert(yyvsp[-1]._ident,tp);
          	width = 0;    /*类型长度*/
	   	p->isargv = 0;
	   	p->offset = offset_stack[offset_len-1];
	   	if(yyvsp[-3]._value == INT){
		   	p->type = INT_POINT;
		   	width = 4;
	   	}
          	else if(yyvsp[-3]._value == CHAR){
		   	p->type = CHAR_POINT;
		   	width = 4;
	   	}
	   	else if(yyvsp[-3]._value == VOID){
		   	p->type = VOID_POINT;
		   	width = 4;
          	}
         	offset_stack[offset_len-1] += width;
	}
	}
break;
case 10:
#line 398 "c--.y"
{  
	struct table *tp = table_stack[table_len-1];
	symbol *p;
	int width;

	trace("var_declar =>  type_spec * ID [ NUM ] ;\n");  
	if((lookup(yyvsp[-4]._ident,tp))!=0){      /*had exist in this block*/
		fprintf(errfile,"line %d : %s define repeatedly\n",nline,yyvsp[-4]._ident); 
		is_error++; 
		return 0 ;
       }
	else{
	    	p = insert(yyvsp[-4]._ident,tp);
          	width = 0;    /*类型长度*/
	   	p->isargv = 0;
	   	p->offset = offset_stack[offset_len-1];
	   	if(yyvsp[-6]._value == INT){
			p->type = INT_POINT_ARRAY;
		   	width = 4*atoi(yyvsp[-2]._ident);
	   	}
          	else if(yyvsp[-6]._value == CHAR){
		   	p->type = CHAR_POINT_ARRAY;
		   	width = 4*atoi(yyvsp[-2]._ident);
	   	}
          	offset_stack[offset_len-1] += width;
	}
	}
break;
case 11:
#line 427 "c--.y"
{  yyval._value = INT;   }
break;
case 12:
#line 429 "c--.y"
{  yyval._value = VOID;  }
break;
case 13:
#line 431 "c--.y"
{  yyval._value = CHAR;  }
break;
case 14:
#line 433 "c--.y"
{
	fprintf(errfile,"type name expected\n"); 
	is_error++; 
	exit(0);
	}
break;
case 15:
#line 441 "c--.y"
{  
	paramtp *i,*j;    /*用于检查参数是否一致 */
	int t;

	t=is_C(yyvsp[-5]._sym->name);
	if(t==1)	fprintf(yyout, "_%s	proc	near\n", yyvsp[-5]._sym->name);  
	else	fprintf(yyout, "%s	proc	near\n", yyvsp[-5]._sym->name);  
	fprintf(yyout, "	push      ebp\n");
	fprintf(yyout, "	mov       ebp,esp\n");
       yyvsp[-5]._sym->offset = offset_stack[offset_len-1]; 
	fprintf(yyout, "	sub	  esp, %d\n", yyvsp[-5]._sym->offset);
	fprintf(yyout, "%s\n", yyvsp[0]._expr.code);    /* 所有语句的代码*/
	/*fprintf(yyout, "	.....\n");*/
	fprintf(yyout,"	mov	esp,ebp\n");
	fprintf(yyout,"	pop	ebp\n");
	fprintf(yyout, "	ret\n");
	if(t==1)	fprintf(yyout, "_%s	endp\n\n", yyvsp[-5]._sym->name);  
	else	fprintf(yyout, "%s	endp\n\n", yyvsp[-5]._sym->name);  

	/* 函数返回类型检查 */
	if(is_funret && yyvsp[-6]._value == VOID){ 
		fprintf(errfile,"line %d: function ret type is unreasonable\n",nline);
		is_error++;
		return 0;	
	}
	else if (!is_funret && yyvsp[-6]._value != VOID){
		fprintf(errfile,"line %d: function ret type is unreasonable\n",nline);
		is_error++;	
		return 0;
	}
	else{
		if(yyvsp[0]._expr.is_const){
			yyvsp[-5]._sym->is_fun_ret=1;
			yyvsp[-5]._sym->ret_const=1;
			yyvsp[-5]._sym->retvalue = yyvsp[0]._expr.value;
		}
		else{
			yyvsp[-5]._sym->is_fun_ret=1;
			yyvsp[-5]._sym->ret_const=0;
		}
	}	

	/* 函数参数检查 */
	if(yyvsp[-5]._sym != NULL){
		if(yyvsp[-5]._sym->type == FUN_DECLAR){  /*函数声明*/
		   /*检验类型,参数及其类型是否一致*/
                      for(i = yyvsp[-5]._sym->paramer,j = yyvsp[-2]._param;i!=NULL &&j!=NULL;i=i->next,j=j->next){
				if(i->type!=j->type){
					/*fprintf(yyout,"in line %d : the type of function argvments is unexpected!\n",nline);*/
					break;
				}
			 }
			 if(i!=NULL || j!=NULL){
				fprintf(errfile,"line %d : the type of function argvments is unexpected!\n",nline);
				is_error++; 
				yyvsp[-5]._sym->paramer = yyvsp[-2]._param;  
			 }
		        if(yyvsp[-5]._sym->ret != yyvsp[-6]._value){  /*检验返回值类型*/
				fprintf(errfile,"line %d : the return type of function argvments is unexpected!",nline);
				is_error++; 
				yyvsp[-5]._sym->ret = yyvsp[-6]._value;
			}
	       }
		else{   /* 新定义的函数*/
			yyvsp[-5]._sym->type = FUN_DEFINE;  /*函数定义类型*/
		  	yyvsp[-5]._sym->paramer = yyvsp[-2]._param;
		 	yyvsp[-5]._sym->ret = yyvsp[-6]._value;

		}
	}

	offset_len--;
       table_len--;
       level--;

      }
break;
case 16:
#line 518 "c--.y"
{  
	int t;
	t = is_C(yyvsp[-5]._sym->name);
	if(t == 1)	fprintf(yyout, "extrn	_%s:near\n\n", yyvsp[-5]._sym->name);  
	else	fprintf(yyout, "extrn	%s:near\n\n", yyvsp[-5]._sym->name); 

	if(yyvsp[-5]._sym != NULL){
		if(yyvsp[-5]._sym->type == FUN_DECLAR){ 
                 	fprintf(errfile,"line %d :warning function %s had been defined!",nline,yyvsp[-5]._sym);
              }
		else{
			yyvsp[-5]._sym->type = FUN_DECLAR;
			yyvsp[-5]._sym->paramer = yyvsp[-2]._param;
			yyvsp[-5]._sym->ret = yyvsp[-6]._value;
		}

	}
	offset_len--;
       table_len--;
       level--;

	}
break;
case 17:
#line 543 "c--.y"
{	
	level++;      /* 在后面调用insert时,可能需要建立符号表(见insert)*/
	insert(NULL, table_stack[table_len-1]);    /*防止出现表未建时,进行lookup变量  INPORTANCE ERROR*/
	}
break;
case 18:
#line 549 "c--.y"
{  
	struct table *tp = table_stack[table_len-1];
       symbol *ttp;

	trace("fun_tag => callconvent  ID\n");  
	if((ttp = lookup_all(yyvsp[0]._ident,tp))!=0){   /*已经在符号表出现(声明)*/
		if(ttp->type == FUN_DECLAR){  /*函数声明*/
			yyval._sym = ttp;
		}
		else if(ttp->type == FUN_DEFINE){  /*先定义*/
			fprintf(errfile,"line %d : function %s define repeatedly\n",nline,yyvsp[0]._ident);
			is_error++;
			yyval._sym = NULL;
		}
		else{  /* 函数名可能和一变量名相同*/
			fprintf(errfile,"line %d : warning the function argvment(s) is expected\n");
			yyval._sym = NULL;
		}
	}
	else{	
		ttp = insert(yyvsp[0]._ident,tp);   /*插入*/
		yyval._sym = ttp;
	}
	}
break;
case 19:
#line 574 "c--.y"
{  
	struct table *tp = table_stack[table_len-1];
       symbol *ttp;

	trace("fun_tag => callconvent  ID\n");  
	if((ttp = lookup_all(yyvsp[0]._ident,tp))!=0){   /*已经在符号表出现(声明)*/
		if(ttp->type == FUN_DECLAR){  /*函数声明*/
			yyval._sym = ttp;
		}
		else if(ttp->type == FUN_DEFINE){  /*先定义*/
			fprintf(errfile,"line %d : function %s define repeatedly\n",nline,yyvsp[0]._ident);
			is_error++;
			yyval._sym = NULL;
		}
		else{  /* 函数名可能和一变量名相同*/
			fprintf(errfile,"line %d : warning the function argvment(s) is expected\n");
			yyval._sym = NULL;
		}
	}
	else{	
		ttp = insert(yyvsp[0]._ident,tp);   /*插入*/
		yyval._sym = ttp;
	}
	}
break;
case 20:
#line 600 "c--.y"
{  
	   trace("params => param_list \n");  
          offset_stack[offset_len-1] = 0; 
	   yyval._param = yyvsp[0]._param;
         }
break;
case 21:
#line 606 "c--.y"
{  yyval._param = 0;  }
break;
case 22:
#line 609 "c--.y"
{  trace("param_list => param_list , param \n");  
	    yyval._param = yyvsp[0]._param;
	    yyval._param->next = yyvsp[-2]._param;   /*c-- 函数参数从右到左顺序压栈*/
	}
break;
case 23:
#line 614 "c--.y"
{  trace("param_list => param\n");  
	   yyval._param = yyvsp[0]._param; 
	}
break;
case 24:
#line 619 "c--.y"
{
	struct table * tp = table_stack[table_len-1];  /*当前层*/
	symbol *p;
	int width;
	paramtp *i;

	trace("param => type_spec ID\n");  
 	if(lookup(yyvsp[0]._ident,tp)!=0){  
		fprintf(errfile,"line %d : %s had been defined!",nline,yyvsp[0]._ident);
		is_error++; 
		return 0;
	}
	else{
		p = insert(yyvsp[0]._ident,tp); /*可能在此时创建一个新的符号表*/
		width = 0;    /*类型长度*/
		p->isargv = 1;
	       p->offset = offset_stack[offset_len-1];
		p->type = yyvsp[-1]._value;
		if(yyvsp[-1]._value == INT){
			width = 4;
		}
 	       else if(yyvsp[-1]._value == CHAR){
			width = 1;
              }
		offset_stack[offset_len-1] += width;

              i = (paramtp *)malloc(sizeof(paramtp)); /*插入参数表*/
		yyval._param = i; 
		yyval._param->next = NULL;                       
     		yyval._param->type = p->type ;
          
	}
	}
break;
case 25:
#line 653 "c--.y"
{  
	struct table *tp = table_stack[table_len-1];  /*当前层*/
	symbol *p;
	int width;
	paramtp *i;

	trace("param => type_spec * ID \n");  
	if(lookup(yyvsp[0]._ident,tp)!=0){
		fprintf(errfile,"line %d : %s had been defined!",nline,yyvsp[0]._ident);
		is_error++; 
		return 0;
	}
	else{
		p = insert(yyvsp[0]._ident,tp);
		width = 0;    /*类型长度*/
	       p->isargv = 1;
	       p->offset = offset_stack[offset_len-1];
		if(yyvsp[-2]._value == INT){
			width = 4;
			p->type = INT_POINT;
		}
 	       else if(yyvsp[-2]._value == CHAR){
			width = 4;
			p->type = CHAR_POINT;
              }
		else if(yyvsp[-2]._value == VOID){
			width = 4;
			p->type = VOID_POINT;
		}
		offset_stack[offset_len-1] += width;

              i = (paramtp *)malloc(sizeof(paramtp)); /*插入参数表*/
		yyval._param = i;
		yyval._param->type = p->type;                       
     		yyval._param->next = NULL;
	}
	}
break;
case 26:
#line 691 "c--.y"
{  
	struct table * tp = table_stack[table_len-1];  /*当前层*/
	symbol *p;
	int width;
       paramtp *i;

	trace("param => type_spec ID [ ]\n");  
	if(lookup(yyvsp[-2]._ident,tp)!=0){
		fprintf(errfile,"line %d : %s had been defined!",nline,yyvsp[-2]._ident);
		is_error++;
		return 0;
	}
	else{
		p = insert(yyvsp[-2]._ident,tp);
		width = 0;    /*类型长度*/
		p->isargv = 1;
	       p->offset = offset_stack[offset_len-1];
		if(yyvsp[-3]._value == INT){
			width = 4;
			p->type = INT_ARRAY;
		}
 	       else if(yyvsp[-3]._value == CHAR){
			width = 4;
			p->type = CHAR_ARRAY;
              }
		offset_stack[offset_len-1] += width;

              i = (paramtp *)malloc(sizeof(paramtp)); /*插入参数表*/
		yyval._param = i;                       
     		yyval._param->next = NULL;
		yyval._param->type = p->type; 
	}	
	}
break;
case 27:
#line 726 "c--.y"
{ 
	is_first++; 
	if(is_first==1) fprintf(yyout,"	.code\n");

	if(is_funret){   /*函数有返回值*/
		if(yyvsp[-1]._expr.is_const){ 
			yyval._expr.is_const = 1;
			yyval._expr.value = yyvsp[-1]._expr.value;
		}
		else{
			yyval._expr.is_const  =0;
		}
	}
	yyval._expr.code = yyvsp[-1]._expr.code;  
	}
break;
case 29:
#line 744 "c--.y"
{  trace(" local_declars => ε\n");  }
break;
case 30:
#line 748 "c--.y"
{   
	
	char  code[1000];

	if(is_funret){   /*函数有返回值*/
		if(yyvsp[0]._expr.is_const){ 
			yyval._expr.is_const = 1;
			yyval._expr.value = yyvsp[0]._expr.value;
		}
		else{
			yyval._expr.is_const  =0;
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -