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;iSystem.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;iSystem.out.print((String)end.get(i)+",");
}
System.out.println("");
System.out.print("非终结符输出如下:
");
for(inti=0;iSystem.out.print((String)noend.get(i)+",");
}
System.out.println("");
System.out.print("产生式输出如下:
");
for(inti=0;iSystem.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;iSystem.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;iSystem.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;iSystem.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;iVectortemp=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;iVectortemp=newproduce.get(i);
Stringleft=temp.get(0);
Stringright=temp.get
(1);
for(intj=0;jStrings=""+right.charAt(j);
if(elements.IsNoENd(s))
flag.add(s);
}//forj
}//fori
for(inti=0;iVectortemp=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;iSystem.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("最新的产生式输出如下:
");