1、課程設計內容java
1.一、前置條件:有效文法及正確的算符優先表數據結構
1.二、功能需求:根據文法及算符優先表,實現算符優先分析過程,輸入串採用教材133頁習題2中(2)中的字符串(#(((a,a),x,(a)),a)#)。app
1.三、開發環境: Java(jdk1.8)函數
Windows 10測試
2、數據結構設計ui
2.一、類this
2.1.一、文法對象類設計
public class Grammar {對象
private String left;blog
private String right;
public Grammar(String l, String r) {
this.left = l;
this.right = r;
}
public String getLeft() {
return left;
}
public String getRight() {
return right;
}
}
2.1.二、文法集合對象
public class GrammarList {
private List<Grammar> list = new ArrayList<>();
private List<String> leftList = new ArrayList<>();
private List<String> rightList = new ArrayList<>();
public void addGrammar(Grammar grammar) {
leftList.add(grammar.getLeft());
rightList.add(grammar.getRight());
list.add(new Grammar(grammar.getLeft(), grammar.getRight()));
}
public List<Grammar> getGrammarList() {
return list;
}
public List<String> getLeftList() {
return leftList;
}
public List<String> getRightList() {
return rightList;
}
}
2.二、全局變量
2.2.一、當前操做符
String signTemp
2.2.二、符號棧
Stack<String> signStack
2.2.三、字符串棧
Stack<String> opStack
2.2.四、分析步驟計數
int num
3、程序整體設計
public class Main {
public static void main(String[] args) {
Priority[][] matrix = MakeMatrix.getMatrix();
Map<String, Integer> keyMap = MakeMatrix.getKeyMap();
// 一、輸入字符串
String str = "#(((a,a),x,(a)),a)#"; // ^ 用x代替
String[] strings = str.split("");
// 二、建立文法
Grammar grammar0 = new Grammar("S", "a");
Grammar grammar1 = new Grammar("S", "x");
Grammar grammar2 = new Grammar("S", "(R)");
Grammar grammar3 = new Grammar("T", "S,T");
Grammar grammar4 = new Grammar("T", "S");
Grammar grammar5 = new Grammar("R", "T");
// 三、添加文法
GrammarList list = new GrammarList();
list.addGrammar(grammar0);
list.addGrammar(grammar1);
list.addGrammar(grammar2);
list.addGrammar(grammar3);
list.addGrammar(grammar4);
list.addGrammar(grammar5);
// 四、算符優先分析
Analyse runner = new Analyse();
runner.analyse(list, matrix, keyMap, strings);
}
}
4、詳細設計
4.一、向文法集合中添加文法
public void addGrammar(Grammar grammar)
4.二、獲取文法集合
public List<Grammar> getGrammarList()
4.三、獲取算符優先矩陣
public static Priority[][] getMatrix()
4.四、獲取終結符對應行列
public static Map<String, Integer> getKeyMap()
4.五、進行算符優先分析
GrammarList list 文法集合
Priority[][] matrix 算符優先矩陣
Map<String, Integer> keyMap 終結符行列號
String[] strings 輸入串
public void analyse(GrammarList list, Priority[][] matrix, Map<String, Integer> keyMap, String[] strings)
4.六、規約函數
String now 規約串
private boolean gy(String now, GrammarList list)
4.七、打印信息
Stack<String> opStack 字符串棧
String signTemp 符號棧頂元素
int index 分析步驟序號
String action 分析動做(移進/歸約)
private void outputProcess(Stack<String> opStack, String signTemp, String[] strings, int index, String action)
4.八、流程圖
5、測試數據設計
輸入串:#(((a,a),x,(a)),a)#
6、程序輸出結果分析
7、設計重點難點分析
8、參考文獻
9、附錄
9.一、Main.class
import analysestring.Analyse;
import enums.Priority;
import grammar.Grammar;
import grammar.GrammarList;
import makematrix.MakeMatrix;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Priority[][] matrix = MakeMatrix.getMatrix();
Map<String, Integer> keyMap = MakeMatrix.getKeyMap();
String str = "#(((a,a),x,(a)),a)#"; // ^ 用x代替
String[] strings = str.split("");
Grammar grammar0 = new Grammar("S", "a");
Grammar grammar1 = new Grammar("S", "x");
Grammar grammar2 = new Grammar("S", "(R)");
Grammar grammar3 = new Grammar("T", "S,T");
Grammar grammar4 = new Grammar("T", "S");
Grammar grammar5 = new Grammar("R", "T");
GrammarList list = new GrammarList();
list.addGrammar(grammar0);
list.addGrammar(grammar1);
list.addGrammar(grammar2);
list.addGrammar(grammar3);
list.addGrammar(grammar4);
list.addGrammar(grammar5);
Analyse runner = new Analyse();
runner.analyse(list, matrix, keyMap, strings);
}
}
9.二、Grammar.class
public class Grammar {
private String left;
private String right;
public Grammar(String l, String r) {
this.left = l;
this.right = r;
}
public String getLeft() {
return left;
}
public String getRight() {
return right;
}
}
9.三、GrammarList.class
import java.util.ArrayList;
import java.util.List;
public class GrammarList {
private List<Grammar> list = new ArrayList<>();
private List<String> leftList = new ArrayList<>();
private List<String> rightList = new ArrayList<>();
public void addGrammar(Grammar grammar) {
leftList.add(grammar.getLeft());
rightList.add(grammar.getRight());
list.add(new Grammar(grammar.getLeft(), grammar.getRight()));
}
public List<Grammar> getGrammarList() {
return list;
}
public List<String> getLeftList() {
return leftList;
}
public List<String> getRightList() {
return rightList;
}
}
9.四、Priority.class
public enum Priority {
NULL(0, "無關係"),
SMALL(1, "小於"),
EQUAL(2, "等於"),
BIG(3, "大於");
private int state;
private String stateInfo;
private Priority(int state, String stateInfo) {
this.state = state;
this.stateInfo = stateInfo;
}
public int getState() {
return this.state;
}
public String getStateInfo() {
return this.stateInfo;
}
}
9.五、MakeMatrix.class
import enums.Priority;
import java.util.HashMap;
import java.util.Map;
public class MakeMatrix {
private static Map<String, Integer> key = new HashMap<>();
static {
key.put("a", 0);
key.put("x", 1);
key.put("(", 2);
key.put(")", 3);
key.put(",", 4);
key.put("#", 5);
}
private static Priority[][] operationMatrix = {
{Priority.NULL, Priority.NULL, Priority.NULL, Priority.BIG, Priority.BIG, Priority.BIG},
{Priority.NULL, Priority.NULL, Priority.NULL, Priority.BIG, Priority.BIG, Priority.BIG},
{Priority.SMALL, Priority.SMALL, Priority.SMALL, Priority.EQUAL, Priority.SMALL, Priority.NULL},
{Priority.NULL, Priority.NULL, Priority.NULL, Priority.BIG, Priority.BIG, Priority.BIG},
{Priority.SMALL, Priority.SMALL, Priority.SMALL, Priority.BIG, Priority.SMALL, Priority.NULL},
{Priority.SMALL, Priority.SMALL, Priority.SMALL, Priority.NULL, Priority.NULL, Priority.NULL}};
public static Priority[][] getMatrix() {
return operationMatrix;
}
public static Map<String, Integer> getKeyMap() {
return key;
}
}
9.六、Analyse.class
import enums.Priority;
import grammar.Grammar;
import grammar.GrammarList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
public class Analyse {
private String signTemp = new String("");
private Stack<String> signStack = new Stack<>();
private Stack<String> opStack = new Stack<>();
private int num = 0;
public void analyse(GrammarList list, Priority[][] matrix, Map<String, Integer> keyMap, String[] strings) {
for (int i = 0; i < strings.length; i++) {
if (i == 0) {
opStack.push(strings[i]);
signStack.push(strings[i]);
signTemp = strings[i];
outputProcess(opStack, signTemp, strings, i + 1, "開始");
continue;
}
if (strings[i].equals(strings[0])) {
outputProcess(opStack, signTemp, strings, i, "結束");
System.out.println("程序結束");
break;
}
int v1 = keyMap.get(signTemp);
int v2 = keyMap.get(strings[i]);
if (matrix[v1][v2].equals(Priority.BIG)) { // 大於 則規約
String now = opStack.peek(); // 取出單字符大小的字符串
if (signStack.peek().equals(",")) {
if (now.equals("S")) {
// 用 T -> S 規約
outputProcess(opStack, signTemp, strings, i, "歸約");
if (!gy(now, list)) {
break;
}
i--;
continue;
}
if (now.equals("T")) {
// 用T -> S,T 規約
outputProcess(opStack, signTemp, strings, i, "歸約");
StringBuilder sb = new StringBuilder();
sb.append(opStack.pop());
sb.append(opStack.pop());
sb.append(opStack.peek());
if (!gy(new String(sb.reverse()), list)) {
break;
}
i--;
continue;
}
}
if (now.equals("T")) {
if (signTemp.equals("(")) {
outputProcess(opStack, signTemp, strings, i, "歸約");
if (!gy(now, list)) {
break;
}
i--;
continue;
}
}
outputProcess(opStack, signTemp, strings, i, "歸約");
if (!gy(now, list)) {
break;
}
i--;
} else if (matrix[v1][v2].equals(Priority.SMALL)) { // 小於 則移進
outputProcess(opStack, signTemp, strings, i, "移進");
opStack.push(strings[i]);
signStack.push(strings[i]);
signTemp = signStack.peek();
} else if (matrix[v1][v2].equals(Priority.EQUAL)) { // 去括號
StringBuilder sb = new StringBuilder();
while (!opStack.peek().equals("R")) {
outputProcess(opStack, signTemp, strings, i, "歸約");
gy(opStack.peek(), list);
}
outputProcess(opStack, signTemp, strings, i, "移進");
opStack.push(strings[i]);
signStack.push(strings[i]);
signTemp = signStack.peek();
outputProcess(opStack, signTemp, strings, i + 1, "歸約");
signStack.pop();
signTemp = signStack.peek();
sb.append(opStack.pop());
sb.append(opStack.pop());
sb.append(opStack.peek());
if (!gy(new String(sb.reverse()), list)) {
break;
}
continue;
} else { // Priority.NULL 出錯
System.out.println("規約出錯 Priority.NULL");
break;
}
}
}
private boolean gy(String now, GrammarList list) {
List<Grammar> grammars = list.getGrammarList();
if (list.getRightList().contains(now)) {
for (int j = 0; j < grammars.size(); j++) {
String right = grammars.get(j).getRight();
if (right.equals(now)) {
opStack.pop();
opStack.push(grammars.get(j).getLeft());
if (right.equals("T") || right.equals("S")) { // 當T->S 和R->T時 無關符號
break;
}
signStack.pop();
signTemp = signStack.peek();
break;
}
}
return true;
} else {
System.out.println("規約出錯: 找不到規約串");
return false;
}
}
private void outputProcess(Stack<String> opStack, String signTemp, String[] strings, int index, String action) {
if (num == 0) {
System.out.println("步驟 棧 當前符號 剩餘符號 動做");
}
StringBuilder sbStack = new StringBuilder();
StringBuilder sbStrs = new StringBuilder();
Stack<String> temp = new Stack<>();
while (!opStack.isEmpty()) {
temp.push(opStack.pop());
}
while (!temp.isEmpty()) {
sbStack.append(temp.peek());
opStack.push(temp.pop());
}
for (int i = index; i < strings.length; i++) {
sbStrs.append(strings[i]);
}
num++;
System.out.printf("%-5d" + " " + "%-20s" + " " + "%-10s" + " " + "%-20s" + " " + action + "\n", num, sbStack, signTemp, sbStrs, action);
}
}