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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

Tiny语言的词法分析器C++版课程设计报告.docx

1、Tiny语言的词法分析器C+版课程设计报告实 验 报 告 学号: 姓名: 专业:计算机科学与技术 班级: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-

2、else*endrepeat=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; st

3、ring line = ; 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.ope

4、n(SampleOutput.txt, ios: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;/不处理,直接输

5、出 if (linei + 1 != NULL) i+; else break; 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) | li

6、nei + 1 = | linei + 1 = | linei + 1 = | linei + 1 = NULL) if (isToken(token) output t rowCounter : token endl; else int 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:

7、 str endl; break; else output t rowCounter : ID, name= str endl; break; if (linei + 1 != NULL) i+; str = ; if (linei + 1 != NULL) i+; else break; if (linei + 1 = NULL) if (linei = ;) output t rowCounter : linei; break; /清空,以备下一行读取 line = ; str = ; temp = 0; token = ; output endl; output.close(); boo

8、l isResearvedWord(string s)/存储保留字,并判断 string reservedWord8 = if, then, else, end, 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 j

9、udge = false; for (int i = 0; i 10; i+) if (s = tokeni) judge = true; break; return judge; ;int main() 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 =

10、0; remove(SampleOutput.txt); for (j = 0; j i; j+) Lex lex(linej); cout endl endl Writing file completed! endl endl endl; return 0;四、重要数据结构string line:用于存储每一行的字符,并逐个读取分析。string token:用于存储TINY语言的符号,并调用遍历进行判断。string reservedWord:用于存储TINY语言的保留字,遍历进行判断,若为真,则输出Reserved word。static int rowCounter:静态变量,存储行号

11、,每创建一个类的实例便加一。int temp:用于存储数字,并输出。static int bracketExist:静态变量,标记注释是否存在。string token, str分别用于临时存储读取的符号的字母串。五、算法总结 建立Lex class,并读取每一行,创建Lex的实例,在Lex中处理。先判断是否在注释范围内,若是,则输出注释内容,直至产生“”字符。若不在注释区内,则读取单个字符,根据DFA进行判断。若为符号,则当下一个字符不是符号时输出;若为数字,则继续往下读,直至下一个字符不是数字为止,输出。若为字母,继续读取,直至下一个字符不是字母,把这一串字母和预先定义的保留字比对,若是,

12、则输出“Reserved word”,若不是,则输出“ID,name=”字样。一行处理完毕,便开始创建下一行实例,直至文件尾。数据记录和计算 Tiny测试程序结 论(结 果) 1: Sample program 1: Sample program2: in TINY language - 2: in TINY language -3: computes factorial 3: computes factorial4: 4: 5: read x; input an integer 5: Reversed Word: read 5: ID, name= x 5: ; 5: input an in

13、teger 6: if 0 x then dont compute if x = 0 6: Reversed Word: if 6: NUM, val= 0 6: 6: ID, name= x 6: Reversed Word: then 6: dont compute if x = 0 7: fact := 1; 7: ID, name= fact 7: := 7: NUM, val= 1 7: ;8: repeat 8: Reversed Word: repeat9: fact := fact * x; 9: ID, name= fact 9: := 9: ID, name= fact 9

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

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

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