输入:3+4*6输出:27(当然中间包括LR(1)语法分析过程程序提供主要为了给大家提供一些方便,你懂得。。。呵呵如有问题,QQ:718561468来自:大连理工大学软件学院#includeiostreamusingnamespacestd;#includestack#includequeue#includefstream#includestringstringAnalyzeChart[16][11]={//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,};stackfloatdigit;queuefloatdigit1;queueintinputid;char*FinalSymbol[6]={i,+,*,(,),#//终结符};char*UnfinalSymbol[5]={E,E',T,T',F//非终结符};stackcharufstack;//状态栈voidinitialize(){ufstack.push('0');inputid.push(0);}char*keyword[6]={for,if,then,else,while,do};intflag1=-1,flag2=-1;typedefstructsign{intline;inttoken;chartemp[10];inti;};voidreset(sign&s){s.i=0;memset(s.temp,'$',10);s.token=-1;}boolis_key_word(sign&s){inti,r;for(i=0;i6;i++){r=memcmp(s.temp,keyword[i],s.i);if(r==0){s.token=i+1;returntrue;break;}}returnfalse;}boolis_id(sign&s){inti;boolb=false;if((s.temp[0]='a'&&s.temp[0]='z')|(s.temp[0]='A'&&s.temp[0]='Z'))b=true;elsereturnb;for(i=1;istrlen(s.temp);i++){if((s.temp[i]='a'&&s.temp[i]='z')|(s.temp[i]='A'&&s.temp[i]='Z')|(s.temp[i]='0'&&s.temp[i]='9'));else{b=false;returnb;}}returnb;}boolis_num(sign&s){digit.push(0);boolb=true;charc;intstate=12;for(inti=0;istrlen(s.temp);i++){c=s.temp[i];inttn;switch(state){case12:{if(c='0'&&c='9'||c=='-'){state=13;}elsereturnfalse;break;}case13:{if(c='0'&&c='9')state=13;elseif(c=='.')state=14;elseif(c=='E'||c=='e')state=16;elsereturnfalse;break;}case14:{if(c='0'&&c='9')state=15;elsereturnfalse;break;}case15:{if(c='0'&&c='9')state=15;elseif(c=='E'||c=='e')state=16;elsereturnfalse;break;}case16:{if(c=='+'||c=='-')state=17;elseif(c='0'&&c='9')state=18;elsereturnfalse;break;}case17:{if(c='0'&&c='9')state=18;elsereturnfalse;break;}case18:{if(c='0'&&c='9')state=18;elsereturnfalse;break;}}}returnb;}inthandle(sign&s){s.temp[s.i]='\0';if(strlen(s.temp)==0)return0;if(is_key_word(s)){cout(s.token,keyword[s.token-1]);inputid.push(s.token);returns.token;}elseif(is_id(s)){cout(10,s.temp);inputid.push(10);return10;}elseif(is_num(s)){cout(11,s.temp);inputid.push(11);inti=0;floatn=0;intn1=0;while(is.i){n1=n1*10;if(s.temp[i]=='.'){n1=1;i++;}else{n=n*10+(s.temp[i]-'0');i++;}}if(n1!=0)n=n/n1;//couts.is.tempnendl;digit1.push(n);n=0;reset(s);return11;}else{return0;}reset(s);}voidscan(FILE*fp){signmark={1,-1,{'$','$','$','$','$','$','$','$','$','$'},0};charch;do{ch=fgetc(fp);if(ch=='\n'){mark.line++;handle(mark);}elseif(ch=='')handle(mark);elseif(ch=='+'){handle(mark);cout(13,ch);inputid.push(13);}elseif(ch=='-'){handle(mark);cout(14,ch);inputid.push(14);}elseif(ch=='*'){handle(mark);cout(15,ch);inputid.push(15);}elseif(ch=='/'){handle(mark);cout(16,ch);inputid.push(16);}elseif(ch==':'){handle(mark);mark.temp[mark.i++]=ch;cout(17,ch);inputid.push(17);}elseif(ch==''){handle(mark);cout(20,ch);inputid.push(20);}elseif(ch==''){handle(mark);cout(23,ch);inputid.push(23);}elseif(ch=='='){if(mark.temp[mark.i-1]==':'){cout(18,:=);reset(mark);inputid.push(18);}else{handle(mark);cout(25,ch);inputid.push(25);}}elseif(ch==';'){handle(mark);cout(26,ch);inputid.push(26);}elseif(ch=='('){handle(mark);cout(27,ch);inputid.push(27);}elseif(ch==')'){handle(mark);cout(28,ch);inputid.push(28);}else{mark.temp[mark.i++]=ch;//handle(mark);}}while(ch!='#');coutendl;}intmain(intargc,char*argv[]){initialize();//初始化stackFILE*fp;fp=fopen(test.txt,r);scan(fp);//coutinputid.size()endl;stringtemp;inth=0;intz=30;inputid.pop();inputid.push(0);while((!inputid.empty())&&z0){z--;while(true){intstate=-1,sflag2=0;chartemp1=ufstack.top();intsflag1=inputid.front();//coutsflag1endl;{if(sflag1==11)flag1=0;elseif(sflag1==13)flag1=1;elseif(sflag1==15)flag1=2;elseif(sflag1==27)flag1=3;elseif(sflag1==28)flag1=4;elseif(sflag1==0)flag1=5;elseif(sflag1==14)flag1=9;elseif(sflag1==16)flag1=10;elseflag1=-1;}if(flag1==-1){coutUnexpectedsymbol!endl;}state=temp1-'0';//coutstateVVVVVflag1endl;charsta=0;chart1=ufstack.top();ufstack.pop();if(!ufstack.empty()&&ufstack.top()='0'&&ufstack.top()='9'){sta=ufstack.top()-'0';if(sta0)state=state+sta*10;}elseufstack.push(t1);cout(state,flag1)------AnalyzeChart[state][flag1].c_str()-----endl;if(strcmp(AnalyzeChart[state][flag1].c_str(),s5)==0)//移近{ufstack.push('i');ufstack.push('5');cout移进idendl;//ip++;digit.push(digit1.front());digit1.pop();coutinputid.front();inputid.pop();}elseif(strcmp(Ana