java实现:把LL(1)分析表设计为一个可配置(可初始化)的参数,采用LL(1)预测分析法进行分析程序的设计,对输入的简单算术表达式,分析并按 “根(子树1子树2子树3……)”的先序遍历形成的语法树字符串的格式输出其对应的语法树(若存在)。 例如,如果输入文件是形如i+i*i的字符串,那么输出文件内容是: E(T(F(i)T')E'(+T(F(i)T'(*F(i)T'))E')) 给出实验代码
时间: 2023-06-27 14:08:26 浏览: 83
YUCEFENXI.rar_LL(1)_预测分析
以下是Java实现的代码:
```
import java.util.*;
public class LL1Parser {
private String[][] parsingTable;
private Map<String, Integer> nonTerminals;
private Map<String, Integer> terminals;
public void initParsingTable(String[][] table, String[] nonTerminalSymbols, String[] terminalSymbols) {
parsingTable = table;
nonTerminals = new HashMap<>();
terminals = new HashMap<>();
for (int i = 0; i < nonTerminalSymbols.length; i++) {
nonTerminals.put(nonTerminalSymbols[i], i);
}
for (int i = 0; i < terminalSymbols.length; i++) {
terminals.put(terminalSymbols[i], i);
}
}
public String parse(String input) {
Stack<String> stack = new Stack<>();
stack.push("$");
stack.push("E");
String output = "";
int inputIndex = 0;
while (!stack.isEmpty()) {
String top = stack.pop();
if (nonTerminals.containsKey(top)) {
int nonTerminalIndex = nonTerminals.get(top);
int terminalIndex = terminals.get(Character.toString(input.charAt(inputIndex)));
String production = parsingTable[nonTerminalIndex][terminalIndex];
if (!production.equals("-")) {
String[] symbols = production.split(" ");
for (int i = symbols.length - 1; i >= 0; i--) {
stack.push(symbols[i]);
}
}
} else if (terminals.containsKey(top)) {
String terminal = top;
char inputChar = input.charAt(inputIndex);
if (terminal.equals(Character.toString(inputChar))) {
output += inputChar;
inputIndex++;
} else {
return null;
}
} else if (top.equals("$")) {
if (inputIndex == input.length()) {
return output;
} else {
return null;
}
}
}
return null;
}
}
public class SyntaxTree {
private String symbol;
private List<SyntaxTree> children;
public SyntaxTree(String symbol) {
this.symbol = symbol;
children = new ArrayList<>();
}
public void addChild(SyntaxTree child) {
children.add(child);
}
public String toString() {
String result = symbol;
if (!children.isEmpty()) {
result += "(";
for (int i = 0; i < children.size(); i++) {
result += children.get(i).toString();
if (i != children.size() - 1) {
result += " ";
}
}
result += ")";
}
return result;
}
}
public class LL1ParserDemo {
public static void main(String[] args) {
String[] nonTerminalSymbols = {"E", "T", "F"};
String[] terminalSymbols = {"+", "*", "(", ")", "i"};
String[][] parsingTable = {
{"", "", "T E'", "", "", "T E'"},
{"", "", "F T'", "", "", "F T'"},
{"i", "", "", "", "", "( E )"}
};
LL1Parser parser = new LL1Parser();
parser.initParsingTable(parsingTable, nonTerminalSymbols, terminalSymbols);
String input = "i+i*i";
String parseResult = parser.parse(input);
if (parseResult != null) {
SyntaxTree syntaxTree = buildSyntaxTree(parseResult);
System.out.println(syntaxTree.toString());
} else {
System.out.println("Invalid input");
}
}
private static SyntaxTree buildSyntaxTree(String parseResult) {
Stack<SyntaxTree> stack = new Stack<>();
for (int i = parseResult.length() - 1; i >= 0; i--) {
char symbol = parseResult.charAt(i);
if (symbol == '+' || symbol == '*') {
SyntaxTree node = new SyntaxTree(Character.toString(symbol));
node.addChild(stack.pop());
node.addChild(stack.pop());
stack.push(node);
} else {
SyntaxTree node = new SyntaxTree(Character.toString(symbol));
stack.push(node);
}
}
return stack.pop();
}
}
```
其中,LL1Parser类实现了LL(1)语法分析器,initParsingTable方法用于初始化语法分析表,parse方法用于对输入的字符串进行分析,并返回语法树字符串。SyntaxTree类实现了语法树节点,toString方法用于输出语法树字符串。LL1ParserDemo类用于演示如何使用LL1Parser和SyntaxTree来生成语法树。
阅读全文