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

📄 y.tab.c

📁 c—语言的句法分析器。读入一个C--语言程序
💻 C
📖 第 1 页 / 共 4 页
字号:
           yyvsp[-5]._sym->type=yyvsp[-6].value;
        /*   printf("function type is %d\n",$2->type);*/
	   fprintf(yyout, "	sub	  esp, %d\n", yyvsp[-5]._sym->offset);
	   fprintf(yyout, "%s\n", yyvsp[0]._expr.code);
	   fprintf(yyout, "	.....\n");
	   fprintf(yyout, "	ret\n");
	   fprintf(yyout, "_%s	endp\n\n", yyvsp[-5]._sym->name);  
	   
	    printf("_%s	:\n", yyvsp[-5]._sym->name); 	
	      if(yyvsp[-5]._sym->isdeclar==1) {
	      	 	fun_type *p1=yyvsp[-5]._sym->functype,*p2=funtype;int i;
	      	 	if(yyvsp[-5]._sym->funargc!=yyvsp[-2].value) printf("     Type mismatch(defined more than declartion) in redeclaration of %s\n",yyvsp[-5]._sym->name);      	
	      		 for(i=1;i<=yyvsp[-2].value;i++)
	      		  {  
	      		  	if(p1==0||p2==0) break; 	      		  	 
	      		  	if(p1->type!=p2->type) 
	      		  	 printf("        Parater %d Type mismatch in redeclaration of %s\n",i,yyvsp[-5]._sym->name); 
	      		  	   p1=p1->next; p2=p2->next;	      		  	  
	      		  }   		 
	      		}else {
	      			yyvsp[-5]._sym->funargc=yyvsp[-2].value;
	             yyvsp[-5]._sym->functype=funtype;
	      		/*	printf("%d  = \n",$2->funargc);*/
	      			
	      			}
           offset_len--;
           table_len--;
          /* printf("                                            argc %d\n", $5);*/
           level--;
           funtype=0;
        /* printf("Level -------- %d   %d\n",level, table_len-1);*/
           trace("fun_declar =>  type_spec  fun_tag   '(' LL params   ')'  comp_stmt	\n");
        }
break;
case 17:
#line 356 "c--.y"
{
/* fprintf(yyout, "extrn	_%s:near\n", $2->name);   */
	/*55 */
	trace("fun_declar => type_spec  fun_tag   '(' LL   params   ')'      ';'	\n");
/* printf("ok here\n");*/
/*printf("$5=========  %d\n",$5);*/
	yyvsp[-5]._sym->isdeclar=1;
	yyvsp[-5]._sym->funargc=yyvsp[-2].value;
	yyvsp[-5]._sym->functype=funtype;
	      funtype=0;
	      table_len--;
       /* printf("aideclar %d   far    %d\n", $2->isdeclar,$5);*/
        level--;
       /* printf("end of function declar _%s	\n\n", $2->name); */
		}
break;
case 18:
#line 372 "c--.y"
{
		 	printf("line %d:function %s declaration missing ;\n",nline-1,yyvsp[-4]._sym->name);
		 	}
break;
case 19:
#line 377 "c--.y"
{level++;trace("LL => ;\n"); insert(NULL, table_stack[table_len-1]);
	/* printf("Level -------- %d   %d\n",level, table_len-1); */
	 }
break;
case 20:
#line 381 "c--.y"
{  
			struct table * tp = table_stack[table_len-1];
			symbol *tmp;
			isfunction=1;
			trace("fun_tag => callconvent  ID\n");  
		/*   printf("Before insert %s inserat  level  %d       tp.level  %d       table_len-1 %d\n",$2,level,tp->level,table_len-1);*/
			if((tmp=lookupred(yyvsp[0]._ident,tp))==0){	 
			 symbol * p;
	     yyval._sym = insert(yyvsp[0]._ident, table_stack[table_len-1]);  
	   }else {
	   	yyval._sym=tmp;
	   	if(tmp->type==1){
	        if(tmp->isdeclar==0) printf("line %d :error: %s :fuction redefinition\n", nline, yyvsp[0]._ident);
	         else  { yyval._sym=tmp ;}
	   }else		
	    	printf("line %d :error: %s :redefinition\n", nline, yyvsp[0]._ident);
	  }
	   	isfunction=0;
	}
