西安邮电大学-编译原理词法分析

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

西安邮电大学(计算机学院)课内实验报告实验名称:词法分析器专业名称:班级:学生姓名:学号(8位):指导教师:实验日期:年月日一.实验目的及实验环境设计一个词法分析器,并更好的理解词法分析实现原理,掌握程序设计语言中各类单词的词法分析方法,并实现对一种程序设计语言的词法分析。二.实验内容1.对java程序语言的词法分析:单词分类表:keyvaluetypekeyvaluetype1abstractKEYWORD42throwKEYWORD2assertKEYWORD43throwsKEYWORD3booleanKEYWORD44transientKEYWORD4breakKEYWORD45tryKEYWORD5caseKEYWORD46voidKEYWORD6catchKEYWORD47volatileKEYWORD7charKEYWORD48whileKEYWORD8classKEYWORD49+OPERATOR9constKEYWORD50++OPERATOR10continueKEYWORD51-OPERATOR11defaultKEYWORD52--OPERATOR12doKEYWORD53*OPERATOR13doubleKEYWORD54/OPERATOR14elseKEYWORD55=OPERATOR15enumKEYWORD56==OPERATOR16extendsKEYWORD57%OPERATOR17finalKEYWORD58!OPERATOR18finallyKEYWORD59!=OPERATOR19floatKEYWORD60OPERATOR20forKEYWORD61=OPERATOR21gotoKEYWORD62OPERATOR22ifKEYWORD63OPERATOR23implementsKEYWORD64=OPERATOR24importKEYWORD65OPERATOR25instanceofKEYWORD66&OPERATOR26intKEYWORD67&&OPERATOR27interfaceKEYWORD68~OPERATOR28longKEYWORD69|OPERATOR29nativeKEYWORD70||OPERATOR30newKEYWORD71^OPERATOR31packageKEYWORD72;DEVIDER32protectedKEYWORD73,DEVIDER33publicKEYWORD74.DEVIDER34returnKEYWORD75(DEVIDER35strictfpKEYWORD76)DEVIDER36shortKEYWORD77{DEVIDER37staticKEYWORD78}DEVIDER38superKEYWORD79[DEVIDER39switchKEYWORD80]DEVIDER40synchronizedKEYWORD81“DEVIDER41thisKEYWORD82‘DEVIDER2.单词结构描述(正规式或正规文法)字母表→变量数字|变量|关键字|字母|分隔符关键字→abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|longnative|new|package|private|protected|public|return|strictfp|shortstatic|super|switch|synchronized|this|throw|throws|transient|tryvoid|volatile|while操作符→+|++|-|--|*|/|=|==|%|!|!=|%||=|||=||&|&&|~||||||^数字→数字.数字|0|1|2|3|4|5|6|7|8|9字母→A|B|...|X|Y|Z|a|b|...|x|y|z分隔符→;|,|.|(|)|{|}|[|]|”|’3.单词状态转换图操作符非字母非数字字母或数字关键字非字母非字母非数字非关键字字母字母分隔符S空白012B数字非数字小数点数字非数字或.数字789A03456C4.算法描述读取文件到内存,逐个字母分析,并将连续的字母使用超前搜索组合成为变量或关键字;若是数字,则要判断是否为浮点数,即使用超前搜索的时候,判断扫描到的字符是否为小数点;若是分隔符或者操作符,则要到响应的表中查找并输出。三.方案设计程序结构:1.终结符封装的javabean:用于存储id,代表的实体entity,种类type;2.关键字表存放在一个Map集合KeyWordTable中;分隔符表存放在一个Map集合DeviderTable中;操作符表存放在OperatorTable中;3.Scanner工具类用于扫描输入的java源程序;4.Analysis类用于分析词法,analysis方法分析词法。程序流程图四.测试数据及运行结果1.正常测试数据(3组)及运行结果:YNYNNYNNYY开始读取源文件扫描数字?读取源文件扫描字母?是分隔符?添加分隔符数字或.?添加数字添加操作符扫描字母?关键字?Y添加关键字添加变量扫描Analyst.javaWordType[id=78,entity=},type=4,content=}]WordType[id=78,entity=},type=4,content=}]WordType[id=34,entity=return,type=1,content=return]WordType[id=0,entity=,type=5,content=list]WordType[id=72,entity=;,type=4,content=;]WordType[id=78,entity=},type=4,content=}]...扫描Test.javaWordType[id=31,entity=package,type=1,content=package]WordType[id=0,entity=,type=5,content=com]WordType[id=74,entity=.,type=4,content=.]WordType[id=0,entity=,type=5,content=zc]WordType[id=74,entity=.,type=4,content=.]WordType[id=0,entity=,type=5,content=ca]WordType[id=74,entity=.,type=4,content=.]WordType[id=0,entity=,type=5,content=test]WordType[id=72,entity=;,type=4,content=;]WordType[id=33,entity=public,type=1,content=public]WordType[id=8,entity=class,type=1,content=class]WordType[id=0,entity=,type=5,content=Test]WordType[id=77,entity={,type=4,content={]WordType[id=0,entity=,type=5,content=private]WordType[id=37,entity=static,type=1,content=static]WordType[id=26,entity=int,type=1,content=int]WordType[id=0,entity=,type=5,content=a]WordType[id=55,entity==,type=3,content==]WordType[id=0,entity=,type=2,content=0]WordType[id=72,entity=;,type=4,content=;]WordType[id=0,entity=,type=5,content=private]WordType[id=17,entity=final,type=1,content=final]WordType[id=13,entity=double,type=1,content=double]WordType[id=0,entity=,type=5,content=b]WordType[id=55,entity==,type=3,content==]WordType[id=0,entity=,type=2,content=1.234]WordType[id=72,entity=;,type=4,content=;]WordType[id=33,entity=public,type=1,content=public]WordType[id=37,entity=static,type=1,content=static]WordType[id=46,entity=void,type=1,content=void]WordType[id=0,entity=,type=5,content=main]五.总结实验过程中遇到的问题及解决办法:扫描过程中出现非法字符的处理:\t,\n等:跳过六.附录:源代码(电子版)DeviderTable.javapackagecom.zc.ca.db;importjava.util.HashMap;importjava.util.Map;importcom.zc.ca.model.WordType;publicclassDeviderTable{publicstaticMapString,WordTypedb=newHashMapString,WordType();static{db.put(;,newWordType(72,;,WordType.TYPE.DEVIDER,;));db.put(,,newWordType(73,,,WordType.TYPE.DEVIDER,,));db.put(.,newWordType(74,.,WordType.TYPE.DEVIDER,.));db.put((,newWordType(75,(,WordType.TYPE.DEVIDER,());db.put(),newWordType(76,),WordType.TYPE.DEVIDER,)));db.put({,newWordType(77,{,WordType.TYPE.DEVIDER,{));db.put(},newWordType(78,},WordType.TYPE.DEVIDER,}));db.put([,newWordType(84,.,WordType.TYPE.DEVIDER,[));db.put(],newWordType(85,.,WordType.TYPE.DEVIDER,]));}}KeyWordTable.javapackagecom.zc.ca.db;importjava.util.HashMap;importjava.util.Map;importcom.zc.ca.model.WordType;publicclassKeyWordTable{publicstaticMapString,WordTypedb=newHashMapString,WordType();static{db.put(abstract,newWordType(1,abstract,WordType.TYPE.KEYWORD,abstract));db.put(assert,newWordType(2

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功