《编译原理词法分析实验》由会员分享,可在线阅读,更多相关《编译原理词法分析实验(20页珍藏版)》请在金锄头文库上搜索。
1、编译原理上机实验试题一、实验目的通过本实验使学生进一步熟悉和掌握程序设计语言的词法分析程序的设计原理及相关的设计技术,如何针对确定的有限状态自动机进行编程序;熟悉和掌握程序设计语言的语法分析程序的设计原理、熟悉和掌握算符优先分析方法。二、实验要求本实验要求:要求能熟练使用 C+程序设计语言编程;在上机之前要有详细的设计报告(预习报告) ;要编写出完成相应任务的程序并在计算机上准确地运行;实验结束后要写出上机实验报告。三、实验题目针对下面文法 G(S):S v = EE E+EE-EE*EE/E(E) v i其中,v 为标识符,i 为整型或实型数。要求完成 使用自动机技术实现一个词法分析程序;
2、使用算符优先分析方法实现其语法分析程序; 在语法分析过程中同时完成常量表达式的计算。针对下面文法 G(S):S v = EE E+EE-EE*EE/E(E) v i其中,v 为标识符,i 为整型或实型数。要求完成 使用自动机技术实现一个词法分析程序; 使用算符优先分析方法实现其语法分析程序; 在语法分析过程中同时完成常量表达式的计算。/编译原理课程设计,词法自动机。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.io.*;import ja
3、va.util.*;class frame extends JFrame implements ActionListenerJButton button1=new JButton(打开现有文件 );JButton button2=new JButton(现场输入文件 ); Choice ch=new Choice();JButton button=new JButton(执行);TextArea ta=new TextArea();TextArea tb=new TextArea();JFileChooser chooser=new JFileChooser();FileInputStream
4、 readfile=null;int b;byte buffer=new byte25000;String str=null;String id=asm,cout,main,FALSE,TRUE,include,boolean,default,float,operator,abs,static_cast,union,auto,delete,for,private,struct,unsigned,bool,do,friend,protected,switch,using,break,double,goto,public,template,virtual,case,dynamic_cast,if,
5、register,this,void,catch,else,inline,reinterpret_cast,throw,volatile,char,enum,int,return,true,wchar_t,class,explicit,long,short,try,while,const,export,mutable,signed,typedef,const_cast,extern,namespace,sizeof,typeid,Continue,false,new,static,typename;FileReader file=null;BufferedReader in=null;File
6、 f=null;frame()super();Container c=this.getContentPane();ch.add(C 语言常数识别 );ch.add(C 语言标识符识别 );ch.add(C 语言注释字母大写 );ch.add(C 语言保留字大写 );c.setLayout(null);c.add(button1);button1.setBounds(5,5,120,25);button1.addActionListener(this);c.add(button2);button2.setBounds(130,5,120,25);button2.addActionListener
7、(this); c.add(ch);ch.setBounds(260,5,120,25);c.add(button);button.setBounds(390,5,80,25);button.addActionListener(this);c.add(ta);ta.setBounds(5,40,482,200);ta.setEditable(false);c.add(tb);tb.setBounds(5,250,482,200);tb.setEditable(false);this.setBounds(300,100,500,500);this.setVisible(true);this.se
8、tResizable(false);this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e) System.exit(0););public void actionPerformed(ActionEvent ae)/-输入原文件-if(ae.getSource()=button1)String s;ta.setText(null);int state=chooser.showOpenDialog(null);f=chooser.getSelectedFile();if(f!=null&s
9、tate=JFileChooser.APPROVE_OPTION)tryreadfile=new FileInputStream(f);catch(IOException ee)try b=readfile.read(buffer,0,25000);str=new String(buffer,0,b);ta.append(str);catch(IOException e1)System.out.println(File read error);/-输入原文件-else if(ae.getSource()=button2)frame.this.ta.setEditable(true);else
10、if(ae.getSource()=button)/-C 语言常数识别-if(ch.getSelectedIndex()=0)tb.setText(null);StringTokenizer sign=new StringTokenizer(str, ()=#;n );int n=sign.countTokens();while(sign.hasMoreTokens()String s=sign.nextToken();int n1=s.length();String s1=s.substring(0,1);byte d=s1.getBytes();if(d0=48)|d0=45)int j=
11、0;for(int i=1;i=48)j+=1;if(j=n1-1) tb.append(s+n); if(d0=48)|d0=45)int j=0;for(int i=1;i=48)|f0=46)j+=1;if(j=n1-1)tb.append(s+n); if(d0=39) int j=0;for(int i=1;i=97)|(f0=65)j+=1;if(j=n1-1)tb.append(s+n);elseString t=TRUE;String f=FALSE; if(s.equals(t)|s.equals(f)tb.append(s+n);/-C 语言常数识别-/-C 语言标志符识别
12、-else if(ch.getSelectedIndex()=1)tb.setText(null);StringTokenizer sign=new StringTokenizer(str, ()=#;n );int n=sign.countTokens();while(sign.hasMoreTokens()String s=sign.nextToken();int n1=s.length();String s1=s.substring(0,1);byte d=s1.getBytes();if(d0=97)|(d0=65)|d0=95)int j=0;for(int i=1;i=97)|(f
13、0=65)|(f0=95)|(f0=48)j+=1;if(j=n1-1)int m=0;for(int k=0;k=97&f0=97&d0=97&g0*+-/!);while(o.hasMoreTokens()String s1=o.nextToken();int m=0;for(int i=1;i*+-/!);while(o.hasMoreTokens()String s1=o.nextToken();int m=0; for(int i=1;iid.length;i+)if(s1.equals(idi)m+;if(!(m=0)s1=s1.toUpperCase();tb.append(s1+n);catch(IOException ext)/-C 语言保留字大写- public class Designpublic static void main(String args)new frame();