break;
case 21:
#line 401 "c--.y"
{ 
		struct table * tp = table_stack[table_len-1];
		   symbol *tmp;
			isfunction=1;
		
		  trace("fun_tag =>ID\n");  
	/*   printf("Before insert %s inserat  level  %d       tp.level  %d       table_len-1 %d\n",$1,level,tp->level,table_len-1);*/
			if((tmp=lookupred(yyvsp[0]._ident,tp))==0){	
			 symbol * p;
	     yyval._sym = insert(yyvsp[0]._ident, table_stack[table_len-1]);  
	     }else {
	     		yyval._sym=tmp;
	   	if(tmp->type==1){
	        if(tmp->isdeclar==0)  {printf("line %d :error: %s :fuction redefinition\n", nline, yyvsp[0]._ident);}
	        	 else {yyval._sym=tmp ;}
	   }else		
	    	{printf("line %d :error: %s :redefinition\n", nline, yyvsp[0]._ident);}
	  }
			 isfunction=0; 
		 }
break;
case 22:
#line 423 "c--.y"
{  trace("params => param_list \n");  
        yyval.value = yyvsp[0].value;         
         }
break;
case 23:
#line 427 "c--.y"
{ trace("params => VOID \n");  argccount=0;
	  yyval.value = 0; 
	  }
break;
case 24:
#line 432 "c--.y"
{  trace("param_list => param_list , param \n");  yyval.value = yyvsp[-2].value+yyvsp[0].value;   }
break;
case 25:
#line 434 "c--.y"
{  trace("param_list => param\n");  yyval.value = yyvsp[0].value;  }
break;
case 26:
#line 438 "c--.y"
{  trace("param => type_spec ID\n"); 
	         yyval.value = 1; 
        {
	struct table * tp = table_stack[table_len-1];
  /*33       */
 if(lookupred(yyvsp[0]._ident,tp)==0){
	      symbol * p = insert(yyvsp[0]._ident, tp);
	       int width=0;
	      fun_type *tmp; fun_type *pp;
	      tmp=(fun_type *)malloc(sizeof(fun_type));
	      tmp->type=yyvsp[-1].value;
	      tmp->next=0;
	      if(funtype==0) {
	      	funtype=tmp; 
	      }
	      	else {	      
	      	  	pp=funtype;
	      		  while(pp->next){ 
	      		 	   pp=pp->next;
	      		  	}
	      	  	pp->next=tmp;
	      		}

	      p->type = yyvsp[-1].value;
	      p->offset = offset_stack[offset_len-1]; 
       /* if ($1 == INT) width = 4; else if ($1 == CHAR) width = 1;*/
       /*offset_stack[offset_len-1] -= width;*/
    }else printf("line %d :error %s :redefinition\n", nline, yyvsp[0]._ident);  
    
    }
        yyval.value = 1; /* printf("ok here\n");*/
  }
break;
case 27:
#line 471 "c--.y"
{  trace("param => type_spec * ID \n");
		
		 {
	struct table * tp = table_stack[table_len-1];
	 if(lookupred(yyvsp[0]._ident,tp)==0){
      	symbol * p = insert(yyvsp[0]._ident, tp);
        int width = 0;
            fun_type *tmp; fun_type *pp;
	      tmp=(fun_type *)malloc(sizeof(fun_type));
	      tmp->type=yyvsp[-2].value;
	      tmp->next=0;
	      if(funtype==0) {
	      	funtype=tmp; 
	      }
	      	else {	      
	      	  	pp=funtype;
	      		  while(pp->next){ 
	      		 	   pp=pp->next;
	      		  	}
	      	  	pp->next=tmp;
	      		}
	      p->type = yyvsp[-2].value;
	      p->offset = offset_stack[offset_len-1]; 
        if (yyvsp[-2].value == INT) width = 4; else if (yyvsp[-2].value == CHAR) width = 1;
        offset_stack[offset_len-1] -= width;
        }else   printf("line %d :error: %s :redefinition\n", nline, yyvsp[0]._ident);  

        } yyval.value = 1; 
		  }
