编译原理 LL(1)语法分析器java版 完整源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
public class Accept2 {
public static StringBuffer stack=new StringBuffer("#E");
public static StringBuffer stack2=new StringBuffer("i+i*#");
public static void main(String arts[]){
//stack2.deleteCharAt(0);
System.out.print(accept(stack,stack2));
}
public static boolean accept(StringBuffer stack,StringBuffer stack2){//判断识别与否
boolean result=true;
outer:while (true) {
System.out.format("%-9s",stack+"");
System.out.format("%9s",stack2+"\n");
char c1 = stack.charAt(stack.length() - 1);
char c2 = stack2.charAt(0);
if(c1=='#'&&c2=='#')
return true;
switch (c1) {
case'E':
if(!E(c2)) {result=false;break outer;}
break;
case'P': //P代表E’if(!P(c2)) {result=false;break outer;}
break;
case'T':
if(!T(c2)) {result=false;break outer;}
break;
case'Q': //Q代表T’if(!Q(c2)) {result=false;break outer;}
break;
case'F':
if(!F(c2)) {result=false;break outer;}
break;
default: {//终结符的时候
if(c2==c1){
stack.deleteCharAt(stack.length()-1);
stack2.deleteCharAt(0);
//System.out.println();
}
else{
return false;
}
}
}
if(result=false)
break outer;
}
return result;
}
public static boolean E(char c) {//语法分析子程序 E
boolean result=true;
if(c=='i') {
stack.deleteCharAt(stack.length()-1);
stack.append("PT");
}
else if(c=='('){
stack.deleteCharAt(stack.length()-1);
stack.append("PT");
else{
System.err.println("E 推导时错误!不能匹配!");
result=false;
}
return result;
}
public static boolean P(char c){//语法分析子程序 P
boolean result=true;
if(c=='+') {
stack.deleteCharAt(stack.length()-1);
stack.append("PT+");
}
else if(c==')') {
stack.deleteCharAt(stack.length()-1);
//stack.append("");
System.out.println("P->/");
}
else if(c=='#') {
stack.deleteCharAt(stack.length()-1);
//stack.append("");
System.out.println("P->/");
else{
System.err.println("P 推导时错误!不能匹配!");
result=false;
}
return result;
}
public static boolean T(char c) {//语法分析子程序 T
boolean result=true;
if(c=='i') {
stack.deleteCharAt(stack.length()-1);
stack.append("QF");
}
else if(c=='(') {
stack.deleteCharAt(stack.length()-1);
stack.append("QF");
}
else{
result=false;
System.err.println("T 推导时错误!不能匹配!");
}
return result;