1、编译原理实验报告实验三最新版中南大学编译原理课程实验实验报告题 目 实验三 学生姓名 陈亮名 学生学号 专业班级 1005 完成日期 2012/6/2 实验三 算符优先分析法设计与实现一、实验目的加深对语法分析器工作过程的理解;加强对算符优先分析法实现语法分析程序的掌握;能够采用一种编程语言实现简单的语法分析程序;能够使用自己编写的分析程序对简单的程序段进行语法翻译。二、实验内容在实验1的基础上,用算符优先分析法编制语法分析程序,语法分析程序的实现可以采用任何一种编程语言和工具。三、实验要求:1. 对语法规则有明确的定义;2. 编写的分析程序能够对实验一的结果进行正确的语法分析;3. 对于遇到
2、的语法错误,能够做出简单的错误处理,给出简单的错误提示,保证顺利完成语法分析过程;四、实验步骤1. 定义目标语言的语法规则;A.关键字后不能跟字符串运算符B.数字后不能跟关键字,数字,字符串,标示符C.字符串后不能跟关键字,数字,字符串,标示符D.标示符后不能跟数字,字符串2.依次读入实验一的分析结果,根据预测分析的方法进行语法分析,直到源程序结束;读入程序如下:package com.demo;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.
3、io.FileOutputStream;import java.io.IOException;public class copy public boolean cop(String src,String des) File sorFile,desFile; sorFile=new File(src); desFile=new File(des); FileInputStream fis=null; FileOutputStream fos=null; int n=0; byteb=new byte4*1024; try fis=new FileInputStream(src); fos=new
4、 FileOutputStream(des); while (n=fis.read(b)!=-1) fos.write(b, 0, n); / tyfkfuuofiiu /* * fhshtjsjtrjrs */ fos.flush(); fis.close(); fos.close(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); return true; public static void main(String args) copy cp=
5、new copy(); String src=args0; String des=args1; if (cp.cop(src, des) public 文件复制成功=String string; /(这一句有错误) System.out.println(文件复制成功); else System.out.println(shibai); 词法分析:进行语法分析:3.对遇到的语法错误做出错误处理。注:由于java整套语言出错的情况太多,以上只考虑了部分出错情况分析器代码如下:zpackage demo;import java.awt.Dimension;import java.awt.Toolki
6、t;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;
7、import java.io.IOException;import java.io.UnsupportedEncodingException;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class WordAnalys
8、es extends JFrame private JButton choosefileButton; private JButton analyseButton; private JButton grammarButton; private JButton exportButton; private JTextField jtf; private JTextArea outpuTextArea; JScrollPane jsp;/ 新建一个滚动条界面,将文本框传入 String s = ; String s2 = ; String word= new String8*10243; priva
9、te int index = 0; File file; int m = 0; int f= 0; FileInputStream fileInputStream; / 关键字 private String code1 = new String if, int, for, while, do, return, break, continue, package, public, class, static, void, main, new, null, abstract, boolean, byte, case, catch, char, default, double, else, enum,
10、 extends, final, finally, float, implements, import, instanceof, interface, long, native, private, protected, strictfp, short, super, switch, synchronized, this, throw, throws, transient, try, volatile ; / 运算符 private String code4 = new String +, -, *, /, =, , &, &, |, or, and, xor, ! ; / 标点 private
11、 String code5 = new String , ;, , , (, ), , , . ; private StringBuffer sBuffer ; public WordAnalyses(String title) super(title); Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension scrnsize = toolkit.getScreenSize(); setSize(600, 700); setLocation(scrnsize.width - 600) / 2, (scrnsize.height - 7
12、00) / 2); myframe(); add(choosefileButton).setBounds(25, 10, 100, 25); add(analyseButton).setBounds(175, 10, 100, 25); add(grammarButton).setBounds(325, 10, 100, 25); add(exportButton).setBounds(475, 10, 100, 25); add(jsp).setBounds(0, 45, 590, 600); add(jtf).setBounds(0, 35, 600, 10); / add(jsp).se
13、tBounds(0, 45, 600, 665); setVisible(true); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); public void myframe() choosefileButton = new JButton(选择文件); choosefileButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)
14、 f= 1; int n; sBuffer = new StringBuffer(s2); try JFileChooser jfc = new JFileChooser(); jfc.showOpenDialog(WordAnalyses.this); file = jfc.getSelectedFile(); fileInputStream = new FileInputStream(file); while (n = fileInputStream.read() != -1) sBuffer.append( + (char) n); catch (FileNotFoundExceptio
15、n e1) e1.printStackTrace(); catch (IOException e1) e1.printStackTrace(); try s2 = new String(sBuffer.toString().getBytes(ISO8859-1); catch (UnsupportedEncodingException e1) / TODO Auto-generated catch block e1.printStackTrace(); sBuffer = new StringBuffer(s2); ); analyseButton = new JButton(开始分析); a
16、nalyseButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if (f =0) JOptionPane.showMessageDialog(rootPane, 请先选择要分析的文件!); else System.out.println(sBuffer.length(); m = 0; for (int i = 0; i sBuffer.length(); i+) s = s + sBuffer.charAt(i); if (s.equals( ) | s.equal
17、s( ) | s.equals(rn) | s.equals(n)| s.equals(r) s = ; continue; /注释 if (s.equals(/) & ( + sBuffer.charAt(i + 1).equals(/) i = i + 2; s = /; while (!( + sBuffer.charAt(i).equals(r) s = s + sBuffer.charAt(i); i+; System.out.println( + 2 , + s + ); outpuTextArea.setText(outpuTextArea.getText() + ( + 2 ,
18、 + s + ) + rn); wordindex1= s; wordindex2= 2; index +; s = ; continue; /段注释 if (s.equals(/) & ( + sBuffer.charAt(i + 1).equals(*) i = i + 2; s = /*; while (!( + sBuffer.charAt(i).equals(*) & ( + sBuffer .charAt(i + 1).equals(/) s = s + sBuffer.charAt(i); i+; i = i + 2; s = s + */; System.out.println
19、( + 3 , + s + ); outpuTextArea.setText(outpuTextArea.getText() + ( + 3 , + s + ) + rn); wordindex1= s; wordindex2= 3; index +; s = ; continue; /关键字 if (!s.equals() for (int j = 0; j code1.length; j+) if (s.equals(code1j) & ( + sBuffer.charAt(i + 1).equals( ) | ( + sBuffer.charAt(i + 1) .equals(r) |
20、( + sBuffer .charAt(i + 1).equals(n) System.out.println( + 1 , + code1j + ); outpuTextArea.setText(outpuTextArea.getText() + ( + 1 , + code1j + ) + rn); wordindex1= s; wordindex2= 1; index +; s = ; break; /运算符 if (!s.equals() String sss = ; for (int j = 0; j code4.length; j+) if (s.equals(code4j) if
21、 (s.equals(or) | s.equals(and) | s.equals(xor) System.out.println( + 4 , + s + ); outpuTextArea.setText(outpuTextArea .getText() + ( + 7 , + s + ) + rn); wordindex1= s; wordindex2= 7; index +; s = ; break; sss = sss + code4j; for (int k = 0; k = 48 & ( + sBuffer).charAt(i) 57) if ( + sBuffer).charAt
22、(i + 1) 57) System.out.println( + 3 + , + s + ); outpuTextArea.setText(outpuTextArea.getText() + ( + 4 + , + s + ) + rn); wordindex1= s; wordindex2= 4; index +; s = ; continue; /分隔符 if (!s.equals() for (int j = 0; j code5.length; j+) if (s.equals(code5j) System.out.println( + 5 + , + code5j + ); out
23、puTextArea.setText(outpuTextArea.getText() + ( + 8 + , + code5j + ) + rn); wordindex1= s; wordindex2= 8; index +; s = ; break; /字符串 if (s.equals() i+; while (!( + sBuffer.charAt(i).equals() s = s + sBuffer.charAt(i); i+; s = s + sBuffer.charAt(i); System.out.println( + 字符串 + , + s + ); outpuTextArea.setText(outpuTextArea.getText()