正规文法转换成正规式.docx

上传人:b****5 文档编号:6345006 上传时间:2023-01-05 格式:DOCX 页数:26 大小:57.98KB
下载 相关 举报
正规文法转换成正规式.docx_第1页
第1页 / 共26页
正规文法转换成正规式.docx_第2页
第2页 / 共26页
正规文法转换成正规式.docx_第3页
第3页 / 共26页
正规文法转换成正规式.docx_第4页
第4页 / 共26页
正规文法转换成正规式.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

正规文法转换成正规式.docx

《正规文法转换成正规式.docx》由会员分享,可在线阅读,更多相关《正规文法转换成正规式.docx(26页珍藏版)》请在冰豆网上搜索。

正规文法转换成正规式.docx

正规文法转换成正规式

!

f诚诃

f行璧

年级/专业/班:

11级计算机类

(二)班

姓名:

徐勇兵

学号:

E01114278

实验名称:

正规文法转换成正规式

实验目的:

1.了解并熟悉词法分析中单词的描述工具正规文法和正规式表示单词的方式及其之间的差异性和等价性。

2.利用计算机编程实现正规文法转换成等价的正规式。

实验要求:

1.正规文法的输入应简便。

2.输出的正规式以利用3条转换规则得出为标准。

输入:

一组产生式所构成的正规文法。

输出:

对应的等价的正规式。

实验原理:

1.多数程序设计语言的单词的语法都能用正规文法或3型文法来描述。

2.3型文法的特征是P中的每一条规则都有下述形式:

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

实验算法:

实验算法定义一个函数实现转换正规文法为正规式。

函数根据三个转换规则,首先合并形如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)Ao

算法默认开始符号为S且放在第一个产生式,这样就能根据第一条产生式最终得到等价的一个开始符号定义的正规式。

实验结果:

Convert[JavaApplication]G:

\MyEclipseProtessionaT

终结符输出如下

非终结符输出如下;AA,

产生式输出如下:

S->aA

S->a

A->aA

A->dA

A->d

压缩后的产生式输出如下:

S->aA

A->aA

A->dA

A->a

A->d

A的结果是作d)*(a|d)

对于S分布求出结果是3(a|d)*(a|d)

求5的第M步结果是a(a|d)*(a|d)

theresultis:

(a)|a(a|d)*(a|d)

importjava.util.Vector;

importjavax.swing」OptionPane;

classTools{

publicVectoraddElements(Vectorvs,Vectortemp){for(inti=O;i

//if(!

vs.contains(temp.get(i)))

vs.add(temp.get(i));

}//for

returnvs;

}//publicVectoraddElements(Vectorvs,Vectortemp){publicVectorprotection(Vectorvs){

Vectornewvector二newVector();

for(inti=O;i

newvector.add(vs.get(i));

returnnewvector;

}

publicVector>doubleprotection(Vector>vs){Vector

Vectorproduce=(Vector)vs.get(i);

Vectortemp二newVector();

for(intj=0;j

temp.add((String)produce.get(j));

}//forj

newvector.add(temp);

}//fori

returnnewvector;

}

}//classtools

