编译原理实验报告--词法分析器
更新时间:2023-05-09 15:32:01 阅读量: 实用文档 文档下载
编译原理实验—词法分析器
姓名:王吉军
学号:09208015
班级:软件92
一、实验目的
通过动手实践,使学生对构造编译系统的基本理论、编译程序的基本结构有更为深入的理解和掌握;使学生掌握编译程序设计的基本方法和步骤;能够设计实现编译系统的重要环节。同时增强编写和调试程序的能力。
二、实验内容及要求
对某特定语言A ,构造其词法规则。
该语言的单词符号包括:
保留字(见左下表)、标识符(字母大小写不敏感)、整型常数、界符及运算符(见右下表) 。
功能要求如下所示:
·按单词符号出现的顺序,返回二元组序列,并输出。
·出现的标识符存放在标识符表,整型常数存放在常数表,并输出这两个表格。
·如果出现词法错误,报出:错误类型,位置(行,列)。
·处理段注释(/* */),行注释(//)。
·有段注释时仍可以正确指出词法错误位置(行,列)。
三、实验过程
1、词法形式化描述
使用正则文法进行描述,则可以得到如下的正规式:
其中ID表示标识符,NUM表示整型常量,RES表示保留字,DEL表示界符,OPR表示运算符。
A→(ID | NUM | RES | DEL | OPR) *
ID→letter(letter | didit)*
NUM→digit digit*
letter→a| … | z | A | … | Z
digit→ 0 | … | 9
RES→ program | begin | end | var | int | and | or | not | if | then | else | while | do
DEL→( | ) | . | ; | ,
OPR→+ | * | := | > | < | = | >= | <= | <>
如果关键字、标识符和常数之间没有确定的算符或界符作间隔,则至少用一个空格作间隔。空格由空白、制表符和换行符组成。
2、单词种别定义;
3、状态转换图;
语言A的词法分析的状态转换图如下所示:
空格符,制表符
或回车符字母或数字
4、运行环境介绍;
本次实验采用MyEclipse进行代码的编写和编译及运行,编写语言为java 语言;程序的运行环境为windows 7 旗舰版
5、关键算法的流程图及文字解释;
程序中用到的函数列表:
A类定义各种类函数以及包含主函数public static void main()
变量ch储存当前最新读进的字符的地址
strToken存放当前字符串
main() //主函数
Analysis()//分析函数,每次读入一行文件,进行识别处理;
charGetChar(); //取得当前位置的字符的内容放入ch,并提前指向下一个字符;
char GetNextChar();//取得当前位置的下一位置的字符,
StringConCat(); //将ch指向的字符连接到strToken后面
isLetter(); //判断ch指向的字符是否字母
isDigit(); //判断ch指向的字符是否数字
add(p,str); //向p表中插入当前strToken的字符串
BooleanfindKeyWord(str); //检测当前strToken中的字符串是否保留字,若是,则执行getKeyWordKey(str),返回保留字的id,否则,判别其是否是已存在的标示符,若是,返回标示符的id以及该标示符在表中的位置;
findPunctuation()//判断是否是一个保留的符号;
getindex() //返回已经识别的标示符或者是数字的位置下标;
Booleanexist(); //检测当前strToken中的字符串是否在标识符表中已存在,若是,则返回true,否则返回false
void callError(); //出错处理过程,将错误的位置报告出来
(1)main()函数的流程图如下:
(2)具体分析流程图:
6、测试报告(测试用例,测试结果);
首先输入一个不含错误的程序(两种注释)进行检测:
运行后在控制台上得到的结果如下所示:
得到的二元组序列如下:
经检验,输出的是正确的二元组序列。
再输入一个含有错误的程序(含有注释)进行检验:
运行后在控制台上得到的结果如下所示:
经检验,错误的位置是正确的
如果是空文件,编译也会通过;
四、实验总结
通过本次试验,我加深了对编译原理中的词法分析的理解,同时通过动手,更加锻炼了自己。本次试验由于使用的刚刚学习的java语言,通过这次机会加强了自己对java语言的熟悉的使用。
在这次试验中,感谢老师的认真的讲解与同学的无私的帮助。总结来说,自己在这次试验中收获很大。
附:以下为本次实验的源代码:
package Analysis;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Map;
public class A {
private static char ch;
private static String strToken;
private static int index = 0;
private static int line = 1;
private static booleannoteTag=false;
private Map
privateHashMap
// get and set 函数
public char getCh() {
returnch;
}
public void setCh(char ch) {
A.ch = ch;
}
public String getStrToken() {
returnstrToken;
}
public void setStrToken(String strToken) {
A.strToken = strToken;
}
public void setPunctuations(HashMap
}
public Map
return punctuations;
}
public void setKeywords(Map
}
public Map
return keywords;
}
// 构造函数
public A() {
this.keywords = new HashMap
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 = new HashMap
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置0 char temp1;
introwLength = strArray.length;
outer:while (index strToken = ""; ch = GetChar(strArray); if (ch == ';'){ System.out.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--; 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,<)"); } } else if(ch=='*'&¬eTag==false) { System.out.println("(17,*)"); } else if (e92009d1c1c708a1284a4449ng.Character.isLetter(ch)&¬eTag==false) { strToken = contact(strToken, ch); ch = getNextChar(strArray); while ((e92009d1c1c708a1284a4449ng.Character.isLetter(ch)) || (e92009d1c1c708a1284a4449ng.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"); int i=getKeyWordKey(strToken); System.out.println("("+i+",--)"); } else { if(!exist(p,strToken)) p.add(strToken); int i=getindex(p,strToken); //System.out.println("(14," + strToken.toString() + ")\n"); System.out.println("(14,"+i+")"); } } else if (e92009d1c1c708a1284a4449ng.Character.isDigit(ch)&¬eTag==false) { strToken = this.contact(strToken, ch); ch = this.getNextChar(strArray); while (e92009d1c1c708a1284a4449ng.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("(15,"+i+")"); strToken = ""; } else if (ch == '/'||noteTag==true) { intstartj=index; //注释起始位置标记 intstarti=line; if(noteTag==false){ //System.out.println("该部分是注释注释,从第"+starti+"行第"+startj+"列开始"); } char temp = this.getNextChar(strArray); if (temp == '*'&¬eTag==false) { temp = this.getNextChar(strArray); while(index temp = this.getNextChar(strArray); if(temp=='*'&&( temp1=this.getNextChar(strArray))=='/') { index--; break; } if(index>=rowLength) { noteTag=true; break outer; } } } else if(noteTag==true&&ch!='*') { while(index temp = this.getNextChar(strArray); if(temp=='*'&&(temp1=this.getNextChar(strArray))=='/') { noteTag=false; //System.out.println(/*"该部分是注释注释,从第"+starti+"行第"+startj+"列开始*/"到第"+line+"行"+"第"+(index-0)+"列为止"); break; } } } else if(temp=='/') { while(true) { index++; if(index>=rowLength) break outer; } } else return false; } else { String key = String.valueOf(ch); if (this.findPunctuation(key)) { System.out.println("("+this.getPunctuation(key)+"," + key + ")"); } else if (key.equals(" ") || key.equals(" ")) { // ************????????????????????*********** break; } else return false; //System.out.println( "[未知符号] " + key + "\n"); //strToken = ""; } } return true; } public char GetChar(char[] array) { try { while ((array[index]) == ' ') { index++; } index++;// 提前指向下一个字符******************** } catch (ArrayIndexOutOfBoundsException e) { return ' '; } return array[index - 1]; } public char getNextChar(char[] strChar) { index++; returnstrChar[index - 1]; }
正在阅读:
编译原理实验报告--词法分析器05-09
我爱校园的银杏作文500字06-20
造价员工程造价基础知识考试试题08-29
选煤厂安全风险报告 - 图文10-15
关于城镇化对农村经济影响的问卷调查08-19
欧盟科研框架计划用前沿技术实现竞争超越09-12
中国新针刺10-10
什么叫“新常态”02-08
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 词法
- 分析器
- 编译
- 原理
- 实验
- 报告
- 2009年普通高等学校招生全国统一考试数学卷(福建.文)含详解
- 建筑施工企业安全生产事故隐患排查治理制度
- 2014年山东省卫生系统“六五”普法考试题库最全
- 小学四年级上册寒假作业练习题(每日一练综合练习)
- 公司差旅费管理办法
- 《相似三角形的判定(3)》教学设计【人教版九年级数学下册】
- 2017年齐齐哈尔大学基础化学实验理论II之无机化学考研复试核心题库
- 2017年西北民族大学经济学院807经济学综合之西方经济学(微观部分)考研冲刺密押题
- 美国宾夕法尼亚州公证认证
- 部编本四年级语文上册:第四单元基础知识复习检测(附答案)-巩固练习
- 2020年高考物理二轮复习讲练测 专题11 波粒二象性 原子结构与原子核(练)(解析版)
- 2011-2015年我国新发现成年女性艾滋病感染者流行特征分析
- 五年级下册英语测试题
- 论我国国债投资在投资组合中的地位和作用(1)分析
- 北师大版历史八下第15课《展示五彩缤纷的社会生活》word教案
- 同等学力人员申请硕士学位外国语水平全国统一考试
- 工程光学 郁道银版 习题解答(一题不落)第十二章光的衍射.doc
- 九年级道德与法治上册第三单元文明与家园第六课建设美丽中国第1框正视发展挑战课时练习新人教版
- 职业培训补贴管理办法
- 物理学教程下册答案(第二版)