ImageVerifierCode 换一换
格式:DOCX , 页数:27 ,大小:203.34KB ,
资源ID:24243408      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/24243408.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(编译原理综训报告词法分析器.docx)为本站会员(b****2)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

编译原理综训报告词法分析器.docx

1、编译原理综训报告词法分析器软 件 学 院 综合训练项目报告书课程名称 编译原理 项目名称 词法分析器的设计 专业班级 组 别 姓 名 成 员 任课教师 郭伟 2015 年 6 月 1 设计时间 2015年6月242 设计任务 编译原理涉及词法分析,语法分析,语义分析及优化设计等各方面。词法分析阶段是编译过程的第一个阶段,是编译的基础。这个阶段的任务是从左到右一个字符一个字符地读入源程序,即对构成源程序的字符流进行扫描然后根据构词规则识别单词(也称单词符号或符号)。词法分析程序实现这个任务。词法分析程序可以使用Lex等工具自动生成。从左到右逐个字符对构成源程序的字符串进行扫描,依据词法规则,识别

2、出一个一个的标记(token),把源程序变为等价的标记串序列。执行词法分析的程序称为词法分析器,也称为扫描器。词法分析是所有分析优化的基础,涉及的知识较少,如状态转换图等,易于实现。本次实验使用java代码实现。 3 设计内容 3.1设计要求 掌握词法分析设计的基本原理及思想,巩固所学的理论知识,培养综合运用所学知识解决实际问题的能力。设计要求:1)用C语言开发一个词法分析程序,词法分析程序可以分析包含以下符号测试代码;关键字:if、int、for、while、do、return、break、continue;单词种别码为1。标识符;单词种别码为2。常数为无符号整形数;单词种别码为3。分隔符包

3、括:,、;、(、); 单词种别码为4。运算符包括:+、-、*、/、=、 、 =、=、!= ;单词种别码为5。2)以的形式存储符号表;3)删除注释、空格和无用符号,将删除后的程序代码输出在控制台上;4)发现并定位词法错误,需要输出错误的位置在源程序中的第几行,将错误信息(错误信息为错误种类,错误种类包括未知的标识符、操作符、错误格式)输出到屏幕上;5)对于普通标识符和常量,分别建立标识符表和常量表(使用线性表存储),在控制台中输出的符号表(来自测试代码中出现的符号)。其中符号表含有的属性列为:单词符号和种别码。 3.2问题分析3.2.1单词的构词规则对某特定语言A ,构造其词法规则。A的内容如下

4、:表1构词规则表该语言的单词符号包括1、关键字 if int for while do return break continue表2关键字表2、运算符及界符表3运算符及界符表3、标识符(字母大小写不敏感),整型常数 3.2.2词汇表 对于后文正则式中可能出现的符号定义如下,以便清晰地描述A语言的正则式符号说明a字母b数字c符号(不包括字母和数字)*闭包运算符|或运算符.连接运算符(可省略)空#结束符表4 词汇表3.2.3单词种别定义if1contine1)4!=5int1标识符2+5/5for1常数3*5;5while1,4-5do14=5return145break1 (4=5表5 单词种

5、别表对于标识符或保留字的推导 对于常数的推导 对于符号的推导 3.3程序设计3.3.1总体设计 图一、扫描子程序主要部分流程3.3.2状态转换图图二 读取状态转换3.4测试与分析3.4.1测试 我们对程序的测试如下,采用如下用例 用例:运行结果为:错误:常数表:标志符表:保留字及符号对应的二元组表:二元组及错误信息均无误,且注释成功跳过3.5 代码public class Compiler extends JFrame implements ActionListener int row = 1; int line = 1; int err=0; JMenuBar mb = new JMenuB

6、ar(); JMenu fileMenu = new JMenu(文件); JMenu actionMenu = new JMenu(词法分析); JMenuItem closeWindow = new JMenuItem(退出); JMenuItem openFile = new JMenuItem(打开); JMenuItem lexical_check = new JMenuItem(开始); int begin = 0; int end = 0; TextArea text = new TextArea(); TextArea error_text = new TextArea();

7、TextArea end_text = new TextArea(); FileDialog file_dialog_load = new FileDialog(this, Open file., ileDialog.LOAD); JPanel pan1=new JPanel(); JPanel pan2=new JPanel(); Compiler() this.add(end_text);end_text.setEditable(false); this.add(text); this.add(error_text);error_text.setEditable(false); pan1.

