📄 ll_1_frame.java
字号:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class LL_1_Frame extends JFrame {
final JTextField textField;
final JTextArea textArea_2;
final JTextArea textArea_1;
final JTextArea textArea;
final JButton goButton = new JButton();
Stack<Character> mystack = new Stack<Character>();
String[][] analysistable = new String[5][6];
Nonterminal E = new Nonterminal('E', 0);
Nonterminal D = new Nonterminal('D', 1);// replace E'
Nonterminal T = new Nonterminal('T', 2);
Nonterminal R = new Nonterminal('R', 3);// replace T'
Nonterminal F = new Nonterminal('F', 4);
private char[] inputsymbol = new char[6];
String output;
String input;
public LL_1_Frame() {
super("LL(1)文法分析模拟");
analysistable[0][0] = "TD";
analysistable[0][3] = "TD";
analysistable[1][1] = "+TD";
analysistable[1][4] = "";
analysistable[1][5] = "";
analysistable[2][0] = "FR";
analysistable[2][3] = "FR";
analysistable[3][1] = "";
analysistable[3][2] = "*FR";
analysistable[3][4] = "";
analysistable[3][5] = "";// replace:reduce to ∈
analysistable[4][0] = "i";
analysistable[4][3] = "(E)";
output = "";
inputsymbol[0] = 'i';
inputsymbol[1] = '+';
inputsymbol[2] = '*';
inputsymbol[3] = '(';
inputsymbol[4] = ')';
inputsymbol[5] = '$';
mystack.push('$');
mystack.push(E.symbol);
// /////////////////////////////////////////////////////
this.setVisible(true);
getContentPane().setLayout(null);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(43, 99, 132, 193);
getContentPane().add(scrollPane);
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBorder(new TitledBorder(null, "栈", TitledBorder.CENTER,
TitledBorder.DEFAULT_POSITION, null, null));
scrollPane.setViewportView(textArea);
final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(192, 99, 100, 193);
getContentPane().add(scrollPane_1);
textArea_1 = new JTextArea();
textArea_1.setEditable(false);
textArea_1.setBorder(new TitledBorder(null, "输入", TitledBorder.CENTER,
TitledBorder.DEFAULT_POSITION, null, null));
scrollPane_1.setViewportView(textArea_1);
final JScrollPane scrollPane_2 = new JScrollPane();
scrollPane_2.setBounds(307, 99, 109, 193);
getContentPane().add(scrollPane_2);
textArea_2 = new JTextArea();
textArea_2.setEditable(false);
textArea_2.setBorder(new TitledBorder(null, "输出", TitledBorder.CENTER,
TitledBorder.DEFAULT_POSITION, null, null));
scrollPane_2.setViewportView(textArea_2);
final JLabel label = new JLabel();
label.setText("请输入表达式( e.g. i+i*i$):");
label.setBounds(43, 21, 152, 25);
getContentPane().add(label);
textField = new JTextField();
textField.setBounds(201, 22, 120, 22);
getContentPane().add(textField);
goButton.addActionListener(new Running());
goButton.setText("Run");
goButton.setBounds(357, 19, 82, 28);
getContentPane().add(goButton);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
dispose();
System.exit(0);
}
});
this.setLocation(300, 200);
this.setPreferredSize(new Dimension(500, 400));
this.pack();
this.setResizable(false);
}
class Running implements ActionListener {
public void actionPerformed(ActionEvent e) {
textField.setEditable(false);
new Exec().start();
goButton.setEnabled(false);
}
}
class Exec extends Thread {
public void run() {
input = textField.getText();
for (int i = 0; i < input.length(); i++) {
/*System.out.println(mystack.toString() + "\t"
+ input.substring(i) + "\t" + output);*/
textArea.append(mystack.toString()+"\n");
textArea_1.append(input.substring(i)+"\n");
textArea_2.append(output+"\n");
output = "";
if (mystack.peek() != input.charAt(i)) {
char temp = mystack.pop();
char temp2 = input.charAt(i);
String str = analysistable[LL_1.getseq(temp)][LL_1
.getinputseq(temp2)];
for (int j = str.length() - 1; j >= 0; j--) {
mystack.push(str.charAt(j));
}
output = temp + "->" + str;
i = i - 1;
} else {
mystack.pop();
}
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new LL_1_Frame();
}
public static int getseq(char ch) {//得到非终结符在分析表中的对应的行数
int temp = -1;
switch (ch) {
case 'E':
temp = 0;
break;
case 'D':
temp = 1;
break;
case 'T':
temp = 2;
break;
case 'R':
temp = 3;
break;
case 'F':
temp = 4;
break;
}
return temp;
}
public static int getinputseq(char ch) {//得到输入符号在分析表中的对应列数
int temp = -1;
switch (ch) {
case 'i':
temp = 0;
break;
case '+':
temp = 1;
break;
case '*':
temp = 2;
break;
case '(':
temp = 3;
break;
case ')':
temp = 4;
break;
case '$':
temp = 5;
break;
}
return temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -