ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:409.88KB ,
资源ID:454493      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/454493.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(编译原理实验语法器递归下降子程序实现解析.docx)为本站会员(b****2)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

编译原理实验语法器递归下降子程序实现解析.docx

1、编译原理实验语法器递归下降子程序实现解析编译原理程序设计实验报告表达式语法分析器的设计班级:计算机1306班 姓名:张涛 学号:20133967实验目标:用递归下降子程序设计实现表达式语法分析器实验内容:概要设计:通过对实验一的此法分析器的程序稍加改造,使其能够输出正确的表达式的token序列。然后利用LL(1)分析法实现语法分析。数据结构:int op=0; /当前判断进度char ch; /当前字符char nowword10=; /当前单词char operate4=+,-,*,/; /运算符char bound2=(,); /界符struct Token int code; char

2、ch10; /Token定义struct Token tokenlist50; /Token数组struct Token tokentemp; /临时Token变量流程图:关键函数:int IsLetter(char ch) /判断ch是否为字母int IsDigit(char ch) /判断ch是否为数字int Iskey(char *string) /判断是否为关键字int Isbound(char ch) /判断是否为界符int Isboundnum(char ch) /给出界符所在token值void T(); /分析子程序void F(); /分析子程序void E1(); /分析子

3、程序void E(); /分析子程序void T1(); /分析子程序源程序代码:(加入注释)#include#include#include#includeint op=0; /当前判断进度char ch; /当前字符char nowword10=; /当前单词char operate4=+,-,*,/;char bound2=(,);struct Token int code; char ch10;struct Token tokenlist50;struct Token tokentemp;void T();void F();void E1();void E();void T1();in

4、t IsLetter(char ch) /判断ch是否为字母 int i; for(i=0;i=a&ch=A&ch=Z) return 1; return 0; int IsDigit(char ch) /判断ch是否为数字 int i; for(i=0;i=0&ch=9) return 1; return 0; int Isbound(char ch) int i; for(i=0;i2;i+) if(ch=boundi) return i+1; return 0;int Isoperate(char ch) int i; for(i=0;i4;i+) if(ch=operatei) ret

5、urn i+3; return 0;int main() FILE *fp; int q=0,m=0; char sour200= ; printf(请将源文件置于以下位置并按以下方式命名:F:2.txtn); if(fp=fopen(F:2.txt,r)=NULL) printf(文件未找到!n); else while(!feof(fp) if(isspace(ch=fgetc(fp); else sourq=ch; q+; int p=0; printf(输入句子为:n); for(p;p=q;p+) printf(%c,sourp); printf(n); int state=0,no

6、wlen=0; BOOLEAN OK=TRUE,ERR=FALSE; int i,flagpoint=0; for(i=0;iq;i+) switch(state) case 0: ch=souri; if(Isbound(ch) if(ERR) printf(无法识别n); ERR=FALSE; OK=TRUE; else if(!OK) printf(标识符n,nowword); tokentemp.code=10; int i=0; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; OK=TRUE; state=4; else if(

7、IsDigit(ch) if(OK) memset(nowword,0,strlen(nowword); nowlen=0; nowwordnowlen=ch; nowlen+; state=3; OK=FALSE; break; else nowwordnowlen=ch; nowlen+; else if(IsLetter(ch) if(OK) memset(nowword,0,strlen(nowword); nowlen=0; nowwordnowlen=ch; nowlen+; OK=FALSE; if(souri+1=#) printf(标识符n,nowword); struct

8、Token tokentemp; tokentemp.code=10; int i=0; for(i;i=nowlen;i+) tokentemp.chi=nowwordi; tokenlistm=tokentemp; m+; else nowwordnowlen=ch; nowlen+; else if(Isoperate(ch) if(!OK) printf(标识符n,nowword); tokentemp.code=10; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; OK=TRUE; printf(运算符n,Isoperate(

9、ch),ch); tokentemp.code=Isoperate(ch); tokentemp.ch10=ch; tokenlistm=tokentemp; m+; break; case 3: if(IsLetter(ch) printf(错误n); nowwordnowlen=ch; nowlen+; ERR=FALSE; state=0; break; if(IsDigit(ch=souri) nowwordnowlen=ch; nowlen+; else if(souri=.&flagpoint=0) flagpoint=1; nowwordnowlen=ch; nowlen+; e

10、lse printf(数字n,nowword); i-; state=0; OK=TRUE; tokentemp.code=20; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; break; case 4: i-; printf(界符n,Isbound(ch),ch); tokentemp.code=Isbound(ch); tokentemp.ch10=ch; tokenlistm=tokentemp; m+; state=0; OK=TRUE; break; int io=0; for(io;io=m;io+) printf(tok

11、enlist%d.code值为%dn,io,tokenlistio.code); E(); if(op=m) printf(OK!); else printf(WRONG!); return 0;void E() T(); E1();void E1() if(tokenlistop.code=3|tokenlistop.code=4) tokenlistop+; T(); E1(); void T1() if(tokenlistop.code=5|tokenlistop.code=6) tokenlistop+; F(); T1(); void T() F(); T1();void F() i

12、f(tokenlistop.code=10|tokenlistop.code=20) tokenlistop+; else if(tokenlistop.code=1) tokenlistop+; E(); if(tokenlistop.code=2) tokenlistop+; else printf(WRONG!); /exit(1); else printf(WRONG!); /exit(1); 程序运行结果:(截屏)输入:(Aa+Bb)*(88.2/3)#注:如需运行请将文件放置F盘,并命名为:2.txt输出:思考问题回答: 在递归下降子程序中要注意对于错误情况的处理,以防程序无限循环,无法结束。解决这个问题的方法就是设置两个位移变量进行判断即可。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1