天津理工大学编译原理实验2:语法分析_第1页
天津理工大学编译原理实验2:语法分析_第2页
天津理工大学编译原理实验2:语法分析_第3页
天津理工大学编译原理实验2:语法分析_第4页
天津理工大学编译原理实验2:语法分析_第5页
免费预览已结束,剩余8页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、人建田大殍实验报告学院(系)名称: 计算机与通信工程学院姓名学号专业班级实验项目实验二:语法分析课程名称编译原理课程代码实验时间2016年4月21日第1、2节2016年4月26日第3、4节2016年4月28日第1、2节实验地点计算机软件实验室7-220批改意见成绩教师签字:实验内容:可选择LL1分析法、算符优先分析法、LR分析法之一,实现如下表达式文法的语法分析器:(1) E- E+T | E-T | T(2) T-T*F | T/F | F(3) F-PF | P(4) P-(E) | i实验目的:1 .掌握语法分析的基本概念和基本方法;2 .正确理解LL1分析法、算符优先分析法、LR分析法

2、的设计与使用方法。实验要求:1 .按要求设计实现能识别上述文法所表示语言的语法分析器,并要求输出全部分析过程;2 .要求详细描述所选分析方法针对上述文法的分析表构造过程;3 .完成对所设计语法分析器的功能测试,并给出测试数据和实验结果;4 .为增加程序可读性,请在程序中进行适当注释说明;5 .整理上机步骤,总结经验和体会;6 .认真完成并按时提交实验报告。【实验过程记录(源程序、测试用例、测试结果及心得体会等)使用的是LL(1) 分析法,其分析表的构造方法和构造过程如下:实验源代码:#include<iostream>#include<cstring>#define s

3、ize 1024using namespace std;int getLength(char strsize)int i=0;while(stri!='0')i+;return i;int getstringLength(string str)int i=0;while(stri!='0')i+;return i;char gettop(char stacksize,int top)if(stacktop!='0')return stacktop;elsereturn '#'void popstack(char *stack,in

4、t *pointer)int p=*pointer;cout<<"tPopup "<<stackp<<" out!"stackp='0'(*pointer)-;void pushstack(char *stack,int *pointer,string str)int i=0;int length=getstringLength(str);cout<<" push "<<str<<" in stack reversed order.&qu

5、ot;for(i=length-1;i>=0;i-)(*pointer)+;stack(*pointer)=stri;int getcol(char top)switch(top)case '+':return 0;case '-':return 1;casereturn 2; case '/':return 3;case '"return 4;case ')':return 5;case '#':return 6;case '(':return 7;case 'i&

6、#39;:return 8;default:cout<<"Error! This character string is not this grammer's sentence."<<endl; return -1; void show(char strsize,int index)int length=getLength(str);if(index!=-1)cout<<"t"for(int i=index+1;i<length;i+)cout<<stri;int main()char str

7、size;/ 接受字符串的数组char stacksize;/进行比对的栈int pointer=-1;/ 指向栈顶的指针int length=0;/ 记录字符串长度int index=0;/ 记录输入字符串char top;int i,j;/i 表示行,j 表示列string production;bool match=false;string table79=/ +-*/ A )#( i/* E */ "error", "error", "error", "error", "error",

8、"error", "error", "TX", "TX",/* X */"+TX", "-TX", "error", "error", "error", "empty", "empty", "error", "error",/* T */"error","error","error&quo

9、t;,"error","error","error","error","FY","FY",/* Y */"empty","empty","*FY","/FY","error","empty","empty","error","error",/* F */"error","

10、;error","error","error","error","error","error","PZ","PZ",/* Z */"empty","empty","empty","empty","AF","empty","empty","error","error",/

11、* P */"error","error","error","error","error","error","error","(E)","i"cout<<"Please input character string: " cin>>str;length=getLength(str);strlength='#'strlength+1='0'/

12、cout<<length<<endl;/ cout<<table00;/已验证可以实现cout<<" 符号栈 t 当前符号t 输入串 tttt 说明 "<<endl;pointer+;stackpointer='#'pointer+;#Estackpointer='E'/ 初始化栈,使栈底是 while(strindex!='0') top=gettop(stack,pointer); switch(top) case 'E':i=0;/第 1 行 j

13、=getcol(strindex); production=tableij; if(production="error") cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"<<strinde

14、x<<"t"show(str,index);popstack(stack,&pointer);/说明可以进行分解非终结符 else cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case 'X':i=

15、1;第2行 j=getcol(strindex);production=tableij;if(production="error") cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;else if(production="empty") cout<<"n"show(stack,-1);cout<<"t"<<s

16、trindex<<"t" show(str,index);popstack(stack,&pointer);else/说明可以进行分解非终结符cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t" show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case 'T&#

17、39;:i=2;/第 3 行 j=getcol(strindex);production=tableij; if(production="error") cout<<"Error! This character string is not this grammer's sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"&

18、lt;<strindex<<"t"show(str,index);popstack(stack,&pointer);else/说明可以进行分解非终结符cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);break;case 

19、9;Y':i=3;/第 4 行j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t&quo

20、t;<<strindex<<"t"show(str,index);popstack(stack,&pointer);else/说明可以进行分解非终结符cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case

21、 'F':i=4;第5行j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t&qu

22、ot;<<strindex<<"t"show(str,index);popstack(stack,&pointer);else /说明可以进行分解非终结符cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);break;cas

23、e 'Z':i=5;/第 6 行j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"

24、t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);/说明可以进行分解非终结符 else cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t" show(str,index);popstack(stack,&pointer); pushstack(stack,&pointer,production); b

25、reak;case 'P':i=6;/第 7 行j=getcol(strindex);production=tableij; if(production="error") cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;else if(production="empty") cout<<"n"show(stack,-1);cout<

26、;<"t"<<strindex<<"t" show(str,index);popstack(stack,&pointer);else/说明可以进行分解非终结符cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t" show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,produ

27、ction); break;case '+':switch(strindex) case '+':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch + ! "/" Popup + and next char in."<<endl;popstack(stack,&pointer);m

28、atch=true;break;default:cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;break;case '-':switch(strindex)case '-':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(st

29、r,index);cout<<"tMatch - ! "/Popup - and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;break;case '*':switch(strindex)case &#

30、39;*':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch * ! "/Popup * and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This characte

31、r string is not this grammer's sentence."<<endl; return 0;break;case '/': switch(strindex) case '/':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch / ! "/Popup / and nex

32、t char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammer's sentence."<<endl;return 0;break;case '(':switch(strindex)case '(':cout<<"n"show(stack,-1);cout<<

33、;"t"<<strindex<<"t"show(str,index);cout<<"tMatch ( ! "/Popup ( and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammer's sentence."<<end

34、l; return 0;break;case ')':switch(strindex)case ')':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch ) ! "/Popup ) and next char in."<<endl;popstack(stack,&pointer);match

35、=true;break;default:cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;break;case 竺switch(strindex)case 竺cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"

36、;tMatch A ! "Popup A and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;break;case 'i':switch(strindex)case 'i':cout<<"

37、;n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch i ! "/Popup i and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this gramme

38、r's sentence."<<endl; return 0;break;case '#':switch(strindex)case '#':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch # ! 分析成功! "/Popup # and next char in."<<endl;return 0;default:cout<<"Error! This character string is not this grammer's sentence."<<endl; return 0;bre

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论