www.pudn.com > yufafenxiqi.rar > y.tab.c
#ifndef lint static char yysccsid[] = "@(#)yaccpar 1.8 (Berkeley) 01/02/91\n\ Modified 11/4/96 by Wensong Zhang to make getenv() function right\n\ and to remove the warning of unreferenced yyerrlab and yynewerror labels"; #endif #define YYBYACC 1 #line 2 "c--.y" #include#include #include #include #include #define HASHSIZE 128 /*s*/ typedef struct aa { int type; struct aa *next; } fun_type; typedef struct { char *name; /* symbol name */ int scope; /* scope level */ int type; int value; int funargc; fun_type *functype; int isdeclar; int offset; /* for functions: size of local variables */ } symbol ; struct sym_entry { symbol sym; /* this symbol */ struct sym_entry *next; /* next entry on hash chain */ }; struct table { int level; /* scope level for this table */ struct table *previous; /* table at lower scope */ struct sym_entry *buckets[HASHSIZE]; /* hash buckets */ }; typedef struct { char op; symbol *x, *y, *z; } Quadruple; typedef struct { char *translation; int variable; } myattribute; struct string_data { int index; char * value; }; int n_const_strings = 0; struct string_data const_strings[100]; struct table * table_stack[100]; struct table * global_table; int table_len = 0; int offset_stack[100]; int offset_len = 0; int level = 0; int verbose = 0; int argccount=0; int isfunction=0; int isdeclartion=0; fun_type *funtype=0; void trace(char *s) { if (verbose) fprintf(stderr, s); } /*0*/ symbol *lookup(char *name, struct table *tp) { struct sym_entry *p; unsigned h = ((unsigned)name)&(HASHSIZE-1); /*not素数 */ /* printf("look up %s level %d tp.level %d table_len-1 %d\n",name,level,tp->level,table_len-1);*/ do for (p = tp->buckets[h]; p; p = p->next) if (name == p->sym.name) {return &p->sym;} while (tp = tp->previous); return 0; } symbol *lookupred(char *name, struct table *tp) { struct sym_entry *p; unsigned h = ((unsigned)name)&(HASHSIZE-1); /*not素数 */ for (p = tp->buckets[h]; p; p = p->next) if (name == p->sym.name){/*printf("find the symobl %s\n",p->sym.name); */ return &p->sym;} return 0; } struct table * mktable(struct table *previous, int level) { int i; struct table *new = (struct table *)malloc(sizeof *new); new->previous = previous; new->level = level; for (i = 0; i < HASHSIZE; i++) new->buckets[i] = 0; return new; } /*i*/ symbol *insert(char *name, struct table *tpp) { unsigned h = ((unsigned)name)&(HASHSIZE-1); struct table *tp = tpp; struct sym_entry *p = (struct entry *)malloc(sizeof *p); /*printf("&&&&&&&&&tp->level =%d level=%d inserting %s\n", tp->level,level,name);*/ if (tp->level < level) { /* 当前scope level*/ tp = mktable(tp, level); /* printf("make new table\n");*/ table_stack[table_len] = tp; table_len ++; offset_stack[offset_len] = 0; offset_len ++; } p->sym.name = name; p->sym.scope = level; if(isfunction) { p->sym.type = 1; } else p->sym.type = 0; /*假设是常数, 常数的类型为0*/ p->next = tp->buckets[h]; p->sym.funargc=0; tp->buckets[h] = p; /* 新名字在表头 */ /*printf("Inset %s level %d tp.level %d table_len-1 %d\n",name,level,tp->level,table_len-1);*/ return &p->sym; } typedef struct { int is_const; int value; int type; symbol * place; symbol * offset; /* for array */ char * code; } expr; char * insert_id(char *name); symbol * newtemp() { static int index = 1; char tmpname[20]; char * p; symbol * s; sprintf(tmpname, "__t%d", index); index++; s = insert(insert_id(tmpname), table_stack[table_len-1]); s->offset = offset_stack[offset_len-1]; offset_stack[offset_len-1] += 4; return s; } #line 160 "c--.y" typedef union{ char * _ident; char _op; char _relop[3]; int value; symbol *_sym; expr _expr; } YYSTYPE; #line 164 "y.tab.c" #define ID 257 #define NUM 258 #define INT 259 #define VOID 260 #define CHAR 261 #define IF 262 #define ELSE 263 #define WHILE 264 #define RETURN 265 #define CDECL 266 #define STDCALL 267 #define EQ 268 #define NE 269 #define LT 270 #define GT 271 #define LE 272 #define GE 273 #define AND 274 #define OR 275 #define ADDOP 276 #define CHARATER 277 #define STRING 278 #define YYERRCODE 256 short yylhs[] = { -1, 0, 29, 30, 30, 31, 31, 32, 32, 32, 32, 32, 1, 1, 1, 1, 3, 3, 3, 33, 4, 4, 5, 5, 7, 7, 6, 6, 6, 21, 35, 35, 24, 24, 25, 25, 25, 25, 25, 26, 26, 22, 22, 23, 27, 27, 17, 17, 2, 2, 16, 16, 15, 15, 14, 14, 13, 13, 12, 12, 11, 11, 10, 10, 10, 10, 10, 10, 28, 8, 8, 9, 9, 34, 34, 20, 20, 20, 20, 20, 20, 18, 18, 18, 19, 19, }; short yylen[] = { 2, 2, 0, 2, 1, 1, 1, 3, 6, 4, 7, 2, 1, 1, 1, 1, 7, 7, 6, 0, 2, 1, 1, 1, 3, 1, 2, 3, 4, 4, 2, 0, 2, 0, 1, 1, 1, 1, 1, 2, 1, 5, 7, 5, 2, 3, 3, 1, 1, 4, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 3, 1, 1, 1, 1, 1, 4, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }; short yydefred[] = { 2, 0, 0, 0, 12, 13, 14, 0, 6, 0, 4, 5, 11, 0, 73, 74, 0, 0, 0, 3, 7, 0, 0, 19, 20, 0, 9, 0, 0, 0, 0, 15, 0, 0, 0, 25, 0, 8, 0, 0, 0, 0, 0, 10, 0, 27, 17, 31, 16, 24, 28, 0, 0, 0, 30, 0, 0, 65, 0, 0, 0, 67, 66, 40, 0, 29, 84, 85, 0, 61, 59, 0, 0, 53, 0, 0, 0, 0, 35, 36, 37, 32, 34, 38, 64, 0, 0, 0, 0, 44, 0, 0, 0, 81, 82, 83, 0, 79, 80, 76, 77, 75, 78, 0, 0, 0, 0, 39, 63, 60, 0, 0, 0, 72, 0, 0, 45, 62, 46, 58, 0, 0, 52, 0, 49, 68, 0, 0, 0, 71, 0, 43, 0, 42, }; short yydgoto[] = { 1, 7, 68, 8, 17, 34, 35, 36, 111, 112, 69, 70, 71, 72, 73, 74, 75, 76, 96, 77, 104, 78, 79, 80, 53, 81, 82, 83, 84, 2, 9, 10, 11, 28, 18, 51, }; short yysindex[] = { 0, 0, -111, -2, 0, 0, 0, 13, 0, -111, 0, 0, 0, -46, 0, 0, -227, -7, -221, 0, 0, -216, -42, 0, 0, -56, 0, -215, -91, -11, -43, 0, 0, -38, 16, 0, 14, 0, 2, -31, -190, -48, -85, 0, -9, 0, 0, 0, 0, 0, 0, -111, -36, -33, 0, -46, -26, 0, 47, 49, -30, 0, 0, 0, 7, 0, 0, 0, 30, 0, 0, 69, -109, 0, -181, -179, 38, 7, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 41, 61, 7, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 0, 17, 68, 74, 0, 71, 80, 0, 0, 0, 0, 69, -152, 0, -181, 0, 0, 7, 11, 11, 0, -138, 0, 11, 0, }; short yyrindex[] = { 0, 0, 0, 20, 0, 0, 0, 0, 0, 127, 0, 0, 0, 89, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -18, 0, 0, 0, 90, 0, 0, -19, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -24, 0, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35, 0, 0, 44, -39, 0, 63, -13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 94, 0, 0, 0, 0, 0, 0, 0, 54, 27, 0, 64, 0, 0, 0, 0, 0, 0, -6, 0, 0, 0, }; short yygindex[] = { 0, -10, 37, 0, 0, 0, 96, 0, 0, 0, 0, -58, 33, 40, 34, 60, 0, 66, 0, 0, 0, 105, 0, 0, 0, -54, 0, 0, 0, 0, 0, 146, 121, 0, 0, 0, }; #define YYTABLESIZE 339 short yytable[] = { 66, 18, 55, 66, 40, 55, 16, 64, 12, 33, 64, 46, 67, 20, 86, 67, 33, 26, 33, 109, 55, 33, 26, 23, 13, 26, 63, 41, 47, 89, 22, 47, 33, 23, 41, 33, 24, 29, 119, 41, 66, 52, 25, 30, 66, 21, 47, 64, 37, 27, 38, 64, 67, 41, 55, 16, 67, 41, 42, 48, 44, 43, 15, 48, 48, 85, 48, 45, 54, 48, 63, 54, 63, 130, 131, 47, 63, 63, 133, 63, 47, 48, 63, 48, 50, 57, 54, 87, 57, 88, 47, 92, 65, 105, 63, 56, 106, 107, 56, 33, 116, 33, 117, 57, 51, 50, 95, 51, 50, 125, 124, 93, 127, 56, 108, 48, 94, 41, 126, 41, 54, 128, 51, 50, 103, 132, 90, 1, 63, 21, 91, 22, 70, 108, 47, 69, 120, 57, 49, 122, 108, 108, 108, 108, 121, 3, 48, 56, 4, 5, 6, 110, 113, 114, 115, 19, 51, 50, 118, 97, 98, 99, 100, 101, 102, 31, 123, 103, 4, 32, 6, 31, 54, 0, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 129, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 0, 55, 0, 0, 56, 57, 0, 56, 57, 58, 0, 59, 60, 33, 33, 55, 55, 0, 33, 13, 33, 33, 0, 0, 61, 62, 0, 61, 62, 0, 0, 41, 41, 33, 33, 0, 41, 18, 41, 41, 18, 18, 18, 0, 56, 57, 0, 0, 56, 57, 13, 41, 41, 58, 0, 59, 60, 15, 0, 14, 15, 0, 0, 0, 61, 62, 15, 15, 61, 62, 48, 48, 48, 48, 48, 48, 48, 48, 48, 0, 0, 54, 54, 63, 63, 63, 63, 63, 63, 63, 63, 63, 57, 57, 57, 57, 57, 57, 57, 57, 57, 0, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0, 0, 0, 0, 0, 51, 50, }; short yycheck[] = { 33, 0, 41, 33, 42, 44, 42, 40, 10, 33, 40, 59, 45, 59, 40, 45, 40, 59, 28, 77, 59, 45, 41, 41, 42, 44, 59, 33, 41, 59, 257, 44, 42, 40, 40, 59, 257, 93, 96, 45, 33, 51, 258, 258, 33, 91, 59, 40, 59, 91, 93, 40, 45, 59, 93, 42, 45, 41, 44, 37, 91, 59, 42, 41, 42, 91, 44, 257, 41, 47, 59, 44, 37, 127, 128, 123, 41, 42, 132, 44, 93, 59, 47, 61, 93, 41, 59, 40, 44, 40, 123, 61, 125, 274, 59, 41, 275, 59, 44, 123, 59, 125, 41, 59, 41, 41, 37, 44, 44, 41, 93, 42, 41, 59, 77, 93, 47, 123, 44, 125, 93, 41, 59, 59, 276, 263, 60, 0, 93, 40, 64, 41, 41, 96, 123, 41, 103, 93, 42, 105, 103, 104, 105, 106, 104, 256, 41, 93, 259, 260, 261, 85, 86, 87, 88, 9, 93, 93, 92, 268, 269, 270, 271, 272, 273, 256, 106, 276, 259, 260, 261, 256, 51, -1, 259, 260, 261, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 126, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 257, -1, 257, -1, -1, 257, 258, -1, 257, 258, 262, -1, 264, 265, 257, 258, 274, 275, -1, 262, 257, 264, 265, -1, -1, 277, 278, -1, 277, 278, -1, -1, 257, 258, 277, 278, -1, 262, 256, 264, 265, 259, 260, 261, -1, 257, 258, -1, -1, 257, 258, 257, 277, 278, 262, -1, 264, 265, 257, -1, 266, 267, -1, -1, -1, 277, 278, 266, 267, 277, 278, 268, 269, 270, 271, 272, 273, 274, 275, 276, -1, -1, 274, 275, 268, 269, 270, 271, 272, 273, 274, 275, 276, 268, 269, 270, 271, 272, 273, 274, 275, 276, -1, 268, 269, 270, 271, 272, 273, 274, 275, 276, -1, -1, -1, -1, -1, -1, -1, 275, 275, }; #define YYFINAL 1 #ifndef YYDEBUG #define YYDEBUG 0 #endif #define YYMAXTOKEN 278 #if YYDEBUG char *yyname[] = { "end-of-file",0,0,0,0,0,0,0,0,0,"'\\n'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,"'!'",0,0,0,"'%'",0,0,"'('","')'","'*'",0,"','","'-'",0,"'/'",0,0,0,0,0,0,0, 0,0,0,0,"';'",0,"'='",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, "'['",0,"']'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"'{'",0, "'}'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"ID","NUM","INT","VOID","CHAR","IF","ELSE","WHILE", "RETURN","CDECL","STDCALL","EQ","NE","LT","GT","LE","GE","AND","OR","ADDOP", "CHARATER","STRING", }; char *yyrule[] = { "$accept : program", "program : M declar_list", "M :", "declar_list : declar_list declar", "declar_list : declar", "declar : var_declar", "declar : fun_declar", "var_declar : type_spec ID ';'", "var_declar : type_spec ID '[' NUM ']' ';'", "var_declar : type_spec '*' ID ';'", "var_declar : type_spec '*' ID '[' NUM ']' ';'", "var_declar : error '\\n'", "type_spec : INT", "type_spec : VOID", "type_spec : CHAR", "type_spec : error", "fun_declar : type_spec fun_tag '(' LL params ')' comp_stmt", "fun_declar : type_spec fun_tag '(' LL params ')' ';'", "fun_declar : type_spec fun_tag '(' LL params ')'", "LL :", "fun_tag : callconvent ID", "fun_tag : ID", "params : param_list", "params : VOID", "param_list : param_list ',' param", "param_list : param", "param : type_spec ID", "param : type_spec '*' ID", "param : type_spec ID '[' ']'", "comp_stmt : '{' local_declars stmt_list '}'", "local_declars : local_declars var_declar", "local_declars :", "stmt_list : stmt_list stmt", "stmt_list :", "stmt : expr_stmt", "stmt : comp_stmt", "stmt : if_stmt", "stmt : while_stmt", "stmt : return_stmt", "expr_stmt : expr ';'", "expr_stmt : ';'", "if_stmt : IF '(' expr ')' stmt", "if_stmt : IF '(' expr ')' stmt ELSE stmt", "while_stmt : WHILE '(' expr ')' stmt", "return_stmt : RETURN ';'", "return_stmt : RETURN expr ';'", "expr : var '=' expr", "expr : logic_expr", "var : ID", "var : ID '[' expr ']'", "logic_expr : logic_expr OR logicand_expr", "logic_expr : logicand_expr", "logicand_expr : logicand_expr AND simple_expr", "logicand_expr : simple_expr", "simple_expr : add_expr relop add_expr", "simple_expr : add_expr", "add_expr : add_expr ADDOP term", "add_expr : term", "term : term mulop unary_expr", "term : unary_expr", "unary_expr : unaryop unary_expr", "unary_expr : factor", "factor : '(' expr ')'", "factor : var", "factor : call", "factor : NUM", "factor : STRING", "factor : CHARATER", "call : ID '(' args ')'", "args : arg_list", "args :", "arg_list : arg_list ',' expr", "arg_list : expr", "callconvent : CDECL", "callconvent : STDCALL", "relop : LE", "relop : LT", "relop : GT", "relop : GE", "relop : EQ", "relop : NE", "mulop : '*'", "mulop : '/'", "mulop : '%'", "unaryop : '!'", "unaryop : '-'", }; #endif #define yyclearin (yychar=(-1)) #define yyerrok (yyerrflag=0) #ifdef YYSTACKSIZE #ifndef YYMAXDEPTH #define YYMAXDEPTH YYSTACKSIZE #endif #else #ifdef YYMAXDEPTH #define YYSTACKSIZE YYMAXDEPTH #else #define YYSTACKSIZE 600 #define YYMAXDEPTH 600 #endif #endif int yydebug; int yynerrs; int yyerrflag; int yychar; short *yyssp; YYSTYPE *yyvsp; YYSTYPE yyval; YYSTYPE yylval; short yyss[YYSTACKSIZE]; YYSTYPE yyvs[YYSTACKSIZE]; #define yystacksize YYSTACKSIZE #line 948 "c--.y" #include "lex.yy.c" #include void help(void) { fprintf(stderr, "c-- [-v] sourcefile [targetfile]\n"); exit(1); } int main(int argc, char **argv) { time_t start, finish; char filename[256]; ++argv; --argc; if((argc > 0) && (strcmp(argv[0], "-v") == 0)) { verbose++; ++argv; --argc; } if (argc > 0) yyin = fopen(argv[0],"r"); else help(); ++argv; --argc; if(argc > 0) strcpy(filename, argv[0]); else { char * dot; strcpy(filename, argv[-1]); dot = strrchr(filename, '.'); if (dot != NULL) *dot = 0; strcat(filename, ".asm"); } yyout = fopen(filename, "w"); if (yyout == NULL) { fprintf(stderr, "cannot open %s\n", filename); exit(1); } if (verbose) yydebug = 1; time( &start ); yyparse( ); time( &finish ); fprintf(stderr, "compiling to %s, takes %1.1f seconds.\n", filename, difftime( finish, start )); return 0; } yyerror(char *s) { static char * tokens[]={"ID", "NUM", "INT", "VOID", "CHAR", "if", "else", "while", "return", "cdecl", "stdcall", "=", "!=", "<", ">", "<=", ">=", "and", "or"}; char *errtok, buf[256]; if (yychar < 256) { sprintf(buf, "%c", yychar); errtok = buf; } else if (yychar == ID) errtok = yylval._ident; else if (yychar < OR) errtok = tokens[yychar-257]; else if (yychar == ADDOP) errtok = yylval._op; else { sprintf(buf, "%d", yychar); errtok = buf; } fprintf(stderr, "line %d :%s at token %s \n", nline,s, errtok); yyerrok; } #line 523 "y.tab.c" #define YYABORT goto yyabort #define YYACCEPT goto yyaccept #define YYERROR goto yyerrlab int yyparse() { register int yym, yyn, yystate; #if YYDEBUG register char *yys; if (yys = getenv("YYDEBUG")) { yyn = *yys; if (yyn >= '0' && yyn <= '9') yydebug = yyn - '0'; } #endif yynerrs = 0; yyerrflag = 0; yychar = (-1); yyssp = yyss; yyvsp = yyvs; *yyssp = yystate = 0; yyloop: if (yyn = yydefred[yystate]) goto yyreduce; 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", yystate, yychar, yys); } #endif } if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == yychar) { #if YYDEBUG if (yydebug) printf("yydebug: state %d, shifting to state %d\n", yystate, yytable[yyn]); #endif if (yyssp >= yyss + yystacksize - 1) { goto yyoverflow; } *++yyssp = yystate = yytable[yyn]; *++yyvsp = yylval; yychar = (-1); if (yyerrflag > 0) --yyerrflag; goto yyloop; } if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == yychar) { yyn = yytable[yyn]; goto yyreduce; } if (yyerrflag) goto yyinrecovery; #ifdef lint goto yynewerror; yynewerror: #endif yyerror("syntax error"); #ifdef lint goto yyerrlab; yyerrlab: #endif ++yynerrs; yyinrecovery: if (yyerrflag < 3) { yyerrflag = 3; for (;;) { if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE) { #if YYDEBUG if (yydebug) printf("yydebug: state %d, error recovery shifting\ to state %d\n", *yyssp, yytable[yyn]); #endif if (yyssp >= yyss + yystacksize - 1) { goto yyoverflow; } *++yyssp = yystate = yytable[yyn]; *++yyvsp = yylval; goto yyloop; } else { #if YYDEBUG if (yydebug) printf("yydebug: error recovery discarding state %d\n", *yyssp); #endif if (yyssp <= yyss) goto yyabort; --yyssp; --yyvsp; } } } else { if (yychar == 0) goto yyabort; #if YYDEBUG if (yydebug) { yys = 0; if (yychar <= YYMAXTOKEN) yys = yyname[yychar]; if (!yys) yys = "illegal-symbol"; printf("yydebug: state %d, error recovery discards token %d (%s)\n", yystate, yychar, yys); } #endif yychar = (-1); goto yyloop; } yyreduce: #if YYDEBUG if (yydebug) printf("yydebug: state %d, reducing by rule %d (%s)\n", yystate, yyn, yyrule[yyn]); #endif yym = yylen[yyn]; yyval = yyvsp[1-yym]; switch (yyn) { case 1: #line 206 "c--.y" { fprintf(yyout, " .data\n"); { int i; struct sym_entry *p; for (i = 0; i< n_const_strings; i++) { fprintf(yyout, "s@%d label byte", const_strings[i].index); fprintf(yyout, " db \"%s,0\"",const_strings[i].value); /* \n可能需要变成 10 */ } /* 生成全局变量 */ for (i = 0; i< HASHSIZE; i++) { p = global_table->buckets[i]; /* hash buckets */ while (p) { if (p->sym.type == INT) fprintf(yyout, "_%s DD ?\n", p->sym.name); else if (p->sym.type == CHAR) fprintf(yyout, "_%s DB ?\n", p->sym.name); p = p->next; } } fprintf(yyout, " public _main\n"); fprintf(yyout, " end\n"); }} break; case 2: #line 228 "c--.y" { fprintf(yyout, " .386p\n"); fprintf(yyout, " model flat\n"); fprintf(yyout, " .code\n"); global_table = mktable(NULL, 0); table_stack[table_len] = global_table; table_len ++; offset_stack[offset_len] = 0; offset_len ++; } break; case 3: #line 241 "c--.y" { trace("declar_list => declar_list declar\n"); } break; case 7: #line 248 "c--.y" { /*1*/ struct table * tp = table_stack[table_len-1]; if(lookupred(yyvsp[-1]._ident,tp)==0){ symbol * p = insert(yyvsp[-1]._ident, tp); int width = 0; 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[-1]._ident); trace("var_declar => type_spec ID ;\n"); } break; case 8: #line 263 "c--.y" { struct table * tp = table_stack[table_len-1]; trace("var_declar => type_spec ID [ NUM ] ;\n"); if(lookupred(yyvsp[-4]._ident,tp)==0){ symbol * p = insert(yyvsp[-4]._ident, tp); int width = 0; int widthall=width; p->type = yyvsp[-5].value; p->offset = offset_stack[offset_len-1]; if (yyvsp[-5].value == INT) width = 4; else if (yyvsp[-5].value == CHAR) width = 1; offset_stack[offset_len-1] += widthall; }else printf("line %3d error: %s :redefinition\n", nline, yyvsp[-4]._ident); } break; case 9: #line 276 "c--.y" { struct table * tp = table_stack[table_len-1]; trace(" var_declar => type_spec * ID ;\n"); if(lookupred(yyvsp[-1]._ident,tp)==0){ symbol * p = insert(yyvsp[-1]._ident, tp); int width = 0; int widthall=width; 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] += widthall; }else printf("line %3d error: %s :redefinition\n", nline, yyvsp[-1]._ident); } break; case 10: #line 289 "c--.y" { struct table * tp = table_stack[table_len-1]; trace("var_declar => type_spec * ID [ NUM ] ;\n"); if(lookupred(yyvsp[-4]._ident,tp)==0){ symbol * p = insert(yyvsp[-4]._ident, tp); int width = 0; int widthall=width; p->type = yyvsp[-6].value; p->offset = offset_stack[offset_len-1]; if (yyvsp[-6].value == INT) width = 4; else if (yyvsp[-6].value == CHAR) width = 1; offset_stack[offset_len-1] += widthall; }else printf("line %3d error: %s :redefinition\n", nline, yyvsp[-4]._ident); } break; case 12: #line 306 "c--.y" { yyval.value = INT; /*printf("type_spec %d\n",$$); */ } break; case 13: #line 309 "c--.y" { yyval.value = VOID; /*printf("type_spec %d\n",$$); */ } break; case 14: #line 312 "c--.y" { yyval.value = CHAR; /*printf("type_spec %d\n",$$);*/ } break; case 15: #line 314 "c--.y" {fprintf(stderr, "line %d :type name expected\n",nline); yyerrok; } break; case 16: #line 318 "c--.y" { 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]; 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);*/ } 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 "){ 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 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); }