#include <iostream.h> #include <string.h> #include <stdio.h> #define STACKSIZE 100 struct list //存放预测分析表 { char termin; //用于存放终结符 char non_ter; //用于存放非终结符 char beget[10]; //用于存放产生式 }; struct stack //定义堆栈stack { int top; //定义top为-1表示该栈为空 char items[STACKSIZE]; };
void push(struct stack *ps,char x[10]) //入栈 { ps->items[++(ps->top)]=x; return; }
int empty(struct stack *ps) //判断栈是否为空 { if(ps->top==-1) return true; else return false; }
char pop(struct stack *ps) //出栈 { if(empty(ps)) { printf("%","栈为空"); } return (ps->items[ps->top--]); }
char top1(struct stack *ps) //取栈顶元素 { char x; x=ps->items[ps->top]; return x; }
struct stack s={-1,'#'};
struct list yuce[]={{'i','E','TA'},{'i','T','FB'},{'i','F','i'},{'+','A','+TA'},{'+','B','0'},{'*','B','*FB'},{'(','E','TA'},{'(','T','FB'},{'(','F','(E)'},{')','A','0'},{')','B','0'},{'#','A','0'},{'#','B','0'}};
int judgement(char expression[50]) //进行判断输入字符串第一位 { char p,top2,a=0; p= expression[a]; top2=top1(&s); while(top2!='#' && p!='#') { if(top2==p) { cout<<"匹配"<<endl; break; } else { switch (p) { case 'i': { char x=top1(&s); // X=top() for (int i=0;i<3;i++) { if(yuce[i].non_ter==x) { pop(&s); //pop push(&s,yuce[i].beget); //push:用yuce[i].beget代替yuce[i].non_ter cout<<yuce[i].non_ter<<"->"<<yuce[i].beget<<endl; break; } } if (i>=3) { return false; //stop } } case '+': { char x=top1(&s); for(int i=3;i<5;i++) { if(yuce[i].non_ter==x) { pop(&s); //pop push(&s,yuce[i].beget); //push:用yuce[i].beget代替yuce[i].non_ter cout<<yuce[i].non_ter<<"->"<<yuce[i].beget<<endl; break; } } if(i>=5) { return false; //stop } } case '*': { char x=top1(&s); if(yuce[6].non_ter==x) { pop(&s); //pop push(&s,yuce[6].beget); //push:用yuce[i].beget代替yuce[i].non_ter cout<<yuce[6].non_ter<<"->"<<yuce[6].beget<<endl; break; } else { return false; //stop } } case '(': { char x=top1(&s); for(int i=6;i<9;i++) { if(yuce[i].non_ter==x) { pop(&s); //pop push(&s,yuce[i].beget); //push:用yuce[i].beget代替yuce[i].non_ter cout<<yuce[i].non_ter<<"->"<<yuce[i].beget<<endl; break; } } if(i>=9) { return false; //stop } } case ')': { char x=top1(&s); for(int i=9;i<11;i++) { if(yuce[i].non_ter==x) { pop(&s); //pop push(&s,yuce[i].beget); //push:用yuce[i].beget代替yuce[i].non_ter cout<<yuce[i].non_ter<<"->"<<yuce[i].beget<<endl; break; } } if(i>=11) { return false; //stop } } case '#': { char x=top1(&s); for(int i=11;i<13;i++) { if(yuce[i].non_ter==x) { pop(&s); //pop push(&s,yuce[i].beget); //push:用yuce[i].beget代替yuce[i].non_ter cout<<yuce[i].non_ter<<"->"<<yuce[i].beget<<endl; break; } } if(i>=13) { return false; break; //stop } } default: { return false; break; } } top2=top(&s); p=expression[a++]; } return true; } void main() { char expression[50]; //用于存放输入符号串 cout<<"表达式文法为:E->E+T|T"<<endl; cout<<" T->T*F|F"<<endl; cout<<" F->i|(E)"<<endl; cout<<"消除左递归后为:E->TA; A=E'"<<endl; cout<<" A->+TA|ε "<<endl; cout<<" T->FB F=T'"<<ednl; cout<<" B->*FB|ε "<<endl; cout<<" F->i|(E) "<<ednl; cout<<"输入分析符号串(并以#结束):"; cin>>expression; cout<<"分析符号串为:"<<expression<<endl; if(judgment(char expression[50])) cout<<"语法正确"<<endl; else cout<<"语法错误"<<endl; }