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

📄 y.tab.c

📁 这是我自己实现的一个微小的编译程序,附可执行代码的虚拟机,是理解编译原理的绝好材料.
💻 C
📖 第 1 页 / 共 3 页
字号:
#if defined(__STDC__) || defined(__cplusplus)
#define YYCONST const
#define YYPARAMS(x) x
#define YYDEFUN(name, arglist, args) name(args)
#define YYAND ,
#define YYPTR void *
#else
#define YYCONST
#define YYPARAMS(x) ()
#define YYDEFUN(name, arglist, args) name arglist args;
#define YYAND ;
#define YYPTR char *
#endif
#ifndef lint
YYCONST static char yysccsid[] = "@(#)yaccpar	1.8 (Berkeley +Cygnus.28) 01/20/91";
#endif
#define YYBYACC 1
#ifndef YYDONT_INCLUDE_STDIO
#include <stdio.h>
#endif
#ifdef __cplusplus
#include <stdlib.h> /* for malloc/realloc/free */
#endif
#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 59 "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
static YYCONST 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,
};
static YYCONST 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,
};
static YYCONST 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,
};
static YYCONST short yydgoto[] = {                                       1,
    2,    4,    8,    5,   29,   17,   25,   18,   19,   20,
   21,   22,   23,   24,   37,   38,   39,   40,   41,   42,
   43,
};
static YYCONST 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,
};
static YYCONST 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,
};
static YYCONST 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
static YYCONST 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,
};
static YYCONST 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
static YYCONST char *YYCONST 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",
};
static YYCONST char *YYCONST 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 YYLEX yylex()
#define YYEMPTY -1
#define yyclearin (yychar=(YYEMPTY))
#define yyerrok (yyerrflag=0)
#ifndef YYINITDEPTH
#define YYINITDEPTH 200
#endif
#ifdef YYSTACKSIZE
#ifndef YYMAXDEPTH
#define YYMAXDEPTH YYSTACKSIZE
#endif
#else
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 500
#define YYMAXDEPTH 500
#endif
#endif
#ifndef YYMAXSTACKSIZE
#define YYMAXSTACKSIZE 10000
#endif
int yydebug;
int yynerrs;
int yyerrflag;
int yychar;
YYSTYPE yyval;
YYSTYPE yylval;
static short *yyss;
static YYSTYPE *yyvs;
static int yystacksize;
#define yyfree(x) free(x)
extern int yylex();

static YYPTR
YYDEFUN (yymalloc, (bytes), unsigned bytes)
{
    YYPTR ptr = (YYPTR) malloc (bytes);
    if (ptr != 0) return (ptr);
    yyerror ("yyparse: memory exhausted");
    return (0);
}

static YYPTR
YYDEFUN (yyrealloc, (old, bytes), YYPTR old YYAND unsigned bytes)
{
    YYPTR ptr = (YYPTR) realloc (old, bytes);
    if (ptr != 0) return (ptr);
    yyerror ("yyparse: memory exhausted");
    return (0);
}

static int
#ifdef __GNUC__
inline
#endif
yygrow ()
{
    int old_stacksize = yystacksize;
    short *new_yyss;
    YYSTYPE *new_yyvs;

    if (yystacksize == YYMAXSTACKSIZE)
        return (1);
    yystacksize += (yystacksize + 1 ) / 2;
    if (yystacksize > YYMAXSTACKSIZE)
        yystacksize = YYMAXSTACKSIZE;
#if YYDEBUG
    if (yydebug)
        printf("yydebug: growing stack size from %d to %d\n",
               old_stacksize, yystacksize);
#endif
    new_yyss = (short *) yyrealloc ((char *)yyss, yystacksize * sizeof (short));
    if (new_yyss == 0)
        return (1);
    new_yyvs = (YYSTYPE *) yyrealloc ((char *)yyvs, yystacksize * sizeof (YYSTYPE));
    if (new_yyvs == 0)
    {
        yyfree (new_yyss);
        return (1);
    }
    yyss = new_yyss;
    yyvs = new_yyvs;
    return (0);
}
#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;

⌨️ 快捷键说明

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