📄 y_tab.c
字号:
#ifndef lint
static char yysccsid[] = "@(#)yaccpar 1.8 (Berkeley) 01/02/91\n\
Modified 5/15/91 by J. Roskind to support graphic debugging modes";
#endif
#define YYBYACC 1
#define YYPREFIX "yy"
#line 30 "grammer.y"
typedef union{
double dval;
int ival;
int sym_off; /*符号表中的offset*/
int relop;
int idname; /*变量名指针*/
} YYSTYPE;
#line 39 "grammer.y"
#include<stdio.h>
#include "global.h"
int defid=-1;
extern char yytext[];
extern int yylineno;
void yyerror();
void type_error();
int lookup(int nameoff);
void addType(int sym_off,int type);
void emitFCode( enum code_ops operation, double src1 ,int src2 , int dest );
void emitFCode( enum code_ops operation, double src1 ,int src2 , int dest );
void backpatch( struct Jmplist * list, int jmp_dest);
void enterID(int offset,int nameoff,int type); /*原由于此处原少了分号,产生非常多的warning*/
struct Jmplist * makelist(int i);
int offset = 0 , name_off = 0 , mem_off = 0 , sym_off = 0 , code_off = 0;
int operator = 0;
Code code[NUM_INS]; /*存储生成的指令*/
char str[20];
struct Symtable symtab[SYMTAB_LEN];
char name[NAME_LEN];
#line 42 "y_tab.c"
#define NOT 257
#define INT 258
#define REAL 259
#define BOOL 260
#define READ 261
#define WRITE 262
#define ASSIGN 263
#define IF 264
#define THEN 265
#define ELSE 266
#define FI 267
#define BEGINS 268
#define END 269
#define AND 270
#define OR 271
#define XOR 272
#define VAR 273
#define WHILE 274
#define DO 275
#define identifier 276
#define RelOP 277
#define RA 278
#define RAE 279
#define RB 280
#define RBE 281
#define RE 282
#define realConst 283
#define integerConst 284
#define YYERRCODE 256
short yylhs[] = { -1,
0, 1, 2, 2, 4, 5, 5, 3, 6, 8,
8, 9, 9, 9, 9, 9, 9, 10, 10, 7,
11, 12, 13, 14, 16, 16, 16, 17, 17, 17,
18, 18, 18, 18, 15, 15, 15, 19, 19, 20,
20, 21,
};
short yylen[] = { 2,
3, 0, 3, 1, 4, 1, 1, 2, 3, 4,
1, 1, 1, 1, 1, 1, 1, 9, 6, 0,
6, 3, 2, 2, 3, 3, 1, 3, 3, 1,
3, 1, 1, 1, 1, 4, 4, 1, 4, 1,
4, 3,
};
short yydefred[] = { 2,
0, 0, 0, 0, 0, 0, 0, 1, 20, 0,
0, 0, 0, 0, 20, 0, 12, 0, 0, 13,
14, 15, 16, 17, 8, 3, 7, 6, 5, 23,
24, 0, 34, 33, 32, 0, 20, 0, 0, 30,
0, 0, 40, 0, 0, 9, 20, 0, 0, 0,
0, 0, 0, 0, 0, 20, 20, 20, 0, 0,
0, 0, 31, 0, 0, 0, 0, 28, 29, 0,
0, 0, 20, 10, 41, 0, 36, 37, 39, 0,
20, 19, 21, 0, 0, 18,
};
short yydgoto[] = { 1,
2, 4, 8, 5, 29, 17, 25, 18, 19, 20,
21, 22, 23, 24, 37, 38, 39, 40, 41, 42,
43,
};
short yysindex[] = { 0,
0, -257, -255, -249, -24, -26, -218, 0, 0, -257,
-230, -239, -237, -40, 0, -208, 0, -224, 11, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 0, 0, 0, -38, 0, -42, -34, 0,
-241, -206, 0, -40, -38, 0, 0, -40, -31, -194,
-38, -38, -38, -38, -38, 0, 0, 0, -203, -28,
-218, 32, 0, -218, -28, -34, -34, 0, 0, -40,
-40, -40, 0, 0, 0, -219, 0, 0, 0, -218,
0, 0, 0, -218, -193, 0,
};
short yyrindex[] = { 0,
0, 0, 0, 0, -192, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, -190, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, -18, 0,
-36, 24, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, -55,
0, 0, 0, 0, 16, -5, 8, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
};
short yygindex[] = { 0,
0, 65, 0, 0, 0, 73, 5, 19, -58, 0,
0, 0, 0, 0, -37, -27, 7, 14, 9, 0,
0,
};
#define YYTABLESIZE 299
short yytable[] = { 36,
52, 36, 53, 22, 35, 76, 59, 54, 49, 63,
62, 52, 55, 53, 52, 3, 53, 60, 7, 44,
6, 83, 27, 65, 27, 85, 27, 27, 28, 56,
57, 11, 77, 78, 10, 25, 30, 25, 31, 25,
27, 50, 12, 13, 46, 14, 81, 82, 26, 7,
26, 61, 26, 25, 45, 15, 42, 16, 66, 67,
70, 71, 72, 58, 38, 48, 26, 68, 69, 47,
64, 73, 75, 86, 26, 4, 9, 80, 11, 74,
79, 0, 0, 0, 0, 84, 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,
22, 22, 0, 22, 0, 0, 32, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 35, 0,
0, 0, 0, 0, 51, 33, 0, 33, 35, 0,
0, 0, 34, 35, 34, 35, 27, 27, 27, 0,
27, 27, 27, 27, 0, 0, 27, 0, 27, 25,
25, 25, 0, 25, 25, 25, 25, 0, 0, 25,
0, 25, 26, 26, 26, 0, 26, 26, 26, 26,
42, 0, 26, 0, 26, 42, 42, 42, 38, 0,
42, 0, 0, 0, 38, 38, 0, 0, 38,
};
short yycheck[] = { 40,
43, 40, 45, 59, 41, 64, 44, 42, 36, 41,
48, 43, 47, 45, 43, 273, 45, 45, 268, 15,
276, 80, 41, 51, 43, 84, 45, 258, 259, 271,
272, 58, 70, 71, 59, 41, 276, 43, 276, 45,
59, 37, 261, 262, 269, 264, 266, 267, 41, 268,
43, 47, 45, 59, 263, 274, 41, 276, 52, 53,
56, 57, 58, 270, 41, 40, 59, 54, 55, 59,
265, 275, 41, 267, 10, 268, 4, 73, 269, 61,
72, -1, -1, -1, -1, 81, -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, -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, -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, -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,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
266, 267, -1, 269, -1, -1, 257, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, 265, -1,
-1, -1, -1, -1, 277, 276, -1, 276, 275, -1,
-1, -1, 283, 284, 283, 284, 265, 266, 267, -1,
269, 270, 271, 272, -1, -1, 275, -1, 277, 265,
266, 267, -1, 269, 270, 271, 272, -1, -1, 275,
-1, 277, 265, 266, 267, -1, 269, 270, 271, 272,
265, -1, 275, -1, 277, 270, 271, 272, 265, -1,
275, -1, -1, -1, 271, 272, -1, -1, 275,
};
#define YYFINAL 1
#ifndef YYDEBUG
#define YYDEBUG 0
#endif
#define YYMAXTOKEN 284
#if YYDEBUG
char *yyname[] = {
"end-of-file",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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"NOT","INT","REAL","BOOL","READ","WRITE","ASSIGN","IF","THEN","ELSE","FI",
"BEGINS","END","AND","OR","XOR","VAR","WHILE","DO","identifier","RelOP","RA",
"RAE","RB","RBE","RE","realConst","integerConst",
};
char *yyrule[] = {
"$accept : P",
"P : M Dec Body",
"M :",
"Dec : VarDec ';' Dec",
"Dec : VarDec",
"VarDec : VAR identifier ':' Type",
"Type : REAL",
"Type : INT",
"Body : CompSt M1",
"CompSt : BEGINS StatementList END",
"StatementList : Statement ';' M1 StatementList",
"StatementList : Statement",
"Statement : CompSt",
"Statement : IfSt",
"Statement : WhileSt",
"Statement : AssignSt",
"Statement : ReadSt",
"Statement : WriteSt",
"IfSt : IF BE M1 THEN Statement ELSE M1 Statement FI",
"IfSt : IF BE M1 THEN Statement FI",
"M1 :",
"WhileSt : WHILE M1 BE DO M1 Statement",
"AssignSt : identifier ASSIGN E",
"ReadSt : READ identifier",
"WriteSt : WRITE identifier",
"E : E '+' T",
"E : E '-' T",
"E : T",
"T : T '*' F",
"T : T '/' F",
"T : F",
"F : '(' E ')'",
"F : integerConst",
"F : realConst",
"F : identifier",
"BE : AndBE",
"BE : AndBE OR M1 BE",
"BE : AndBE XOR M1 BE",
"AndBE : NotBE",
"AndBE : NotBE AND M1 AndBE",
"NotBE : RelBE",
"NotBE : NOT '(' BE ')'",
"RelBE : E RelOP E",
};
#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 459 "grammer.y"
int lookup(int nameoff) /*查找标识符表*/
{
int i,rel;
for(i=0;i<sym_off;i++)
{
if(symtab[i].class==identifier)
{
rel=strcmp(name+symtab[i].idname,name+nameoff);
if(rel==0){return i;}
}
}
return -1;
}
void enterID(int offset,int nameoff,int type)
{
symtab[offset].idname=nameoff;
symtab[offset].type=type;
symtab[offset].class=identifier;
}
void addType(int sym_off,int type)
{
//symtab[sym_off].type = type;
symtab[sym_off].place = mem_off++;
//attrib[i].place = offset;
//return TRUE;
}
void emitCode( enum code_ops operation, int src1 ,int src2 , int dest )
{
code[code_off].op = operation;
code[code_off].src1.isrc1 = src1;
code[code_off].src2 = src2;
code[code_off].dest = dest;
code_off++;
}
void emitFCode( enum code_ops operation, double src1 ,int src2 , int dest )
{
code[code_off].op = operation;
code[code_off].src1.fsrc1 = src1;
code[code_off].src2 = src2;
code[code_off].dest = dest;
code_off++;
}
void backpatch( struct Jmplist * list, int jmp_dest) /*把指令序号i所相应的指令地址回填到由指针p指向的转移指令中*/
{
struct Jmplist * temp=list; /*回填后释放内存,*/
while(list!=NULL)
{
code[list->index].dest = jmp_dest;
list=list->next;
free(temp); /*释放内存*/
temp=list;
}
}
struct Jmplist * merge(struct Jmplist * list1,struct Jmplist * list2)
{
list1->next = list2;
return list1;
}
struct Jmplist * makelist(int i) /*建立仅含有指令诒i的新转移指令表,回头该表的指针*/
{
struct Jmplist * list = (struct Jmplist *)malloc(sizeof(struct Jmplist));
list->index = i;
list->next = NULL;
return list;
}
void yyerror(char * s)
{
fflush(stdout);
printf("ERROR:line %d",yylineno);
}
void type_error()
{
printf("ERROR: line %d type error",yylineno);
}
#line 368 "y_tab.c"
#define YYABORT goto yyabort
#define YYACCEPT goto yyaccept
#define YYERROR goto yyerrlab
#if YYDEBUG
#ifndef YYDEBUG_LEXER_TEXT /* pointer to the text isolated by the lexer*/
#define YYDEBUG_LEXER_TEXT "YYDEBUG_LEXER_TEXT not defined"
#endif
#ifndef YYDEBUG_INDENT_STRING
#define YYDEBUG_INDENT_STRING "| "
#endif
#ifndef YYDEBUG_REDUCE_STRING
#define YYDEBUG_REDUCE_STRING "+-------"
#endif
#ifndef YYDEBUG_INDENT
YYDEBUG_INDENT(yyindent)
int yyindent;
{
while(yyindent-- > 0)
printf("%s", YYDEBUG_INDENT_STRING);
}
#endif /* YYDEBUG_INDENT */
#ifndef YYDEBUG_REDUCE
YYDEBUG_REDUCE(yynew_state, yyrule_num, yyrule_string, yynew_indent,
yyrhs_count)
int yynew_state;
int yyrule_num;
char * yyrule_string;
int yynew_indent;
int yyrhs_count;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -