📄 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 <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*#include <assert.h>*/
#define HASHSIZE 128
typedef struct paramp{ /*函数参数*/
int type;
char *code; /* 参数的代码 push ...*/
struct paramp *next;
}paramtp;
typedef struct {
char *name; /* symbol name */
int scope; /* scope level */
int type;
int offset; /* for functions: size of local variables */
int isargv; /* 判定所给ID 是否是函数参数 */
int is_fun_ret; /*函数是否有返回值*/
int ret_const; /*函数是否返回常数*/
int retvalue; /*函数的常数返回值*/
int ret; /*return value */
paramtp *paramer; /* 参数表 */
} 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;
typedef struct /* ??*/
{ char * name;
} fun_type;
struct string_data {
int index; /*在数据段只能感的位置*/
int is_n; /*是否有带回车*/
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; /*是否DEBUG*/
int is_error = 0; /*可用于判断是否出错,若有错,verbose++;最后判断verbose是否为0 ,做相应的动作*/
int is_first = 0; /*决定.code 的位置*/
int is_one_var = 0; /* 判断表达式右边是否只有一个操作数*/
int is_funret = 0; /* 是否有函数返回*/
int data_place = 0; /*数据段的位移*/
symbol *temp_ret; /*函数返回的临时存放单元*/
void trace(char *s) { if (verbose) fprintf(stderr, s); }
symbol *lookup_all(char *name, struct table *tp) {
struct sym_entry *p;
unsigned h = ((unsigned)name)&(HASHSIZE-1); /*not素数 */
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 *lookup(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)
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;
}
symbol *insert(char *name, struct table *tpp) {
unsigned h = ((unsigned)name)&(HASHSIZE-1);
struct table *tp = tpp;
struct sym_entry *p = (struct sym_entry *)malloc(sizeof *p);
if (tp->level < level) { /* 当前scope level*/
tp = mktable(tp, level);
table_stack[table_len] = tp;
table_len ++;
offset_stack[offset_len] = 0;
offset_len ++;
}
p->sym.name = name;
p->sym.scope = level;
p->sym.type = 0; /*假设是常数, 常数的类型为0*/
p->next = tp->buckets[h];
tp->buckets[h] = p; /* 新名字在表头 */
return &p->sym;
}
typedef struct { /* 表达式类型*/
int is_const;
int type; /* 类型 */
int value; /* 表达式的值 */
int aa;
symbol * place;
symbol * offset; /* for array */
char * code; /*对应的代码*/
} expr;
char * insert_id(char *name); /* define in c--.lex.l HASH*/
symbol * newtemp()
{ /*生成一个临时变量*/
static int index = 1;
char tmpname[20];
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;
}
char * newlabel()
{ /*生成一个标号*/
static int index = 1;
char tmpname[20];
char *temp;
int len;
sprintf(tmpname, "__@%d", index);
index++;
len = strlen(tmpname); /*不能直接返回tmpname*/
temp = (char *)malloc(len+1);
strcpy(temp,tmpname);
return temp;
}
int is_C(char *fun_name)
{ /* 因为是C 函数,没有bool型*/
char *C_FUN[7] = {"main","puts","gets","write","read","printf","scanf"}; /*应该不止这些*/
int i = 0;
while(i<7){
if(strcmp(C_FUN[i],fun_name)==0) break;
i++;
}
if(i==7) return 0;
else return 1;
}
#line 216 "c--.y"
typedef union{
char * _ident;
char _op;
int _value;
paramtp * _param; /*函数参数类型*/
symbol *_sym;
expr _expr;
} YYSTYPE;
#line 207 "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 STRING_LITERAL 276
#define CHAR_LITERAL 277
#define FUN_DECLAR 278
#define FUN_DEFINE 279
#define INT_ARRAY 280
#define CHAR_ARRAY 281
#define INT_POINT 282
#define CHAR_POINT 283
#define VOID_POINT 284
#define INT_POINT_ARRAY 285
#define CHAR_POINT_ARRAY 286
#define LOW_ELSE 287
#define YYERRCODE 256
short yylhs[] = { -1,
0, 31, 32, 32, 33, 33, 6, 6, 6, 6,
4, 4, 4, 4, 7, 7, 34, 8, 8, 11,
11, 12, 12, 13, 13, 13, 23, 22, 22, 26,
26, 27, 27, 27, 27, 27, 28, 28, 24, 24,
25, 29, 29, 15, 15, 14, 14, 20, 20, 19,
19, 18, 18, 17, 17, 16, 16, 16, 16, 16,
16, 30, 9, 9, 10, 10, 5, 5, 1, 1,
21, 21, 21, 21, 21, 21, 2, 2, 2, 3,
3,
};
short yylen[] = { 2,
2, 0, 2, 1, 1, 1, 3, 6, 4, 7,
1, 1, 1, 1, 7, 7, 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, 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, 1,
1,
};
short yydefred[] = { 2,
0, 0, 14, 11, 12, 13, 0, 5, 6, 0,
4, 0, 67, 68, 0, 0, 0, 3, 7, 0,
0, 18, 17, 0, 9, 0, 0, 0, 0, 0,
0, 0, 0, 23, 8, 0, 0, 0, 0, 0,
10, 0, 25, 16, 29, 15, 22, 26, 0, 0,
28, 0, 0, 0, 59, 0, 0, 0, 60, 61,
81, 38, 0, 27, 80, 0, 0, 0, 55, 53,
0, 0, 45, 33, 34, 35, 30, 32, 36, 58,
0, 0, 0, 0, 42, 0, 0, 57, 54, 0,
37, 77, 78, 79, 0, 75, 76, 72, 73, 71,
74, 69, 70, 0, 0, 0, 0, 0, 66, 0,
0, 43, 56, 44, 52, 0, 0, 47, 62, 0,
0, 0, 65, 0, 41, 0, 40,
};
short yydgoto[] = { 1,
104, 95, 66, 7, 16, 8, 9, 17, 107, 108,
32, 33, 34, 67, 68, 69, 70, 71, 72, 73,
105, 49, 74, 75, 76, 52, 77, 78, 79, 80,
2, 10, 11, 27,
};
short yysindex[] = { 0,
0, -206, 0, 0, 0, 0, -31, 0, 0, -206,
0, -53, 0, 0, -237, -235, -12, 0, 0, -225,
-42, 0, 0, -54, 0, -202, -140, 9, -35, 0,
-38, 28, 31, 0, 0, 19, -6, -170, -46, -206,
0, 4, 0, 0, 0, 0, 0, 0, -206, -37,
0, -33, -53, -32, 0, 56, 65, -30, 0, 0,
0, 0, 3, 0, 0, 3, 47, 51, 0, 0,
-23, 2, 0, 0, 0, 0, 0, 0, 0, 0,
3, 3, 3, 3, 0, 52, 71, 0, 0, 3,
0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
0, 0, 0, 3, 3, 21, 82, 85, 0, 89,
90, 0, 0, 0, 0, -23, -1, 0, 0, 3,
1, 1, 0, -130, 0, 1, 0,
};
short yyrindex[] = { 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 139,
0, 100, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, -40,
0, 0, 101, 0, 0, 0, -14, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, -24, 0,
0, 0, 0, 20, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 29, 0, 0, 0,
39, 45, 0, 0, 0, 0, 0, 0, 0, 0,
0, 105, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 106, 0, 0,
0, 0, 0, 0, 0, 50, 59, 0, 0, 0,
0, 0, 0, -8, 0, 0, 0,
};
short yygindex[] = { 0,
0, 0, 0, -9, 0, 99, 0, 0, 0, 0,
0, 0, 109, 40, 44, 0, -43, 46, 48, 0,
0, 0, 112, 0, 0, 0, 15, 0, 0, 0,
0, 0, 144, 0,
};
#define YYTABLESIZE 323
short yytable[] = { 65,
21, 12, 65, 38, 15, 19, 63, 82, 31, 63,
15, 61, 44, 94, 61, 31, 25, 31, 92, 21,
31, 22, 89, 93, 39, 62, 24, 23, 85, 24,
31, 39, 24, 65, 31, 65, 39, 20, 28, 50,
63, 102, 63, 103, 102, 61, 103, 61, 26, 3,
39, 115, 4, 5, 6, 29, 46, 36, 81, 62,
46, 46, 46, 46, 46, 57, 46, 35, 39, 57,
57, 57, 57, 57, 40, 57, 45, 41, 46, 51,
46, 51, 51, 51, 42, 49, 43, 57, 49, 45,
50, 64, 50, 50, 50, 83, 48, 51, 31, 48,
31, 86, 48, 49, 84, 88, 87, 90, 50, 91,
112, 113, 46, 118, 39, 3, 39, 48, 4, 30,
6, 57, 119, 45, 106, 109, 110, 111, 120, 121,
122, 51, 126, 114, 88, 124, 125, 49, 1, 19,
127, 20, 50, 88, 88, 64, 63, 51, 47, 116,
46, 48, 117, 18, 0, 0, 0, 0, 0, 0,
0, 0, 0, 123, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 12, 0, 37, 53,
0, 0, 0, 54, 55, 12, 54, 55, 56, 0,
57, 58, 31, 31, 13, 14, 0, 31, 0, 31,
31, 0, 59, 60, 0, 59, 60, 0, 39, 39,
0, 31, 31, 39, 0, 39, 39, 54, 55, 54,
55, 0, 56, 0, 57, 58, 0, 39, 39, 96,
97, 98, 99, 100, 101, 0, 59, 60, 59, 60,
0, 0, 0, 0, 0, 0, 0, 46, 46, 46,
46, 46, 46, 0, 0, 0, 57, 57, 57, 57,
57, 57, 0, 0, 0, 0, 51, 51, 51, 51,
51, 51, 0, 0, 0, 0, 0, 50, 50, 50,
50, 50, 50,
};
short yycheck[] = { 33,
41, 42, 33, 42, 42, 59, 40, 40, 33, 40,
42, 45, 59, 37, 45, 40, 59, 27, 42, 257,
45, 257, 66, 47, 33, 59, 41, 40, 59, 44,
40, 40, 258, 33, 59, 33, 45, 91, 93, 49,
40, 43, 40, 45, 43, 45, 45, 45, 91, 256,
59, 95, 259, 260, 261, 258, 37, 93, 91, 59,
41, 42, 43, 44, 45, 37, 47, 59, 41, 41,
42, 43, 44, 45, 44, 47, 123, 59, 59, 41,
61, 43, 44, 45, 91, 41, 257, 59, 44, 123,
41, 125, 43, 44, 45, 40, 93, 59, 123, 41,
125, 58, 44, 59, 40, 66, 63, 61, 59, 59,
59, 41, 93, 93, 123, 256, 125, 59, 259, 260,
261, 93, 41, 123, 81, 82, 83, 84, 44, 41,
41, 93, 263, 90, 95, 121, 122, 93, 0, 40,
126, 41, 93, 104, 105, 41, 41, 49, 40, 104,
39, 93, 105, 10, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 120, -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, 257, -1, 257, 257,
-1, -1, -1, 257, 258, 257, 257, 258, 262, -1,
264, 265, 257, 258, 266, 267, -1, 262, -1, 264,
265, -1, 276, 277, -1, 276, 277, -1, 257, 258,
-1, 276, 277, 262, -1, 264, 265, 257, 258, 257,
258, -1, 262, -1, 264, 265, -1, 276, 277, 268,
269, 270, 271, 272, 273, -1, 276, 277, 276, 277,
-1, -1, -1, -1, -1, -1, -1, 268, 269, 270,
271, 272, 273, -1, -1, -1, 268, 269, 270, 271,
272, 273, -1, -1, -1, -1, 268, 269, 270, 271,
272, 273, -1, -1, -1, -1, -1, 268, 269, 270,
271, 272, 273,
};
#define YYFINAL 1
#ifndef YYDEBUG
#define YYDEBUG 0
#endif
#define YYMAXTOKEN 287
#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,"ID","NUM","INT","VOID","CHAR","IF","ELSE","WHILE",
"RETURN","CDECL","STDCALL","EQ","NE","LT","GT","LE","GE","AND","OR",
"STRING_LITERAL","CHAR_LITERAL","FUN_DECLAR","FUN_DEFINE","INT_ARRAY",
"CHAR_ARRAY","INT_POINT","CHAR_POINT","VOID_POINT","INT_POINT_ARRAY",
"CHAR_POINT_ARRAY","LOW_ELSE",
};
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 ']' ';'",
"type_spec : INT",
"type_spec : VOID",
"type_spec : CHAR",
"type_spec : error",
"fun_declar : type_spec fun_tag '(' LL params ')' comp_stmt",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -