📄 y.tab.c
字号:
"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 <time.h>
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];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -