您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 西安邮电大学词法分析器的实现
西安邮电大学(计算机学院)课内实验报告实验名称:词法分析器的实现专业名称:软件工程班级:学生姓名:学号(8位):指导教师:王曙燕实验日期:2015年4月10日一.实验目的1.深入理解有限自动机及其应用2.掌握根据语言的词法规则构造识别其单词的有限自动机的方法3.基本掌握词法分析程序的开发。二.实验内容1、词法分析器的功能和输出格式词法分析器的功能是输入源程序,输出单词符号。词法分析器的单词符号常常表示成以下的二元式(单词种别码,单词符号的属性值)。本实验中,采用的是一类符号一种别码的方式。2、单词的BNF表示标识符-字母字母数字串字母数字串-字母字母数字串|数字字母数字串|下划线字母数字串|ε无符号整数-数字数字串数字串-数字数字串|ε加法运算符-+减法运算符--大于关系运算符-大于等于关系运算符-=3、“超前搜索”方法词法分析时,常常会用到超前搜索方法。如当前待分析字符串为“a+”,当前字符为’’,此时,分析器倒底是将其分析为大于关系运算符还是大于等于关系运算符呢?显然,只有知道下一个字符是什么才能下结论。于是分析器读入下一个字符’+’,这时可知应将’’解释为大于运算符。但此时,超前读了一个字符’+’,所以要回退一个字符,词法分析器才能正常运行。在分析标识符,无符号整数等时也有类似情况。4、三.方案设计1、词法形式化描述使用正则文法进行描述,则可以得到如下的正规式:其中ID表示标识符,NUM表示整型常量,RES表示保留字,DEL表示界符,OPR表示运算符。A→(ID|NUM|RES|DEL|OPR)*ID→letter(letter|didit)*NUM→digitdigit*letter→a|…|z|A|…|Zdigit→0|…|9RES→program|begin|end|var|int|and|or|not|if|then|else|while|doDEL→(|)|.|;|,OPR→+|*|:=|||=|=|=|如果关键字、标识符和常数之间没有确定的算符或界符作间隔,则至少用一个空格作间隔。空格由空白、制表符和换行符组成。2、单词种别定义;A语言中的单词符号及其对应的种别编码如下表所示:单词符号种别编码单词符号种别编码program1+16begin2*17end3(18var4)19int5,20and6.21or7:=22not8;23if924then1025else11=26while12=27do13=28标识符1429整型常量15-303、状态转换图;语言A的词法分析的状态转换图如下所示:空格符,制表符或回车符字母或数字数字非数字非字母或数字2字母013+数字56*7(8)41110.9,12:138)16=15;2214非==2120=191817=非=23其他-2425其他27/2625*/28其他四.测试数据及运行结果五.总结1.实验过程中遇到的问题及解决办法;在实验中也遇到很多的错误,比如刚开时不能读写含有数字和不能识别的单词的语句,直接程序运行出错,经过很长时间的调试和分析,最后用单步调试方法找到了原因所在2.对设计及调试过程的心得体会。通过本次试验,我加深了对编译原理中的词法分析的理解,同时通过动手,更加锻炼了自己。本次试验由于使用的刚刚学习的java语言,通过这次机会加强了自己对java语言的熟悉的使用。六.实验程序的源代码如下:packageAnalysis;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Map;publicclassA{privatestaticcharch;privatestaticStringstrToken;privatestaticintindex=0;privatestaticintline=1;privatestaticbooleannoteTag=false;privateMapInteger,Stringkeywords;privateHashMapString,Integerpunctuations;privatestaticArrayListStringp=newArrayListString();privatestaticArrayListStringq=newArrayListString();//getandset函数publicchargetCh(){returnch;}publicvoidsetCh(charch){A.ch=ch;}publicStringgetStrToken(){returnstrToken;}publicvoidsetStrToken(StringstrToken){A.strToken=strToken;}publicvoidsetPunctuations(HashMapString,Integerpunctuations){this.punctuations=punctuations;}publicMapString,IntegergetPunctuations(){returnpunctuations;}publicvoidsetKeywords(MapInteger,Stringkeywords){this.keywords=keywords;}publicMapInteger,StringgetKeywords(){returnkeywords;}//构造函数publicA(){this.keywords=newHashMapInteger,String();keywords.put(1,Program);keywords.put(2,begin);keywords.put(3,end);keywords.put(4,var);keywords.put(5,int);keywords.put(6,and);keywords.put(7,or);keywords.put(8,not);keywords.put(9,if);keywords.put(10,then);keywords.put(11,else);keywords.put(12,while);keywords.put(13,do);this.punctuations=newHashMapString,Integer();punctuations.put(+,16);punctuations.put(*,17);punctuations.put((,18);punctuations.put(),19);punctuations.put(,,20);punctuations.put(;,21);punctuations.put(:=,22);punctuations.put(,23);punctuations.put(=,24);punctuations.put(,25);punctuations.put(=,26);punctuations.put(.,27);punctuations.put(,28);punctuations.put(=,29);}//函数定义(词法分析函数)publicbooleanAnalyse(char[]strArray){index=0;//每次分析一行完成后就将index置0chartemp1;introwLength=strArray.length;outer:while(indexrowLength){strToken=;ch=GetChar(strArray);if(ch==';'){System.out.println((21,;));}elseif(ch==':'){index++;System.out.println((22,:=));}elseif(ch=='.'){System.out.println((27,.));}elseif(ch==''){if((temp1=this.getNextChar(strArray))=='=')System.out.println((24,=));else{index--;System.out.println((23,));}}elseif(ch==''){if((temp1=this.getNextChar(strArray))=='=')System.out.println((26,=));elseif(temp1=='')System.out.println((28,));else{index--;System.out.println((25,));}}elseif(ch=='*'&¬eTag==false){System.out.println((17,*));}elseif(java.lang.Character.isLetter(ch)&¬eTag==false){strToken=contact(strToken,ch);ch=getNextChar(strArray);while((java.lang.Character.isLetter(ch))||(java.lang.Character.isDigit(ch))){strToken=contact(strToken,ch);ch=getNextChar(strArray);}index--;//System.err.println(!!!+strToken);if(findKeyword(strToken)){//System.out.println((15,+strToken.toString()+)\n);inti=getKeyWordKey(strToken);System.out.println((+i+,--));}else{if(!exist(p,strToken))p.add(strToken);inti=getindex(p,strToken);//System.out.println((14,+strToken.toString()+)\n);System.out.println((14,+i+));}}elseif(java.lang.Character.isDigit(ch)&¬eTag==false){strToken=this.contact(strToken,ch);ch=this.getNextChar(strArray);while(java.lang.Character.isDigit(ch)){strToken=this.contact(strToken,ch);ch=this.getNextChar(strArray);}index--;//System.out.println((15,+strToken.toString()+)\n);if(!exist(q,strToken))q.add(strToken);inti=getindex(q,strToken);System.out.println((15,+i+));strToken=;}elseif(ch=='/'||noteTag==true){intstartj=index;//注释起始位置标记intstarti=line;if(noteTag==false){//System.out.println(该部分是注释注释,从第+starti+行第+startj+列开始);}chartemp=this.getNextChar(strArray);if(temp=='*'&¬
本文标题:西安邮电大学词法分析器的实现
链接地址:https://www.777doc.com/doc-8560308 .html