编译原理词法分析实验报告.doc

上传人:文库蛋蛋多 文档编号:2396885 上传时间:2023-02-17 格式:DOC 页数:8 大小:1.06MB
返回 下载 相关 举报
编译原理词法分析实验报告.doc_第1页
第1页 / 共8页
编译原理词法分析实验报告.doc_第2页
第2页 / 共8页
编译原理词法分析实验报告.doc_第3页
第3页 / 共8页
编译原理词法分析实验报告.doc_第4页
第4页 / 共8页
编译原理词法分析实验报告.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《编译原理词法分析实验报告.doc》由会员分享,可在线阅读,更多相关《编译原理词法分析实验报告.doc(8页珍藏版)》请在三一办公上搜索。

1、一、目的(本次实验所涉及并要求掌握的知识点)通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个单词的内部编码及单词符号自身值。(遇到错误时可显示“Error”,然后跳过错误部分继续显示)二、实验内容与设计思想(设计思路、主要数据结构、主要代码结构、主要代码段分析等)程序输入/输出示例:如源程序为C语言。输入如下一段:main() int a,b;a = 10; b = a

2、+ 20;要求输出如右图。(2,”main”)(5,”(“)(5,”)“)(5,”“)(1,”int”)(2,”a”)(5,”,”)(2,”b”)(5,”;”)(2,”a”)(4,”=”)(3,”10”)(5,”;”)(2,”b”)(4,”=”)(2,”a”)(4,”+”)(3,”20”)(5,”;”)(5,”“)要求:识别保留字:if、int、for、while、do、return、break、continue;单词种别码为1。其他的都识别为标识符;单词种别码为2。常数为无符号整形数;单词种别码为3。运算符包括:+、-、*、/、=、=、=、!= ;单词种别码为4。分隔符包括:,、;、(、);

3、 单词种别码为5。三、实验使用环境(本次实验所使用的平台和相关软件)平台:WindowsXP SP3软件:MyElicpse四、实验步骤和调试过程(实验步骤、测试数据设计、测试结果分析)1) 定义常量及变量: private JTextArea ta1;private JTextArea ta2;private JButton jb=new JButton(词法分析); private JButton jb1=new JButton(清空文本区);private JLabel jl1=new JLabel(输入源代码:);private JLabel jl2=new JLabel(分析结果:)

4、;static int m=0;/标识字符位置标记static String str1 = new String();String blz=int,return,break,while,for,do,continue,if,else;2) 主要实现的函数: public void actionPerformed(ActionEvent e) if(e.getSource()=jb1) int a=JOptionPane.showConfirmDialog(null, 确定清空吗?,提示!,JOptionPane.YES_NO_OPTION); if( a=JOptionPane.YES_OPT

5、ION) ta1.setText(); ta2.setText(); if(e.getSource()=jb) String a=ta1.getText(); /把输入的软代码赋值给字符串变量achar b=new chara.length(); /把a中的字符一个一个放入字符数组b中for(int i=0;ia.length();i+)bi=a.charAt(i);while(a!=null & ma.length()if(Character.isSpace(bm)m+;else if(Character.isDigit(bm) IsDigit(a,b); else if(Character

6、.isLetter(bm) IsLetter(a,b); else Others(a,b); public void IsDigit(String a,char b) while(m& bm!=& bm!=& bm!=,& bm!=;| (ma.length() & Character.isDigit(bm)|(ma.length() & bm=.)|(ma.length() & Character.isLetter(bm) str1=str1+String.valueOf(bm); m+; ta2.setText(ta2.getText()+str1+t+数字+t+3+n);str1=;pu

7、blic void IsLetter(String a,char b) int i=0; while(m& bm!=& bm!=& bm!=,& bm!=;) str1=str1+str1.valueOf(bm); m+; if(str1.equals(blzi) ta2.setText(ta2.getText()+str1+t+保留字+t+1+n); else ta2.setText(ta2.getText()+str1+t+标识符+t+2+n); str1=;public void Others(String a,char b) if(bm=+) m+; if(ma.length() &

8、Character.isDigit(bm) while(ma.length() & Character.isDigit(bm) )| (ma.length() & bm=.) str1=str1+str1.valueOf(bm); m+; ta2.setText(ta2.getText()+str1+t+运算符+t+4+n); else if(ma.length()& bm=+) ta2.setText(ta2.getText()+t+运算符+t+4+n); m+; else m-; ta2.setText(ta2.getText()+bm+t+运算符+t+4+n); m+; /m+; str

9、1=; else if(bm=-) m+; if(ma.length() & Character.isDigit(bm) while(ma.length() & Character.isDigit(bm) )| (ma.length() & bm=.) str1=str1+str1.valueOf(bm); m+; ta2.setText(ta2.getText()+str1+t+运算符+t+4+n); else if(ma.length()&bm=-) ta2.setText(ta2.getText()+-+t+运算符+t+4+n); m+; else m-; ta2.setText(ta2

10、.getText()+bm+t+运算符+t+4+n); m+; /m+; str1=; else if(bm=*) ta2.setText(ta2.getText()+str1+t+运算符+t+4+n); m+; else if(bm=) str1=str1+str1.valueOf(bm); m+; if(bm=) str1=str1+str1.valueOf(bm); ta2.setText(ta2.getText()+str1+t+运算符+t+4+n); m+; else ta2.setText(ta2.getText()+str1+t+运算符+t+4+n); str1=; else i

11、f(bm=) str1=str1+str1.valueOf(bm); m+; if(bm=) str1=str1+str1.valueOf(bm); ta2.setText(ta2.getText()+str1+t+运算符+t+4+n); m+; else ta2.setText(ta2.getText()+str1+t+运算符+t+4+n); str1=; else if(bm=!) str1=str1+str1.valueOf(bm); m+; if(bm=) str1=str1+str1.valueOf(bm); ta2.setText(ta2.getText()+str1+t+运算符+

12、t+4+n); m+; else ta2.setText(ta2.getText()+str1+t+运算符+t+4+n); str1=; else if(bm=;) ta2.setText(ta2.getText()+bm+t+分隔符+t+5+n); m+; else if(bm=,) ta2.setText(ta2.getText()+bm+t+分隔符+t+5+n); m+; else if(bm=() ta2.setText(ta2.getText()+bm+t+分隔符+t+5+n); m+; else if(bm=) ta2.setText(ta2.getText()+bm+t+分隔符+

13、t+5+n); m+; else if(bm=) ta2.setText(ta2.getText()+bm+t+分隔符+t+5+n); m+; else if(bm=) ta2.setText(ta2.getText()+bm+t+分隔符+t+5+n); m+; else if(bm=) ta2.setText(ta2.getText()+bm+t+分隔符+t+5+n); m+; else if(bm=) ta2.setText(ta2.getText()+bm+t+分隔符+t+5+n); m+; 结构:主要运行结果:五、实验小结(实验中遇到的问题及解决过程、实验中产生的错误及原因分析、实验体会和收获)本实验完成时,没有碰到太大的困难,很多问题百度后都有结果。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号