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

📄 y.tab.c

📁 c—语言的句法分析器。读入一个C--语言程序
💻 C
📖 第 1 页 / 共 4 页
字号:
break;
case 54:
#line 667 "c--.y"
{  
	/* printf("here is me %%%%%%%%%%%\n");*/
	  trace("simple_expr => add_expr relop add_expr\n");  
		yyval._expr.type=INT;
	/*	printf("simple_expr type is %d\n",$$.type);*/
		 if ((yyvsp[-2]._expr.type == INT || yyvsp[-2]._expr.type == CHAR) && (yyvsp[0]._expr.type == INT || yyvsp[0]._expr.type == CHAR)) {
	  if(yyvsp[-1]._relop=="<"){
	    if(yyvsp[-2]._expr.value<yyvsp[0]._expr.value){yyval._expr.value=1;}else yyval._expr.value=0;
	  }else if(yyvsp[-1]._relop=="<="){
	    if(yyvsp[-2]._expr.value<=yyvsp[0]._expr.value){yyval._expr.value=1;}else yyval._expr.value=0;
	  }else if(yyvsp[-1]._relop=="=="){
	    if(yyvsp[-2]._expr.value==yyvsp[0]._expr.value){yyval._expr.value=1;}else yyval._expr.value=0;
	  }else if(yyvsp[-1]._relop==">"){
	    if(yyvsp[-2]._expr.value>yyvsp[0]._expr.value){yyval._expr.value=1;}else yyval._expr.value=0;
	  }else if(yyvsp[-1]._relop==">="){
	    if(yyvsp[-2]._expr.value>=yyvsp[0]._expr.value){yyval._expr.value=1;}else yyval._expr.value=0;
	  }
	}
	 if (yyvsp[-2]._expr.type != INT && yyvsp[-2]._expr.type != CHAR) {
               printf("line %3d error: %s :illegal, left operand is not constant or character\n", nline, yyvsp[-1]._relop);
           }
   if (yyvsp[0]._expr.type != INT && yyvsp[0]._expr.type != CHAR) {
              printf("line %3d error: %s :illegal, right operand is not constant or character\n", nline, yyvsp[-1]._relop);
         }
	  }
break;
case 55:
#line 692 "c--.y"
{
       trace("simple_expr => add_expr\n");  
       if(yyvsp[0]._expr.value!=0) yyval._expr.value=0;else yyval._expr.value=1;
       	yyval._expr.type = yyvsp[0]._expr.type;
       /*	printf("simple_expr type is %d\n",$$.type);*/
      }
break;
case 56:
#line 700 "c--.y"
{  
		trace("add_expr  =>  add_expr ADDOP term\n");  
		yyval._expr.type = INT;
           if (yyvsp[-2]._expr.type == INT && yyvsp[0]._expr.type == INT) {
               if (yyvsp[-1]._op == '+') {
                   yyval._expr.value = yyvsp[-2]._expr.value + yyvsp[0]._expr.value;
               } else {
                   yyval._expr.value = yyvsp[-2]._expr.value - yyvsp[0]._expr.value;   
               }
           }
           if (yyvsp[-2]._expr.type != INT) {
               printf("line %3d error: %c :illegal, left operand is not constant\n", nline, yyvsp[-1]._op);
           }
           if (yyvsp[0]._expr.type != INT) {
               printf("line %3d error: %c :illegal, right operand is not constant\n", nline, yyvsp[-1]._op);
           }
		
		
	         if (yyvsp[-2]._expr.is_const && yyvsp[0]._expr.is_const) {  /* constant folding */
               yyval._expr.is_const = 1;
               if (yyvsp[-1]._op == '+') yyval._expr.value = yyvsp[-2]._expr.value + yyvsp[0]._expr.value; else yyval._expr.value = yyvsp[-2]._expr.value - yyvsp[0]._expr.value; 
           } else {
               char code [1000];
               int len;
               char * op = (yyvsp[-1]._op == '+')? "add" : "sub";
               yyval._expr.place = newtemp();
               yyval._expr.is_const = 0;
               /* load the first oprand */
               if (yyvsp[-2]._expr.is_const) sprintf(code, "	mov	eax, %d\n", yyvsp[-2]._expr.value); else
                  sprintf(code, "	mov	eax, [ebp-%d]	;%s\n", yyvsp[-2]._expr.place->offset+4, yyvsp[-2]._expr.place->name);  
               len = strlen(code);
               if (yyvsp[0]._expr.is_const) sprintf(&code[len], "	%s	eax, %d\n", op, yyvsp[0]._expr.value); else
                   sprintf(&code[len], "	%s	eax, %s\n", op, yyvsp[0]._expr.place->name); 
               len = strlen(code);
              sprintf(&code[len], "	mov	[ebp-%d], eax	;%s\n", yyval._expr.place->offset+4, yyval._expr.place->name);  
                yyval._expr.code = (char *)malloc(strlen(code)+1);
                strcpy(yyval._expr.code, code);
               } 
        }
