⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 y.tab.c

📁 c—语言的句法分析器。读入一个C--语言程序
💻 C
📖 第 1 页 / 共 4 页
字号:
#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
/*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",

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -