文法:
1) E→E+T
2) E→T
3) T→T*F
4) T→F
5) F→(E)
6) F→id
核心代码:(其中的一个方法)
public String Method(int i,Node node)
{
String message="";
Expression currentExp;
if (ACTION[node.state][i] > 0) {
if(ACTION[node.state][i]==10000)
{
System.out.println("分析成功");
return "分析成功";
}
node = new Node(ACTION[node.state][i], letter[i]);
message = "移进:"+letter[i]+"\n";
System.out.println("移进:"+letter[i]+"\n");
parseStack.push(node);
} else if (ACTION[node.state][i] < 0) {
currentExp = exps.get(-ACTION[node.state][i]);
for (int j = 0; j < currentExp.length; j++) {
parseStack.pop();
}
node = parseStack.pop();
parseStack.push(node);
message = "规约:" + currentExp.left + "->"
+ currentExp.right + "\n";
System.out.println("规约:" + currentExp.left + "->"
+ currentExp.right + "\n");
String left = currentExp.left;
if (left.equals("E")) {
node = new Node(GOTO[node.state][0], left);
parseStack.push(node);
} else if (left.equals("T")) {
node = new Node(GOTO[node.state][1], left);
parseStack.push(node);
} else if (left.equals("F")) {
node = new Node(GOTO[node.state][2], left);
parseStack.push(node);
}
} else {
message = "分析失败";
}
return message;
}
源代码下载链接:
http://download.csdn.net/detail/snxhkg/8188841