资源描述:
《编译原理—语法分析器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjava.util.Stack;publicclassLL1Compuliter{publicstaticvoidmain(String[]args){String[]topString={"i","+","*","(",")","#"};String[]leftString={"E","G","T","S","F","@"};String[][]bottomString={{"TG","","","TG","",""},{"","+TG","","","@","@"},{"FS","","","FS","",""},{"","
2、@","*FS","","@","@"},{"i","","","(E)","",""}};LL1Compuliterll1=newLL1Compuliter();Stringtemp="i*i+i+(i)#";Stackst=newStack();st.push("#");//入栈Stringsstr="E";//开始符进栈st.push(sstr);while(temp.indexOf("#")>=0){Stringss=temp.substring(0,1);//获取第一个字符System.out.print(ss);intindex
3、Cell=ll1.isString(topString,ss);//存在终结符的获取列下标if(indexCell>=0){StringSSRow=(String)st.lastElement();//获取栈顶元素//判断栈顶元素是非终结符如果是非终结符进循环判断操作while(ll1.isString(leftString,SSRow)>=0){SSRow=(String)st.lastElement();//获取栈顶元素if(SSRow.equals("@")
4、
5、SSRow=="@")//判断栈顶元素是不是空如果是出栈{st.pop()
6、;continue;}else{intindexRow=ll1.isString(leftString,SSRow);//找到行下标第4页共4页if(indexRow>=0){System.out.print(""+"出栈元素:"+st.lastElement());st.pop();Stringstrr=bottomString[indexRow][indexCell];//找到表格元素if(strr==""
7、
8、strr.equals("")){System.out.print(""+"元素所在位置为空出错"+st.lastElement
9、()+"现在元素:"+temp);break;}else{ll1.comeStack(st,strr);//进栈}}else{if(ll1.isString(topString,(String)st.lastElement())>=0){st.pop();break;}else{System.out.print(""+(String)st.lastElement());break;}}}}if(SSRow.equals(ss)
10、
11、SSRow==ss)//匹配成功{System.out.print(""+"匹配成功"+SSRow);}else
12、{System.out.print(""+"匹配失败"+ss);break;}第4页共4页}else{System.out.print(""+"没有此字符串");break;}temp=temp.substring(1,temp.length());//截取字符串System.out.println();}if(st.isEmpty()){System.out.print(""+"执行成功");}else{Stringtop=(String)st.lastElement();if((top.equals("@#")&&temp.equals
13、("#"))
14、
15、(top.equals("#")&&temp.equals("#"))){System.out.println(""+"执行成功");}else{System.out.println(""+"匹配结果:栈顶元素为:"+top+"缓冲区元素为:"+temp);System.out.println(""+"执行失败!!!!!");}}}//判断是不是和标头和左列对应相等publicintisString(String[]str,Stringss){intindex=-1;for(inti=0;i16、if(ss==str[i]
17、
18、ss.equals(str[i])){index=i;break;}}returnindex;}第4页共4页publicvoidcomeStack(