break;
case 57:
#line 740 "c--.y"
{
      yyval._expr.type = yyvsp[0]._expr.type;
   /*   printf("add_expr  => term type is %d\n",$$.type);*/
      	trace("add_expr  => term\n");  
      }
break;
case 58:
#line 747 "c--.y"
{  if (verbose)  fprintf(stderr, "term => term mulop unary_expr   mulop=%c\n", yyvsp[-1]._op);  
	  	trace("term => term mulop unary_expr \n");
	 yyval._expr.type = INT;
           if (yyvsp[-2]._expr.type == INT && yyvsp[0]._expr.type == INT) {
               if (yyvsp[-1]._op == '*') {
                   yyval._expr.value = yyvsp[-2]._expr.value * yyvsp[0]._expr.value;
               } else if (yyvsp[-1]._op == '/') {
                   yyval._expr.value = yyvsp[-2]._expr.value / yyvsp[0]._expr.value;   
               } else {
                   yyval._expr.value = yyvsp[-2]._expr.value % yyvsp[0]._expr.value; 
               }
           }
           if (yyvsp[-2]._expr.type != INT) {
               printf("line %3d error: %c :illegal, left operand is not constant\n", nline, yyvsp[-1]._op);
           }
           if (yyvsp[0]._expr.type != INT) {
               printf("line %3d error: %c :illegal, right operand is not constant\n", nline, yyvsp[-1]._op);
           }
	
	
	if (yyvsp[-2]._expr.is_const && yyvsp[0]._expr.is_const) {  /* constant folding */
	  
               yyval._expr.is_const = 1;
               if (yyvsp[-1]._op == '*') yyval._expr.value = yyvsp[-2]._expr.value * yyvsp[0]._expr.value; else yyval._expr.value = yyvsp[-2]._expr.value / yyvsp[0]._expr.value; /* does not handle '%' yet */
           } else {
               char code [1000];
               int len;
               char * op = (yyvsp[-1]._op == '*')? "mul" : "div";
               yyval._expr.place = newtemp();
               yyval._expr.is_const = 0;
               /* load the first operand */
               if (yyvsp[-2]._expr.is_const) sprintf(code, "	mov	eax, %d\n", yyvsp[-2]._expr.value); else
                  sprintf(code, "	mov	eax, [ebp-%d]	;%s\n", yyvsp[-2]._expr.place->offset+4, yyvsp[-2]._expr.place->name);  
               len = strlen(code);
               if (yyvsp[0]._expr.is_const) sprintf(&code[len], "	%s	eax, %d\n", op, yyvsp[0]._expr.value); else
                   sprintf(&code[len], "	%s	eax, %s\n", op, yyvsp[0]._expr.place->name); 
               len = strlen(code);
               sprintf(&code[len], "	mov	[ebp-%d], eax	;%s\n", yyval._expr.place->offset+4, yyval._expr.place->name);  
                yyval._expr.code = (char *)malloc(strlen(code)+1);
                strcpy(yyval._expr.code, code);
               } 
        }
break;
case 59:
#line 789 "c--.y"
{
      	  yyval._expr.type = yyvsp[0]._expr.type;
      	 /*   printf("term =>unary_expr  type is %d\n",$$.type);*/
      	   	trace("term =>unary_expr \n");
      	}
