资源描述
输入:3+4*6输出:27(当然中间包括LR(1)语法分析过程程序提供主要为了给大家提供一些方便,你懂得。呵呵如有问题,QQ:718561468来自:大连理工大学软件学院#includeusing namespace std;#include#include#include#includestring AnalyzeChart1611=/LR文法的分析表s5, e1, e1, s4, e2, e1, 1, 2, 3, e1, e1,e3, s6, e1, e3, e2, acc, error, error, error, s7, e1,e6, r2, s8, e6, r2, r2, error, error, error, r2, s9,e6, r4, r4, e6, r4, r4, error, error, error, r4, r4,s5, e1, e1, s4, e2, e1, 10, 2, 3, e1, e1,e5, r6, r6, e3, r6, r6, error, error, error, r6, r6, s5, e1, e1, s4, e2, e1, error, 11, 3, e1, e1,s5, e1, e1, s4, e2, e1, error, 12, 3, e1, e1,s5, e1, e1, s4, e2, e1, error, error, 13, e1, e1,s5, e1, e1, s4, e1, e1, error, error, 14, e1, e1, e3, s6, e1, e3, s15, r3, error, error, error, s9, e1,e6, r1, s8, e6, r1, r1, error, error, error, r1, s9,e6, r7, s8, e6, r7, r7, error, error, error, r7, s9,e6, r3, r3, e6, r3, r3, error, error, error, r3, r3,e6, r8, r8, e6, r8, r8, error, error, error, r8, r8,e6, r5, r5, e6, r5, r5, error, error, error, r5, r5,;stack digit;queue digit1;queue inputid;char *FinalSymbol6= i,+,*,(,),#/终结符;char *UnfinalSymbol5=E,E,T,T,F/非终结符;stack ufstack;/状态栈void initialize() ufstack.push(0); inputid.push(0); char * keyword6 = for,if,then,else,while,do;int flag1=-1,flag2=-1;typedef struct signint line;int token;char temp10;int i;void reset(sign & s)s.i = 0;memset(s.temp,$,10);s.token = -1;bool is_key_word(sign & s)int i,r;for(i=0;i=a & s.temp0=A & s.temp0=Z) )b = true;elsereturn b;for(i=1;i=a & s.tempi=A & s.tempi=0 & s.tempi=9);elseb = false;return b;return b;bool is_num(sign & s) digit.push(0);bool b = true;char c;int state = 12;for(int i=0;i=0 & c=0 & c=0 & c=0 & c=0 & c=0 & c=0 & c=9) state=18;else return false;break;return b;int handle(sign & s)s.temps.i = 0;if(strlen(s.temp)=0)return 0;if(is_key_word(s)cout(s.token,keywords.token-1);inputid.push(s.token); return s.token; else if(is_id(s) cout(10,s.temp);inputid.push(10);return 10; else if(is_num(s) cout(11,s.temp);inputid.push(11); int i=0; float n=0; int n1=0; while(is.i) n1=n1*10;if(s.tempi=.)n1=1;i+;elsen=n*10+(s.tempi-0); i+; if(n1!=0) n=n/n1; / couts.i s.temp nendl;digit1.push(n);n=0;reset(s);return 11;elsereturn 0;reset(s);void scan(FILE * fp)sign mark = 1,-1,$,$,$,$,$,$,$,$,$,$,0;char ch;doch = fgetc(fp);if(ch=n)mark.line+;handle(mark);else if(ch= )handle(mark);else if(ch=+)handle(mark);cout(13,ch);inputid.push(13);else if(ch=-)handle(mark);cout(14,ch);inputid.push(14);else if(ch=*)handle (mark);cout(15,ch);inputid.push(15);else if(ch=/)handle (mark);cout(16,ch);inputid.push(16);else if(ch=:)handle (mark);mark.tempmark.i+ = ch;cout(17,ch);inputid.push(17);else if(ch=)handle (mark);cout(20,ch)handle (mark);cout(23,ch);inputid.push(23);else if(ch=)if(mark.tempmark.i-1=:)cout(18,:=);reset(mark);inputid.push(18);elsehandle(mark);cout(25,ch);inputid.push(25);else if(ch=;)handle (mark);cout(26,ch);inputid.push(26);else if(ch=()handle (mark);cout(27,ch);inputid.push(27);else if(ch=)handle (mark);cout(28,ch);inputid.push(28);elsemark.tempmark.i+ = ch;/handle(mark);while(ch!=#);coutendl;int main(int argc ,char *argv) initialize();/初始化stack FILE * fp;fp = fopen(test.txt,r);scan(fp);/coutinputid.size()0) z-;while(true) int state=-1,sflag2=0; char temp1=ufstack.top(); int sflag1=inputid.front(); /coutsflag1endl;if(sflag1=11)flag1=0;else if(sflag1=13)flag1=1;else if(sflag1=15)flag1=2;else if(sflag1=27)flag1=3;else if(sflag1=28)flag1=4;else if(sflag1=0)flag1=5;else if(sflag1=14)flag1=9;else if(sflag1=16)flag1=10;else flag1=-1;if(flag1=-1)coutUnexpected symbol!endl; state=temp1-0;/coutstateVVVVVflag1=0 & ufstack.top()0)state=state+sta*10; elseufstack.push(t1); cout(state, flag1) -AnalyzeChartstateflag1.c_str ()-endl; if(strcmp(AnalyzeChartstateflag1.c_str (),s5)=0)/移近 ufstack.push(i);ufstack.push(5);cout移进idendl;/ip+;digit.push(digit1.front();digit1.pop();coutinputid.front();inputid.pop();else if(strcmp(AnalyzeChartstateflag1.c_str (),s4)=0)/移近 ufstack.push();ufstack.push(4);cout移进 ( endl;/ip+;inputid.pop();else if(strcmp(AnalyzeChartstateflag1.c_str (),s6)=0)/移近 ufstack.push(+);ufstack.push(6);cout移进 + endl;/ip+;inputid.pop();else if(strcmp(AnalyzeChartstateflag1.c_str (),s7)=0)/移近 ufstack.push(-);ufstack.push(7);cout移进 - endl;/ip+;inputid.pop();else if(strcmp(AnalyzeChartstateflag1.c_str (),s8)=0)/移近 ufstack.push(*);ufstack.push(8);cout移进 * endl;/ip+;inputid.pop();else if(strcmp(AnalyzeChartstateflag1.c_str (),s9)=0)/移近 ufstack.push(/);ufstack.push(9);cout移进 / endl;/ip+;inputid.pop(); else if(strcmp(AnalyzeChartstateflag1.c_str (),r1)=0)/归约 for(int i=0;i6;i+) ufstack.pop();coutE+Tendl;float ntop=digit.top();/digit.pop();digit.pop();coutntop+digit.top()=;ntop+=digit.top();digit.pop();digit.push(ntop);coutdigit.top()endl;int st=ufstack.top()-0;ufstack.push(E);char ch=*AnalyzeChartst6.c_str ();if(strcmp(AnalyzeChartst6.c_str (),10)=0)ufstack.push(*AnalyzeChartst6.c_str ();ufstack.push(*(AnalyzeChartst6.c_str ()+1);elseufstack.push(ch); else if(strcmp(AnalyzeChartstateflag1.c_str (),r7)=0)/归约 for(int i=0;i6;i+) ufstack.pop();coutE-Tendl;float ntop=digit.top();/digit.pop();digit.pop();coutdigit.top()-ntop=;ntop=digit.top()-ntop;digit.pop();digit.push(ntop);coutdigit.top()endl;int st=ufstack.top()-0;ufstack.push(E);char ch=*AnalyzeChartst6.c_str ();if(strcmp(AnalyzeChartst6.c_str (),10)=0)ufstack.push(*AnalyzeChartst6.c_str ();ufstack.push(*(AnalyzeChartst6.c_str ()+1);elseufstack.push(ch);else if(strcmp(AnalyzeChartstateflag1.c_str (),r2)=0)/归约 ufstack.pop();ufstack.pop();coutTendl;int st=ufstack.top()-0;ufstack.push(E);char ch=*AnalyzeChartst6.c_str ();if(strcmp(AnalyzeChartst6.c_str (),10)=0)ufstack.push(*AnalyzeChartst6.c_str ();ufstack.push(*(AnalyzeChartst6.c_str ()+1);elseufstack.push(ch);else if(strcmp(AnalyzeChartstateflag1.c_str (),r3)=0)/归约 for(int i=0;i6;i+) ufstack.pop();coutT*Fendl;float ntop=digit.top();digit.pop();/digit.pop();coutntop*digit.top()=;ntop=ntop*digit.top();digit.pop();digit.push(ntop);coutntopendl;int st=ufstack.top()-0;ufstack.push(T);char ch=*AnalyzeChartst7.c_str ();if(strcmp(AnalyzeChartst7.c_str (),11)=0)ufstack.push(*AnalyzeChartst7.c_str ();ufstack.push(*(AnalyzeChartst7.c_str ()+1);else if(strcmp(AnalyzeChartst7.c_str (),12)=0)ufstack.push(*AnalyzeChartst7.c_str ();ufstack.push(*(AnalyzeChartst7.c_str ()+1);elseufstack.push(ch);else if(strcmp(AnalyzeChartstateflag1.c_str (),r8)=0)/归约 for(int i=0;i6;i+) ufstack.pop();coutT/Fendl;float ntop=digit.top();digit.pop();/digit.pop();coutdigit.top()/ntop=;if(ntop!=0)ntop=digit.top()/ntop;elsecouterror,除数不能是零endl;ntop=0; coutntopendl;digit.pop();digit.push(ntop);/coutntop_digit.top()endl;int st=ufstack.top()-0;ufstack.push(T);char ch=*AnalyzeChartst7.c_str ();if(strcmp(AnalyzeChartst7.c_str (),11)=0)ufstack.push(*AnalyzeChartst7.c_str ();ufstack.push(*(AnalyzeChartst7.c_str ()+1);else if(strcmp(AnalyzeChartst7.c_str (),12)=0)ufstack.push(*AnalyzeChartst7.c_str ();ufstack.push(*(AnalyzeChartst7.c_str ()+1);elseufstack.push(ch);else if(strcmp(AnalyzeChartstateflag1.c_str (),r4)=0)/归约 ufstack.pop();ufstack.pop();coutFendl;int st=ufstack.top()-0;ufstack.push(T);char ch=*AnalyzeChartst7.c_str ();if(strcmp(AnalyzeChartst7.c_str (),11)=0)ufstack.push(*AnalyzeChartst7.c_str ();ufstack.push(*(AnalyzeChartst7.c_str ()+1);else if(strcmp(AnalyzeChartst7.c_str (),12)=0)ufstack.push(*AnalyzeChartst7.c_str ();ufstack.push(*(AnalyzeChartst7.c_str ()+1);elseufstack.push(ch);else if(strcmp(AnalyzeChartstateflag1.c_str (),r5)=0)/归约 for(int i=0;i6;i+) ufstack.pop();cout(E)endl; int st=ufstack.top()-0; ufstack.push(F);char ch=*AnalyzeChartst8.c_str ();if(strcmp(AnalyzeChartst8.c_str (),13)=0)ufstack.push(*AnalyzeChartst8.c_str ();ufstack.push(*(AnalyzeChartst8.c_str ()+1);else if(strcmp(AnalyzeChartst8.c_str (),14)=0)ufstack.push(*AnalyzeChartst8.c_str ();ufstack.push(*(AnalyzeChartst8.c_str ()+1);elseufstack.push(ch);else if(strcmp(AnalyzeChartstateflag1.c_str (),r6)=0)/归约 for(int i=0;i2;i+) ufstack.pop();coutidendl;int st=ufstack.top()-0;ufstack.push(F);char ch=*AnalyzeChartst8.c_str ();if(strcmp(AnalyzeChartst8.c_str (),13)=0)ufstack.push(*AnalyzeChartst8.c_str ();ufstack.push(*(AnalyzeChartst8.c_str ()+1);else if(strcmp(AnalyzeChartst8.c_str (),14)=0)ufstack.push(*AnalyzeChartst8.c_str ();ufstack.push(*(AnalyzeChartst8.c_str ()+1);elseufstack.push(ch);else if(strcmp(AnalyzeChartstateflag1.c_str (),acc)=0)coutAnalyze complate! endl;inputid.pop();break;else if(strcmp(AnalyzeChartstateflag1.c_str (),e2)=0)cout不配对的右括号! endl;inputid.pop();else if(strcmp(AnalyzeChartstateflag1.c_str (),e3)=0)cout缺少运算符 endl;inputid.pop();else if(strcmp(AnalyzeChartstateflag1.c_str (),e1)=0)cout缺少运算对象 endl;/ufstack.push(i);/ufstack.push(5);inputid.pop();else if(strcmp(AnalyzeChartstateflag1.c_str (),e4)=0)cout缺少右括号 endl;ufstack.push();ufstack.push(11);else if(strcmp(AnalyzeChartstateflag1.c_str (),e5)=0)cout算符重复 endl;inputid.pop();else /MX,a=一个产生式 coutStrange error happen !endl;break;h+; cout最终结果是digit.top()endl; return 0;
展开阅读全文