资源描述:
《编译原理上机源代码lr语法分析器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、输入:3+4*6输出:27(当然中间包括LR(1)语法分析过程程序提供主要为了给大家提供一些方便,你懂得。。。呵呵如有问题,QQ:718561468来自:大连理工大学软件学院#includeusingnamespacestd;#include#include#include#includestringAnalyzeChart[16][11]={//LR文法的分析表"s5","e1","e1","s4","e2","e1","1","2","3
2、","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","
3、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","
4、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","er
5、ror","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",};stackdigit;queuedigit1;queueinputid;char*FinalSymbol[6]={"i","+","*","(",")","#"//终结符};char*
6、UnfinalSymbol[5]={"E","E'","T","T'","F"//非终结符};stackufstack;//状态栈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
7、(sign&s){s.i=0;memset(s.temp,'$',10);s.token=-1;}boolis_key_word(sign&s){inti,r;for(i=0;i<6;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')
8、(s.temp[0]>
9、='A'&&s.temp[0]<='Z'))b=true;elsereturnb;for(i=1;i='a'&&s.temp[i]<='z')
10、(s.temp[i]>='A'&&s.temp[i]<='Z')
11、(s.temp[i]>=