break;
case 60:
#line 796 "c--.y"
{  trace("unary_expr => unaryop unary_expr\n");
		 yyval._expr.type = INT;
		   if (yyvsp[0]._expr.type == INT) {
               if (yyvsp[-1]._op == '!') {
                   	if(yyvsp[0]._expr.value==0) yyval._expr.value=1;else yyval._expr.value=0;
               } else {
                   yyval._expr.value = -yyvsp[0]._expr.value; 
               }
           }
           if (yyvsp[0]._expr.type != INT) {
               printf("line %3d error: %c :illegal, right operand is not constant\n", nline, yyvsp[-1]._op);
           }
		 
		   
		 if(yyvsp[-1]._op=='!')
		 	if(yyvsp[0]._expr.value==0) yyval._expr.value=1;else yyval._expr.value=0;
		 else if(yyvsp[-1]._op=='-') yyval._expr.value =-yyvsp[0]._expr.value;
		}
break;
case 61:
#line 815 "c--.y"
{
      	 trace("unary_expr =>  factor\n");  
      	yyval._expr.value=yyvsp[0]._expr.value;  
      	yyval._expr.type = yyvsp[0]._expr.type;
      	/*  printf("unary_expr =>factor  type is %d\n",$$.type);*/
      	}
break;
case 62:
#line 823 "c--.y"
{  trace("factor => ( expr )\n"); yyval._expr.value=yyvsp[-1]._expr.value;yyval._expr.type = yyvsp[-1]._expr.type; }
break;
case 63:
#line 825 "c--.y"
{  trace("factor => var\n"); yyval._expr.is_const = 0; yyval._expr.place = yyvsp[0]._sym; yyval._expr.type = yyvsp[0]._sym->type; }
break;
case 64:
#line 827 "c--.y"
{  trace("factor => call\n"); yyval._expr.value=yyvsp[0]._expr.value; yyval._expr.type = yyvsp[0]._expr.type;  /* printf("factor>>call  type is %d\n",$$.type);*/
	}
break;
case 65:
#line 830 "c--.y"
{  trace("factor => NUM\n"); yyval._expr.is_const = 1; yyval._expr.value = atoi(yyvsp[0]._ident);  yyval._expr.type = INT; }
break;
case 66:
#line 831 "c--.y"
{
      trace("factor => STRING \n");yyval._expr.type=CHAR;
      }
break;
case 67:
#line 834 "c--.y"
{
     trace("factor => CHARATER \n"); yyval._expr.type=CHAR;
      }
break;
case 68:
#line 839 "c--.y"
{  
		 symbol *tmp = lookup(yyvsp[-3]._ident, table_stack[table_len-1]);
		 trace("call => ID ( args )\n"); 
		  if(0!=tmp)  { yyval._expr.type=tmp->type;    }
	/*   printf("                                               fun call args is %d\n",$3);*/
	 /*  printf("                                  call >> id  type is %d\n",$$.type);*/
	 
	  
	   if (0 == tmp) {
          printf("line %3d error: %s :undeclared function\n", nline, yyvsp[-3]._ident);             
           }
      else {
      
      		fun_type *p1=tmp->functype,*p2=funtype;int i;
      			 /*printf("args   %d   ===%d \n",$3,tmp->funargc);*/
      	   if(tmp->funargc>yyvsp[-1].value) printf("line %d  Too fewer (%d)parameters in call to fuction of %s \n",nline,tmp->funargc-yyvsp[-1].value,tmp->name);
	         else 	if(tmp->funargc<yyvsp[-1].value) printf("line %d Too much (%d)parameters in call to fuction of %s \n",nline,yyvsp[-1].value-tmp->funargc,tmp->name);  	   	 
	       	 for(i=1;i<=yyvsp[-1].value;i++)
	      		  { 
	      		  	if(p1==0||p2==0) break; 	      		  	 
	      		  	if(p1->type!=p2->type)  printf("line %d Parater %d Type mismatch in call to fuction  of %s \n",nline,i,tmp->name); 
	      		  	   p1=p1->next; p2=p2->next;	      		  	  
	      		  }    
      	}  
             funtype=0; 
	       yyval._expr.code = malloc(1000);
	       sprintf(yyval._expr.code, "	push ...\n	call %s\n", yyvsp[-3]._ident);
	}
