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

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

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

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

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

正规文法转换成正规式

 

课程名称:

正规文法转换成正规式

年级/专业/班:

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)A。

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

实验结果:

importjava.util.Vector;

importjavax.swing.JOptionPane;

classTools{

publicVectoraddElements(Vectorvs,Vectortemp){

for(inti=0;i

//if(!

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

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

}//for

returnvs;

}//publicVectoraddElements(Vectorvs,Vectortemp){

publicVectorprotection(Vectorvs){

Vectornewvector=newVector();

for(inti=0;i

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

returnnewvector;

}

publicVector>doubleprotection(Vector>vs){

Vector>newvector=newVector>();

for(inti=0;i

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>produce=newVector>();//产生式

publicvoidsetend(){//终结符元素添加

while(true)

{

Strings=JOptionPane.showInputDialog(null,"请输入终结符");

if(s==null)

{return;

}//if

end.add(s);

}//while

}//publicvoidaddend(){//元素添加

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

while(true)

{

Strings=JOptionPane.showInputDialog(null,"非请输入终结符");

if(s==null)

{return;

}//if

noend.add(s);

}//while

}//publicvoidaddnoend(){//

publicvoidsetproduce(){

while(true)

{

Strings=JOptionPane.showInputDialog(null,"请输入产生式,->隔开");

if(s==null)

return;

Vectortemp=newVector();

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

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

produce.add(temp);

}//while

}//publicvoidaddproduce()

publicVectorgetend(){

returnend;

}

publicVectorgetnoend(){

returnnoend;

}

publicVector>getproduce(){

returnthis.produce;

}

publicvoidrun(){

/*************************TEST********************************/

end.add("a");

end.add("d");

noend.add("S");

noend.add("A");

Vectortemp=newVector();

temp.add("S");

temp.add("aA");

produce.add(temp);

/*************************/

/*************************/

Vectortemp4=newVector();

temp4.add("S");

temp4.add("a");

produce.add(temp4);

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

/***********************TEST**********************************/

/*************************/

Vectortemp7=newVector();

temp7.add("A");

temp7.add("aA");

produce.add(temp7);

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

/***********************TEST**********************************/

Vectortemp1=newVector();

temp1.add("A");

temp1.add("dA");

produce.add(temp1);

/*************************/

Vectortemp2=newVector();

temp2.add("A");

temp2.add("a");

produce.add(temp2);

/*************************/

Vectortemp3=newVector();

temp3.add("A");

temp3.add("d");

produce.add(temp3);

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

/***********************TEST**********************************/

/*noend.add("B");

Vectortemp6=newVector();

temp6.add("A");

temp6.add("aB");

produce.add(temp6);

Vectortemp5=newVector();

temp5.add("B");

temp5.add("d");

produce.add(temp5);*/

//this.setend();

//this.setnoend();

//this.setproduce();

}

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

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

for(inti=0;i

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

System.out.println();

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

intlength=s.length();

for(inti=0;i

{

Stringa=""+s.charAt(i);

if(end.contains(a))

{continue;}

elsereturnfalse;

}//for

returntrue;

}//publicbooleanisRGPcontain(Strings)

publicbooleanIsNoENd(Strings){//判断某个符号是否是非终结符

Stringss=""+s.charAt(0);

if(!

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

returntrue;

returnfalse;

}//publicboolean

publicvoidshow(){

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

");

for(inti=0;i

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

}

System.out.println("");

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

");

for(inti=0;i

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

}

System.out.println("");

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

");

for(inti=0;i

System.out.println("");

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

System.out.print((String)temp.get(0)+"->"+(String)temp.get

(1));

}

System.out.println("");

}

}//classElements

classCompression{

Toolstools=newTools();

Elementselements;

Vectorend=null;

Vectornoend=null;

Vector>produce=newVector>();

Vectornewend;

Vectornewnoend;

Vector>newproduce;

publicvoidshowdelete(Vector>harm,Vector>unreach,Vector>unend){

if(harm.isEmpty()){

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

}

else{

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

");

for(inti=0;i

System.out.println("");

Vectortemp=(Vector)harm.get(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(inti=0;i

System.out.println("");

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

System.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(inti=0;i

System.out.println("");

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

System.out.print((String)temp.get(0)+"->"+(String)temp.get

(1));

}

System.out.println();

System.out.println("***********************************************");

}

}

publicVector>deleteharm(Vector>newproduce){//删除有害规则

Vector>delete=newVector>();

for(inti=0;i

Vectortemp=newproduce.get(i);

Stringleft=temp.get(0);

Stringright=temp.get

(1);

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

newproduce.remove(temp);

delete.add(temp);

}

}

returndelete;

}//publicVector>deleteharm(Vector>newproduce)

publicVector>deleteunreachable(Vector>newproduce){//删除不可到达的规则

booleantag=true;

Vector>delete=newVector>();

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(0);

Stringright=temp.get

(1);

for(intj=0;j

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

if(elements.IsNoENd(s))

flag.add(s);

}//forj

}//fori

for(inti=0;i

Vectortemp=newproduce.get(i);

Stringleft=temp.get(0);

Stringright=temp.get

(1);

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("");

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

System.out.print((String)temp.get(0)+"->"+(String)temp.get

(1));

}

System.out.println();

}

publicVector>getproduce(){

returnthis.newproduce;

}

publicVectorgetnoend(){

returnthis.noend;

}

publicVectorgetend(){

returnthis.end;

}

classUnEndable{

Vector>thenewproduce;

Vector>flagtable=newVector>();

publicUnEndable(){

thenewproduce=tools.doubleprotection(newproduce);

//showthenewproduce();

initial_table();

firststep();

secondstep();

thirdstep();

showflagtable();

}

publicvoidshowthenewproduce(){

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

");

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

当前位置:首页 > 小学教育 > 语文

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

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