




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国水表计数器行业投资前景及策略咨询研究报告
- 2025年中国智能票据打印机行业投资前景及策略咨询研究报告
- 2025年中国常温除尘布行业投资前景及策略咨询研究报告
- 2025年中国台面式空气清香剂行业投资前景及策略咨询研究报告
- 2025年中国LED美容镜行业投资前景及策略咨询研究报告
- 2025届四川省成都嘉祥外国语学校高一下化学期末复习检测试题含解析
- 山西省太原市迎泽区五中2025届高一化学第二学期期末质量检测试题含解析
- 2025届广西柳江中学高一下化学期末考试模拟试题含解析
- 单招第6类考试题及答案
- 大学思政恋爱观考试题及答案
- 人教版(2024)七年级下册英语期末模拟测试卷(含答案)
- 兵团开放大学2025年春季《公共关系学》终结考试答案
- 电线电缆出入库管理制度
- T/CADCC 003-2024汽车漆面保护膜施工技术规程
- 福建省厦门市双十中学2025届七年级生物第二学期期末联考模拟试题含解析
- 【小学】新苏教版小学数学四年级下册暑假每日一练(02):计算题-应用题(含答案)
- 2025猪蓝耳病防控及净化指南(第三版)
- TCUWA20059-2022城镇供水管网模型构建与应用技术规程
- 2025至2030中国压缩空气储能产业现状调查及项目投资策略建议报告
- 三台县2024-2025学年小学六年级数学毕业检测指导卷含解析
- 宅基地互换合同协议书范本
评论
0/150
提交评论