www.pudn.com > hsgbiyiyuanli.rar > define.h
#include#include #include #include #include #include #include #include using namespace std; #define MAXLEN 127 #define SIZE 10 //单个标示符长度不能超过10个字符 #define true 1 #define false 0 ////以下为将用到的函数定义 void scaner(); void sort(); char *readline(); char fetch(); //到缓冲区中取字符 int unfetch( int n ); //将指向缓冲区的指针退后n个,仅仅指针退后,取到变量中的内容不变。 void dataproces(); //数字处理 int symproces(); //符号处理 void alfaproces(); //字母处理 int char_process(); //字符常数处理 int searchkeyID(char *s); //查找关键字 void searchflag(char *s); //查填符号表中是否有标识符,若有则返回入口,若无,则添加并返回入口 int fill_flagtab_int( int temp);//将立即数整数填入符号表并得到一个索引 int fill_flagtab_float(float temp);//将立即数浮点数填入符号表并得到一个索引 int fill_flagtab_char(char *temp); //int fill_flagtab_bool(bool temp); ///////////// int synditer(); //语法分析 void get_token(); //从token文件读取一行 int fill_quad(struct tk &); int varst( tk &token );//处理变量说明 int IDS(struct tk &);//匹配产生式 int push_stack(struct tk); int pop_stack(struct tk &); int fill_type(struct tk token,int type); int STL(struct tk &);//处理复合句中的语句串,匹配产生式 -> ;|<>,遇到end 返回, //使用语义子程序BACKPATCH(q,t) vector STSORT(struct tk &);//处理赋值语句及散转处理各控制语句 vector IFS(); //IF语句 vector REPEATS();//REPEAT语句 int pop_vector(vector &s_chain);//将出口链弹空 int BE(vector &,vector &); int BT(vector &,vector &); int BF(vector &,vector &); vector MERG(vector &P1,vector &P2);//合并 int BACKPATCH( vector &,int ); //回填 vector AE();//算术运算 int precede(struct tk,struct tk);//比较优先级 int OPRATE(struct tk ,struct tk, struct tk);//进行规约运算 int push_functor(struct tk );//将算符压栈 int pop_functor(struct tk &); int get_top_fun(struct tk &);//将算数压栈 int push_term(struct tk ); int pop_term(struct tk &); int GENCODE(int ,int ,int ,int);//生成四元式 int print_quad();//打印四元式 int tran_to_asm();//由四元式产生汇编代码 ///以上为将用到的函数定义 ///以下为将用到的变量 FILE *in; FILE *out; FILE *testin; FILE *testout; FILE *otherout; //关键字列表 struct key//关键字表 { char word[SIZE]; int ID ; }keyword[] = { {"and",1},{"array",2},{"begin",3},{"bool",4},{"call",5},{"case",6},{"char",7},{"constant",8},{"do",9},{"else",10}, {"end",11},{"false",12},{"for",13},{"if",14},{"input",15},{"integer",16},{"not",17},{"of",18},{"or",19}, {"output",20},{"procedure",21},{"program",22},{"read",23},{"real",24},{"repeat",25},{"set",26},{"then",27}, {"to",28},{"true",29},{"until",30},{"var",31},{"while",32},{"write",33} ,{"SYS",34}}; enum flagkind { simplevar, constval,arrayname,coursename,program,T_PLACE=128,SYS=65535,} ;//简单变量,常数名,数组名,过程名 enum flagtype { int_num, real_num,bool_num,char_num} type; //整数、实数,布尔,字符型 struct flag { //short unsigned index; char name[SIZE]; int kind; int type; union { int val_int; float val_float; bool val_bool; char val_char[6]; } val; } FLAGTAB[MAXLEN];//符号表 char *head = (char *)malloc(sizeof(char *)); //指向缓冲区的指针 char newline[MAXLEN]; short unsigned int f_amount = 0;//标示符表中的标示符个数-1 struct tk{ int kind_code; int val; } token; struct tk stack[MAXLEN];//用于IDS中的堆栈 struct{ int opcode; int ag1; int ag2; int result; }quad[MAXLEN];//静态的四元式表,最大长度127*/ short int quad_index = 0; short int stack_index = 0; struct tk functor[MAXLEN];//算符栈 int fun_index=0; struct tk term[MAXLEN];//算数栈 int term_index=0; int NXQ;//初值1 bool y_fuzhi=true;//用来区分简单赋值与算术运算后的赋值