c++实现无用产生式的消除

来源:互联网 发布:rds数据库 编辑:程序博客网 时间:2024/06/07 10:19


过程有点麻烦,在判断string是否含有某个字符的时候使用迭代器重复了太多

其实可以用一个函数来包装的

/*
   the program is for algorithm of “编译原理 无关文法的消除 ” by-Li
   */
   #include<iostream>
   #include<string>
   using namespace std;
   class P{
    public:
    char left;
    string right;
   };
   class G{
    private:
    P p[20];//最多有20条产生式
    P p1[20];
 P p2[20];
    string Vn;//非终结符号集
    string Vt;//终结符号集
    string Vt2;
    string Vn1;
    string Vn2;
    int len;
    int len1;
    int len2;
    public:
    void create(string s);
    void algorithm2_1();
    void algorithm2_2();
 void displaybegin();
 void displaymid();
 void displayend();
 void display();
 G(string a,string b);
   };
   G::G(string a,string b){
    len=0;
    len1=0;
    len2=0;
    Vn=a;
    Vt=b;
    Vt2="";
    Vn1="";
    Vn2="S";
   }
   void G::create(string s){
    int sl=s.length();
    p[len].left=s[0];
    p[len].right=s.substr(3,sl-3);
    len++;
   }
   void G::algorithm2_1(){
    string com;
    do{
     com=Vn1;
     for(int i=0;i<len;i++){
      int j=0;
      int jend=p[i].right.length();
      while(j<jend){
       string::size_type idx=Vt.find(p[i].right[j]);
       string::size_type idx2=Vn1.find(p[i].right[j]);
       if(idx!=string::npos||idx2!=string::npos){
        j++;
       }
       else{
        break;
       }
      }
      if(j==jend){
       string::size_type idx11=Vn1.find(p[i].left);
       if(idx11==string::npos){
        Vn1.append(1,p[i].left);
       }   
      }
     }
    }while(Vn1.compare(com)!=0);
    for(int i=0;i<len;i++){//--------------------实现p1
    string::size_type idx14=Vn1.find(p[i].left);
    if(idx14!=string::npos) {
     int j=0;
     int jend=p[i].right.length();
     while(j<jend){
      string::size_type idx4=Vt.find(p[i].right[j]);
      string::size_type idx5=Vn1.find(p[i].right[j]);
      if(idx4!=string::npos||idx5!=string::npos){
       j++;
      }
      else{
       break;
      }   
    }
    if(j==jend){
     p1[len1].left=p[i].left;
     p1[len1].right=p[i].right;
     len1++;
    }
    }
    }
     
    cout<<"end of algorithm2_1"<<endl;
   }

void G::algorithm2_2(){
 string Vn2temp;
 string Vt2temp;
 do{
  Vn2temp=Vn2;
  Vt2temp=Vt2;
  for(int i=0;i<len1;i++){
     string::size_type idx3= Vn2.find(p1[i].left);
     if(idx3!=string::npos){
      int j=0;
      int jend=p1[i].right.length();
      while(j<jend){
       string::size_type idx6=Vt.find(p1[i].right[j]);
       if(idx6!=string::npos){
        string::size_type idx12=Vt2.find(p1[i].right[j]);
        if(idx12==string::npos){
         Vt2.append(1,p1[i].right[j]);
         
        }
       j++; 
       }
       else{
        string::size_type idx13=Vn2.find(p1[i].right[j]);
        if(idx13==string::npos){
         Vn2.append(1,p1[i].right[j]);
         
        }
       j++;
       }
       
      }
    }
     
    }
  
 }while(Vn2.compare(Vn2temp)!=0||Vt2.compare(Vt2temp)!=0);
 for(int i=0;i<len1;i++){
  string::size_type idx7=Vn2.find(p1[i].left);
  if(idx7!=string::npos){
   int j=0;
   int jend=p1[i].right.length();
   while(j<jend){
    string::size_type idx8=Vn2.find(p1[i].right[j]);
    string::size_type idx9=Vt2.find(p1[i].right[j]);
    if(idx8!=string::npos||idx9!=string::npos){
     j++;
    }
    else{
     break;
    }
   }
   if(j==jend){
    p2[len2]=p1[i];
    len2++;
   }
  }
 }
 cout<<"end of algorithm2_2"<<endl;
    
}
void G::displayend(){
 for(int i=0;i<len2;i++){
  cout<<p2[i].left<<"->";
  cout<<p2[i].right<<endl;
 }
}
   int main(){
    string a;
    string b;
    cout<<"please input Vn and Vt:";
    cin>>a>>b;
    G one(a,b);
    cout<<"please input the number of createlist:";
    string list;
    int num;
    cin>>num;
    cout<<"please input list:"<<endl;
    for(int n=0;n<num;n++){
     cin>>list;
     one.create(list);
    }
 one.algorithm2_1();
 one.algorithm2_2();
 cout<<"afterprocess:"<<endl;
 one.displayend();
    return 0;
 }

0 0
原创粉丝点击