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