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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

原版第2组编译原理实验报告.docx

1、原版第2组编译原理实验报告院 系: 计算机科学学院 专 业:计算机科学与技术年 级: 09级 课程名称: 编译原理 组 号: 2 学号姓名: 09061010 张鹏 学号姓名: 09061015 陈鹏 学号姓名: 09061017 马菘璠 指导教师: 李 航 高 2011年 12月 12日年级2009班号计科一大班(1)小班专业计算机科学与技术学号09061010学号09061015学号09061017姓名张鹏姓名陈鹏姓名马菘璠实验名称实验一:词法分析实验类型设计型综合型创新型实验目的或要求通过编写一个具体的词法分析程序,加深对词法分析原理的理解。掌握在对程序设计语言源程序进行扫描过程中将其分

2、解为各类单词的词法分析方法。编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。依次输出各个单词的内部编码及单词符号自身值。要求用可视化编程工具编写;要求有界面(即一般windows下应用程序界面)。输入为c语言源代码。要求输出如下(也可以以文件形式输出)。(2,”main”)(5,”(“)(5,”)“)(5,”“(1,”int”)(2,”a1”)(5,”,”)(2,”b2”)(5,”;”)(2,”a1”)(4,”=”)(3,”10”)(5,”;”)(2,”b2”)(4,”=”)(2,”a1”)(4,”+”)(3,”20.35”

3、)(5,”;”) (5,”“) 注:为右大括号要求(可根据实际情况加以扩充和修改):识别保留字:if、int、for、while、do、return、break、continue等等;单词种别码为1。其他的都识别为标识符;单词种别码为2。常数为无符号数;单词种别码为3。运算符包括:+、-、*、/、=、=、=、!= ;单词种别码为4。分隔符包括: “,”“;”“(”“)”“”“”等; 单词种别码为5。实验记录1、算法流程图开 始读取文件判 断判断保留字和标识符判断运算符、关系运算符、分界符判断数值,整型和浮点型2、程序代码public class FristNoteBook extends JF

4、rame private JTextArea filetextArea; private JTextArea filetextArea_2; private JButton btn; private JButton btn_2; String name = null; public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try UIManager.setLookAndFeel(UIManager .getSystemLookAndFeelClassName();

5、 FristNoteBook frame = new FristNoteBook(); frame.setVisible(true); catch (Exception e) e.printStackTrace(); public FristNoteBook() super(); setTitle(词法分析工具); this.setResizable(false); getContentPane().setLayout(null); setBounds(100, 100, 600, 450); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JM

6、enuBar jmBar = new JMenuBar(); this.setJMenuBar(jmBar); JMenu fileMenu = new JMenu(文件); JMenu editeMenu = new JMenu(编辑); JMenu helpMenu = new JMenu(帮助); jmBar.add(fileMenu); jmBar.add(editeMenu); /* jmBar.add(helpMenu); */ JMenuItem newFile = new JMenuItem(新建); newFile.addActionListener(new ActionLi

7、stener() public void actionPerformed(final ActionEvent e) filetextArea.setText(); / 实现将文本域中的内容清空 );JMenuItem openFile = new JMenuItem(打开); /定义打开菜单项openFile.addActionListener(new ActionListener() /绑定单击事件 public void actionPerformed(final ActionEvent e) openFile(e); /调用openFile()方法 BufferedReader br;

8、/创建BufferedReader对象 try if (name != null) /如果String对象不为空 br = new BufferedReader(new FileReader(name); /实例化BufferedReader对象 StringBuffer sb = new StringBuffer(); /定义字符创生成器对象 String aline; while (aline = br.readLine() != null) /循环读取流中数据 sb.append(aline + n); /向字符串生成器中追加内容 br.close(); /将流关闭 filetextAr

9、ea.setText(sb.toString(); /将读取内容,设置为文本域内容 catch (Exception e1) e1.printStackTrace(); );JMenuItem saveFile = new JMenuItem(保存);saveFile.addActionListener(new ActionListener() public void actionPerformed(final ActionEvent e) MyFileChooser myFileChooser = new MyFileChooser(); /创建包含有保存文件对话框的MyFileChoose

10、r类对象 myFileChooser.getFileChooser(); /调用该类的获取保存文件对话框方法 BufferedWriter bw; /创建BufferedWriter对象 try bw = new BufferedWriter(new FileWriter(myFileChooser .getPath(); /根据用户选择地址,实例化BufferedWriter对象 bw.write(filetextArea.getText(); /将文本域中内容写入流中 bw.close(); /将流关闭 catch (Exception e1) e1.printStackTrace();

11、); JMenuItem exit = new JMenuItem(退出); exit.addActionListener(new ActionListener() public void actionPerformed(final ActionEvent e) System.exit(0); ); fileMenu.add(newFile); fileMenu.add(openFile); fileMenu.add(saveFile); fileMenu.add(exit);JMenuItem copeEdite = new JMenuItem(复制);copeEdite.addAction

12、Listener(new ActionListener() public void actionPerformed(final ActionEvent e) ClipUtil clipUtil = new ClipUtil(); /创建ClipUtil对象 clipUtil.setData(filetextArea.getText(); /调用复制数据方法 );JMenuItem cutEdite = new JMenuItem(剪切);cutEdite.addActionListener(new ActionListener() public void actionPerformed(fin

13、al ActionEvent e) ClipUtil clipUtil = new ClipUtil(); clipUtil.setData(filetextArea.getSelectedText(); /调用复制数据方法 filetextArea.replaceRange(, filetextArea.getSelectionStart(), filetextArea.getSelectionEnd(); /将选中数据用空字符串替换 );JMenuItem stickEdite = new JMenuItem(粘贴);stickEdite.addActionListener(new Act

14、ionListener() public void actionPerformed(final ActionEvent e) ClipUtil clipUtil = new ClipUtil(); String str = clipUtil.getData(); /调用粘贴数据方法 filetextArea.replaceRange(str, filetextArea.getSelectionStart(), filetextArea .getSelectionEnd(); /设置文本域中数据 );JMenuItem allEdite = new JMenuItem(全选);allEdite.

15、addActionListener(new ActionListener() public void actionPerformed(final ActionEvent e) filetextArea.setSelectionStart(0); /设置文本域中选定文本的开始位置 filetextArea.setSelectionEnd(filetextArea.getText().length(); /设置文本域中选定文本的结束位置 ); editeMenu.add(copeEdite); editeMenu.add(cutEdite); editeMenu.add(stickEdite);

16、editeMenu.add(allEdite); JMenuItem heplEdite = new JMenuItem(关于词法分析器); heplEdite.addActionListener(new ActionListener() public void actionPerformed(final ActionEvent e) MyAbout myAbout = new MyAbout(); myAbout.setVisible(true); ); /*helpMenu.add(heplEdite);*/ final JScrollPane scrollPane = new JScro

17、llPane(); scrollPane.setBounds(0, 0, 350, 400); getContentPane().add(scrollPane); filetextArea = new JTextArea(); scrollPane.setViewportView(filetextArea); final JScrollPane scrollPane_2 = new JScrollPane(); scrollPane_2.setBounds(430, 0, 165, 400); getContentPane().add(scrollPane_2); filetextArea_2

18、 = new JTextArea(); scrollPane_2.setViewportView(filetextArea_2); /* * 用于词法分析的按钮触发 */btn =new JButton(分析); btn.setBounds(355, 130, 70, 40); btn.addActionListener(new ActionListener() public void actionPerformed(final ActionEvent e) try String str=filetextArea.getText(); StringReader sr=new StringRea

19、der(str); BufferedReader br=new BufferedReader(sr); String line=new String(); int i=0; char token; String strWord=; Word look=new Word(); filetextArea_2.setText(); while(line=br.readLine()!=null) i=0; strWord=; while(i=a&token=A&token=a&token=A&token=0&token=9) i+; if(i=a&token=A&token=0&token=0&tok

20、en=a&token=A&token=0&token=9)|token=.) i+; if(i=a&token=A&token=0&token=9)|token=.) strWord=strWord+token; else if(look.lookFloatNum(strWord) filetextArea_2.append(strWord+ 浮点型n); strWord=; break; else filetextArea_2.append(strWord+ 整型n); strWord=; break; else if(look.lookFloatNum(strWord) filetextA

21、rea_2.append(strWord+ 浮点型n); strWord=; break; else filetextArea_2.append(strWord+ 整型n); strWord=; break; /* 判断运算符、关系运算符、分界符*/ else if(look.lookAWord(token) strWord=strWord+token; while(look.lookAWord(token) i+; (strWord+token)|look.lookBianjie(strWord+token)if(iline.length() token=line.charAt(i); if

22、(look.lookAWord(token) if(look.lookOperator(strWord+token)|look.lookRelationOpr(strWord+token)|look.lookBianjie(strWord+token) if(look.lookOperator(strWord) strWord=strWord+token; filetextArea_2.append(strWord+ 运算符n); strWord=; i+; break; else if(look.lookRelationOpr(strWord) strWord=strWord+token;

23、filetextArea_2.append(strWord+ 关系运算符n); strWord=; i+; break; else if(look.lookBianjie(strWord) strWord=strWord+token; filetextArea_2.append(strWord+ 分界符n); strWord=; i+; break; else if(look.lookOperator(strWord) filetextArea_2.append(strWord+ 运算符n); strWord=; break; else if(look.lookRelationOpr(strW

24、ord) filetextArea_2.append(strWord+ 关系运算符n); strWord=; break; else if(look.lookBianjie(strWord) filetextArea_2.append(strWord+ 分界符n); strWord=; break; else if(look.lookOperator(strWord) filetextArea_2.append(strWord+ 运算符n); strWord=; break; else if(look.lookRelationOpr(strWord) filetextArea_2.append(strWord+ 关系运算符n); strWord=;

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

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