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-e
2、lse*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; str
3、ing line = ; Lex(string inputLine) line = inputLine; scan(Trim(line); rowCounter+; string Trim(string &str)/函数用于去除每行前后空格 int s = ( t); int e = ( t); str = (s, e - s + 1); str += 0; return str; void scan(string inputLine) ofstream output; (, ios:app); string line = inputLine; int i = 0; string str =
4、; 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+; else break; if (linei = )/注释结束 output lineiendl; b
5、racketExist = 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 = NULL) if (isToken(token) ou
6、tput 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: str endl; break; else output t rowCounter : ID, name= str endl; break; if (lin
7、ei + 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; (); bool isResearvedWord(string s)/存储保留字,并判断 string reservedWord8 = if, then, else, end, repeat, u
8、ntil, 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;
9、;int main() ifstream input; (); string line50; int i = 0; while (getline(input, linei) /cout linei endl; i+; (); cout endl endl Reading source file completed! endl; int j = 0; remove(); for (j = 0; j i; j+) Lex lex(linej); cout endl endl Writing file completed! endl endl endl; return 0;四、重要数据结构strin
10、g line:用于存储每一行的字符,并逐个读取分析。string token:用于存储TINY语言的符号,并调用遍历进行判断。string reservedWord:用于存储TINY语言的保留字,遍历进行判断,若为真,则输出Reserved word。static int rowCounter:静态变量,存储行号,每创建一个类的实例便加一。int temp:用于存储数字,并输出。static int bracketExist:静态变量,标记注释是否存在。string token, str分别用于临时存储读取的符号的字母串。五、算法总结 建立Lex class,并读取每一行,创建Lex的实例,在
11、Lex中处理。先判断是否在注释范围内,若是,则输出注释内容,直至产生“”字符。若不在注释区内,则读取单个字符,根据DFA进行判断。若为符号,则当下一个字符不是符号时输出;若为数字,则继续往下读,直至下一个字符不是数字为止,输出。若为字母,继续读取,直至下一个字符不是字母,把这一串字母和预先定义的保留字比对,若是,则输出“Reserved word”,若不是,则输出“ID,name=”字样。一行处理完毕,便开始创建下一行实例,直至文件尾。数据记录和计算 Tiny测试程序结 论(结 果) 1: Sample program 1: Sample program2: in TINY language
12、- 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 integer 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 =
13、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: * 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: un
14、til 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