编译原理实践教程2010(5)

2019-03-15 18:40

/************ PL0.h *************/

#include

#define NRW 11 // number of reserved words #define TXMAX 500 // length of identifier table

#define MAXNUMLEN 14 // maximum number of digits in numbers

#define NSYM 10 // maximum number of symbols in array ssym and csym #define MAXIDLEN 10 // length of identifiers

#define MAXADDRESS 32767 // maximum address

#define MAXLEVEL 32 // maximum depth of nesting block #define CXMAX 500 // size of code array

#define MAXSYM 30 // maximum number of symbols

#define STACKSIZE 1000 // maximum storage

enum symtype {

SYM_NULL,

SYM_IDENTIFIER, SYM_NUMBER, SYM_PLUS, SYM_MINUS, SYM_TIMES, SYM_SLASH, SYM_ODD, SYM_EQU, SYM_NEQ, SYM_LES, SYM_LEQ, SYM_GTR, SYM_GEQ, SYM_LPAREN, SYM_RPAREN, SYM_COMMA, SYM_SEMICOLON, SYM_PERIOD, SYM_BECOMES, SYM_BEGIN, SYM_END, SYM_IF, SYM_THEN,

20

SYM_WHILE, SYM_DO, SYM_CALL, SYM_CONST, SYM_VAR,

SYM_PROCEDURE };

enum idtype {

ID_CONSTANT, ID_VARIABLE, ID_PROCEDURE };

enum opcode {

LIT, OPR, LOD, STO, CAL, INT, JMP, JPC };

enum oprcode {

OPR_RET, OPR_NEG, OPR_ADD, OPR_MIN, OPR_MUL, OPR_DIV, OPR_ODD, OPR_EQU, OPR_NEQ, OPR_LES, OPR_LEQ, OPR_GTR, OPR_GEQ };

typedef struct {

int f; // function code int l; // level

int a; // displacement address } instruction;

////////////////////////////////////////////////////////////////////// char* err_msg[] = {

/* 0 */ \

/* 1 */ \

/* 2 */ \

/* 3 */ \/* 4 */ \must be an identifier to follow 'const', 'var', or 'procedure'.\/* 5 */ \

/* 6 */ \

21

/* 7 */ \

/* 8 */ \/* 9 */ \/* 10 */ \

/* 11 */ \/* 12 */ \/* 13 */ \

/* 14 */ \/* 15 */ \/* 18 */ \/* 16 */ \

/* 17 */ \/* 19 */ \

/* 20 */ \

/* 21 */ \/* 22 */ \

/* 23 */ \

/* 24 */ \/* 25 */ \/* 26 */ \/* 27 */ \/* 28 */ \/* 29 */ \/* 30 */ \/* 31 */ \

/* 32 */ \};

////////////////////////////////////////////////////////////////////// char ch; // last character read int sym; // last symbol read

char id[MAXIDLEN + 1]; // last identifier read int num; // last number read int cc; // character count int ll; // line length int kk; int err;

int cx; // index of current instruction to be generated. int level = 0; int tx = 0;

char line[80];

instruction code[CXMAX];

22

char* word[NRW + 1] = {

\

\ \};

int wsym[NRW + 1] = {

SYM_NULL, SYM_BEGIN, SYM_CALL, SYM_CONST, SYM_DO, SYM_END, SYM_IF, SYM_ODD, SYM_PROCEDURE, SYM_THEN, SYM_VAR, SYM_WHILE };

int ssym[NSYM + 1] = {

SYM_NULL, SYM_PLUS, SYM_MINUS, SYM_TIMES, SYM_SLASH,

SYM_LPAREN, SYM_RPAREN, SYM_EQU, SYM_COMMA, SYM_PERIOD, SYM_SEMICOLON };

char csym[NSYM + 1] = {

' ', '+', '-', '*', '/', '(', ')', '=', ',', '.', ';' };

#define MAXINS 8

char* mnemonic[MAXINS] = {

\};

typedef struct {

char name[MAXIDLEN + 1]; int kind; int value; } comtab;

comtab table[TXMAX];

23

typedef struct {

char name[MAXIDLEN + 1]; int kind; short level; short address; } mask;

FILE* infile;

// EOF PL0.h

24


编译原理实践教程2010(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:数据结构与算法实验题答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: