《西安邮电大学 编译原理词法分析》由会员分享,可在线阅读,更多相关《西安邮电大学 编译原理词法分析(13页珍藏版)》请在金锄头文库上搜索。
1、 西 安 邮 电 大 学 (计算机学院)课内实验报告实验名称: 词法分析器 专业名称:班 级: 学生姓名:学号(8位):指导教师:实验日期: 年月日一。 实验目的及实验环境设计一个词法分析器,并更好的理解词法分析实现原理,掌握程序设计语言中各类单词的词法分析方法,并实现对一种程序设计语言的词法分析。二. 实验内容1.对java程序语言的词法分析:单词分类表:keyvaluetypekeyvaluetype1abstractKEYWORD42throwKEYWORD2assertKEYWORD43throwsKEYWORD3booleanKEYWORD44transientKEYWORD4bre
2、akKEYWORD45tryKEYWORD5caseKEYWORD46voidKEYWORD6catchKEYWORD47volatileKEYWORD7charKEYWORD48whileKEYWORD8classKEYWORD49+OPERATOR9constKEYWORD50+OPERATOR10continueKEYWORD51OPERATOR11defaultKEYWORD52-OPERATOR12doKEYWORD53OPERATOR13doubleKEYWORD54/OPERATOR14elseKEYWORD55=OPERATOR15enumKEYWORD56=OPERATOR1
3、6extendsKEYWORD57OPERATOR17finalKEYWORD58!OPERATOR18finallyKEYWORD59!=OPERATOR19floatKEYWORD60OPERATOR20forKEYWORD61=OPERATOR21gotoKEYWORD62OPERATOR22ifKEYWORD63OPERATOR23implementsKEYWORD64=OPERATOR24importKEYWORD65OPERATOR25instanceofKEYWORD66OPERATOR26intKEYWORD67&OPERATOR27interfaceKEYWORD68OPER
4、ATOR28longKEYWORD69|OPERATOR29nativeKEYWORD70|OPERATOR30newKEYWORD71OPERATOR31packageKEYWORD72;DEVIDER32protectedKEYWORD73,DEVIDER33publicKEYWORD74.DEVIDER34returnKEYWORD75(DEVIDER35strictfpKEYWORD76)DEVIDER36shortKEYWORD77DEVIDER37staticKEYWORD78DEVIDER38superKEYWORD79DEVIDER39switchKEYWORD80DEVIDE
5、R40synchronizedKEYWORD81“DEVIDER41thisKEYWORD82DEVIDER2。单词结构描述(正规式或正规文法)数字|关键字字母|分隔符abstractassertboolean|breakbytecase|catch|char|class constcontinue|defaultdodouble|else|enumextendsfinal|finallyfloat|for|goto|if|implementsimportinstanceof|intinterface|long nativenew|package|private|protected|publi
6、creturnstrictfp|short static|superswitch|synchronizedthisthrow|throwstransienttry voidvolatile|while 操作符+|*/=%|!!=%|=|0|1|2|3|456|78|9字母A|B|。.X|YZ|ab。.xy|z分隔符;,.(|)|”3. 单词状态转换图字母空白字母非字母非数字非关键字210SSS关键字非字母436非字母非数字5字母或数字数字非数字小数点数字非数字或.数字789A0分隔符BC操作符4. 算法描述 读取文件到内存,逐个字母分析,并将连续的字母使用超前搜索组合成为变量或关键字;若是数字
7、,则要判断是否为浮点数,即使用超前搜索的时候,判断扫描到的字符是否为小数点;若是分隔符或者操作符,则要到响应的表中查找并输出。三方案设计程序结构:1. 终结符封装的javabean:用于存储id,代表的实体entity,种类type;2. 关键字表存放在一个Map集合KeyWordTable中;分隔符表存放在一个Map集合DeviderTable中;操作符表存放在OperatorTable中;3. Scanner工具类用于扫描输入的java源程序;4. Analysis类用于分析词法,analysis方法分析词法。程序流程图开始读取源文件扫描数字?添加分隔符NYYN是分隔符?扫描字母?读取源文
8、件YYNY数字或.?扫描字母?添加操作符NN关键字?添加数字添加变量Y添加关键字四测试数据及运行结果1 正常测试数据(3组)及运行结果:扫描Analyst。javaWordType id=78, entity=, type=4, content=WordType id=78, entity=, type=4, content=WordType id=34, entity=return, type=1, content=returnWordType id=0, entity=, type=5, content=listWordType id=72, entity=;, type=4, conte
9、nt=;WordType id=78, entity=, type=4, content=。.扫描Test。javaWordType id=31, entity=package, type=1, content=packageWordType id=0, entity=, type=5, content=comWordType id=74, entity=。, type=4, content=.WordType id=0, entity=, type=5, content=zcWordType id=74, entity=。, type=4, content=.WordType id=0, e
10、ntity=, type=5, content=caWordType id=74, entity=., type=4, content=.WordType id=0, entity=, type=5, content=testWordType id=72, entity=;, type=4, content=;WordType id=33, entity=public, type=1, content=publicWordType id=8, entity=class, type=1, content=classWordType id=0, entity=, type=5, content=T
11、estWordType id=77, entity=, type=4, content=WordType id=0, entity=, type=5, content=privateWordType id=37, entity=static, type=1, content=staticWordType id=26, entity=int, type=1, content=intWordType id=0, entity=, type=5, content=aWordType id=55, entity=, type=3, content=WordType id=0, entity=, typ
12、e=2, content=0WordType id=72, entity=;, type=4, content=;WordType id=0, entity=, type=5, content=privateWordType id=17, entity=final, type=1, content=finalWordType id=13, entity=double, type=1, content=doubleWordType id=0, entity=, type=5, content=bWordType id=55, entity=, type=3, content=WordType id=0, entity=, type=2, content=1。234WordType id=72, entity=;, type=4, content=;WordType id=33, entity=public, type=1, content=publicWordType id=37, entity=static, type=1, content=