package complier;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableModel;
public class SyntaxDirectedTranslation implements ActionListener {
JFrame f=null;
JButton JBT[]=new JButton[9];
JTextPane Testin1=null;
JScrollPane ScrollText1=null;
JScrollPane FirstScrollout=null;
JScrollPane FollowScrollout=null;
JScrollPane GenpreScrollout=null;
JTextPane ProPosition=null;
JScrollPane ResultScrollout=null;
JTextPane FourElemFormu=null;
String Res4ele="";
int st1=1;
JTable Projects=null;
JTable PrdecitLR=null;
JTable Result=null;
DefaultTableModel ProjectsModel =null;
DefaultTableModel PrdecitLRModel=null;
DefaultTableModel ResultModel=null;
HashMap<String,Integer> isempt=null;
HashMap<String,HashSet<String>> First=null;
HashMap<String,HashSet<String>> Follow=null;
HashMap<String,HashMap<String,Integer>> ProjectMapInteger =null;
HashMap<Integer,HashMap<String,String>> IntegerMapProject =null;
ArrayList<HashMap<String,HashSet<String>>> ISet=null;
HashMap<String,HashSet<String>> Grammer=null;
HashSet<Character> charSet=null;
HashMap<Integer,HashMap<String,Integer>> con=null;
HashMap<Integer,HashMap<String,HashSet<Integer>>> Action=null;
HashMap<Integer,HashMap<String,HashSet<Integer>>> Goto=null;
String start = null;
boolean isclash=false;
Stack<Integer> ST=null;
Stack<String> CX=null;
int step=0;
Stack<String> RT=null;
Stack<Integer> Cau=null;
Stack<String> Sau=null;
void processIJ(String i,String j) {
//String Res4ele="";
// int st1=1;
if (i.trim().equals("E")&&j.trim().equals("E+T")) {
int a=Cau.pop();
int b=Cau.pop();
//Res4ele
String sa=Sau.pop();
String sb=Sau.pop();
Res4ele=Res4ele+"("+"+,"+sb+","+sa+","+"T"+st1+")"+'\n';
Sau.push("T"+st1);
Cau.push(a+b);
st1++;
}
if (i.trim().equals("E")&&j.trim().equals("E-T")) {
int a=Cau.pop();
int b=Cau.pop();
String sa=Sau.pop();
String sb=Sau.pop();
Res4ele=Res4ele+"("+"-,"+sb+","+sa+","+"T"+st1+")"+'\n';
Sau.push("T"+st1);
Cau.push(b-a);
st1++;
}
if (i.trim().equals("T")&&j.trim().equals("T*F")) {
int a=Cau.pop();
int b=Cau.pop();
String sa=Sau.pop();
String sb=Sau.pop();
Res4ele=Res4ele+"("+"*,"+sb+","+sa+","+"T"+st1+")"+'\n';
Sau.push("T"+st1);
Cau.push(a*b);
st1++;
}
if (i.trim().equals("T")&&j.trim().equals("T/F")) {
int a=Cau.pop();
int b=Cau.pop();
String sa=Sau.pop();
String sb=Sau.pop();
Res4ele=Res4ele+"("+"/,"+sb+","+sa+","+"T"+st1+")"+'\n';
Sau.push("T"+st1);
Cau.push(b/a);
st1++;
}
}
int isblank (){
//-1 未定 1 可以推导出空 0 不可以推导出空
HashMap<String,HashSet<String>> Mp,MT;
Mp=new HashMap<String,HashSet<String>>();
for (String i:Grammer.keySet()) {
Mp.put(i, new HashSet<String>(Grammer.get(i)));
}
///文法符号 A B C
HashSet<String> p=new HashSet<String>();
for (String i:Grammer.keySet()) {
if (Mp.get(i)==null) continue;
HashSet<String> j=new HashSet<String>(Mp.get(i));
for (String s:j) {
if (s.equals("#")) {
Mp.remove(i);
isempt.replace(i, 1);
break;
}
for (int k=0;k<s.length();k++) {
char c=s.charAt(k);
if (!isLetter(c)) {
HashSet<String> pp=Mp.get(i);
if (pp==null) break;
pp.remove(s);
if (pp.size()<=0) {
isempt.replace(i, 0);
Mp.remove(i);
}
break;
}
}
}
}
boolean t=true;
MT=new HashMap<String,HashSet<String>>(Mp);
while(t) {
t=false;
for (String ps:Mp.keySet()) {
p=new HashSet<String>(Mp.get(ps));
if (MT.get(ps)==null ) continue;
if (MT.get(ps).size()<=0) continue;
for (String pps:p) {
if (MT.get(ps)==null ) continue;
if (MT.get(ps).size()<=0) continue;
if (DeStrEmpty(pps)==1) {
MT.remove(ps);
isempt.replace(ps, 1);
t=true;
}else
if (DeStrEmpty(pps)==0) {
MT.get(ps).remove(pps);
if (MT.get(ps).size()<=0) {
MT.remove(ps);
isempt.replace(ps, 0);
t=true;
}
}
}
}
Mp=new HashMap<String,HashSet<String>>(MT);
}
GET_FIRST();
GET_FOLLOW();
return 0;
}
int DeStrEmpty(String s) {
//产生式右端可以推出空 1 推出非空 0 不确定 -1
int t1=0;
for (int i=0;i<s.length();i++) {
if (isempt.get(s.charAt(i)+"")==1) {
t1++;
}
if (isempt.get(s.charAt(i)+"")==0) {
return 0;
}
}
if (t1==s.length())
return 1;
return -1;
}
void GET_FIRST() {
First=new HashMap<String,HashSet<String>>();
//判断是否可以退出空
for (String i:isempt.keySet()) {
HashSet<String> p=new HashSet<String>();
First.put(i, p);
if (isempt.get(i)==1) {
p.add("#");
}
}
boolean t=true;
while(t) {
t=false;
for (String i:isempt.keySet()) {
HashSet<String> p=First.get(i);
HashSet<String> ss=Grammer.get(i);
for (String j:ss) {
HashSet<String> ps=find_first(i,j);
int len=p.size();
p.addAll(ps);
if (len!=p.size()) {
t=true;
}
}
}
}
}
HashSet<String> find_first(String i,String j){
HashSet<String> p=new HashSet<String>();
for (int k=0;k<j.length();k++) {
if (!isLetter(j.charAt(k))){
p.add(j.charAt(k)+"");
p.remove("#");
return p;
}
if (First.get(j.charAt(k)+"").size()<=0) {
p.remove("#");
return p;
}
p.addAll(First.get(j.charAt(k)+""));
if (!First.get(j.charAt(k)+"").contains("#")) {
p.remove("#");
return p;
}
}
p.remove("#");
return p;
}
void GET_FOLLOW() {
Follow=new HashMap<String,HashSet<String>>();
for (String i:isempt.keySet()) {
HashSet<String> p=new HashSet<String>();
Follow.put(i, p);
}
Follow.get(start).add("#");
boolean t=true;
while(t) {
t=false;
for (String i:Grammer.keySet()) {
for (String j:Grammer.get(i)) {
for (int k=0;k<j.length()-1;k++) {
char c=j.charAt(k);
if(isLetter(c)) {
char cc=j.charAt(k+1);
if (!isLetter(cc)) {
HashSet<String> p=Follow.get(c+"");
int len=p.size();
if(cc!='#')
p.add(cc+"");
if (len!=p.size()) {
t=true;
}
}else {
HashSet<String> p=Follow.get(c+"");
boolean t1=false,t2=false;
int tp=k+2;
int len=p.size();
HashSet<String> pp=new HashSet<String> (First.get(cc+""));
if (!pp.contains("#")) {
p.addAll(pp);
}
else
while(pp.contains("#")) {
pp.remove("#");
p.addAll(pp);
if (tp==j.length()) {
t1=true;
break;
}
cc=j.charAt(tp);
if (!isLetter(cc)) {
t2=true;
p.add(cc+"");
break;
}
pp=new HashSet<String> (First.get(cc+""));
tp++;
}
if (t1) {
HashSet<String> sp=new HashSet<String> (Follow.get(i));
p.addAll(sp);
}
if (t2&&tp+1==j.length()&&j.charAt(tp)=='#') {
HashSet<String> sp=new HashSet<Stri