tiny语言的词法分析器-c++版-课程设计报告.doc

上传人:F****n 文档编号:98043144 上传时间:2019-09-07 格式:DOC 页数:13 大小:179KB
返回 下载 相关 举报
tiny语言的词法分析器-c++版-课程设计报告.doc_第1页
第1页 / 共13页
tiny语言的词法分析器-c++版-课程设计报告.doc_第2页
第2页 / 共13页
tiny语言的词法分析器-c++版-课程设计报告.doc_第3页
第3页 / 共13页
tiny语言的词法分析器-c++版-课程设计报告.doc_第4页
第4页 / 共13页
tiny语言的词法分析器-c++版-课程设计报告.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《tiny语言的词法分析器-c++版-课程设计报告.doc》由会员分享,可在线阅读,更多相关《tiny语言的词法分析器-c++版-课程设计报告.doc(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*endrepeat=until/

2、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 = ;Lex(string

3、 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

4、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 = )

5、/注释结束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 = NULL)if (i

6、sToken(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 endl;break;if

7、(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, unti

8、l, 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

9、 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;retur

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

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