📄 controller.java
字号:
package cminus;
/**
* 控制整个编译流程的主类
*
* @author Administrator
*
*/
import java.io.*;
public class Controller {
private static BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
private Parser scanner ;// 词法分析器类
private String grammarfilename;//文法文件名
private String programfilename;//程序文件名
private ParsingTree tree;//分析树类
private TreeFrame treeFrame;//图形化显示分析树的类
/***
* 构造函数
*/
public Controller() {
scanner = new Parser();
try {
System.out.print("Enter the grammar file path:\t");
this.grammarfilename = stdIn.readLine();
tree = new ParsingTree(this.grammarfilename);
treeFrame = new TreeFrame(tree);
System.out
.print("Enter the program file path which is wrote in c-minus:\t");
this.programfilename = stdIn.readLine();
} catch (IOException e) {
System.out.println("I/O error: " + e.getMessage());
}
}
public void doScannerWork() {
try {
scanner.setSourceFile(programfilename);
scanner.getToken();
scanner.showTokens();
} catch (Exception e) {
System.err.println("Scanning error: " + e.getMessage());
}
}
public void doFirstSetWork() {
try {
tree.addToList(); // 将文法文件装入数据结构
tree.validateGrammar();// 检查文法有无语法错误
tree.getFirstSet(); // 得到初期存储在多个链表中的first集合
tree.constructFirstSet(); // 将多个链表存储的first集合装入vector中。
tree.printFirstSet();
} catch (Exception e) {
System.err.println("Generating first set error: " + e.getMessage());
}
}
public void doFollowSetWork() {
try {
tree.getFollowSet();
tree.constructFollowSet();
tree.printFollowSet();
tree.isLL1();
} catch (Exception e) {
System.err
.println("Generating follow set error: " + e.getMessage());
}
}
public void doParsingTableWork() {
try {
tree.initParsingTable();
tree.constructParsingTable();
tree.printParsingTable();
} catch (Exception e) {
System.err.println("Generating parsing table error:"
+ e.getMessage());
}
}
public void doParsingTreeWork() {
try {
tree.setStackToken();
tree.setStackTokenType();
tree.parsingAction();
treeFrame.init();
treeFrame.displayParsingTree();
} catch (Exception e) {
System.err.println("Generating parsing tree error:"
+ e.getMessage());
}
}
public void displayChoice() {
System.out.println("*******************C-minus simple compiler V1.0******************");
System.out.println(" scan --execute the scann work.");
System.out.println(" first --display the first set of grammar.");
System.out.println(" follow --display the follow set of grammar.");
System.out.println(" table --display the parsing table of grammar.");
System.out.println(" tree --show the parsing tree for the program with c-minus.");
System.out.println(" exit --exit the program.");
System.out.println("*****************************************************************");
System.out.print("> ");
}
/**
* @param args
*/
public static void main(String[] args) {
Controller control = new Controller();
boolean seq[] = new boolean[5];
boolean isExit = false;
String command = "";
try {
control.displayChoice();
while (!isExit) {
command = stdIn.readLine();
if (command.equalsIgnoreCase("scan")) {
control.doScannerWork();
control.displayChoice();
seq[0] = true;
} else if (command.equalsIgnoreCase("first")) {
if (seq[0]) {
control.doFirstSetWork();
control.displayChoice();
seq[1] = true;
} else {
System.err.println("Must do the scan work first.");
control.displayChoice();
}
} else if (command.equalsIgnoreCase("follow")) {
if (seq[0] && seq[1]) {
control.doFollowSetWork();
control.displayChoice();
seq[2] = true;
} else {
System.err.println("Must do the first set work first.");
control.displayChoice();
}
} else if (command.equalsIgnoreCase("table")) {
if (seq[0] && seq[1] && seq[2]) {
control.doParsingTableWork();
control.displayChoice();
seq[3] = true;
} else {
System.err
.println("Must do the follow set work first.");
control.displayChoice();
}
} else if (command.equalsIgnoreCase("tree")) {
if (seq[0] && seq[1] && seq[2] && seq[3]) {
control.doParsingTreeWork();
control.displayChoice();
seq[4] = true;
} else {
System.err
.println("Must construct the parsing table first.");
control.displayChoice();
}
} else if (command.equalsIgnoreCase("exit")) {
System.out.println("System exit successfully!!");
isExit = true;
} else {
System.err.print("Invalid command ,Try again > ");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}// end of the main function
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -