资源描述:
《实验五 SLR语法分析器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、《编译原理》实验报告实验序号: 05 实验项目名称:SLR语法分析器学 号姓 名专业、班实验地点指导教师实验时间一、实验目的及要求利用SLR文法的原理,掌握非递归预测分析的编程方法。二、实验设备(环境)及要求确定开发工具,如TC、VC、VC++、Delphi等;花一周时间熟悉开发工具。花一周时间确定被处理的语言的语法特点(初步确定,也可使用现成语言如Pascal、C等)。写好实验报告,编好程序。三、实验内容与步骤利用c语言实现以下SLR文法的分析程序。文法的开始符号为:E-->E+TE-->TT-->T*FT-->FF-->(E)F-->id实验步骤1、建立该文法的
2、分析表;2、编程实现LR分析程序。四、实验结果与数据处理1文法分析表:112.程序分析:实验源程序:#include#includeintAction[12][6]={105,0,0,104,0,0,0,106,0,0,0,-1,110,52,107,0,52,52,0,54,54,0,54,54,105,0,0,104,0,0,0,56,56,0,56,56,105,0,0,104,0,0,105,0,0,104,0,0,0,106,0,0,111,0,0,51,107,0,51,51,0,53,53,0,53,53,0,55,55,0,55
3、,55};intGoto[12][3]={1,2,3,0,0,0,0,0,0,0,0,0,8,2,3,0,0,0,0,9,3,0,0,10,0,0,0,0,0,0,0,0,0,0,0,0};charGrammar[20][10]={' '};charVT[10],VN[10];charAVT[6]={'i','+','*','(',')','#'};charGVN[3]={'E','T','F'};intvnNum,vtNum,stateNum=12;intVNum[10];intgrammarNum;11typedefstruct{char*base;char*top;}Sym
4、bolStack;typedefstruct{int*base;int*top;}StateStack;StateStackstate;SymbolStacksymbol;intScanGrammar(){FILE*fp=fopen("SLR文法.txt","r");FILE*tp;charsingleChar,nextChar;inti=0,j=0,k,count;while(!feof(fp)){fscanf(fp,"%c",&singleChar);if(singleChar=='?'){Grammar[i][j]=' ';break;}if(singleChar=='
5、'){Grammar[i][j]=' ';i++;j=0;continue;}if(singleChar=='-'){tp=fp;fscanf(tp,"%c",&nextChar);if(nextChar=='>'){fp=tp;continue;}}if(singleChar=='
6、'){Grammar[i+1][0]=Grammar[i][0];11Grammar[i][j]=' ';i++;j=1;continue;}Grammar[i][j]=singleChar;if(singleChar>='A'&&singleChar<='Z'){count=0;while(VN
7、[count]!=singleChar&&VN[count]!=' '){count++;}if(VN[count]==' '){vnNum=count+1;if(singleChar=='S'){j++;continue;}VN[count]=singleChar;vnNum=count+1;}}else{count=0;while(VT[count]!=singleChar&&VT[count]!=' '){count++;}if(VT[count]==' '){VT[count]=singleChar;vtNum=count+1;}}j++;}printf("输入的文
8、法:");for(k=0;k<=i;k++){j=0;11while(Grammar[k][j]!=' '){if(j==1){printf("->");}printf("%c",Grammar[k][j]);j++;}printf("");}count=0;printf("VT:");while(VT[count]!=' '){printf("%3c",VT[count]);count++;}VT[count]='#';vtNum=count+1;pri