正规文法转换成正规式文档格式.docx
《正规文法转换成正规式文档格式.docx》由会员分享,可在线阅读,更多相关《正规文法转换成正规式文档格式.docx(28页珍藏版)》请在冰豆网上搜索。
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->
y对应A=x*y和规则3的A->
y对应A=x|y将文法产生式变换为等价的正规式。
在规则以外还需要另外一个处理,这个处理可以从书本上的例子中看到,即将公因子提取出来,如A=aA|dA变换为A=(a|d)A。
算法默认开始符号为S且放在第一个产生式,这样就能根据第一条产生式最终得到等价的一个开始符号定义的正规式。
实验结果:
importjava.util.Vector;
importjavax.swing.JOptionPane;
classTools{
publicVector<
String>
addElements(Vector<
vs,Vector<
temp){
for(inti=0;
i<
temp.size();
i++){
//if(!
vs.contains(temp.get(i)))
vs.add(temp.get(i));
}//for
returnvs;
}//publicVector<
protection(Vector<
vs){
Vector<
newvector=newVector<
();
vs.size();
i++)
newvector.add(vs.get(i));
returnnewvector;
}
Vector<
>
doubleprotection(Vector<
Vector<
produce=(Vector<
)vs.get(i);
temp=newVector<
for(intj=0;
j<
produce.size();
j++){
temp.add((String)produce.get(j));
}//forj
newvector.add(temp);
}//fori
}//classtools
classElements{
Vector<
end=newVector<
//表示终结符
noend=newVector<
//表示非终结符
produce=newVector<
//产生式
publicvoidsetend(){//终结符元素添加
while(true)
{
Strings=JOptionPane.showInputDialog(null,"
请输入终结符"
);
if(s==null)
{return;
}//if
end.add(s);
}//while
}//publicvoidaddend(){//元素添加
publicvoidsetnoend(){//非终结符元素添加
非请输入终结符"
noend.add(s);
}//publicvoidaddnoend(){//
publicvoidsetproduce(){
while(true)
{
Strings=JOptionPane.showInputDialog(null,"
请输入产生式,->
隔开"
if(s==null)
return;
temp.add(s.split("
->
"
)[0]);
)[1]);
produce.add(temp);
}//while
}//publicvoidaddproduce()
getend(){
returnend;
getnoend(){
returnnoend;
getproduce(){
returnthis.produce;
publicvoidrun(){
/*************************TEST********************************/
end.add("
a"
d"
noend.add("
S"
A"
temp.add("
aA"
/*************************/
temp4=newVector<
temp4.add("
produce.add(temp4);
//System.out.println("
produce.size()="
+produce.size());
/***********************TEST**********************************/
temp7=newVector<
temp7.add("
produce.add(temp7);
temp1=newVector<
temp1.add("
dA"
produce.add(temp1);
temp2=newVector<
temp2.add("
produce.add(temp2);
temp3=newVector<
temp3.add("
produce.add(temp3);
/*noend.add("
B"
temp6=newVector<
temp6.add("
aB"
produce.add(temp6);
temp5=newVector<
temp5.add("
produce.add(temp5);
*/
//this.setend();
//this.setnoend();
//this.setproduce();
publicbooleanIscontainend(Strings)//判断某个符号是否是终结符可以是一个字符也可是字符串
{/*System.out.print("
输出终结符"
end.size();
System.out.print(end.get(i));
System.out.println();
System.out.println("
传送过来的需要判断的是"
+s);
intlength=s.length();
for(inti=0;
length;
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("
终结符输出如下:
System.out.print((String)end.get(i)+"
"
}
"
非终结符输出如下:
noend.size();
System.out.print((String)noend.get(i)+"
产生式输出如下:
System.out.println("
temp=(Vector<
)produce.get(i);
System.out.print((String)temp.get(0)+"
+(String)temp.get
(1));
}//classElements
classCompression{
Toolstools=newTools();
Elementselements;
Vector<
end=null;
noend=null;
newend;
newnoend;
newproduce;
publicvoidshowdelete(Vector<
harm,Vector<
unreach,Vector<
unend){
if(harm.isEmpty()){
没有有害规则被删除"
else{
System.out.print("
被删除的有害产生式输出如下:
for(inti=0;
harm.size();
System.out.println("
Vector<
)harm.get(i);
System.out.print((String)temp.get(0)+"
}
System.out.println();
***********************************************"
if(unreach.isEmpty()){
没有不可到达规则被删除"
被删除的不可到达产生式输出如下:
unreach.size();
)unreach.get(i);
if(unend.isEmpty()){
没有不可终止规则被删除"
被删除的不可终止产生式输出如下:
unend.size();
)unend.get(i);
publicVector<
deleteharm(Vector<
newproduce){//删除有害规则
delete=newVector<
newproduce.size();
temp=newproduce.get(i);
Stringleft=temp.get(0);
Stringright=temp.get
(1);
if(left.equals(right)){//形如A->
A
newproduce.remove(temp);
delete.add(temp);
returndelete;
newproduce)
deleteunreachable(Vector<
newproduce){//删除不可到达的规则
booleantag=true;
flag=newVector<
//用以记录那些出现在右部的非终结符
Stringstart_character="
;
flag.add(start_character);
while(tag){
flag.clear();
flag.add(start_character);
tag=false;
i++){//扫描产生式右部的非终结符
right.length();
Strings="
+right.charAt(j);
if(elements.IsNoENd(s))
flag.add(s);
i++){//开始进行删除
if(flag.contains(left))
continue;
else{
tag=true;
}//for
}//while
}//public
publicvoidshownewproduce(){
压缩后的产生式输出如下:
)newproduce.get(i);
returnthis.newproduce;
returnthis.noend;
returnthis.end;
classUnEndable{
thenewproduce;
flagtable=newVector<
publicUnEndable(){
thenewproduce=tools.doubleprotection(newproduce);
//showthenewproduce();
initial_table();
firststep();
secondstep();
thirdstep();
showflagtable();
publicvoidshowthenewproduce(){
最新的产生式输出如下: