import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableCellRenderer;
public class LR1 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
//private char VN[]={'S','E','T','F'};//非终结符
//private char VT[]={'+','*','(',')','i','$','ε'};//终结符
//private char Total[]={'S','E','T','F','+','*','(',')','i','$'};
private char VN[]=new char[100];//定义终结符数组元素个数
private char VT[]=new char[100];
private char Total[]=new char[100];
private String[] First=new String[VN.length];//单个非终结符的first集
private String M[][]=new String[100][100];
private int N[][]=new int[100][100];
private int Goto[][]=new int[100][Total.length];
private String ACtion[][]=new String[100][VT.length];
private boolean Visited[]=new boolean[100];
//private String G[]={"S->E","E->E+T","E->T","T->T*F","T->F","F->(E)","F->i"};//文法
private String G[]=new String[100];
//建立表格
private Object[] colname = {"步骤","分析栈","分析符号","剩余字符串","动作"};
private Object[][] data=new Object[50][5];
private JTable table;
private JTextField j3;
private JLabel j8;
private JLabel j9;
private JLabel j10;
private JTextArea j5;
private JTextArea j12;
private JTextArea j13;
private JTextArea j14;
private JTextArea j15;
private JScrollPane jsPan;
private JScrollPane jsPane;
private JScrollPane jsPan1;
private JScrollPane jsPan2;
private JScrollPane jsPan3;
private JScrollPane jsPan4;
private File imgFile = null;// 声明所选择文件
private BufferedReader in1;
private GridBagLayout layout;
private GridBagConstraints s;
private int h = 0;
private String getstr=null;
private int ONE_SECOND = 1000;
private String DEFAULT_TIME_FORMAT = "HH:mm:ss";
private String time;
//获取系统的日期
public String Get_Date() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateNowStr = sdf.format(date);
return dateNowStr;
}
public void Get_Time() {
Timer tmr = new Timer();
tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);
}
//将表格清空
public void Get_Clear() {
int i;
for(i=0;i<50;i++) {
data[i]=new Object[]{"","","","",""};
}
}
public void Init() {//初始化follow集,将#放到开始符号集中去
M[0][0]="S->.E,$";
int i=0,j=0;
for(i=0;i<100;i++) {
for(j=0;j<100;j++) {
N[i][j]=0;
}
}
for(i=0;i<100;i++) {
Visited[i]=false;
}
}
public int IsInTotal(char c) {//判断是否在总的字符中
int i;
int locate=999;
for(i=0;iA.Bb,a]
if(M[I][i]!=null) {//如果字符串非空
j=3;
while(M[I][i].charAt(j)!='.'&&jA.Bb,a]
if(str[i]!=null) {//如果字符串非空
j=3;
while(str[i].charAt(j)!='.'&&j stack) {
/*String s=stack.toString();
int i;
StringBuilder t=new StringBuilder();
for(i=0;i stack) {
String s=stack.toString();
int i;
StringBuilder t=new StringBuilder();
for(i=0;i stack = new Stack(); // 创建堆栈对象
Stack stack1 = new Stack();
stack.push(0);
while(true) {
int s=stack.peek();
if(ACtion[s][IsInVt(getstr.charAt(i))].charAt(0)=='s') {
System.out.print(Print_Stack(stack));
System.out.print(" ");
System.out.print(Print_Stack1(stack1));
System.out.print(" ");
System.out.print(Print_Buffer(getstr,i));
System.out.println();
data[h++]=new Object[]{h,Print_Stack(stack),Print_Stack1(stack1),Print_Buffer(getstr,i),"移入"};
stack1.push(getstr.charAt(i));
stack.push(Integer.parseInt(ACtion[s][IsInVt(getstr.charAt(i))].substring(1,ACtion[s][IsInVt(getstr.charAt(i))].length())));
i++;
} else {
if(ACtion[s][IsInVt(getstr.charAt(i))].charAt(0)=='r') {
System.out.print(Print_Stack(stack));
System.out.print(" ");
System.out.print(Print_Stack1(stack1));
System.out.print(" ");
System.out.print(Print_Buffer(getstr,i));
System.out.print(" ");
j=Integer.parseInt(ACtion[s][IsInVt(getstr.charAt(i))].substring(1,ACtion[s][IsInVt(getstr.charAt(i))].length()));
data[h++]=new Object[]{h,Print_Stack(stack),Print_Stack1(stack1),Print_Buffer(getstr,i),"根据"+G[j]+"归约"};
k=Get_Number(j);
while(k>0) {
stack.pop();
stack1.pop();
k--;
}
stack1.push(G[j].charAt(0));
t=stack.peek();
stack.push(Goto[t][IsInTotal(G[j].charAt(0))]);
System.out.print(G[j]);
System.out.println();
} else {
if(ACtion[s][IsInVt(getstr.charAt(i))].equals("acc")) {
System.out.print(Print_Stack(stack));
System.out.print(" ");
System.out.print(Print_Stack1(stack1));
System.out.print(" ");
System.out.print(Print_Buffer(getstr,i));
System.out.print(" ");
System.out.print(G[0]);
System.out.println();
data[h++]=new Object[]{h,Print_Stack(stack),Print_Stack1(stack1),Print_Buffer(getstr,i),"接受"};
break;
} else {
System.out.println("ERROR");
}
}
}
}
}
public void Get_VN_VT() {//获得非终结符以及终结符
int i,j,h;
for(i=0;i<100;i++) {
if(G[i]!=null) {
for(j=0;j='A') {
if(IsInVn(G[i].charAt(j))==999) {
h=Get_NotNull_B(VN);
VN[h]=G[i].charAt(j);
}
} else {
if(j!=1&&j!=2&&G[i].charAt(j)!='|') {
if(IsInVt(G[i].charAt(j))==999) {
h=Get_NotNull_B(VT);
VT[h]=G[i].charAt(j);
}
}
}
}
}
}
if(IsInVt('$')==999) {
h=Get_NotNull_B(VT);
VT[h]='$';
}
/*if(IsInVt('ε')==999) {
h=Get_NotNull_B(VT);
VT[h]='ε';
}*/
}
public void Get_Total() {//获得Total集
int i;
for(i=0;i<100;i++) {
if(VT[i]!=0&&VT[i]!='ε') {
h=Get_NotNull_B(Total);
Total[h]=VT[i];
}
}
for(i=0;i<100;i++) {
if(VN[i]!=0) {
h=Get_NotNull_B(Total);
Total[h]=VN[i];
}
}
}
public void Get_WenFa(String s) {
int i,h;
String temp=null;
StringBuilder d=new StringBuilder();
for(i=0;i<3;i++) { //获得前缀
d.append(s.charAt(i));
}
temp=d.toString();
while(i