西安邮电大学词法分析器的实现_第1页
西安邮电大学词法分析器的实现_第2页
西安邮电大学词法分析器的实现_第3页
西安邮电大学词法分析器的实现_第4页
西安邮电大学词法分析器的实现_第5页
已阅读5页,还剩10页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 西 安 邮 电 大 学 (计算机学院)课内实验报告实验名称: 词法分析器的实现 专业名称: 软件工程班 级: 学生姓名: 学号(8位): 指导教师: 王曙燕实验日期: 2015年4月10日一. 实验目的1 深入理解有限自动机及其应用2 掌握根据语言的词法规则构造识别其单词的有限自动机的方法3基本掌握词法分析程序的开发。 二. 实验内容1、 词法分析器的功能和输出格式词法分析器的功能是输入源程序,输出单词符号。词法分析器的单词符号常常表示成以下的二元式(单词种别码,单词符号的属性值)。本实验中,采用的是一类符号一种别码的方式。2、 单词的BNF表示<标识符>-> <字母

2、><字母数字串><字母数字串>-><字母><字母数字串>|<数字><字母数字串>|<下划线><字母数字串>|<无符号整数>-> <数字><数字串><数字串>-> <数字><数字串> |<加法运算符>-> +<减法运算符>-> -<大于关系运算符>-> > <大于等于关系运算符>-> >=3、“超前搜索”方法词法分析时,常常

3、会用到超前搜索方法。如当前待分析字符串为“a>+”,当前字符为>,此时,分析器倒底是将其分析为大于关系运算符还是大于等于关系运算符呢?显然,只有知道下一个字符是什么才能下结论。于是分析器读入下一个字符+,这时可知应将>解释为大于运算符。但此时,超前读了一个字符+,所以要回退一个字符,词法分析器才能正常运行。在分析标识符,无符号整数等时也有类似情况。4、三方案设计1、 词法形式化描述使用正则文法进行描述,则可以得到如下的正规式:其中ID表示标识符,NUM表示整型常量,RES表示保留字,DEL表示界符,OPR表示运算符。 A(ID | NUM | RES | DEL | OPR)

4、 *IDletter(letter | didit)*NUMdigit digit*lettera | | z | A | | Zdigit 0 | | 9RES program | begin | end | var | int | and | or | not | if | then | else | while | doDEL( | ) | . | ; | ,OPR+ | * | := | > | < | = | >= | <= | <> 如果关键字、标识符和常数之间没有确定的算符或界符作间隔,则至少用一个空格作间隔。空格由空白、制表符和换行符组成。2

5、、 单词种别定义;A语言中的单词符号及其对应的种别编码如下表所示:单词符号种别编码单词符号种别编码program1+16begin2*17end3(18var4)19int5,20and6.21or7:=22not8;23if9>24then10<25else11=26while12>=27do13<=28标识符14<>29整型常量15-303、 状态转换图;语言A的词法分析的状态转换图如下所示:空格符,制表符 或回车符 字母或数字非字母或数字2字母01 数字非数字3+4数字587*633()2224非=其他-142513812):>9,.=10111

