欢迎来到天天文库
浏览记录
ID:30855431
大小:97.44 KB
页数:11页
时间:2019-01-04
《编译原理ll语法分析器java版完整源代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、publicclassAccept2{publicstaticStringBufferC^E11);publicstaticStringBufferStringBufferstac/?=newstack2=neviStringBuffer("i+i*#11);publicstaticvoidmain(Stringarts[]){//stack2.deleteCharAt(0);System・out・print(accept(stach,stack2y)y);}publicstaticbooleana
2、ccept(StringBufferstacksStringBufferstack2){〃判断识别与否booleanresult=true;outer:while(true){System.out.format("%-9s",stack+H");System.out.format("%9s“,stack2+”rT);charcl=stack.charAt(stack.length()-1);charc2=stack2.charAt(0);if(cl==,#,&&c2==,#1)returntrue
3、;switch(cl){case1E1:if(!E(c2)){result二false;breakouter;}break;case'P*://P代表E‘if(!P(c2)){resulfalse;breakouter;}break;case"T1:讦(”(c2)){resulbreakouter;}break;case*Q'://q代表r讦(!Q(c2)){result=false;breakouter;}break;case'F‘:讦(!F(c2)){result=false;breakoute
4、r;}break;default:{//终结符的时候if(c2==cl){stack.deleteCharAt(stack.length()-1);stack2.deleteCharAt(O);//System•out•printin();}else{returnfalse;if(result二false)breakouter;returnresult;publicstaticbooleanE(charc){//语法分析子程序Ebooleanresultrue;stack.deleteCharAt(
5、stac/?.length()-1);stac/?.append("PT");}elseif(c==1(1){stack.deleteCharAt(stac/?.length()-1);stac/?.append(uPTH);System.err.println(HE推导时错误!不能匹配!“);result二false;}returnresuIt;}publicstaticbooleanP(charc){//语法分析了程序Pbooleanresulif(c==*+'){stack.deleteCha
6、rAt(stac/?.length()-1);stac/?.append(HPT+H);}elseif(c==')1){stack.deleteCharAt(stack.length()-1);//stack.append("”);System.out.printin(,,P->/1');}elseif(c==){stack.deleteCharAt(stack.length()-1);//stack.append(',H);System•out•printin(,,P->/M);System.er
7、r.printlnC1'?推导时错误!不能匹配!“);result二false;}returnresuIt;}publicstaticbooleanT(charc){//语法分析了程序Tbooleanresulstack・deleteCharAt(stack.length()-l);stac/?.append("QF");}elseif(c==1(*){stack.deleteCharAt(stack.length()-1);stack・append(“QF”);}else{result=false
8、;System.err.printIn(NT推导时错误!不能匹配!”);}returnresult;publicstaticbooleanQ(charc){//语法分析子程序Qbooleanresulif(c=='+'){stack.deleteCharAt(stac/?.length()-1);//stack.append(M”);System・out.printin(NQ->/");}elseif(c==,*1){stack.deleteCharAt(stac^.
此文档下载收益归作者所有