1、正规文法转换成正规式课 程 名 称: 正规文法转换成正规式 年级/专业/班: 11级计算机类(二)班 姓 名: 徐勇兵 学 号: E01114278 实验名称:正规文法转换成正规式实验目的:1.了解并熟悉词法分析中单词的描述工具正规文法和正规式表示单词的方式及其之间的差异性和等价性。2.利用计算机编程实现正规文法转换成等价的正规式。实验要求:1.正规文法的输入应简便。2.输出的正规式以利用3条转换规则得出为标准。输入:一组产生式所构成的正规文法。输出:对应的等价的正规式。实验原理:1.多数程序设计语言的单词的语法都能用正规文法或3型文法来描述。2.3型文法的特征是P中的每一条规则都有下述形式:
2、A-aB或A-a。正规文法所描述的是VT上的正规集。3.正规式也称正则表达式,也是表示正规集的工具。也是我们用以描述单词符号的有效工具。4.正规文法和正规式的等价性:一个正规语言可以由正规文法定义,也可以由正规式定义,对任意一个正规文法,存在一个定义同一个语言的正规式;反之,对每个正规式,存在一个生成同一语言的正规文法,有些语言很容易用文法描述,有些语言更容易用正规式定义。5.将正规文法转换成正规式的转换规则有三:(1)A-xB,B-y对应A=xy(2)A-xA,A-y对应A=x*y(3)A-x,A-y对应A=x|y实验算法: 实验算法定义一个函数实现转换正规文法为正规式。函数根据三个转换规则
3、,首先合并形如B-aA,B-bA的产生式为B-aA|bA的形式,其中又包括B=A的形式。然后根据转换规则合并形如S-a,S-b的产生式为S-a|b的形式。再根据转换规则2 的A-xA,A-y对应A=x*y和规则3的A-x,A-y对应A=x|y将文法产生式变换为等价的正规式。 在规则以外还需要另外一个处理,这个处理可以从书本上的例子中看到,即将公因子提取出来,如A=aA|dA变换为A=(a|d)A。 算法默认开始符号为S且放在第一个产生式,这样就能根据第一条产生式最终得到等价的一个开始符号定义的正规式。实验结果: import java.util.Vector;import javax.swin
4、g.JOptionPane;class Tools public Vector addElements(Vector vs,Vectortemp) for(int i=0;itemp.size();i+) /if(!vs.contains(temp.get(i) vs.add(temp.get(i); /for return vs; /public Vector addElements(Vector vs,Vectortemp) public Vector protection(Vector vs) Vector newvector=new Vector(); for(int i=0;ivs.
5、size();i+) newvector.add(vs.get(i); return newvector; public VectorVector doubleprotection(VectorVector vs) VectorVector newvector=new VectorVector(); for(int i=0;ivs.size();i+) Vector produce=(Vector)vs.get(i); Vector temp=new Vector(); for(int j=0;jproduce.size();j+) temp.add(String)produce.get(j)
6、; /for j newvector.add(temp); /for i return newvector; /class toolsclass Elements Vector end=new Vector();/表示终结符 Vector noend=new Vector();/表示非终结符 VectorVector produce=new VectorVector();/产生式 public void setend()/终结符元素添加 while(true) String s=JOptionPane.showInputDialog(null,请输入终结符); if(s=null) retur
7、n; /if end.add(s); /while /public void addend()/元素添加 public void setnoend()/非终结符元素添加 while(true) String s=JOptionPane.showInputDialog(null,非请输入终结符); if(s=null) return; /if noend.add(s); /while /public void addnoend()/ public void setproduce() while(true) String s=JOptionPane.showInputDialog(null,请输入
8、产生式,-隔开); if(s=null) return; Vector temp=new Vector(); temp.add(s.split(-)0); temp.add(s.split(-)1); produce.add(temp); /while /public void addproduce() public Vector getend() return end; public Vector getnoend() return noend; public VectorVector getproduce() return this.produce; public void run() /
9、*TEST*/ end.add(a); end.add(d); noend.add(S); noend.add(A); Vector temp=new Vector(); temp.add(S); temp.add(aA); produce.add(temp); /*/ /*/ Vector temp4=new Vector(); temp4.add(S); temp4.add(a); produce.add(temp4); / System.out.println(produce.size()=+produce.size(); /*TEST*/ /*/ Vector temp7=new Ve
10、ctor(); temp7.add(A); temp7.add(aA); produce.add(temp7); / System.out.println(produce.size()=+produce.size(); /*TEST*/ Vector temp1=new Vector(); temp1.add(A); temp1.add(dA); produce.add(temp1); /*/ Vector temp2=new Vector(); temp2.add(A); temp2.add(a); produce.add(temp2); /*/ Vector temp3=new Vecto
11、r(); temp3.add(A); temp3.add(d); produce.add(temp3); / System.out.println(produce.size()=+produce.size(); /*TEST*/ /* noend.add(B); Vector temp6=new Vector(); temp6.add(A); temp6.add(aB); produce.add(temp6); Vector temp5=new Vector(); temp5.add(B); temp5.add(d); produce.add(temp5);*/ /this.setend();
12、 /this.setnoend(); /this.setproduce(); public boolean Iscontainend(String s)/判断某个符号是否是终结符 可以是一个字符也可是字符串 /*System.out.print(输出终结符); for(int i=0;iend.size();i+) System.out.print(end.get(i); System.out.println(); System.out.println(传送过来的需要判断的是+s);*/ int length=s.length(); for(int i=0;ilength;i+) String
13、 a=+s.charAt(i); if(end.contains(a) continue; else return false; /for return true; /public boolean isRGPcontain(String s) public boolean IsNoENd(String s)/判断某个符号是否是非终结符 String ss=+s.charAt(0); if(! Iscontainend(ss)/如果不含有终结符,则为非终结符 return true; return false; / public boolean public void show() System
14、.out.print(终结符输出如下:); for(int i=0;iend.size();i+) System.out.print(String)end.get(i)+, ); System.out.println( ); System.out.print(非终结符输出如下:); for(int i=0;inoend.size();i+) System.out.print(String)noend.get(i)+, ); System.out.println( ); System.out.print(产生式输出如下:); for(int i=0;iproduce.size();i+) Sys
15、tem.out.println( ); Vector temp=(Vector)produce.get(i); System.out.print(String)temp.get(0)+-+(String)temp.get(1); System.out.println( ); /class Elements class Compression Tools tools=new Tools(); Elements elements; Vector end=null; Vector noend=null; VectorVector produce=new VectorVector(); Vector
16、newend; Vector newnoend; VectorVector newproduce; public void showdelete(VectorVector harm,VectorVector unreach,VectorVector unend) if(harm.isEmpty() System.out.println(没有有害规则被删除); else System.out.print(被删除的有害产生式输出如下:); for(int i=0;iharm.size();i+) System.out.println( ); Vector temp=(Vector)harm.get
17、(i); System.out.print(String)temp.get(0)+-+(String)temp.get(1); System.out.println(); System.out.println(*); if(unreach.isEmpty() System.out.println(没有不可到达规则被删除); else System.out.print(被删除的不可到达产生式输出如下:); for(int i=0;iunreach.size();i+) System.out.println( ); Vector temp=(Vector)unreach.get(i); Syste
18、m.out.print(String)temp.get(0)+-+(String)temp.get(1); System.out.println(); System.out.println(*); if(unend.isEmpty() System.out.println(没有不可终止规则被删除); else System.out.print(被删除的不可终止产生式输出如下:); for(int i=0;iunend.size();i+) System.out.println( ); Vector temp=(Vector)unend.get(i); System.out.print(Stri
19、ng)temp.get(0)+-+(String)temp.get(1); System.out.println(); System.out.println(*); public VectorVector deleteharm(VectorVector newproduce)/删除有害规则 VectorVector delete=new VectorVector(); for(int i=0;inewproduce.size();i+) Vector temp=newproduce.get(i); String left=temp.get(0); String right=temp.get(1
20、); if(left.equals(right)/形如A-A newproduce.remove(temp); delete.add(temp); return delete; /public VectorVector deleteharm(VectorVector newproduce) public VectorVector deleteunreachable(VectorVector newproduce)/删除不可到达的规则 boolean tag=true; VectorVector delete=new VectorVector(); Vector flag=new Vector(
21、);/用以记录那些出现在右部的非终结符 String start_character=S; flag.add(start_character); while(tag) flag.clear(); flag.add(start_character); tag=false; for(int i=0;inewproduce.size();i+)/扫描产生式右部的非终结符 Vector temp=newproduce.get(i); String left=temp.get(0); String right=temp.get(1); for(int j=0;jright.length();j+) St
22、ring s=+right.charAt(j); if(elements.IsNoENd(s) flag.add(s); /for j /for i for(int i=0;inewproduce.size();i+)/开始进行删除 Vector temp=newproduce.get(i); String left=temp.get(0); String right=temp.get(1); if(flag.contains(left) continue; else tag=true; delete.add(temp); newproduce.remove(temp); / for /whi
23、le return delete; /public public void shownewproduce() System.out.print(压缩后的产生式输出如下:); for(int i=0;inewproduce.size();i+) System.out.println( ); Vector temp=(Vector)newproduce.get(i); System.out.print(String)temp.get(0)+-+(String)temp.get(1); System.out.println(); public VectorVector getproduce() re
24、turn this.newproduce; public Vector getnoend() return this.noend; public Vector getend() return this.end; class UnEndable VectorVector thenewproduce; VectorVector flagtable=new VectorVector(); public UnEndable() thenewproduce=tools.doubleprotection(newproduce); /showthenewproduce(); initial_table(); firststep(); secondstep(); thirdstep(); showflagtable(); public void showthenewproduce() System.out.print(最新的产生式输出如下:);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1