下载
加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 天津理工大学编译原理实验2语法分析

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

天津理工大学编译原理实验2语法分析

郑志熊
2018-03-04 0人阅读 举报 0 0 暂无简介

简介:本文档为《天津理工大学编译原理实验2语法分析doc》,可适用于领域

天津理工大学编译原理实验语法分析实验报告学院(系)名称:计算机与通信工程学院姓名学号专业班级实验项目实验二:语法分析课程名称编译原理课程代码年月日第、节实验时间年月日第、节实验地点计算机软件实验室年月日第、节批改意见成绩教师签字:实验内容:可选择LL分析法、算符优先分析法、LR分析法之一实现如下表达式文法的语法分析器:()EET|ET|T()TT*F|TF|F()FP^F|P()P(E)|i实验目的:(掌握语法分析的基本概念和基本方法(正确理解LL分析法、算符优先分析法、LR分析法的设计与使用方法。实验要求:(按要求设计实现能识别上述文法所表示语言的语法分析器并要求输出全部分析过程(要求详细描述所选分析方法针对上述文法的分析表构造过程(完成对所设计语法分析器的功能测试并给出测试数据和实验结果(为增加程序可读性请在程序中进行适当注释说明(整理上机步骤总结经验和体会(认真完成并按时提交实验报告。第页共页【实验过程记录(源程序、测试用例、测试结果及心得体会等)】使用的是LL()分析法其分析表的构造方法和构造过程如下:实验源代码:#include<iostream>#include<cstring>#definesizeusingnamespacestdintgetLength(charstrsize){inti=while(stri!='')ireturni}intgetstringLength(stringstr){inti=while(stri!='')ireturni}chargettop(charstacksize,inttop){if(stacktop!='')returnstacktopelsereturn'#'}voidpopstack(char*stack,int*pointer){intp=*pointercout<<"tPopup"<<stackp<<"out!"stackp=''(*pointer)}voidpushstack(char*stack,int*pointer,stringstr){inti=intlength=getstringLength(str)cout<<"push"<<str<<"instackreversedorder"for(i=lengthi>=i){(*pointer)第页共页stack(*pointer)=stri}}intgetcol(chartop){switch(top){case'':returncase'':returncase'*':returncase'':returncase'^':returncase')':returncase'#':returncase'(':returncase'i':returndefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}}voidshow(charstrsize,intindex){intlength=getLength(str)if(index!=)cout<<"t"for(inti=indexi<lengthi)cout<<stri}intmain(){charstrsize接受字符串的数组charstacksize进行比对的栈intpointer=指向栈顶的指针intlength=记录字符串长度intindex=记录输入字符串第页共页chartopinti,ji表示行j表示列stringproductionboolmatch=falsestringtable={*^)#(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<<"Pleaseinputcharacterstring:"cin>>strlength=getLength(str)strlength='#'strlength=''cout<<length<<endlcout<<table已验证可以实现cout<<"符号栈t当前符号t输入串tttt说明"<<endlpointerstackpointer='#'pointerstackpointer='E'初始化栈使栈底是#Ewhile(strindex!=''){top=gettop(stack,pointer)switch(top){case'E':i=第行j=getcol(strindex)production=tableijif(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}elseif(production=="empty"){cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)第页共页}else说明可以进行分解非终结符{cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)pushstack(stack,pointer,production)}breakcase'X':i=第行j=getcol(strindex)production=tableijif(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}elseif(production=="empty"){cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)}else说明可以进行分解非终结符{cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)pushstack(stack,pointer,production)}breakcase'T':i=第行j=getcol(strindex)production=tableijif(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endl第页共页return}elseif(production=="empty"){cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)}else说明可以进行分解非终结符{cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)pushstack(stack,pointer,production)}breakcase'Y':i=第行j=getcol(strindex)production=tableijif(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}elseif(production=="empty"){cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)}else说明可以进行分解非终结符{cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)pushstack(stack,pointer,production)第页共页}breakcase'F':i=第行j=getcol(strindex)production=tableijif(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}elseif(production=="empty"){cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)}else说明可以进行分解非终结符{cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)pushstack(stack,pointer,production)}breakcase'Z':i=第行j=getcol(strindex)production=tableijif(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}elseif(production=="empty"){cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)第页共页}else说明可以进行分解非终结符{cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)pushstack(stack,pointer,production)}breakcase'P':i=第行j=getcol(strindex)production=tableijif(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}elseif(production=="empty"){cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)}else说明可以进行分解非终结符{cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)popstack(stack,pointer)pushstack(stack,pointer,production)}breakcase'':switch(strindex){case'':cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"第页共页show(str,index)cout<<"tMatch!""Popupandnextcharin"<<endlpopstack(stack,pointer)match=truebreakdefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}breakcase'':switch(strindex){case'':cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)cout<<"tMatch!"Popupandnextcharin"<<endlpopstack(stack,pointer)match=truebreakdefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}breakcase'*':switch(strindex){case'*':cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)cout<<"tMatch*!"Popup*andnextcharin"<<endlpopstack(stack,pointer)match=truebreakdefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}breakcase'':第页共页switch(strindex){case'':cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)cout<<"tMatch!"Popupandnextcharin"<<endlpopstack(stack,pointer)match=truebreakdefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}breakcase'(':switch(strindex){case'(':cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)cout<<"tMatch(!"Popup(andnextcharin"<<endlpopstack(stack,pointer)match=truebreakdefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}breakcase')':switch(strindex){case')':cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)cout<<"tMatch)!"Popup)andnextcharin"<<endlpopstack(stack,pointer)match=truebreak第页共页default:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}breakcase'^':switch(strindex){case'^':cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)cout<<"tMatch^!"Popup^andnextcharin"<<endlpopstack(stack,pointer)match=truebreakdefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}breakcase'i':switch(strindex){case'i':cout<<"n"show(stack,)cout<<"t"<<strindex<<"t"show(str,index)cout<<"tMatchi!"Popupiandnextcharin"<<endlpopstack(stack,pointer)match=truebreakdefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}breakcase'#':switch(strindex){case'#':cout<<"n"show(stack,)第页共页cout<<"t"<<strindex<<"t"show(str,index)cout<<"tMatch#!分析成功~"Popup#andnextcharin"<<endlreturndefault:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence"<<endlreturn}break}if(match){indexmatch=false}}return}实验代码测试结果:下面图片是以给定文法画出的一个语法树。其语法如图右上角第页共页第页共页

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/23

天津理工大学编译原理实验2语法分析

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利