Tiny语言的词法分析器-C++版-课程设计报告(共22页)
精选优质文档-倾情为你奉上实 验 报 告 学号: 姓名: 专业:计算机科学与技术 班级: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<repeat=until/identifier(1 or more letters)read(write);:=二、构造Tiny语言DFAID:letter(letter)*Number: digit(digit)*三、根据DFA编写词法分析器#include<fstream>#include<string>#include<iostream>using namespace std;static int rowCounter = 1;/静态变量,用于存储行数static bool bracketExist = false;/判断注释存在与否,false为不存在class Lexpublic:ofstream output;string 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.open("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;/不处理,直接输出if (linei + 1 != NULL)i+;elsebreak;if (linei = '')/注释结束output << linei<<endl;bracketExist = false;if (bracketExist = false)/数字while (isdigit(linei)temp = temp * 10 + linei;if (!isdigit(linei + 1)output << "t" << rowCounter << ": " << "NUM, val= " << temp - '0' << endl;if (linei + 1 != NULL&&isdigit(linei + 1)i+;elsebreak;temp = 0;/符号while (!(isdigit(linei) | (linei >= 'a'&&linei <= 'z') | (linei >= 'A'&&linei <= 'Z') | linei = ' ' | linei = '' | linei = '')token = token + linei;if (isdigit(linei + 1) | (linei + 1 >= 'a'&&linei + 1 <= 'z') | (linei + 1 >= 'A'&&linei + 1 <= 'Z') | linei + 1 = ' ' | linei + 1 = '' | linei + 1 = '' | linei + 1 = NULL)if (isToken(token)output << "t" << rowCounter << ": " << token << endl;elseint j = 0;while (tokenj != '0')output << "t" << rowCounter << ": " << tokenj << endl;j+;elsei+;continue;if (linei + 1 != NULL)i+;elsebreak;token = ""/字母while (linei >= 'a'&&linei <= 'z') | (linei >= 'A'&&linei <= 'Z')str = str + linei;if (!(linei + 1 >= 'a'&&linei + 1 <= 'z') | (linei + 1 >= 'A'&&linei + 1 <= 'Z')if (isResearvedWord(str) /判断是否是保留字output << "t" << rowCounter << ": " << "Reversed Word: " << str << endl;break;elseoutput << "t" << rowCounter << ": " << "ID, name= " << str << endl;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", "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 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 = 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:静态变量,存储行号,每创建一个类的实例便加一。int temp:用于存储数字,并输出。static int bracketExist:静态变量,标记注释是否存在。string token, str分别用于临时存储读取的符号的字母串。五、算法总结 建立Lex class,并读取每一行,创建Lex的实例,在Lex中处理。先判断是否在注释范围内,若是,则输出注释内容,直至产生“”字符。若不在注释区内,则读取单个字符,根据DFA进行判断。若为符号,则当下一个字符不是符号时输出;若为数字,则继续往下读,直至下一个字符不是数字为止,输出。若为字母,继续读取,直至下一个字符不是字母,把这一串字母和预先定义的保留字比对,若是,则输出“Reserved word”,若不是,则输出“ID,name=”字样。一行处理完毕,便开始创建下一行实例,直至文件尾。数据记录和计算 Tiny测试程序结 论(结 果) 1: Sample program1: Sample 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 don't compute if x <= 0 6: Reversed Word: if6: NUM, val= 06: <6: ID, name= x6: Reversed Word: then6: don't 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: 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词法分析器。增强了自己的编程能力和水平技巧,尝试了很多以前没有尝试过的方法学习到了新知识。指导老师评 议 成绩评定: 指导教师签名:专心-专注-专业