📄 y.tab.c
字号:
"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 : simple_expr",
"var : ID",
"var : ID '[' 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_LITERAL",
"factor : CHAR_LITERAL",
"call : ID '(' args ')'",
"args : arg_list",
"args :",
"arg_list : arg_list ',' expr",
"arg_list : expr",
"callconvent : CDECL",
"callconvent : STDCALL",
"addop : '+'",
"addop : '-'",
"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 1609 "c--.y"
#include "lex.yy.c" /* 词法分析 */
#include <time.h>
void copyright(){
printf("* * * * * * * * C-- ComPiler * * * * * * * * * * *\n");
printf("Auther:JDWang 04120087 Complete Data: 07.08.30\n");
printf("Program Function:\n");
printf("Use the program ,you can compiler a c-- program ,and build\n");
printf("a masm program(call masm code).Then you use some tools(LINK...)\n");
printf("to create an executable file(...exe)\n");
printf("However,because of several reasons,the compiler is not integrated\n");
printf("There are maybe some that C can achieve but it not,for example:\n");
printf("you can main() in C ,but don't use it in my compiler but only\n");
printf("main(void),and if(j).. and so on. I think i can conplete all that\n");
printf("on some day but it still a hand work!\n\n");
printf("Usage: \n");
}
void help(void) {
fprintf(stderr, "c-- [-v] sourcefile [targetfile]\n");
exit(1);
}
int yyparse();
FILE *errfile;
int main(int argc, char **argv) {
time_t start, finish; //计时
char filename[256];
++argv;
--argc;
if((argc > 0) && (strcmp(argv[0], "-v") == 0)){ // c-- -v ....
verbose++;
++argv;
--argc;
}
if (argc > 0) { // c-- [-v] sourcefile ...
yyin = fopen(argv[0],"r"); /* argv[0] 为 soursefile */
if(yyin == NULL){
fprintf(stderr, "cannot open %s\n",argv[0]);
exit(1);
}
}
else{
copyright();
help(); // c-- [-v]
}
++argv;
--argc;
if(argc > 0) strcpy(filename, argv[0]); /* argv[0] 为 targetfile */
else { // 默认为c--.asm
char * dot;
strcpy(filename, argv[-1]); /* argv[-1] 为c-- */
dot = strrchr(filename, '.'); // strrchr
if (dot!= NULL) *dot = 0; /* '\0' */
strcat(filename, ".asm"); // c--.asm
}
if (verbose) yydebug = 1; //-v
yyout = fopen(filename,"w");
if(yyout== NULL) {
fprintf(stderr, "cannot open %s\n", filename);
exit(1);
}
errfile = fopen("errfile.txt","w+");
time(&start);
yyparse( ); //warning
time(&finish);
if(is_error == 0) fprintf(errfile," Congraduate You!\n Not any error in your file\n");
fprintf(stderr, "\nCompiling 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; } // yychar
else if (yychar == ID) errtok = yylval._ident;
else if (yychar < OR) errtok = tokens[yychar-257];
//else if (yychar == addop) errtok = (char *)yylval._op;
else{
sprintf(buf, "%d", yychar); errtok = buf;
}
fprintf(stderr, "%s at token %s in line %d\n", s, errtok, nline);
yyerrok;
}
#line 616 "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 267 "c--.y"
{ fprintf(yyout, " .data\n");
{
int i;
struct sym_entry *p;
/* 生成常量数组 参阅汇编 label 的用法? */
if(n_const_strings != 0) fprintf(yyout, "s@ label byte\n");
for (i = 0; i< n_const_strings; i++) {
/*printf("i = %d %s\n",i,const_strings[i].value);*/
if(const_strings[i].is_n) fprintf(yyout, " db \"%s\",10,0\n",const_strings[i].value);
else fprintf(yyout, " db \"%s\",0\n",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;
}
}
is_first = 0; /*恢复*/
fprintf(yyout, "\n public _main\n");
fprintf(yyout, " end\n");
}}
break;
case 2:
#line 294 "c--.y"
{
fprintf(yyout, " .386p\n");
fprintf(yyout, " model flat\n\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 309 "c--.y"
{ trace("declar_list => declar_list declar\n"); }
break;
case 7:
#line 316 "c--.y"
{
struct table *tp = table_stack[table_len-1];
symbol *p;
int width;
if((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->type = yyvsp[-2]._value;
p->isargv = 0;
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;
}
}
break;
case 8:
#line 338 "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[-5]._value == INT){
p->type = INT_ARRAY;
width = 4*atoi(yyvsp[-2]._ident);
}
else if(yyvsp[-5]._value == CHAR){
p->type = CHAR_ARRAY;
width = atoi(yyvsp[-2]._ident);
}
offset_stack[offset_len-1] += width;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -