LL1语法分析JAVA实现(有界面)

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.sql.*; import java.util.Vector;
t3.append(first[i][j] + " , "); } t3.append("}" + "\n"); }
follow = new char[Vnnum][100]; String followVn[] = null; followComplete = new int[Vnnum]; for (int i = 0; Vn[i] != null; i++) // 求 FOLLOW** {
public class LL1 extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = 1L; JTextField tf1; JTextField tf2; JLabel l; JButton b0; JPanel p1, p2, p3; JTextArea t1, t2, t3; JButton b1, b2, b3; JLabel l0, l1, l2, l3, l4; JTable table; Statement sta; Connection conn; ResultSet rs; DefaultTableModel dtm; String Vn[] = null; Vector<String> P = null;
int firstComplete[] = null;// 存储已判断过 first 的数据 char first[][] = null;// 存储最后 first 结果 int followComplete[] = null;// 存储已判断过 follow 的数据 char follow[][] = null;// 存储最后 follow 结果 char select[][] = null;// 存储最后 select 结果 int LL = 0;// 标记是否为 LL(1) String vt_tou[] = null;// 储存 Vt Object shuju[][] = null;// 存储表达式数据 char yn_null[] = null;// 存储能否推出空
"输入的文法为:
");
l2 = new JLabel(" ");
l3 = new JLabel(
"分析的结果:
");
l4 = new JLabel(
"预测分析表:
");
ቤተ መጻሕፍቲ ባይዱ
// p1=new JPanel();
p2 = new JPanel();
p3 = new JPanel();
t1 = new JTextArea(24, 20);
{ if (t.charAt(j) > 'Z' || t.charAt(j) < 'A') { if (puanduanChar(Vt, t.charAt(j))) { Vt[biaozhi] = t.charAt(j); biaozhi++; } }
} } if (puanduanChar(Vt, '#'))// 若可推出空集,则将#加入 Vt。 {
t3.append(follow[i][j] + " , "); } t3.append("}" + "\n"); }
select = new char[P.size()][100]; for (int i = 0; i < P.size(); i++) // 求 SELECT** {
flag = 0; tianjiaSelect(select[i], (String) P.elementAt(i), flag); } t3.append("select 集:" + "\n"); // 显示 SELECT** for (int i = 0; i < P.size(); i++) { t3.append("select(" + (String) P.elementAt(i) + ")={ "); for (int j = 0; select[i][j] != '\0'; j++) {
LL1() { setLocation(100, 0); setSize(700, 780);
tf1 = new JTextField(13);
tf2 = new JTextField(13);
l = new JLabel(">>");
l0 = new JLabel("输入字符串:");
l1 = new JLabel(
String t = (String) P.elementAt(j);
if (t.substring(0, 1).equals(Vn[i])) { for (k = 0; select[j][k] != '\0'; k++) { if (puanduanChar(save, select[j][k])) {
table = new JTable();
JScrollPane jp1 = new JScrollPane(t1);
JScrollPane jp2 = new JScrollPane(t2);
JScrollPane jp3 = new JScrollPane(t3);
p2.add(tf1);
p2.add(l);
== -1)
flag = 0; followVn = new String[20]; if ((flag = tianjiaFollow(follow[i], Vn[i], followVn, flag))
return; followComplete[i] = 1; } t3.append("follow 集:" + "\n"); // 显示 FOLLOW** for (int i = 0; Vn[i] != null; i++) { t3.append("follow(" + Vn[i] + ")={ "); for (int j = 0; follow[i][j] != '\0'; j++) {
} t3.append("first 集:" + "\n"); // 显示 FIRST** for (int i = 0; Vn[i] != null; i++) {
t3.append("first(" + Vn[i] + ")={ "); for (int j = 0; first[i][j] != '\0'; j++) {
Vt[biaozhi] = '#'; biaozhi++; } vt_tou = new String[biaozhi + 1];// 根据 select 和表达式生成预测分
break; } } if (Vnnum == 0 || k >= Vnnum) { Vn[Vnnum] = s[i].substring(0, 1);// 存入 Vn 数据 Vnnum++; } P.add(s[i]); } else { t3.setText("文法输入有误,请重新输入"); return; } } yn_null = new char[100]; first = new char[Vnnum][100]; int flag = 0; String firstVn[] = null; firstComplete = new int[Vnnum]; for (int i = 0; Vn[i] != null; i++) // 依次求 FIRST** { flag = 0; firstVn = new String[20]; if ((flag = add_First(first[i], Vn[i], firstVn, flag)) == -1) return; firstComplete[i] = 1;
if (e.getSource() == b1) { t3.setText(""); int Vnnum = 0, k; Vn = new String[100]; P = new Vector<String>(); String s[] = t1.getText().split("\n"); for (int i = 0; i < s.length; i++) { if (s.length < 2) { t3.setText("文法输入有误,请重新输入");// 判断长度是否符合 return; } if (s[i].charAt(0) <= 'Z' && s[i].charAt(0) >= 'A' && s[i].charAt(1) == '→') { for (k = 0; k < Vnnum; k++) { if (Vn[k].equals(s[i].substring(0, 1))) {
t3.append(select[i][j] + " , "); } t3.append("}" + "\n"); }
for (int i = 0; Vn[i] != null; i++)// 判断 select 交集是否为空 {
int biaozhi = 0; char save[] = new char[100]; for (int j = 0; j < P.size(); j++) {
b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); table.setPreferredScrollableViewportSize(new Dimension(660, 200)); setVisible(true);
t2 = new JTextArea(1, 30);
t3 = new JTextArea(24, 40);
b0 = new JButton("确定(S 为开始)");
b1 = new JButton("
判断文法
");
b2 = new JButton("输入");
b3 = new JButton("清空");
存入 Vt 析表
save[biaozhi] = select[j][k]; biaozhi++; } else// 当有交集时,不为 LL(1)文法 { t3.append("不是 LL(1)文法!!" + "\n"); return; } } } } } char Vt[] = new char[100]; int biaozhi = 0; for (int i = 0; i < P.size(); i++) { String t = (String) P.elementAt(i); for (int j = 2; j < t.length(); j++)// 提取表达式右侧的终结符
} ////////////////////界面设计
public void actionPerformed(ActionEvent e) { if (e.getSource() == b0) { String a = tf1.getText(); String b = tf2.getText(); t1.append(a + '→' + b + '\n'); }
p2.add(tf2);
p2.add(b0); p2.add(b1); p2.add(l0); p2.add(l2); p2.add(jp2); p2.add(b2); p2.add(b3);
p2.add(l1);
p2.add(l3); p2.add(jp1); p2.add(jp3);
p3.add(l4); p3.add(new JScrollPane(table)); add(p2, "Center"); add(p3, "South");
相关文档
最新文档