




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、编译原理实验报告 计算机与信息学院实验一 词法分析器的设计一、实验目的1. 理解词法分析器的任务和输出形式2. 理解扫描器的工作原理3. 掌握状态转换图的绘制以及单词的识别技术4. 掌握词法分析器的设计过程,能够使用某种高级语言实现一个词法分析器二、实验环境Myeclipse三、实验要求给出一个简单的词法语言规则描述,其中:1开头的种别码为关键词,2开头的为算符,3开头的为界符,4开头的为标识符,5开头的为常数,标识符为字母开头,以字母和数字组成的任意符号串,常数为整数,即以数字组成的符号串。四、实验难点 1. 对整数的二进制转换,以及对指针的操作2. 标识符的设置五、实验代码1. ciFa.
2、Javapackage com.yaoer.test1;import javax.swing.*;import javax.swing.border.TitledBorder;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;SuppressWarnings("serial")public class ciFa extends JFrameprivate JButton jbtShow = new JButton("进行词法分析"
3、;);/按钮private JTextArea jta = new JTextArea();/输入文本框private JTextArea jtaOut = new JTextArea();/输出文本框private JPanel jpl=new JPanel();private String intput =""private String output =""private compiler comp = new compiler();/* * param args */public static void main(String args)ciFa
4、 frame = new ciFa();frame.setTitle("词法分析器");/frame.setLocationRelativeTo(frame);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(500, 400);frame.setVisible(true);public ciFa()jta.setWrapStyleWord(true);jta.setLineWrap(true);jtaOut.setWrapStyleWord(true);jtaOut.setLineWrap
5、(true);jtaOut.setEditable(false);JScrollPane scrollPane = new JScrollPane(jta);JScrollPane scrollPane2 = new JScrollPane(jtaOut);scrollPane.setPreferredSize(new Dimension(300,300);scrollPane2.setPreferredSize(new Dimension(300,300);jtaOut.setBorder(new TitledBorder("词法分析结果");jta.setBorder(
6、new TitledBorder("请在这输入");jpl.setLayout(new GridLayout(2,2);jpl.add(jta);jpl.add(jtaOut);add(jbtShow,BorderLayout.SOUTH);add(jpl);jbtShow.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)intput = jta.getText();output=puterComp(intput);jtaOut.append(output););
7、2. compiler.Javapackage com.yaoer.test1;public class compiler public String computerComp(String str)String output=""int index=0;int k = 0;while(index<str.length()if(isJieFu(str.charAt(index)!=-1) /判断界符output+=("( 30"+isJieFu(str.charAt(index)+" "+str.charAt(index)+&q
8、uot; 界符)"); index+;else if(isMath(str.charAt(index) /判断常数int index1=index;output+=("( ");String sub;String result;while(isMath(str.charAt(index)output+=(str.charAt(index);index+;if(index>=str.length()if(str.charAt(index1)='0') if(index1+1>=str.length()sub =str.substring(
9、index1, index); result = Zhuan(sub);output+=(" "+result+" 数字)");elseif(isMath(str.charAt(index1+1) output+=(" 非法字符)"); else sub =str.substring(index1, index); result = Zhuan(sub);output+=(" "+result+" 数字)");else sub =str.substring(index1, index); res
10、ult = Zhuan(sub);output+=(" "+result+" 数字)"); return output;if(isLetter(str.charAt(index)if(str.charAt(index)=' '|str.charAt(index)='n')/空格或者回车处理 index+;while(!isMath(str.charAt(index)|(isLetter(str.charAt(index)output+=(str.charAt(index);index+; if(index>=str.
11、length() output+=(" 非标识符)");return output; output+=(" 非法字符)");else if(str.charAt(index1)='0') if(isMath(str.charAt(index1+1) output+=(" 非法字符)"); else sub =str.substring(index1, index); result = Zhuan(sub);output+=(" "+result+" 数字)");else sub
12、=str.substring(index1, index); result = Zhuan(sub);output+=(" "+result+" 数字)"); else if(isLetter(str.charAt(index) /标识符判断int i=index; String sub;while(isLetter(str.charAt(index)|isMath(str.charAt(index)index+;if(index>=str.length()/System.out.println(index);sub =str.substring(
13、i, index); if(isKeyword(sub)!=0) if(isKeyword(sub)>=10)output+=("( "+" 1"+isKeyword(sub)+" "+sub+" 关键字)");else output+=("( "+" 10"+isKeyword(sub)+" "+sub+" 关键字)");elseoutput+=("( "+sub+" 标识符)"); re
14、turn output; sub = str.substring(i, index); /判断是不是关键字 if(isKeyword(sub)!=0) if(isKeyword(sub)>=10)output+=("( "+" 1"+isKeyword(sub)+" "+sub+" 关键字)");elseoutput+=("( "+" 10"+isKeyword(sub)+" "+sub+" 关键字)");elseoutput+=
15、("("+sub+" 标识符)"); else if(isCompu(str.charAt(index)!=0)if(index+1>=str.length()if(str.charAt(index)='&'|str.charAt(index)='|')output+=("("+str.charAt(index)+" 非法字符)"); index+;elseif(isCompu(str.charAt(index)<=8)output+=("("
16、+" 20"+isCompu(str.charAt(index)+" "+str.charAt(index)+" 运算符)"); else if(str.charAt(index)='!')output+=("( 218 "+str.charAt(index)+" 运算符)"); index+;elseif(isCompu(str.charAt(index+1)=0)if(str.charAt(index)='&'|str.charAt(index)=
17、39;|')output+=("("+str.charAt(index)+" 非法字符)"); index+;elseif(isCompu(str.charAt(index)<=8)output+=("("+" 20"+isCompu(str.charAt(index)+" "+str.charAt(index)+" 运算符)"); else if(str.charAt(index)='!')output+=("("+&quo
18、t; 218"+str.charAt(index)+" 运算符)"); index+;elseif(index+2>=str.length()if( isCompu(str.charAt(index+1)!=0)if(str.charAt(index)='=')if(str.charAt(index+1)='=')output+=("("+" 210 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;in
19、dex+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='<' )switch (str.charAt(index+1) case '=':output+=("("+" 209 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;ind
20、ex+;break;case '<':output+=("( 215 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;break;case '>':output+=("( 211 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;break;default:output+=("("+str.cha
21、rAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;break;else if(str.charAt(index)='>' )if(str.charAt(index+1)='=')output+=("( 207 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;else if(str.charAt(index+1)='>')output+=(
22、"( 214 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='&')if(str.charAt(index+1)='&')output+=("( 216 "+s
23、tr.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='|')if(str.charAt(index+1)='|')output+=("( 217 "+str.charAt(index)+str.charAt(
24、index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='+')if(str.charAt(index+1)='+')output+=("( 212 "+str.charAt(index)+str.charAt(index+1)+" 运算符)");
25、 index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='-')if(str.charAt(index+1)='-')output+=("( 213 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=(
26、"("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)"); index+;index+;return output;elseif( isCompu(str.charAt(index+1)!=0)if(str.charAt(index)='=')if(str.charAt(index+1)='
27、=')output+=("("+" 210 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='<' )switch (str.charAt(index+1) case '=
28、':output+=("("+" 209 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;break;case '<':output+=("("+" 215 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;break;case '>':output+=("(
29、 211 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;break;default:output+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;break;else if(str.charAt(index)='>' )if(str.charAt(index+1)='=')output+=("( 207 &qu
30、ot;+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;else if(str.charAt(index+1)='>')output+=("( 214 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)&quo
31、t;);index+;index+;else if(str.charAt(index)='&')if(str.charAt(index+1)='&')output+=("( 216 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;el
32、se if(str.charAt(index)='|')if(str.charAt(index+1)='|')output+=("( 217 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='
33、;+')if(str.charAt(index+1)='+')output+=("( 212"+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;else if(str.charAt(index)='-')if(str.charAt(index+1
34、)='-')output+=("( 213 "+str.charAt(index)+str.charAt(index+1)+" 运算符)"); index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)");index+;index+;elseoutput+=("("+str.charAt(index)+str.charAt(index+1)+" 非法字符)"); i
35、ndex+;index+;else if(str.charAt(index)=' '|str.charAt(index)='n')/空格或者回车处理 index+;else /其他字符output+=("("+str.charAt(index)+" 非法字符)");index+; k+;if(k%10=0)output+=("n");return output;/* * 1.判断是不是关键字 * param str * return */public int isKeyword(String str)i
36、nt result = 0;String arr="void","main","int","char","if","else","for","while","return","cout","cin" ;for(int i=0;i<arr.length;i+)if(str.equals(arri)result = i+1;break;else result = 0;retu
37、rn result;/* * 2.判断是不是运算符 * param charr * return */public int isCompu(char charr) char arr='+','-','*','/','=','>','&','<','!','|' for(int i=0;i<arr.length;i+) if (charr=arri)return i+1;return 0;/* * 3.判断是不是界符 * param charr * return */public int isJieFu(char charr)char arr='(',')','','','','',',','&
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网络游戏虚拟物品交易安全认证与技术支持协议
- 农田水利设施灌溉用水权承包转让合同
- 生命科学企业细胞冻存服务及专用储存盒租赁合同
- 保险退保金结算与客户权益保障协议
- 微信小程序电商运营培训与客户关系管理协议
- DB42-T 2018-2023 大水面渔业资源调查评价技术规范
- 上海电子信息职业技术学院《农业相关政策培训》2023-2024学年第二学期期末试卷
- 江西工业职业技术学院《中西医结合重症医学》2023-2024学年第二学期期末试卷
- 四川省乐山市犍为县2025年初三下学期强化选填专练(二)生物试题含解析
- 江西现代职业技术学院《建筑史纲》2023-2024学年第一学期期末试卷
- 2024年安徽安庆安桐城乡发展集团有限公司招聘真题
- 拆除冷库施工方案
- 2025年九江市第一批面向社会公开招聘留置看护队员【68人】笔试备考题库及答案解析
- 2025-2030中国可再生能源行业发展分析及投资前景与战略规划研究报告
- 10.1 美国课件2024-2025学年度七年级下学期人教版地理
- 铆接粘接与锡焊教案
- 工业数字孪生测试要求
- 2025统编版语文六年级下册第二单元解析+任务目标+大单元教学设计
- 灾后救援与重建
- 上海第二工业大学《高等数学B(上)》2023-2024学年第二学期期末试卷
- 2025届上海市(春秋考)高考英语考纲词汇对照表清单
评论
0/150
提交评论