6、5;/16=28/*2627其他23=1718>19非=<=212025其他四测试数据及运行结果五总结1 实验过程中遇到的问题及解决办法;在实验中也遇到很多的错误,比如刚开时不能读写含有数字和不能识别的单词的语句,直接程序运行出错,经过很长时间的调试和分析,最后用单步调试方法找到了原因所在 2 对设计及调试过程的心得体会。 通过本次试验,我加深了对编译原理中的词法分析的理解,同时通过动手,更加锻炼了自己。本次试验由于使用的刚刚学习的java语言,通过这次机会加强了自己对java语言的熟悉的使用。六实验程序的源代码如下:package Analysis;import java.io.

7、BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;public class A private static char ch;private static String strToken;private static int index = 0;private static int line = 1;private stati

8、c boolean noteTag=false;private Map< Integer,String > keywords;private HashMap<String, Integer> punctuations; private static ArrayList<String> p=new ArrayList<String>(); private static ArrayList<String> q=new ArrayList<String>();/ get and set 函数public char getCh()

9、 return ch;public void setCh(char ch) A.ch = ch;public String getStrToken() return strToken;public void setStrToken(String strToken) A.strToken = strToken;public void setPunctuations(HashMap<String, Integer> punctuations) this.punctuations = punctuations;public Map<String, Integer> getPu

10、nctuations() return punctuations;public void setKeywords(Map<Integer, String> keywords) this.keywords = keywords;public Map<Integer, String> getKeywords() return keywords;/ 构造函数public A() this.keywords = new HashMap< Integer,String >();keywords.put(1,"Program");keywords.p

11、ut(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,&quo

12、t;else");keywords.put(12,"while");keywords.put(13,"do");this.punctuations = new HashMap<String, Integer>();punctuations.put("+", 16);punctuations.put("*", 17);punctuations.put("(", 18);punctuations.put(")", 19);punctuations.put(&

13、quot;,", 20);punctuations.put("", 21);punctuations.put(":=", 22);punctuations.put(">", 23);punctuations.put(">=", 24);punctuations.put("<", 25);punctuations.put("<=", 26);punctuations.put(".", 27);punctuations.put

14、("<>", 28);punctuations.put("=", 29);/ 函数定义(词法分析函数)public boolean Analyse(char strArray) index = 0;/ 每次分析一行完成后就将index置0char temp1;int rowLength = strArray.length;outer:while (index < rowLength) strToken = ""ch = GetChar(strArray); if (ch = '') System.ou

15、t.println("(21,;) "); else if(ch=':') index+; System.out.println("(22,:=)"); else if(ch='.') System.out.println("(27,.)"); else if(ch='>') if( temp1=this.getNextChar(strArray)='=') System.out.println("(24,>=)"); else index

16、-; System.out.println("(23,>)"); else if(ch='<') if( temp1=this.getNextChar(strArray)='=') System.out.println("(26,<=)"); else if(temp1='>') System.out.println("(28,<>)"); else index-; System.out.println("(25,<)");

17、else if(ch='*'&& noteTag=false) System.out.println("(17,*)"); else if (java.lang.Character.isLetter(ch)&&noteTag=false) strToken = contact(strToken, ch);ch = getNextChar(strArray);while (java.lang.Character.isLetter(ch)| (java.lang.Character.isDigit(ch) strToken = c

18、ontact(strToken, ch);ch = getNextChar(strArray);index-;/ System.err.println("!"+strToken);if (findKeyword(strToken) /System.out.println("(15," + strToken.toString() + ")n");int i=getKeyWordKey(strToken);System.out.println("("+i+",-)"); elseif(!exist(

19、p,strToken) p.add(strToken);int i=getindex(p,strToken);/System.out.println("(14," + strToken.toString() + ")n");System.out.println("(14,"+i+")"); else if (java.lang.Character.isDigit(ch)&&noteTag=false) strToken = this.contact(strToken, ch);ch = this.g

20、etNextChar(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); int i=getindex(q,strToken); System.out.println

21、("(15,"+i+")");strToken = "" else if (ch = '/'|noteTag=true) int startj=index; /注释起始位置标记int starti=line;if(noteTag=false) /System.out.println("该部分是注释注释,从第"+starti+"行第"+startj+"列开始"); char temp = this.getNextChar(strArray);if (temp =

22、 '*'&&noteTag=false) temp = this.getNextChar(strArray);while(index<rowLength)temp = this.getNextChar(strArray);if(temp='*'&&( temp1=this.getNextChar(strArray)='/')index-;break;if(index>=rowLength)noteTag=true;break outer;else if(noteTag=true&&ch!

23、='*') while(index<rowLength)temp = this.getNextChar(strArray);if(temp='*'&&(temp1=this.getNextChar(strArray)='/')noteTag=false;break; else if(temp='/')while(true)index+;if(index>=rowLength)break outer; else return false; else String key = String.valueOf(

24、ch);if (this.findPunctuation(key) System.out.println("("+this.getPunctuation(key)+","+ key + ")"); else if (key.equals(" ") | key.equals(" ") break; elsereturn false; /System.out.println( "未知符号 " + key + "n"); /strToken = "&q

25、uot;return true;public char GetChar(char array) try while (arrayindex) = ' ') index+;index+;/ 提前指向下一个字符 catch (ArrayIndexOutOfBoundsException e) return ' 'return arrayindex - 1;public char getNextChar(char strChar) index+;return strCharindex - 1;public String contact(String token, ch

26、ar ch) return token + String.valueOf(ch);public boolean findKeyword(String str) for(int i=0;i<13;i+)if(str.equalsIgnoreCase(this.keywords.get(i)return true;return false;public boolean findPunctuation(String str) if (this.punctuations.containsKey(str) return true; elsereturn false;public int getPu

27、nctuation(String str) return this.punctuations.get(str);public boolean Clean() return true;public void callError(int line) System.out.println("出现错误,错误位置在第" + line + "行,第" + index + "列");public boolean exist(ArrayList<String> p,String strToken)if(p.contains(getStrT

28、oken()return true;elsereturn false;public int getKeyWordKey(String str)for(int i=1;i<=13;i+)if(str.equalsIgnoreCase(this.keywords.get(i)return i;return 10000; public int getindex(ArrayList<String> p,String Str) return p.lastIndexOf(Str)+1; /*int j=0; for(int i=0;i<p.size();i+) if(p.get(i).equ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论