#ifndef INCLUDE
#define INCLUDE
#include
#include
#include
#include
//错误编码
#define PROEXP 0
#define IDERR 1
#define SEMAERR 2
#define EOFERR 3
#define BEGINERR 4
#define ENDERR 5
#define PROCEEXP 6
#define VREDEFINE 7
#define PREDEFINE 8
#define SUCCESS 29
#define input "in.pas"
#define output "out.txt"
#define N 30 //二元表最大长度
#define KEYSNUM 35 //关键字数
#define ERR
#define WLEN 11 //字长
#define MAXPAR 10 //过程参数个数
#define VLEN 30 //变量表长
#define PLEN 20 //过程表长
#define NLEN 10 //名长
#define PROGRAM 0
#define VAR 1
#define PROCEDURE 2
#define BEGIN 3
#define END 4
#define IF 5
#define THEN 6
#define ELSE 7
#define WHILE 8
#define DO 9
#define FOR 10
#define STEP 11
#define UNTIL 12
#define CALL 13
#define READ 14
#define WRITE 15
#define IDENT 16
#define CONST 17
#define DOU 31
#define SEMA 32
#define DOT 33
/***********************************************************************************
* 符号编码表 *
***********************************************************************************/
#define KEYWORD 1 //保留字,共有16种如program等
#define ID 2 //标志符 ,用户自定义的变量和过程
//#define CONST 3 //常数整型
#define OPER 4 //运算符
#define INTER 5 //界符
/***********************************************************************************
* 关键字表 *
***********************************************************************************/
char *keys[KEYSNUM] = { "program", //0
"var", //1
"procedure", //2
"begin", //3
"end", //4
"if", //5
"then", //6
"else", //7
"while", //8
"do", //9
"for", //10
"step", //11
"until", //12
"call", //13
"read", //14
"write" , //15
"ident", //16
"const", //17
"+", //18
"-", //19
"*", //20
"/", //21
":=", //22
"=", //23
"", //24
">", //25
">=", //26
" " "(", //29
")", //30
",", //31逗号
";", //32分号
".", //33句号
"\n" //34回车
};
/***********************************************************************************
* 全局变量 *
***********************************************************************************/
char ch; //从文件中读出字符
int p=0; //定位标志
//int table[N]; //二元组序列
struct
{ char name[NLEN];
int no;
}table[100];
int lines=0;//指示当前的行,用于递归下降分析时作为出错信息
int PC=0;//用于四元式地址的指示
/***********************************************************************************
* 字母判断IsLetter() *
***********************************************************************************/
int IsLetter(char c)
{
if ( (c>='a' && c='A' && c return 1;
else
return 0;
}
/***********************************************************************************
* 数字判断IsLetter() *
***********************************************************************************/
int IsDigit(char c)
{
if (c>='0' && c return 1;
else
return 0;
}
/***********************************************************************************
* 错误处理error() *
***********************************************************************************/
void error(int errno)
{
switch(errno)
{
case PROEXP:
printf("行号:%d 错误:应该为program!\n",lines);
break;
case IDERR:
printf("行号:%d 错误:标识符不正确!\n",lines);
break;
case SEMAERR:
printf("行号:%d 错误:应该有';'!\n",lines);
break;
case EOFERR:
printf("行号:%d 错误:文件不合法,有异常结束!\n",lines);
break;
case BEGINERR:
printf("行号:%d 错误:缺少begin!\n",lines);break;
case ENDERR:
printf("行号:%d 错误:缺少end!\n",lines);break;
case PROCEEXP:
printf("行号:%d 错误:缺少procedure!\n",lines);break;
case VREDEFINE:
printf("行号:%d 错误:该变量已经被定义过了!\n",lines);break;
case PREDEFINE:
printf("行号:%d 错误:该过程已经被定义过了!\n",lines);break;
case SUCCESS:
printf("编译成功\n",lines);return;
}
exit(1);
}
//所有函数声明
int Search(char *buf);
void Scan(FILE *fp);
int GetWord();
void PROG();
void enterp(char name[NLEN],int a,int b,int c,int addr);
void enterv(char *name,int no);
void IDP();
void PL();
void PRP();
void PP();
int GetGL(int state,int no);
int newtp();
void gen(char op[],int x,int y,int k);
void PASER();
int action(int state,int no);
int findv(char *c);
#endif