欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOC文档下载  

    天津XX大学编译原理实验2:语法分析.doc

    • 资源ID:4043632       资源大小:369KB        全文页数:14页
    • 资源格式: DOC        下载积分:16金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要16金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    天津XX大学编译原理实验2:语法分析.doc

    实验报告学院(系)名称:计算机与通信工程学院姓名学号专业班级实验项目实验二:语法分析课程名称编译原理课程代码实验时间2016年4月21日 第1、2节2016年4月26日 第3、4节2016年4月28日 第1、2节实验地点计算机软件实验室7-220批改意见成绩教师签字: 实验内容: 可选择LL1分析法、算符优先分析法、LR分析法之一,实现如下表达式文法的语法分析器:(1)EE+T | E-T | T(2)TT*F | T/F | F(3)FPF | P(4)P(E) | i实验目的:1掌握语法分析的基本概念和基本方法;2正确理解LL1分析法、算符优先分析法、LR分析法的设计与使用方法。实验要求:1按要求设计实现能识别上述文法所表示语言的语法分析器,并要求输出全部分析过程;2要求详细描述所选分析方法针对上述文法的分析表构造过程;3完成对所设计语法分析器的功能测试,并给出测试数据和实验结果;4为增加程序可读性,请在程序中进行适当注释说明;5整理上机步骤,总结经验和体会;6认真完成并按时提交实验报告。【实验过程记录(源程序、测试用例、测试结果及心得体会等)】使用的是LL(1)分析法,其分析表的构造方法和构造过程如下:实验源代码:#include<iostream>#include<cstring>#define size 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,int *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."for(i=length-1;i>=0;i-)(*pointer)+;stack(*pointer)=stri;int getcol(char top)switch(top)case '+':return 0;case '-':return 1;case '*':return 2;case '/':return 3;case '':return 4;case ')':return 5;case '#':return 6;case '(':return 7;case 'i':return 8;default:cout<<"Error! This character string is not this grammers 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 strsize;/接受字符串的数组 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=/ + - * / ) # ( i/* E */"error", "error", "error", "error", "error", "error", "error", "TX", "TX",/* X */"+TX", "-TX", "error", "error", "error", "empty", "empty", "error", "error",/* T */"error","error","error","error","error","error","error","FY","FY",/* Y */"empty","empty","*FY","/FY","error","empty","empty","error","error",/* F */"error","error","error","error","error","error","error","PZ","PZ",/* Z */"empty","empty","empty","empty","F","empty","empty","error","error",/* P */"error","error","error","error","error","error","error","(E)","i"cout<<"Please input character string: "cin>>str;length=getLength(str);strlength='#'strlength+1='0'/cout<<length<<endl;/cout<<table00; /已验证可以实现 cout<<"符号栈t当前符号t输入串tttt说明"<<endl; pointer+;stackpointer='#'pointer+;stackpointer='E'/初始化栈,使栈底是 #E while(strindex!='0')top=gettop(stack,pointer);switch(top)case 'E':i=0;/第1行 j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"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 'X':i=1;/第2行 j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"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 'T':i=2;/第3行 j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"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 'Y':i=3;/第4行 j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"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 'F':i=4;/第5行 j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"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 'Z':i=5;/第6行 j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"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 'P':i=6;/第7行 j=getcol(strindex);production=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"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 '+':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=true;break;default:cout<<"Error! This character string is not this grammers 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;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers 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;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers 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;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers 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;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers 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;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers 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;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;break;case 'i':switch(strindex)case 'i':cout<<"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 grammers 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 grammers sentence."<<endl;return 0;break;if(match)index+;match=false; return 0;实验代码测试结果:下面图片是以给定文法画出的一个语法树。其语法如图右上角

    注意事项

    本文(天津XX大学编译原理实验2:语法分析.doc)为本站会员(牧羊曲112)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开