1、词法分析器的构造 南邮编译原理实验一报告 告 报 实 验 学期)二学年2015 / 2016 第 ( 课程名称编译原理 词法分析器的构造实验名称日年实验时间月29 2016 4 计算机软件教学中心 指导单位 指导教师 班级学号wujun 学生姓名 计算机学院、软件计算机科学 学院(专 ) 系 业 与技术学院 实 验 报 告 实验名称 词法分析器的构造 指导教师 实验类型 设计 实验学时4 实验时间2016.4.29 一、 实验目的和要求 1、实验目的: 设计、编制、调试一个词法分析程序,对单词进行识别和编码,加深对词法分析原理的理解。 2、实验要求: 1)、允许用户自己输入源程序并保存为文件;
2、 2)、系统能够输出经过预处理后的源程序(去掉注释、换行、空格等); 3)、能够将该源程序中所有的单词根据其所属类型(整数、保留字、运算符、标识符等。定义的类C语言中的标识符只能以字母或下划线开头)进行归类显示,例如:识别保留字:if、int、for、while、do、return、break、continue等,其他的都识别为标识符;常数为无符号整形数;运算符包括:+、-、*、/、=、=、=、!=等;分隔符包括:,、;、(、)等; 4)、实现文件的读取操作,而不是将文本以字符串形式预存于程序中。文本内容为待分析的类C语言程序。 - 2 - 二、) (实验设备实验环境 硬件:计算机 Visua
3、l C +6.0 软件: 实验原理及内容二、实验内容:1 语言源程序文本文C设计并实现一个词法分析器,实现对指定位置的类件的读取,并能够对该源程序中的所有单词进行分类,指出其所属类型,实 现简单的词法分析操作。 语言源程序:例如下面为一段Cmain() a,b; int a = 10; b = a + 20; : 要求输出如下 2,main)( ,()(5 ,)(5 5,)( ,)(5 ,)(5 - 3 - ), (5 ,int)(1 2(,a) 5,,)( 2,b)( 5,;)( ,a)2( ,=)(4 ,10)(3 ,;)(5 2(,b) 4,=)( ,a)2( ,+)(4 3(,20)
4、,;)5( 5(,) 、实验原理状态转换图 2 - 4 - 3、实验代码: 实验代码: e #includ e #includ e #includ e #include #include #includ struct Char /创建一个结构用于存贮关键字char a15; ; ef struct Char CH; typed /定义关键字efault, CH keyWord67=auto,reak,case,cout,cin,char,const,continue,de,else,enum,endl,extern,loat,or,goto,if,main, oubl do,d ong,egi
5、ster,eturn,short,signed,sizeof,static, include,int,l e, string,struct,switch, ypedef,%union,%unsigned,void,stdio,whil cin,cout,catch,calss,ctype,stdlib,stream,export,iostream, - 5 - - 6 - e(si != n); whil i-; else if (si = / & si+1 = *) o d i+; != *); while(si != / | si-1 continue; else if (si = t |
6、 si = n | si = ?) aj+ = ; else aj+ = si; endl; cout系统经过预处理后的输出(去掉注释和换行): coutan; endl; 潣瑵?系统经过预处理后的输出(去掉注释、换行、空格等): en(a); i+) for (i = 0;i strlif(ai = ) continue; else coutai; coutendlendlendl; 词法分析 /void Handle(char s) char ch; en(s); j+) for (int j = 0; j strl char word20 = 0; int i = 0; ch = sj;
7、 if (ch = ) continue; else if (IsLetter(ch) o d wordi+ = ch; ch = s+j; e(IsLetter(ch)| IsDigit(ch)|ch=_); whil j-; wordi = 0; if(IsKeyWord(word) cout(1,word)endl; else - 7 - cout(2,word)endl; else if(IsDigit(ch) o d wordi+ = ch; ch = s+j; e(IsDigit(ch); whilj-; wordi = 0; cout(3,word)endl; else if(I
8、sSeparator(ch) word0 = ch; cout(5,word)|word0=| word0=&|word0=|) if(sj+1=word0 | sj+1=) word1 = s+j; cout(4,word)endl; else cout(4,word)endl; else if(word0=|word0=*|word0=/| word0=!|word0=%|word0=) if (sj+1 = =) word1 = s+j; cout(4,word)endl; else cout(4,word)endl; else if (word0 = ) if (sj+1 = n |s
9、j+1 = t | sj+1=|sj+1=0) word1 = s+j; cout(4,word)endl; else cout(4,word)endl; - 8 - 4(else ,word)endl; 无法识别字符 cout( cout; int main() int i; char b = 32,ch; FILE *fp; o d i = 0; char s10000; char a10000 = 0; e.txt,b+); fp=fopen(cod if(fp=NULL) coutCan not create fileendl; exit(0); fseek(fp,0,2); e(en
10、d with two?):endl; ease input you cod coutPl o d ch = getchar(); si+ = ch; if(ch = n | ch= t ) for (int j =0; j 4;j+) fputc(b,fp); fputc(ch,fp); e(si-1 != ? | si-2 != ?); whil si = 0; Scanner(s,a); endl; 潣瑵?词法分析如下: e(a); Handl ose(fp); fcl e(true); whil return 0; 、实验测试和截图 )、测试一截图1 - 9 - 输入代码: 输出结果: )测试二截图(2 - 10 - 输入代码: 输出结果: - 11 - 四、实验小结 (包括问题和解决方法、心得体会、意见与建议等) 在本实验中,我进一步学习了如何运用输入输出流,对文件进行读写操作。了解了什么是词法分析,熟悉了词法分析器的构造,更加深入了对词法分析原理的理解。程序将字符分为五类:保留字、标识符、整数、运算符、分隔符,对使用者输入的程序代码进行识别、分析和编码,简单分析了词法。实验过程中开始时由于对换行符(n)、制表符(t)、和注释等处理不好导致实验失败,后来预处理时讲它们全部换成空格符和把注释去掉才成功。 五、指导教师评语 - 12 - 成 绩 批阅人 日 期 - 13 -
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1