您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > LL1语法分析-FIRST集-FOLLOW集-分析表JAVA实现
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.table.DefaultTableModel;importjava.sql.*;importjava.util.Vector;publicclassLL1extendsJFrameimplementsActionListener{/****/privatestaticfinallongserialVersionUID=1L;JTextFieldtf1;JTextFieldtf2;JLabell;JButtonb0;JPanelp1,p2,p3;JTextAreat1,t2,t3;JButtonb1,b2,b3;JLabell0,l1,l2,l3,l4;JTabletable;Statementsta;Connectionconn;ResultSetrs;DefaultTableModeldtm;StringVn[]=null;VectorStringP=null;intfirstComplete[]=null;//存储已判断过first的数据charfirst[][]=null;//存储最后first结果intfollowComplete[]=null;//存储已判断过follow的数据charfollow[][]=null;//存储最后follow结果charselect[][]=null;//存储最后select结果intLL=0;//标记是否为LL(1)Stringvt_tou[]=null;//储存VtObjectshuju[][]=null;//存储表达式数据charyn_null[]=null;//存储能否推出空LL1(){setLocation(100,0);setSize(700,780);tf1=newJTextField(13);tf2=newJTextField(13);l=newJLabel();l0=newJLabel(输入字符串:);l1=newJLabel(输入的文法为:);l2=newJLabel();l3=newJLabel(分析的结果:);l4=newJLabel(预测分析表:);//p1=newJPanel();p2=newJPanel();p3=newJPanel();t1=newJTextArea(24,20);t2=newJTextArea(1,30);t3=newJTextArea(24,40);b0=newJButton(确定(S为开始));b1=newJButton(判断文法);b2=newJButton(输入);b3=newJButton(清空);table=newJTable();JScrollPanejp1=newJScrollPane(t1);JScrollPanejp2=newJScrollPane(t2);JScrollPanejp3=newJScrollPane(t3);p2.add(tf1);p2.add(l);p2.add(tf2);p2.add(b0);p2.add(b1);p2.add(l0);p2.add(l2);p2.add(jp2);p2.add(b2);p2.add(b3);p2.add(l1);p2.add(l3);p2.add(jp1);p2.add(jp3);p3.add(l4);p3.add(newJScrollPane(table));add(p2,Center);add(p3,South);b0.addActionListener(this);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);table.setPreferredScrollableViewportSize(newDimension(660,200));setVisible(true);}////////////////////界面设计publicvoidactionPerformed(ActionEvente){if(e.getSource()==b0){Stringa=tf1.getText();Stringb=tf2.getText();t1.append(a+'→'+b+'\n');}if(e.getSource()==b1){t3.setText();intVnnum=0,k;Vn=newString[100];P=newVectorString();Strings[]=t1.getText().split(\n);for(inti=0;is.length;i++){if(s.length2){t3.setText(文法输入有误,请重新输入);//判断长度是否符合return;}if(s[i].charAt(0)='Z'&&s[i].charAt(0)='A'&&s[i].charAt(1)=='→'){for(k=0;kVnnum;k++){if(Vn[k].equals(s[i].substring(0,1))){break;}}if(Vnnum==0||k=Vnnum){Vn[Vnnum]=s[i].substring(0,1);//存入Vn数据Vnnum++;}P.add(s[i]);}else{t3.setText(文法输入有误,请重新输入);return;}}yn_null=newchar[100];first=newchar[Vnnum][100];intflag=0;StringfirstVn[]=null;firstComplete=newint[Vnnum];for(inti=0;Vn[i]!=null;i++)//依次求FIRST**{flag=0;firstVn=newString[20];if((flag=add_First(first[i],Vn[i],firstVn,flag))==-1)return;firstComplete[i]=1;}t3.append(first集:+\n);//显示FIRST**for(inti=0;Vn[i]!=null;i++){t3.append(first(+Vn[i]+)={);for(intj=0;first[i][j]!='\0';j++){t3.append(first[i][j]+,);}t3.append(}+\n);}follow=newchar[Vnnum][100];StringfollowVn[]=null;followComplete=newint[Vnnum];for(inti=0;Vn[i]!=null;i++)//求FOLLOW**{flag=0;followVn=newString[20];if(i==0){}elseif((flag=tianjiaFollow(follow[i],Vn[i],followVn,flag))==-1)return;followComplete[i]=1;}t3.append(follow集:+\n);//显示FOLLOW**for(inti=0;Vn[i]!=null;i++){t3.append(follow(+Vn[i]+)={);for(intj=0;follow[i][j]!='\0';j++){t3.append(follow[i][j]+,);}t3.append(}+\n);}select=newchar[P.size()][100];for(inti=0;iP.size();i++)//求SELECT**{flag=0;tianjiaSelect(select[i],(String)P.elementAt(i),flag);}t3.append(select集:+\n);//显示SELECT**for(inti=0;iP.size();i++){t3.append(select(+(String)P.elementAt(i)+)={);for(intj=0;select[i][j]!='\0';j++){t3.append(select[i][j]+,);}t3.append(}+\n);}for(inti=0;Vn[i]!=null;i++)//判断select交集是否为空{intbiaozhi=0;charsave[]=newchar[100];for(intj=0;jP.size();j++){Stringt=(String)P.elementAt(j);if(t.substring(0,1).equals(Vn[i])){for(k=0;select[j][k]!='\0';k++){if(puanduanChar(save,select[j][k])){save[biaozhi]=select[j][k];biaozhi++;}else//当有交集时,不为LL(1)文法{t3.append(不是LL(1)文法!!+\n);return;}}}}}charVt[]=newchar[100];intbiaozhi=0;for(inti=0;iP.size();i++){Stringt=(String)P.elementAt(i);for(intj=2;jt.length();j++)//提取表达式右侧的终结符存入Vt{if(t.charAt(j)'Z'||t.charAt(j)'A'){if(puanduanChar(Vt,t.charAt(j))){Vt[biaozhi]=t.charAt(j);biaozhi++;}}}}if(puanduanChar(Vt,'#'))//若可推出空集,则将#加入Vt。{Vt[biaozhi]='#';biaozhi++;}vt_tou=newString[biaozhi+1];//根据select和表达式生成预测分析表shuju=newString[Vnnum][biaozhi+1];Stringf=;vt_tou[0]=f;for(inti=0;ibiaozhi;i++){vt_tou[i+1]=String.valueOf(Vt[i]);}for(inti=0;iVnnum;i++){shuju[i][0]=Vn[i];}for(inti=0;iP.size();i++){Stringt=(String)P.elementAt(i);intj;for(j=0;jVn.length;j++){if(Vn[j].equals(t.substring(0,1))){break;}}for(k=0;select[i][k]!='\0';k++){inty=puanduanXulie(Vt,select[i][k]);shuju[j][y+1]=t.substring(1);}}dtm=newDefaultTableModel(shuju,vt_tou);//显示预测分析表table.setModel(dtm);LL=1;}if(e.getSource()==b3)//清空列表{tf1.setText();tf2.setText();t1.setText();t2.setText();t3.setText();Vn=null;P=null;firstComplete=null;first=null;followComplete=null;follow=null;select=null;dtm=newDefaultTableModel();table.setModel(dtm);}if(e.getSource()==b2)//输入字符串并预测分析{t3.setText();if(LL==1){Strings=t2.getText();for(inti=0;is
本文标题:LL1语法分析-FIRST集-FOLLOW集-分析表JAVA实现
链接地址:https://www.777doc.com/doc-5353638 .html