break;
case 28:
#line 501 "c--.y"
{  trace("param => type_spec ID [ ]\n"); 
		 {
	struct table * tp = table_stack[table_len-1];
	 if(lookupred(yyvsp[-2]._ident,tp)==0){

       	symbol * p = insert(yyvsp[-2]._ident, tp);
        int width = 0;
            fun_type *tmp; fun_type *pp;
	      tmp=(fun_type *)malloc(sizeof(fun_type));
	      tmp->type=yyvsp[-3].value;
	      tmp->next=0;
	      if(funtype==0) {
	      	funtype=tmp; 
	      }
	      	else {	      
	      	  	pp=funtype;
	      		  while(pp->next){ 
	      		 	   pp=pp->next;
	      		  	}
	      	  	pp->next=tmp;
	      		}
        p->type = yyvsp[-3].value;
	      p->offset = offset_stack[offset_len-1]; 
        if (yyvsp[-3].value == INT) width = 4; else if (yyvsp[-3].value == CHAR) width = 1;
        offset_stack[offset_len-1] -= width;
        }else   printf("line %d :error: %s :redefinition\n", nline, yyvsp[-2]._ident);  
    } yyval.value = 1; 
 }
break;
case 29:
#line 532 "c--.y"
{  yyval._expr = yyvsp[-1]._expr; trace(" comp_stmt => local_declars stmt_list\n"); }
break;
case 31:
#line 536 "c--.y"
{  trace(" local_declars => ε\n");  }
break;
case 32:
#line 539 "c--.y"
{   char * s;
		  trace(" stmt_list =>  stmt_list stmt \n");
	    yyval._expr=yyvsp[0]._expr;
	 /*   assert($1.code != NULL);*/
	  /*  assert($2.code != NULL);*/
	 /*   s = (char *)malloc(strlen($1.code) + strlen($2.code)+1);*/
    /*        strcpy(s, $1.code);*/
     /*       strcat(s, $2.code);*/
            
    /*    $$.code = s;  */
        /*释放内存!*/
        /*free($1.code); free($2.code); */
     }
break;
case 33:
#line 553 "c--.y"
{  yyval._expr.code = "";  trace("empty stmt_list\n");}
break;
case 34:
#line 556 "c--.y"
{  yyval._expr=yyvsp[0]._expr;  trace("stmt => expr_stmt\n");  }
break;
case 35:
#line 558 "c--.y"
{   yyval._expr=yyvsp[0]._expr; trace("stmt => comp_stmt\n");  }
break;
case 36:
#line 560 "c--.y"
{   yyval._expr=yyvsp[0]._expr; trace("stmt => if_stmt\n"); }
break;
case 37:
#line 562 "c--.y"
{    trace("stmt => while_stmt\n");  }
break;
case 38:
#line 564 "c--.y"
{   yyval._expr=yyvsp[0]._expr; trace("stmt => return_stmt\n");  }
break;
case 39:
#line 567 "c--.y"
{ 
		trace("expr_stmt => expr ;\n");
		 /*77*/
		/* if (verbose){
		 	  if($1.type==INT) 
		 	  fprintf(stderr, "expr_stmt => expr ; type is int code=%d\n", $1.value);
		 	  else if($1.type==CHAR) 
		 	  	  fprintf(stderr, "expr_stmt => expr ; type is char  code=%s\n", $1);
		 	  }*/
		 	  
	   yyval._expr=yyvsp[-1]._expr;  
}
break;
case 40:
#line 580 "c--.y"
{  trace("expr_stmt => ;\n");  }
break;
case 41:
#line 583 "c--.y"
{  trace("if_stmt => if ( expr ) stmt\n"); yyval._expr=yyvsp[0]._expr; }
break;
case 42:
#line 585 "c--.y"
{  trace("if_stmt => if ( expr ) stmt else stmt\n"); yyval._expr.code="if------"; }
break;
case 43:
#line 588 "c--.y"
{  trace("while_stmt => while ( expr ) stmt\n");  }
break;
case 44:
#line 591 "c--.y"
{  trace("return_stmt => return ; \n");  }
break;
case 45:
#line 593 "c--.y"
{  trace("return_stmt => return expr ;\n"); 
	   yyval._expr = yyvsp[-1]._expr;  }
