Tiny语言的词法分析器-C++版-课程设计报告(共22页)

上传人:风*** 文档编号:58261106 上传时间:2022-02-27 格式:DOC 页数:13 大小:182KB
收藏 版权申诉 举报 下载
Tiny语言的词法分析器-C++版-课程设计报告(共22页)_第1页
第1页 / 共13页
Tiny语言的词法分析器-C++版-课程设计报告(共22页)_第2页
第2页 / 共13页
Tiny语言的词法分析器-C++版-课程设计报告(共22页)_第3页
第3页 / 共13页
资源描述:

《Tiny语言的词法分析器-C++版-课程设计报告(共22页)》由会员分享,可在线阅读,更多相关《Tiny语言的词法分析器-C++版-课程设计报告(共22页)(13页珍藏版)》请在装配图网上搜索。

1、精选优质文档-倾情为你奉上实 验 报 告 学号: 姓名: 专业:计算机科学与技术 班级:2班 第9周 课程名称 编译原理课程设计 实验课时8实验项目手工构造Tiny语言的词法分析器实验时间7-10周实验目的 熟悉Tiny语言词法;构造DFA;设计数据类型、数据结构;用C+实现Tiny语言的词法分析器实验环境 Windows 10 专业版 Microsoft Visual Studio 2013实验内容(算法、程序、步骤和方法) 一、Tiny语言记号Reserved wordsSpecial SymbolsOtherif+number(1 or more digits)then-else*end

2、repeat=until/identifier(1 or more letters)read(write);:=二、构造Tiny语言DFAID:letter(letter)*Number: digit(digit)*三、根据DFA编写词法分析器#include#include#includeusing namespace std;static int rowCounter = 1;/静态变量,用于存储行数static bool bracketExist = false;/判断注释存在与否,false为不存在class Lexpublic:ofstream output;string line

3、= ;Lex(string inputLine)line = inputLine;scan(Trim(line);rowCounter+;string Trim(string &str)/函数用于去除每行前后空格int s = str.find_first_not_of( t);int e = str.find_last_not_of( t);str = str.substr(s, e - s + 1);str += 0;return str;void scan(string inputLine)ofstream output;output.open(SampleOutput.txt, ios

4、:app);string line = inputLine;int i = 0;string str = ;int temp;string token = ;output rowCounter : line endl;/输出每行while (linei != 0)/根据DFA扫描并判断if (linei = )/注释bracketExist = true;if (bracketExist = true)output t rowCounter : ;while (linei != )output linei;/不处理,直接输出if (linei + 1 != NULL)i+;elsebreak;

5、if (linei = )/注释结束output lineiendl;bracketExist = false;if (bracketExist = false)/数字while (isdigit(linei)temp = temp * 10 + linei;if (!isdigit(linei + 1)output t rowCounter : NUM, val= temp - 0 = a&linei = A&linei = a&linei + 1 = A&linei + 1 = Z) | linei + 1 = | linei + 1 = | linei + 1 = | linei + 1

6、 = NULL)if (isToken(token)output t rowCounter : token endl;elseint j = 0;while (tokenj != 0)output t rowCounter : tokenj = a&linei = A&linei = a&linei + 1 = A&linei + 1 = Z)if (isResearvedWord(str) /判断是否是保留字output t rowCounter : Reversed Word: str endl;break;elseoutput t rowCounter : ID, name= str e

7、ndl;break;if (linei + 1 != NULL)i+;str = ;if (linei + 1 != NULL)i+;elsebreak;if (linei + 1 = NULL)if (linei = ;)output t rowCounter : linei;break;/清空,以备下一行读取line = ;str = ;temp = 0;token = ;output endl;output.close();bool isResearvedWord(string s)/存储保留字,并判断string reservedWord8 = if, then, else, end,

8、 repeat, until, read, write ;bool judge = false;for (int i = 0; i 8; i+)if (s = reservedWordi)judge = true;break;return judge;bool isToken(string s)/存储符号,并判断string token10 = +, -, *, /, =, , (, ), ;, := ;bool judge = false;for (int i = 0; i 10; i+)if (s = tokeni)judge = true;break;return judge;int m

9、ain()ifstream input;input.open(SampleInput.tny);string line50;int i = 0;while (getline(input, linei)/cout linei endl;i+;input.close();cout endl endl Reading source file completed! endl;int j = 0;remove(SampleOutput.txt);for (j = 0; j i; j+)Lex lex(linej);cout endl endl Writing file completed! endl e

10、ndl endl;return 0;四、重要数据结构string line:用于存储每一行的字符,并逐个读取分析。string token:用于存储TINY语言的符号,并调用遍历进行判断。string reservedWord:用于存储TINY语言的保留字,遍历进行判断,若为真,则输出Reserved word。static int rowCounter:静态变量,存储行号,每创建一个类的实例便加一。int temp:用于存储数字,并输出。static int bracketExist:静态变量,标记注释是否存在。string token, str分别用于临时存储读取的符号的字母串。五、算法总

11、结 建立Lex class,并读取每一行,创建Lex的实例,在Lex中处理。先判断是否在注释范围内,若是,则输出注释内容,直至产生“”字符。若不在注释区内,则读取单个字符,根据DFA进行判断。若为符号,则当下一个字符不是符号时输出;若为数字,则继续往下读,直至下一个字符不是数字为止,输出。若为字母,继续读取,直至下一个字符不是字母,把这一串字母和预先定义的保留字比对,若是,则输出“Reserved word”,若不是,则输出“ID,name=”字样。一行处理完毕,便开始创建下一行实例,直至文件尾。数据记录和计算 Tiny测试程序结 论(结 果) 1: Sample program1: Samp

12、le program2: in TINY language -2: in TINY language -3: computes factorial3: computes factorial4: 4: 5: read x; input an integer 5: Reversed Word: read5: ID, name= x5: ;5: input an integer 6: if 0 x then dont compute if x = 0 6: Reversed Word: if6: NUM, val= 06: 6: ID, name= x6: Reversed Word: then6:

13、 dont compute if x = 0 7: fact := 1;7: ID, name= fact7: :=7: NUM, val= 17: ;8: repeat8: Reversed Word: repeat9: fact := fact * x;9: ID, name= fact9: :=9: ID, name= fact9: *9: ID, name= x9: ;10: x := x - 1;10: ID, name= x10: :=10: ID, name= x10: -10: NUM, val= 110: ;11: until x = 0;11: Reversed Word:

14、 until11: ID, name= x11: =11: NUM, val= 011: ;12: write fact output factorial of x 12: Reversed Word: write12: ID, name= fact12: output factorial of x 13: end13: Reversed Word: end小 结 顺利完成实验,熟悉了Tiny语言和其词法。根据语言和词法规则,顺利构造DFA。成功用C+语言,根据构造的DFA,实现了Tiny词法分析器。增强了自己的编程能力和水平技巧,尝试了很多以前没有尝试过的方法学习到了新知识。指导老师评 议 成绩评定: 指导教师签名:专心-专注-专业

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!