www.pudn.com > compile_LR.rar > compile3.cpp
#include#include #include #include static int line=1; bool end=1; #define MAXLONG 20 void ACTION(char *chars); void Print(); struct Sentence{//句型结构体 char head;//句型开始字符 int ch_le;//该句型右部的长度 }; Sentence r[7]={{'S',1/*"S→E"*/},{'E',3/*"E→E+T"*/},{'E',1/*"E→T"*/},{'T',3/*"T→T*F"*/}, {'T',1/*"T→F"*/},{'F',3/*"F→(E)"*/},{'F',1/*"F→i"*/}};//句子变量表 struct Table{//定义一个结构体 int state;//状态变量 char V;//终结符或非终结符 char Ne_act[4];//下一个状态 }; Table SLR[45]= {{0,'i',"S5"},{0,'(',"S4"},{0,'E',"1"},{0,'T',"2"},{0,'F',"3"}, {1,'+',"S6"},{1,'#',"acc"}, {2,'+',"r2"},{2,'*',"S7"},{2,')',"r2"},{2,'#',"r2"}, {3,'+',"r4"},{3,'*',"r4"},{3,')',"r4"},{3,'#',"r4"}, {4,'i',"S5"},{4,'(',"S4"},{4,'E',"8"},{4,'T',"2"},{4,'F',"3"}, {5,'+',"r6"},{5,'*',"r6"},{5,')',"r6"},{5,'#',"r6"}, {6,'i',"S5"},{6,'(',"S4"},{6,'T',"9"},{6,'F',"3"}, {7,'i',"S5"},{7,'(',"S4"},{7,'F',"10"}, {8,'+',"S6"},{8,')',"S11"}, {9,'+',"r1"},{9,'*',"S7"},{9,')',"r1"},{9,'#',"r1"}, {10,'+',"r3"},{10,'*',"r3"},{10,')',"r3"},{10,'#',"r3"}, {11,'+',"r5"},{11,'*',"r5"},{11,')',"r5"},{11,'#',"r5"}}; class STACK_I{//定义一个字符栈类 private: int top;//栈顶指针 int bottom;//栈底指针 int stack[MAXLONG];//栈长 public: STACK_I()//栈初始化,构造函数 {top=0;bottom=0;} ~STACK_I(){}//析构函数 void PUSH_STACK(int num)//进栈操作 { if((top-bottom)>=MAXLONG-1)//判断是否栈空 cout<<"The STACK if full!"< 9) cout<<'('< =MAXLONG-1)//判断是否栈空 cout<<"The STACK if full!"< =0;i--)//字符串反向进栈 { char temp=chars[i]; PUSH_STACK(temp); } } void POP_STACK()//出栈顶元素 { if((top-bottom)==0)//判断栈空 cout<<"The STACK is empty!"< =0;j--) cout< =45) {//不能向下推导句子错误 cout<<"The sentence is wrong!"<