break;
case 46:
#line 597 "c--.y"
{  char code [1000];
		trace("expr =>   var  '='  expr  \n");
		    if (yyvsp[-2]._sym != 0) {
               if (yyvsp[-2]._sym->type != yyvsp[0]._expr.type) {
           /*   printf("var type is %d   expr type is %d \n",$1->type,$3.type);*/
                   printf("line %d :error  : = :the left and the right type does not match\n", nline, yyvsp[-2]._sym);                   
               }
               yyval._expr.type = yyvsp[-2]._sym->type;
		  }
	/*	if ($3.is_const) sprintf(code, "	mov	[ebp-%d], %d	;%s\n", $1->offset+4, $3.value, $1->name);  
                else { int len; 
                      strcpy(code, $3.code);
                      len = strlen(code);
                      sprintf(&code[len], "	mov	%s, %s\n", $1->name, $3.place->name);  
                       }
                $$.code = (char *)malloc(strlen(code)+1);
                strcpy($$.code, code);
               fprintf(yyout, code);
*/

}
break;
case 47:
#line 618 "c--.y"
{
      	trace("expr =>  logic_expr ; \n");
      	yyval._expr.type=yyvsp[0]._expr.type;
      /*	printf("expr =>  logic_expr  type is %d \n",$$.type);*/
      	}
break;
case 48:
#line 626 "c--.y"
{  if (verbose) fprintf(stderr, "look for %s\n", yyvsp[0]._ident);
	   yyval._sym = lookup(yyvsp[0]._ident, table_stack[table_len-1]);  
           if (yyval._sym == NULL) { fprintf(stderr, "line %d :undeclared identifier: %s \n", nline, yyvsp[0]._ident); 
           
           	 
           	}/*	else printf("var id type is %d \n",$$->type); */
}
break;
case 49:
#line 634 "c--.y"
{  trace("var => ID  [ expr ]\n"); 
	 yyval._sym = lookup(yyvsp[-3]._ident, table_stack[table_len-1]);  
           if (yyval._sym == NULL) { fprintf(stderr, "line %d :undeclared identifier: %s \n", nline, yyvsp[-3]._ident); 
           	/*exit(0);*/
           	}
	 }
break;
case 50:
#line 642 "c--.y"
{  trace("logic_expr => logic_expr OR logicand_expr\n"); 
	   if(yyvsp[-2]._expr.value||yyvsp[0]._expr.value) yyval._expr.value=1;
	    else yyval._expr.value=0;
	     yyval._expr=yyvsp[0]._expr;
	   }
break;
case 51:
#line 648 "c--.y"
{  trace("logic_expr => logicand_expr\n"); yyval._expr=yyvsp[0]._expr;
	  	/*printf("expr =>  logic_expr  type is %d \n",$$.type);*/
	 }
break;
case 52:
#line 653 "c--.y"
{  trace("logicand_expr => logicand_expr AND simple_expr\n"); 
	  
	   if(yyvsp[-2]._expr.value&&yyvsp[0]._expr.value) yyval._expr.value=1;
	    else yyval._expr.value=0;
	    yyval._expr.type=yyvsp[0]._expr.type;
	 }
break;
case 53:
#line 660 "c--.y"
{ trace("logicand_expr => simple_expr\n"); 
     yyval._expr=yyvsp[0]._expr;
     /*printf("ogicand_expr => simple_expr type is %d \n",$$.type);*/
      }

⌨️ 快捷键说明

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