conveylive.com

Code for NFA Simulation

Non-deterministic finite automaton

Syeda Tasneem Rumy
Syeda Tasneem Rumy
Sep 12, 2009
0 Comments | 3402 Views | 0 Hits




#include<iostream>
#include<string>
using namespace std;

int state[3]={0,0,0,0};
char str[10];
//symbol
    const int S=10;
//number of states
    const int N=4;

void show(){
    int j=0,q=0;
    for(j=0;j<3;j++){
        cout<<"State "<<"q"<<j<<" =";
        if(state[j]==1){cout<<"Active"<<endl;}
        if(state[j]==0){cout<<"Inactive"<<endl;}
    }
   

}

int main(){
   
    int i=0,j=0, flag1=0, flag2=0, flag3=0;
    char ans='a';
    int vStrlen=0;
   
   
   
    cout<<"Start State : q0"<<endl;
    cout<<"Accept State: q0"<<endl;
    cout<<"Intermediate State: q1, q2"<<endl;
    cout<<endl<<"Input symbols: 'a' and  'b' "<<endl;
    do{
   
    for(j=0;j<N;j++){
        state[j]=0;
       
    }
    for(j=0;j<S;j++){
        str[j]='/0';
    }
   
    cout<<"Enter the string of maximum length 8"<<endl;

   
   
    cin>>str;
   
    for(j=0;str[j]!='';j++){
       
        vStrlen++;
    }
    state[0]=1;
   

    for(i=0; i<vStrlen; i++){
       
        if(i==0)
            {
            cout<<"For Input string: "<<str<<endl;
            cout<<"Initially :::"<<endl<<endl;
            show();cout<<endl<<endl;
            }               
   
        if(state[0]==1) flag1=1;
            else flag1=0;
        if(state[1]==1) flag2=2;
            else flag2=0;
        if(state[2]==1) flag3=3;
            else flag3=0;
    //state 0
    if(flag1==1){
    if(state[0]==1 && str[i]=='b'){
        state[1]=1;
        state[0]=0;
       
        cout<<endl<<endl<<"After processing "<<i+1<<" character: ""<<str[i]<<"""<<" of input string ""<<str<<"""<<endl<<endl;
        show();
        cout<<endl<<endl;
       
        }
       
    if(state[0]==1 && str[i]=='a'){
        state[0]=1;
        state[2]=1;
        cout<<endl<<endl<<"After processing "<<i+1<<" character: ""<<str[i]<<"""<<" of input string ""<<str<<"""<<endl<<endl;
        show();
        cout<<endl<<endl;
       
        }
    }
    //state 1
    if(flag2==2){
    if(state[1]==1 && str[i]=='a'){
        state[1]=1;
        state[2]=1;
        cout<<endl<<endl<<"After processing "<<i+1<<" character: ""<<str[i]<<"""<<" of input string ""<<str<<"""<<endl<<endl;
        show();
        cout<<endl<<endl;
   
        }          
    if(state[1]==1 && str[i]=='b'){
        state[2]=1;
        state[1]=0;
        cout<<endl<<endl<<"After processing "<<i+1<<" character: ""<<str[i]<<"""<<" of input string ""<<str<<"""<<endl<<endl;
        show();
        cout<<endl<<endl;
   
        }
    }
    //state 2
    if(flag3==3){
    if(state[2]==1 &&  str[i]=='b'){
        state[2]=0;
        cout<<endl<<endl<<"After processing "<<i+1<<" character: ""<<str[i]<<"""<<" of input string ""<<str<<"""<<endl<<endl;
        show();
        cout<<endl<<endl;
   
        }
    if(state[2]==1 && str[i]=='a'){
        state[0]=1;
        state[2]=0;
        cout<<endl<<endl<<"After processing "<<i+1<<" character: ""<<str[i]<<"""<<" of input string ""<<str<<"""<<endl<<endl;
        show();
        cout<<endl<<endl;
       
   
        }
    }
    }
    if(state[0]==1){   
        cout<<"String is accepted !"<<endl;
    }
    else
        cout<<"String is not accepted!"<<endl;
       
   
    cout<<"Do you want to insert another string? (y/n): ";
    cin>>ans;
   
    }     
    while(ans=='y'||ans=='Y');
   
   
    return 0;          
}

Keywords: Code for NFA Simulation, non-deterministic finite automaton



Please Signup to comment on this article