8、setLayout(new GridLayout(1,1); pan1.add(text); pan2.setLayout(new GridLayout(2,1); pan2.add(error_text,North); pan2.add(end_text,South); getContentPane().add(pan1,West); getContentPane().add(pan2,Center); this.setJMenuBar(mb); mb.add(fileMenu); mb.add(actionMenu); fileMenu.add(openFile); fileMenu.ad

9、d(closeWindow); actionMenu.add(lexical_check); error_text.setText(-词法分析-n); end_text.setText(-词法分析错误信息- n); closeWindow.addActionListener(this); openFile.addActionListener(this); lexical_check.addActionListener(this); pack(); this.addWindowListener(new WindowAdapter() public void windowClosing(Windo

10、wEvent e) System.exit(0); ); this.setVisible(true); public static void main(String args) Compiler compiler = new Compiler(); public void actionPerformed(ActionEvent e) if (e.getSource() = closeWindow) int flag = JOptionPane.showConfirmDialog(null, 是否退出); System.out.println(flag= + flag); if (flag =

11、0) System.exit(0); else if (flag = 1) else if(e.getSource() = openFile) file_dialog_load.setVisible(true); File myfile = new File(file_dialog_load.getDirectory(), file_dialog_load.getFile(); try BufferedReader bufReader = new BufferedReader(new FileReader(myfile); new testxiaochu(myfile); try new er

12、yuandanci(myfile); catch (Exception e1) / TODO Auto-generated catch block e1.printStackTrace(); String content = ; String str; while(str = bufReader.readLine() != null) content += str + n; text.setText(content); catch(IOException ie) System.out.println(IOexception occurs.); else if(e.getSource() = l

13、exical_check) error_text.setText(); row = 0; line = 1; checkLexical(); public void checkLexical() String error_info = error_text.getText(); String content = text.getText(); if(content.equals() error_info += 文 件 尚 未 载 入 !n; error_text.setText(error_info); else int i = 0;/选择第i个字符进行检测。 int N = content.

14、length();/文件大小 int state = 0;/状态标志 for(i = 0; i ) state = 6; else if(c = ) error_text.append(t运算符tt423 +t-+n); else error_text.append(t运算符tt406 +t-+n); i-; row-; state = 0; break; case 3:/标志符是 * if(c = =) error_text.append(t运算符tt407 +t*=+n); else error_text.append(t运算符tt408 +t*+n); i-; row-; state =

15、 0; break; case 4:/标志符是 / if(c = /) while(c) != n) c = content.charAt(i); i+; state = 0; error_text.append(t注释部分tt/ n); else if(c = =) state = 0; error_text.append(t运算符tt409 +t/=+n); else state = 0; error_text.append(t运算符tt410 +t/+n); i-; row-; /state = 0; break; case 5:/标志符是 ! if(c = =) error_text.

16、append(t运算符tt411 +t!=+n); state = 0; else state = 0; i-; row-; error_text.append(t运算符tt412 +t!+n); break; case 6:/标志符是 if(c = =) error_text.append(t运算符tt413 +t=+n); state = 0; if(c=) error_text.append(t运算符tt426 +t+n); state=0; else state = 0; i-; row-; error_text.append(t运算符tt414 +t+n); /state = 0;

17、break; case 7:/标志符是 if(c = =) error_text.append(t运算符tt415 +t=+n); state = 0; if(c=) error_text.append(t运算符tt427 +t+n); state=0; else state = 0; i-; row-; error_text.append(t运算符tt416 +t+n); break; case 8:/标志符是 = if(c = =) error_text.append(t运算符tt417 +t=+n); state = 0; else state=0; i-; row-; error_te

18、xt.append(t运算符tt418 +t=+n); break; case 9:/标志符是 回车 state = 0; i-; row = 1; line +; break; case 10:/标志符是 字母 if(isLetter(c) | isDigit(c) state = 10; else end = i; String id = content.substring(begin, end); if(isKey(id)!=0) int t=isKey(id); error_text.append(t关键字tt+t+ id + n); else error_text.append(t标

19、志符t +t100+id + n); i-; row-; state = 0; /state = 0; break; case 11:/标志符是 数字 if(c = e | c = E) state = 13; else if(isDigit(c) | c = .) /省略跳过,i加一操作 else if(isLetter(c) err+; String b=end_text.getText(); b+=错误: line + line + row + row + 数字格式错误或者标志符错误n; end_text.setText(b); error_text.append(错误: line + line + row + row + 数字格式错误或者标志符错误n); int temp = i; i = find(i,content); row += (i - temp); state = 0; break; case 12:/标志符是 # String id = ; while(c != & ( c != n) i+; c = content.charAt(i); if(c = ) error_text.append(t头文件引入 n); else err+; String d=end_text.getText(); d+=错误: + line

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1