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

上传人:第*** 文档编号:30570417 上传时间:2018-01-30 格式:DOC 页数:11 大小:453.50KB
返回 下载 相关 举报
编译原理实验语法分析器递归下降子程序实现_第1页
第1页 / 共11页
编译原理实验语法分析器递归下降子程序实现_第2页
第2页 / 共11页
编译原理实验语法分析器递归下降子程序实现_第3页
第3页 / 共11页
编译原理实验语法分析器递归下降子程序实现_第4页
第4页 / 共11页
编译原理实验语法分析器递归下降子程序实现_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《编译原理实验语法分析器递归下降子程序实现》由会员分享,可在线阅读,更多相关《编译原理实验语法分析器递归下降子程序实现(11页珍藏版)》请在金锄头文库上搜索。

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

2、uct 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(); /分析子程序void E

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

4、(char ch) /判断 ch 是否为字母int i;for(i=0;i=a&ch=A&ch=0&ch标识符n,nowword);tokentemp.code=10; int i=0;tokentemp.ch10=nowword10;tokenlistm=tokentemp;m+;OK=TRUE;state=4;else if(IsDigit(ch)if(OK)memset(nowword,0,strlen(nowword);nowlen=0;nowwordnowlen=ch;nowlen+;state=3;OK=FALSE;break;elsenowwordnowlen=ch;nowlen

5、+;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 Token tokentemp;tokentemp.code=10;int i=0;for(i;i标识符n,nowword);tokentemp.code=10;tokentemp.ch10=nowword10;tokenlistm=tokentemp;m+;OK=TRUE;printf(运算符n,Isope

6、rate(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+;elseprintf(数字n,n

7、owword);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(tokenlist%d.code 值为%dn,io,tokenlistio.co

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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号