Monday 7 September 2015

Program in c for acceptance of string ((a|b)*abb) using dfa

#include<stdio.h>
void main(){
int state=0;
int prev=0;
int next =0;
int arr[4][2]={{1,0},{1,2},{1,3},{1,0}};
char str[20];
char ch;
int i=0;
  printf("\nEnter the string");
  fgets(str,20,stdin);
 while(1){
    ch=str[i];
    if(ch=='a'){
    next = arr[prev][0];
    printf("\nPrev =  %d and next = %d",prev,next);
    prev= next;
    state = next;
    }
    else if(ch=='b'){
    next = arr[prev][1];
    printf("\nPrev =  %d and next = %d",prev,next);
    prev= next;
    state = next;
    }
    else if(ch=='$'){
    if(state==3){
    state =4;
    next = state;
    printf("\nPrev =  %d and next = %d",prev,next);   
    }
    break;
    }
    else if(ch=='\0'){
    printf("\nString is not terminated... Enter $ to terminate it...");
    break;
    }
    else if(ch==' '){
    printf("\nPlease do not enter blank spaces");
    break;
    }
   i++;

 }
 if(state ==4){
    printf("\nString is accepted");
 }
 else{
    printf("\nString is not accepted");
 }
}