classElements{

Vectorend=newVector();//表示终结符

Vectornoend=newVector();//表示非终结符Vector>();//产生式publicvoidsetend(){//终结符元素添加

while(true)

{

Strings^OptionPane.showInputDialogfnull/'请输入终结符");if(s==null)

{return;

}//if

end.add(s);

}//while

(//publicvoidaddend(){//元素添加publicvoidsetnoend(){//非终结符元素添加

while(true)

{

Strings^OptionPane.showInputDialogfnull/1非请输入终结符");if(s==null)

{return;

}//if

noend.add(s);

}//while

(//publicvoidaddnoend(){//

publicvoidsetproduce(){

while(true)

{

Strings^OptionPane.showInputDialogfnull/'请输入产生式,隔开”);if(s=二null)

return;

Vectortemp二newVector();

temp.add(s.split("->")[0]);

temp.add(s.split("->")[l]);

produce.add(temp);

}//while

(//publicvoidaddproduce()

publicVectorgetend(){

returnend;

publicVectorgetnoend(){returnnoend;

publicVector

}

publicvoidrun(){

*TEST*******************************

end.addCH);end.addC^11);noend.add(,,Sn);

noend.addC'A'1);

Vectortemp=newVector();temp.add(llSn);

temp.addC'aA");

produce.add(temp);

^***********************

Vectortemp4=newVector();

temp4.add(,,S11);

temp4.add(,,aH);

produce.add(temp4);

//System.out.println(”produce・size()二”+produce.size());

 

Vectortempl=newVector();tempi.add(,,A");tempi.add(,,dAH);produce.add(templ);

/穴****穴*宣*宣*宣穴*宣*宣穴*穴****穴/

Vectortemp2=newVector();temp2.add(,,A");

temp2.add(,,a");

produce.add(temp2);

Vectortemp3=newVector();

temp3.add(,,An);

temp3.add("d");

produce.add(temp3);

//System.out.println(”produce.size()="+produce・size());

/*noend.add(HBH);

Vectortemp6=newVector();temp6.add(,,A");

temp6.add(,,aBH);

produce.add(temp6);

Vectortemp5=newVector();

tempS.add(,,BH);

temp5.add("d");

produce.add(temp5);*/

//this.setend();

//this.setnoend();

//this.setproduce();

}

publicbooleanlscontainend(Strings)〃判断某个符号是否是终结符可以是一个字符也可是字符串

{/*System.out.print("输出终结符“);

for(inti=O;i

System.out.print(end.get(i));

System.out.println();

System.out.printin("传送过来的需要判断的是"+s);*/

intlength=s」ength();

for(inti=O;i

{

Stringa=H,,+s.charAt(i);if(end.contains(a))

{continue;}

elsereturnfalse;

}//for

returntrue;

}//publicbooleanisRGPcontain(Strings)

publicbooleanlsNoENd(Strings){//判断某个符号是否是非终结符Stringss=HI,+s.charAt(O);

if(!

Iscontainend(ss))//如果不含有终结符,则为非终结符returntrue;

returnfalse;

}//publicboolean

publicvoidshow(){

System.out.print(H终结符输出如下:

”);

for(inti=O;i

System.out.print((String)end.get(i)+,,/");

}

System.out.println(H");

System.out.print("非终结符输出如下:

");

for(inti=0;i

System.out.print((String)noend.get(i)+n,");

}

System.out.println(H");

System.out.print("产生式输出如下:

");

for(inti=0;i

System.out.println(H");

Vectortemp=(Vector)produce.get(i);

System.out.print((String)temp.get(0)+,,->,,+(String)temp.get(l));}

System.out.println(H");

}//classElementsclassCompression{

Toolstools=newTools();

Elementselements;

Vectorend二null;

Vectornoend二null;

Vector>produce=newVector

Vectornewend;

Vectornewnoend;

Vector

publicvoidshowdelete(Vector>harmzVector

unreach,Vector>unend){

讦(harm.isEmpty()){

System.out.println("没有有害规则被删除”);

}

else{

System.out.print("被删除的有害产生式输出如下:

");

for(inti=O;i

System.out.println(n");

Vectortemp=(Vector)harm.get(i);

System.out.print((String)temp.get(O)+,,->,,+(String)temp.get(l));

}

System.out.println();

Systemoutprintl门(',*)*<*)*<3**]*****]**)*<*)*<**]**)*<*)*<3**]*****]**]**)*<3**]**)*<*)*<**]***")・

}

if(unreach.isEmpty()){

System,out.printin(”没有不可到达规则被删除");

}

else{

System.out.print("被删除的不可到达产生式输出如下:

");for(inti=O;i

System.out.println(HM);

Vectortemp=(Vector)unreach.get(i);

System.out.print((String)temp.get(O)+,,->,,+(String)temp.get(l));

}

System.out.println();

SystemoutprintlnC1****3**3*****3*******3*****3**3*****3**3***3**3*******3***").

if(unend.isEmpty()){

System.out.println("没有不可终止规则被删除”);

}

else{

System.out.print("被删除的不可终止产生式输出如下:

");for(inti=0;i

System.out.println(H");

Vectortemp=(Vector)unend.get(i);

System.out.print((String)temp.get(O)+,,->,,+(String)temp.get(l));

}

System.out.println();

SystemoutprintlnC1****3**3*****3*******3*****3**3*****3**3***3**3*******3***").

publicVector>deleteharm(Vector

Vector

for(inti=0;i

Vectortemp二newproduce.get(i);

Stringleft=temp.get(O);

Stringright=temp.get(l);

if(left.equals(right)){//形如A->A

newproduce.remove(temp);delete.add(temp);

}

}

returndelete;

(//publicVector>deleteharm(Vector>

newproduce){//删除不可到达的规则

booleantag=true;

Vector

Vectorflag=newVector();//用以记录那些出现在右部的非终结符Stringstart_character=,,S";

flag.add(start_character);

while(tag){

flag.clear();

flag.add(start_character);

tag=false;

for(inti=0;i

Vectortemp二newproduce.get(i);

Stringleft=temp.get(O);

Stringright=temp.get(l);

forfintj=O;j

Strings=ll"+right.charAt(j);

if(elements.lsNoENd(s))

flag.add(s);

}//forj

}//fori

for(inti=0;i

Vectortemp二newproduce.get(i);

Stringleft=temp.get(O);

Stringright=temp.get(l);

if(flag.contains(left))

continue;

else{

tag=true;

delete.add(temp);

newproduce.remove(temp);

}

}//for

(//while

returndelete;

}//public

publicvoidshownewproduce(){

System.out.print("压缩后的产生式输出如下:

");

for(inti=0;i

System.out.println(H");

Vectortemp=(Vector)newproduce.get(i);

System.out.print((String)temp.get(0)+,,->,,+(String)temp.get(l));

}

System.out.println();

publicVector>getproduce(){returnthis.newproduce;

}

publicVectorgetnoend(){

returnthis.noend;

}

publicVectorgetend(){

returnthis.end;

}

classUnEndable{

Vector

Vector>();publicUnEndable(){

thenewproduce=tools.doubleprotection(newproduce);

//showthenewproduce();

initial_table();

firststep();

secondstep();

thirdstep();showflagtable();

}

publicvoidshowthenewproduce(){

System.out.print("最新的产生式输出如下:

");

for(inti=0;i

System.out.println(n");

Vectortemp=(Vector)thenewproduce.get(i);

System.out.print((String)temp.get(O)+,,->"+(String)temp.get(l));

}

System.out.println();

}

publicStringwhattag(Stringnoendcharacter){//判断某个非终结符在表中的状态,只有yes,nounsure三种

for(inti=O;i

Vectortemp=(Vector)flagtable.get(i);

if(temp.get(O).equals(noendcharacter)){

return(String)temp.get⑴;

}//if

}//for

return"error11;

}//publicStringwhattag

publicvoidinitial_table(){

for(inti=0;i

Vectortemp二newVector();

temp.add((String)noend.get(i));

temp.add(Hunsure");

flagtable.add(temp);

}//for

}//publicvoidinitial_table(){

pub

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 互联网

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1