您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 编译原理 LL(1)语法分析器java版 完整源代码
publicclassAccept2{publicstaticStringBufferstack=newStringBuffer(#E);publicstaticStringBufferstack2=newStringBuffer(i+i*#);publicstaticvoidmain(Stringarts[]){//stack2.deleteCharAt(0);System.out.print(accept(stack,stack2));}publicstaticbooleanaccept(StringBufferstack,StringBufferstack2){//判断识别与否booleanresult=true;outer:while(true){System.out.format(%-9s,stack+);System.out.format(%9s,stack2+\n);charc1=stack.charAt(stack.length()-1);charc2=stack2.charAt(0);if(c1=='#'&&c2=='#')returntrue;switch(c1){case'E':if(!E(c2)){result=false;breakouter;}break;case'P'://P代表E’if(!P(c2)){result=false;breakouter;}break;case'T':if(!T(c2)){result=false;breakouter;}break;case'Q'://Q代表T’if(!Q(c2)){result=false;breakouter;}break;case'F':if(!F(c2)){result=false;breakouter;}break;default:{//终结符的时候if(c2==c1){stack.deleteCharAt(stack.length()-1);stack2.deleteCharAt(0);//System.out.println();}else{returnfalse;}}}if(result=false)breakouter;}returnresult;}publicstaticbooleanE(charc){//语法分析子程序Ebooleanresult=true;if(c=='i'){stack.deleteCharAt(stack.length()-1);stack.append(PT);}elseif(c=='('){stack.deleteCharAt(stack.length()-1);stack.append(PT);}else{System.err.println(E推导时错误!不能匹配!);result=false;}returnresult;}publicstaticbooleanP(charc){//语法分析子程序Pbooleanresult=true;if(c=='+'){stack.deleteCharAt(stack.length()-1);stack.append(PT+);}elseif(c==')'){stack.deleteCharAt(stack.length()-1);//stack.append();System.out.println(P-/);}elseif(c=='#'){stack.deleteCharAt(stack.length()-1);//stack.append();System.out.println(P-/);}else{System.err.println(P推导时错误!不能匹配!);result=false;}returnresult;}publicstaticbooleanT(charc){//语法分析子程序Tbooleanresult=true;if(c=='i'){stack.deleteCharAt(stack.length()-1);stack.append(QF);}elseif(c=='('){stack.deleteCharAt(stack.length()-1);stack.append(QF);}else{result=false;System.err.println(T推导时错误!不能匹配!);}returnresult;}publicstaticbooleanQ(charc){//语法分析子程序Qbooleanresult=true;if(c=='+'){stack.deleteCharAt(stack.length()-1);//stack.append();System.out.println(Q-/);}elseif(c=='*'){stack.deleteCharAt(stack.length()-1);stack.append(QF*);}elseif(c==')'){stack.deleteCharAt(stack.length()-1);//stack.append();System.out.println(Q-/);}elseif(c=='#'){stack.deleteCharAt(stack.length()-1);//stack.append();System.out.println(Q-/);}else{result=false;System.err.println(Q推导时错误!不能匹配!);}returnresult;}publicstaticbooleanF(charc){//语法分析子程序Fbooleanresult=true;if(c=='i'){stack.deleteCharAt(stack.length()-1);stack.append(i);}elseif(c=='('){stack.deleteCharAt(stack.length()-1);stack.append()E();}else{result=false;System.err.println(F推导时错误!不能匹配!);}returnresult;}/*publicstaticStringBufferchangeOrder(Strings){//左右交换顺序StringBuffersb=newStringBuffer();for(inti=0;is.length();i++){sb.append(s.charAt(s.length()-1-i));}returnsb;}*/}#Ei+i*i+i##PTi+i*i+i##PQFi+i*i+i##PQii+i*i+i##PQ+i*i+i#Q-/#P+i*i+i##PT++i*i+i##PTi*i+i##PQFi*i+i##PQii*i+i##PQ*i+i##PQF**i+i##PQFi+i##PQii+i##PQ+i#Q-/#P+i##PT++i##PTi##PQFi##PQii##PQ#Q-/#P#P-/##True#Ei+i*##PTi+i*##PQFi+i*##PQii+i*##PQ+i*#Q-/#P+i*##PT++i*##PTi*##PQFi*##PQii*##PQ*##PQF**##PQF#falseF推导时错误!不能匹配!
本文标题:编译原理 LL(1)语法分析器java版 完整源代码
链接地址:https://www.777doc.com/doc-3968562 .html