package LL1;
import java.util.*;
import javax.swing.*;
import java.lang.*;
import java.io.StringReader;
import javax.security.auth.Subject;
import javax.swing.text.AbstractDocument.BranchElement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.omg.CORBA.FieldNameHelper;
public class LL1 {
//private char[] VN = {'E', 'T', 'G', 'F', 'S'};//非终结符
//private char[] VT = {'+', '-', '*', '/', '(', ')', 'i', 'e'};//终结符
//private String[] F = {"E->TG", "G->+TG|-TG", "G->e", "T->FS", "S->*FS|/FS", "S->e", "F->(E)", "F->i"};
private String[] F;
private char[] VN;
private char[] VT;
private Vector<String> simF = new Vector<String>();
private StringBuffer[] FirstVN ;
private StringBuffer[] FollowVN ;
private boolean[] VNE ;
private char emp = 'e';
private Map<String, String> paTable;
private String tempVT ;
private Vector<String> stackChange = new Vector<String>();
private Vector<String> inputStrChange = new Vector<String>();
private Vector<String> actionChange = new Vector<String>();
private Stack<Character> symStack = new Stack<Character>();
private JFrame frame, table;
private JTable jt;
private JButton chooseButton, startButton, resetButton;
private JTextField fileField,inputStrField;;
private JPanel northPanel, centralPanel, southPanel;
private JTextArea mapArea;
private String file = "";
private Vector<String> inputF = new Vector<String>();
public LL1(){
createGui();
}
public void dealInputF()
{
F = new String[inputF.size()];
for(int i = 0;i<inputF.size();i++)
{
F[i] = inputF.elementAt(i);
}
simF();
StringBuffer getVN = new StringBuffer();
for(int i = 0;i<simF.size();i++)
{
if(!getVN.toString().contains(String.valueOf(simF.elementAt(i).charAt(0))))
{
getVN.append(simF.elementAt(i).charAt(0));
}
}
VN = new char[getVN.length()];
for(int i = 0;i<getVN.length();i++)
{
VN[i] = getVN.charAt(i);
}
StringBuffer getVT = new StringBuffer();
for(int i = 0;i<simF.size();i++)
{
for(int j = 3;j<simF.elementAt(i).length();j++)
{
if(!getVN.toString().contains(String.valueOf(simF.elementAt(i).charAt(j))))
{
if(!getVT.toString().contains(String.valueOf(simF.elementAt(i).charAt(j))))
{
getVT.append(simF.elementAt(i).charAt(j));
}
}
}
}
VT = new char[getVT.length()];
for(int i = 0;i<getVT.length();i++)
{
VT[i] = getVT.charAt(i);
}
FirstVN = new StringBuffer[VN.length];
FollowVN = new StringBuffer[VN.length];
VNE = new boolean[VN.length];
tempVT= String.valueOf(VT)+"#";
for(int i = 0;i<VN.length;i++)
{
System.out.println(VN[i]);
}
for(int i = 0;i<VT.length;i++)
{
System.out.println(VT[i]);
}
}
public void simF()//去除文法中的|(或)符号,简化
{
for(int i = 0;i<F.length;i++)
{
if(F[i].contains("|"))
{
int orPos1 = 3;
int orpos2 = 0;
for(int j = 0;j<F[i].length();j++)
{
if(F[i].charAt(j)=='|')
{
orpos2 = j;
simF.addElement(F[i].substring(0, 3)+F[i].substring(orPos1, orpos2));
orPos1 = orpos2+1;
}
}
simF.addElement(F[i].substring(0, 3)+F[i].substring(orPos1,F[i].length()));
}
else
{
String temp = F[i];
simF.addElement(temp);
}
}
}
public boolean isInVN(char c)//c是不是VN数组里的一个元素
{
String s = String.valueOf(VN);
if(s.contains(String.valueOf(c)))
return true;
else
return false;
}
public boolean isInVT(char c)//c是不是VT数组里的一个元素
{
String s = String.valueOf(VT);
if(s.contains(String.valueOf(c)))
return true;
else
return false;
}
public void createFirstVN()//求全体非终结符的First集合,放在FirstVN[]中
{
FirstVN = new StringBuffer[VN.length];
for(int i = 0;i<FirstVN.length;i++)
{
FirstVN[i] = new StringBuffer();
}
for(int i = 0;i < FirstVN.length;i++)
{
if(FirstVN[i].toString().isEmpty())//如果还没求过的话
{
myFirstVN(VN[i]);
}
}
for(int i = 0;i < VN.length;i++)
{
if(VNE[i]&&(!FirstVN[i].toString().contains("e")))
{
FirstVN[i].append(emp);
}
}
}
public void myFirstVN(char c)//求单个非终结符的first集合
{
String svn = String.valueOf(VN);//中间变量
for(int i = 0;i < simF.size();i++)
{
String s = simF.elementAt(i);
if(c == s.charAt(0))//产生式左边是和c一样的非终结符
{
for(int j = 3;j<s.length();j++)//S=>AB..则从箭头后面开始读取符号处理
{
if(isInVT(s.charAt(j)))//当为终结符的时候直接加入到FIrst集合当中
{
if(s.charAt(j) == emp)
{
VNE[svn.indexOf(s.charAt(0))] = true;
}
FirstVN[svn.indexOf(s.charAt(0))].append(s.charAt(j));
break;
}
else if(isInVN(s.charAt(j)))//阻止A=>..A..这样的情况,防止出现左递归,当为非终结符且没有左递归,
{
if(c==s.charAt(j))//防止出现左递归
{
break;
}
if(FirstVN[svn.indexOf(s.charAt(j))].toString().isEmpty())//如果这个非终结符还没有求得话,求此非终结符的first集合
{
myFirstVN(s.charAt(j));
}
if(FirstVN[svn.indexOf(s.charAt(j))].toString().contains("e"))//S->A A 如果此非终结符first集合含有空
{
FirstVN[svn.indexOf(s.charAt(0))].append(FirstVN[svn.indexOf(s.charAt(j))].deleteCharAt(FirstVN[svn.indexOf(s.charAt(j))].toString().indexOf('e')));
if(j == s.length()-1)//如果此非终结符是最后一位,则标记此非终结符能推出空
{
VNE[svn.indexOf(s.charAt(0))] = true;
}
continue;
}
else//如果不是最后一位的话
{
FirstVN[svn.indexOf(s.charAt(0))].append(FirstVN[svn.indexOf(s.charAt(j))]);
break;
}
}
}
}
}
}
public void createFollowVN()//求所有非终结符follow集合
{
for(int i = 0;i<FollowVN.length;i++)
{
FollowVN[i] = new StringBuffer();
}
for(int i = 0;i<FollowVN.length;i++)
{
if(FollowVN[i].toString().isEmpty())//如果还没求过的话
{
myFollowVN(VN[i]);
}
}
}
public void myFollowVN(char c)//求单个非终结符follow集合
{
if(c == simF.elementAt(0).charAt(0))//c为开始符号时,将#加入c的follow集中'
{
FollowVN[0].append("#");//开始符号下标为0
}
String svn = String.valueOf(VN);
for(int i = 0;i<simF.size();i++)
{
String s= simF.elementAt(i);
if(s.substring(3).contains(String.valueOf(c)))
{
int cPos = s.substring(3).indexOf(c)+3;
if(cPos != s.length()-1 )
{
for(int j = cPos+1;j<s.length();j++)
{
if(String.valueOf(VT).contains(String.valueOf(s.charAt(j))))//如果非终结符c的下一位是终结符则直接加入
{
if(!FollowVN[String.valueOf(VN).toString().indexOf(c)].toString().contains(String.valueOf(s.charAt(j))))
{
FollowVN[String.valueOf(VN).toString().indexOf(c)].append(s.charAt(j));
}
break;//判断下一文法
}
else//下一位为非终结符
{
if(VNE[String.valueOf(VN).indexOf(s.charAt(j))])//如果下一位的非终结符能推出空
{
if(j==s.length()-1)
{
int ePos = FirstVN[String.valueOf(VN).indexOf(s.charAt(j))].indexOf("e");
String temp = FirstVN[String.valueOf(VN).indexOf(s.charAt(j))].deleteCharAt(ePos).toString();
FirstVN[String.valueOf(VN).indexOf(s.charAt(j))].append("e");
if(!FollowVN[String.valueOf(VN).indexOf(c)].toString().contains(temp))
{
FollowVN[String.valueOf(