从正规文法构造有穷状态自动机Word格式.docx
《从正规文法构造有穷状态自动机Word格式.docx》由会员分享,可在线阅读,更多相关《从正规文法构造有穷状态自动机Word格式.docx(14页珍藏版)》请在冰豆网上搜索。
newvector=newVector<
();
for(inti=0;
i<
vs.size();
i++)
newvector.add(vs.get(i));
returnnewvector;
}
Vector<
>
doubleprotection(Vector<
i++){
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
addElements(Vector<
vs,Vector<
temp){
temp.size();
//if(!
vs.contains(temp.get(i)))
vs.add(temp.get(i));
}//for
returnvs;
}//publicVector<
temp){
}//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"
b"
noend.add("
S"
A"
B"
temp.add("
aA"
/*************************/
temp1=newVector<
temp1.add("
bB"
produce.add(temp1);
temp2=newVector<
temp2.add("
e"
produce.add(temp2);
temp3=newVector<
temp3.add("
aB"
produce.add(temp3);
temp4=newVector<
temp4.add("
bA"
produce.add(temp4);
temp5=newVector<
temp5.add("
aS"
produce.add(temp5);
temp6=newVector<
temp6.add("
produce.add(temp6);
temp7=newVector<
temp7.add("
produce.add(temp7);
temp8=newVector<
temp8.add("
produce.add(temp8);
/*Vector<
temp9=newVector<
temp9.add("
aAA"
produce.add(temp9);
*/
//System.out.println("
produce.size()="
+produce.size());
/***********************TEST**********************************/
//this.setend();
//this.setnoend();
//this.setproduce();
publicbooleanIscontainend(Strings)//正则表达式判断s1是否在END的闭包里面正则忘了怎么写了
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("
终结符输出如下:
end.size();
System.out.print((String)end.get(i)+"
"
}
System.out.println("
"
非终结符输出如下:
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
publicclassTest{
Elementselements;
Toolstools=newTools();
inputTable=newVector<
//表示输入符号的集合即又穷字母表
statusTable=newVector<
//状态表
newproduce=newVector<
//转换函数
Stringstart="
;
//初态
Stringlast="
Z"
//终态
publicvoidfirststep(){
if(elements.Iscontainend("
)==true)
yes"
temp=produce.get(i);
Stringleft=temp.get(0);
Stringright=temp.get
(1);
if(right.length()!
=1){//S->
aA形式
Stringone="
+right.charAt(0);
Stringtwo="
+right.charAt
(1);
Vector<
temp1.add(left);
temp1.add(one);
temp1.add(two);
newproduce.add(temp1);
else{//S->
a形式
temp1.add(last);
}
publicbooleaniszhenggui(){
if(right.length()>
2)
returnfalse;
if(right.length()==1){
if(elements.IsNoENd(right)==false)//S->
A不满足
returnfalse;
if(right.length()==2){
if(elements.Iscontainend(one)==false)//
if(elements.IsNoENd(two)==false)//
returntrue;
publicvoidFA(){//构造自动机
publicvoidsetstatusTable(){//状态表
statusTable.add(noend.get(i));
statusTable.add(last);
publicvoidsetinputTable(){//状态表
inputTable.add(end.get(i));
状态表输出如下:
statusTable.size();
System.out.print((String)statusTable.get(i)+"
字母表输出如下:
inputTable.size();
System.out.print((String)inputTable.get(i)+"
System.out.print("
转换函数输出如下:
newproduce.size();
)newproduce.get(i);
System.out.print((String)temp.get(0)+"
+(String)temp.get
(1)+"
+(String)temp.get
(2));
System.out.println("
初态是"
+start);
终态是"
+last);
}
publicbooleanjudge(){
booleanflag=true;
vs=newVector<
//Vector<
vv=newVector<
temp=newproduce.get(i);
Stringmidle=temp.get
(1);
if(vs.isEmpty()){//如果是第一次放入数据
Vector<
temp2.add(left);
temp2.add(midle);
vs.add(temp2);
}
else{
//System.out.println("
11"
left="
+left+"
midle="
+midle);
if(vs.contains(temp2))
returnfalse;
else
vs.add(temp2);
publicTest(){
elements=newElements();
elements.run();
this.end=elements.getend();
this.noend=elements.getnoend();
this.produce=elements.getproduce();
elements.show();
booleantag1=iszhenggui();
if(tag1)
是正规式"
else
{System.out.println("
不是正规式"
System.out.println("
程序结束"
return;
firststep();
setstatusTable();
setinputTable();
this.show();
Booleantag=judge();
if(tag)
是DFA"
不是NFA"
}//
publicstaticvoidmain(String[]args){
Testapp=newTest();