break;
case 69:
#line 869 "c--.y"
{  yyval.value=yyvsp[0].value;trace("args => arg_list\n");  }
break;
case 70:
#line 871 "c--.y"
{ yyval.value=0;  trace("args => ε\n");  }
break;
case 71:
#line 874 "c--.y"
{  
		    fun_type *tmp; fun_type *pp;
	      tmp=(fun_type *)malloc(sizeof(fun_type));
	      tmp->type=yyvsp[0]._expr.type;
	      tmp->next=0;
	      if(funtype==0) {
	      	funtype=tmp; 
	      }
	      	else {	      
	      	  	pp=funtype;
	      		  while(pp->next){ 
	      		 	   pp=pp->next;
	      		  	}
	      	  	pp->next=tmp;
	      		}
		
		yyval.value=yyvsp[-2].value + 1; 
		trace("arg_list => arg_list , expr\n"); 
		 }
break;
case 72:
#line 894 "c--.y"
{  
		  fun_type *tmp; fun_type *pp;
	      tmp=(fun_type *)malloc(sizeof(fun_type));
	      tmp->type=yyvsp[0]._expr.type;
	      tmp->next=0;
	      if(funtype==0) {
	      	funtype=tmp; 
	      }
	      	else {	      
	      	  	pp=funtype;
	      		  while(pp->next){ 
	      		 	   pp=pp->next;
	      		  	}
	      	  	pp->next=tmp;
	      		}
		yyval.value=1;trace("arg_list => expr\n"); 
		
		 }
break;
case 73:
#line 914 "c--.y"
{  trace("callconvent =>  __cdecl\n");  }
break;
case 74:
#line 916 "c--.y"
{  trace("callconvent =>  __stdcall\n");  }
break;
case 75:
#line 922 "c--.y"
{  trace("relop => <= \n");  }
break;
case 76:
#line 924 "c--.y"
{  trace("relop  => < \n");  }
break;
case 77:
#line 926 "c--.y"
{  trace("relop  => > \n");  }
break;
case 78:
#line 928 "c--.y"
{  trace("relop  => >= \n");  }
break;
case 79:
#line 930 "c--.y"
{  trace("relop  => == \n");  }
break;
case 80:
#line 932 "c--.y"
{  trace("relop => !=\n");  }
break;
case 81:
#line 935 "c--.y"
{  trace("mulop => * \n"); yyval._op = '*'; }
break;
case 82:
#line 937 "c--.y"
{  trace("mulop => / \n"); yyval._op = '/'; }
break;
case 83:
#line 939 "c--.y"
{  trace("mulop => % \n"); yyval._op = '%';  }
break;
case 84:
#line 942 "c--.y"
{  trace("unaryop => !\n");  yyval._op = '!'; }
break;
case 85:
#line 944 "c--.y"
{  trace("unaryop => -\n");  yyval._op = '-';  }
break;
#line 1521 "y.tab.c"
    }
    yyssp -= yym;
    yystate = *yyssp;
    yyvsp -= yym;
    yym = yylhs[yyn];
    if (yystate == 0 && yym == 0)
    {
#if YYDEBUG
        if (yydebug)
            printf("yydebug: after reduction, shifting from state 0 to\
 state %d\n", YYFINAL);
#endif
        yystate = YYFINAL;
        *++yyssp = YYFINAL;
        *++yyvsp = yyval;
        if (yychar < 0)
        {
            if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
            if (yydebug)
            {
                yys = 0;
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
                if (!yys) yys = "illegal-symbol";
                printf("yydebug: state %d, reading %d (%s)\n",
                        YYFINAL, yychar, yys);
            }
#endif
        }
        if (yychar == 0) goto yyaccept;
        goto yyloop;
    }
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
        yystate = yytable[yyn];
    else
        yystate = yydgoto[yym];
#if YYDEBUG
    if (yydebug)
        printf("yydebug: after reduction, shifting from state %d \
to state %d\n", *yyssp, yystate);
#endif
    if (yyssp >= yyss + yystacksize - 1)
    {
        goto yyoverflow;
    }
    *++yyssp = yystate;
    *++yyvsp = yyval;
    goto yyloop;
yyoverflow:
    yyerror("yacc stack overflow");
yyabort:
    return (1);
yyaccept:
    return (0);
}

⌨️ 快捷键说明

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