资源描述:
《编译技术课程LL1文法分析器》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验三LL(1)文法预测分析模拟实验目的:编写程序完成一个LL(1)文法的预测分析过程实验内容:1.给定一个文法,如教材P94E→TE’E’→+TE’
2、εT→FT’T’→*FT’
3、εF→i
4、(E)2.构造预测分析表,将教材P95的表5.3存储与二维数组中3.设置分析栈,编写程序实现P95的表5.4的分析过程#include#include#include#defineLINE5#defineROW8charAnalStack[20];//分析栈charSpareStack[20];//剩余栈
5、charTerSymbol[20]={'i','+','-','*','/','(',')','#'};//终结符charNTerSymbol[20]={'E','D','T','B','F'};//非终结符intj=0,b=0,l=0,top=0;typedefstructLLType{charnonchar;//非终结符charproduction[5];//产生式intlength;//产生式长度}LL1Type;LL1Typee,d,d1,t,c,c1,f,f1;LL1TypePreTable[LINE][ROW];voidprint(){
6、inta;for(a=0;a<=top+1;++a)printf("%c",AnalStack[a]);printf("tt");}voidprint1(){intj;for(j=0;j
7、`e.length=2;d.nonchar='D';strcpy(d.production,"+TD");//E`→+TE`d.length=3;//d1.nonchar='D';//strcpy(d1.production,"-TD");//D→-TD//d1.length=3;d1.nonchar='D';strcpy(d1.production,".");//E`→εd1.length=1;t.nonchar='T';strcpy(t.production,"FB");//T→FT`t.length=2;c.nonchar='B';strcp
8、y(c.production,"*FB");//B→*FT`c.length=3;//c1.nonchar='B';//strcpy(c1.production,"/FB");//B→/FB//c1.length=3;c1.nonchar='B';strcpy(c1.production,".");//T`→εc1.length=1;f.nonchar='F';strcpy(f.production,"(E)");//F→(E)f.length=3;f1.nonchar='F';strcpy(f1.production,"i");//F→if1.l
9、ength=1;for(m=0;m10、3][3]=c;//PreTable[3][4]=c1;PreTable[3][6]=c1;PreTable[3][7]=c1;PreTable[4][0]=f1;PreTable[4][5]=f;printf("PleaseEnterAStringtoanalyze:");do{//读入分析串,放入剩余栈中scanf("%c",&ch);if(ch!='i'&&ch!='+'&&ch!='-'&&ch!='*'&&ch!='/'&&ch!='('&&ch!=')'&&ch!='#'){printf("Inputcharactersareilleg
11、al!");exit(1);}SpareStack[j]=ch;j++;}while(ch!='#');l=j;//分