1、 1 / 9实验三 语法分析器的设计一、 实验内容设计、编写和调试构造 LR(0)项目集规范簇或实现基于 LR 分析表对给定的符号串进行 LR分析的程序。以下两个内容任选其中一项:(1) 对于给定的文法,实现构造识别该文法全部活前缀 DFA 的程序。(2) 对于给定的 LR 分析表和符号串,设计程序以实现所输入符号串是否为合法符号串。要求用 JAVA 语言编程。 (可参考实验指导书 P149 至 P156)二、 程序代码AnalysisOfGrammer.javapackage analysis;import javax.swing.*;import javax.swing.table.Def
2、aultTableModel;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.LinkedList;public class AnalysisOfGrammer extends JAppletprivate JFileChooser jfc = new JFileChooser(new File(“.“);private JButton jbt1 = new JButton(“打开文法文件 “);private JButton jbt2 = new JButton(“构造LR规范簇“);pr
3、ivate JButton jbt3 = new JButton(“构造LR分析表“);private JButton jbt4 = new JButton(“清空“);private JButton jbt5 = new JButton(“退出“);private JLabel jl1 = new JLabel(“LR(0)项目集规范簇“);private JLabel jl2 = new JLabel(“LR(0)分析表“);private JPanel p3 = new JPanel();private JTextArea jta1 = new JTextArea();private S
4、tring grammer = new String50;private int count = 0;private LinkedList list = new LinkedList();private Object content = new Object1004;int num1 = 0;String cache = new String50100;int location = new int50;int back = 0;public void clear1()grammer = null;2 / 9public void clear2()num1 = 0;list = null;con
5、tent = null;cache = null;location = null;back = 0;public AnalysisOfGrammer()JPanel p1 = new JPanel();p1.setLayout(new GridLayout(1,5);p1.add(jbt1);p1.add(jbt2);p1.add(jbt3);p1.add(jbt4);p1.add(jbt5);add(p1,BorderLayout.NORTH);JPanel p4 = new JPanel();p4.setLayout(new GridLayout(1,2);JPanel p2 = new
6、JPanel();p2.setLayout(new BorderLayout();p2.add(new JLabel(“文法为:“),BorderLayout.NORTH);p2.add(new JScrollPane(jta1),BorderLayout.CENTER);p4.add(p2);p4.add(p3);add(p4,BorderLayout.CENTER);jbt1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)jta1.setText(“);open(););jbt
7、2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(jta1.getText().equals(“)JOptionPane.showMessageDialog(null, “请打开文法文件!“);elseDNF();list.get(1).setNextState(“接受态“);for(int k = 0 ; k “+grammer0.substring(0, 1);flagnum1 = createI(start);/初态集建立locationnum1 = getleng(c
8、achenum1);while(back = count else if(i = count)head = grammer0.substring(0,1);/while循环结束int cc = 0; /设置下一状态的值String nextS = new String50;for(int i = 0 ; cachenum1i!=null; i+)int j = 0;boolean f = false;for(; nextSj!=null;j+)if(findNext(cachenum1i).equals(nextSj)f = true; break;/for j 的循环结束,查找有无相同的后继
9、符号,f 作为标记,相同的符号6 / 9跳过,不同的记录下来,放在nestS 里if(f != true)nextScc+ = findNext(cachenum1j);nextsign = nextsign+findNext(cachenum1j)+“ “;/for i 循环结束ViablePrefixe o = new ViablePrefixe(state,set,nextsign,nextstate);list.add(o);return end;public int show(String s, String grammer)/文法的拆分函数,显示在界面上,返回值是文法的条数bool
10、ean lastSign = false;/上一符号为nint flag1 = 0;/上一符号位置int count = 0;String str = s.substring(flag1, flag1+3);for(int i = 0 ; i m.length()-1)t = m.substring(0,3)+“.“+m.substring(3,m.length();return t;private static String findNext(String m)/返回.后的字母String c = “#“;for(int i = 0 ; i m.length()-1 ; i+)if(m.su
11、bstring(i,i+1).equals(“.“)c = m.substring(i+1,i+2);return c;ViablePrefixe.javapackage analysis;public class ViablePrefixe /活前缀类private String state = “;/状态private String projectSet = “;/项目private String nextSign = “;/后继符号private String nextState = “;/后继状态public ViablePrefixe(String state,String proj
12、ectSet,String nextSign,String nextState)this.state = state;this.projectSet = projectSet;this.nextSign = nextSign;this.nextState = nextState;public ViablePrefixe(String state,String projectSet,String nextSign)this.state = state;this.projectSet = projectSet;this.nextSign = nextSign;public ViablePrefix
13、e(String state,String projectSet)this.state = state;this.projectSet = projectSet;public ViablePrefixe()public String getState() return state;public void setState(String state) 9 / 9this.state = state;public String getProjectSet() return projectSet;public void setProjectSet(String projectSet) this.pr
14、ojectSet = projectSet;public String getNextSign() return nextSign;public void setNextSign(String nextSign) this.nextSign = nextSign;public String getNextState() return nextState;public void setNextState(String nextState) this.nextState = nextState;public String toString()return state+“ “+projectSet+“ “+nextSign+“ “+nextState+“n“;三、 实验结果