Monday, August 1

C program to implement DFA

//updated on 11/03/2015

Hello friends,


               This post describes how a Deterministic Finte Automata (DFA) can be implemented using C. Do you know, associated with every programming language compiler there is a program named  recognizer that takes a string , say string S, as input and answers "YES" if S is a sentence of the language and  " NO " otherwise..!! To accomplish this , we have to train recognizer about the syntactic structure of intended language ( done with regular expressions). For this purpose, we construct a transition diagram called a "finite automation". In a Deterministic Finite Automation, only one transition out of state is possible on the same input symbol. On the other hand, in NonDeterministic Finite Automata more than one transitions may possible for same input symbol.


  . Some interesting implementations using DFA are:
  • whether DFA accepts all strings.
  • whether DFA accepts any string
  • whether two DFAs recognize the same language
  • DFA with a minimum number of states for a particular regular language



Illustartion


Consider the DFA represented in this transition diagram.



   
                         


Here is a sample DFA with starting state " 0 ", and accepting state " 1" .  "a" and "b" are the input symbols.
This DFA will accept any string containing 'a's and 'b' and last symbol should be 'a'.


CODE:


#include <stdio.h>
#define TOTAL_STATES 2
#define FINAL_STATES 1
#define ALPHABET_CHARCTERS 2
#define UNKNOWN_SYMBOL_ERR 0
#define NOT_REACHED_FINAL_STATE 1
#define REACHED_FINAL_STATE 2
enum DFA_STATES{q0,q1};
enum input{a,b};
int Accepted_states[FINAL_STATES]={q1};
char alphabet[ALPHABET_CHARCTERS]={'a','b'};
int Transition_Table[TOTAL_STATES][ALPHABET_CHARCTERS];
int Current_state=q0;
void DefineDFA()
{
    Transition_Table[q0][a] = q1;
    Transition_Table[q0][b] = q0;
    Transition_Table[q1][a] = q1;
    Transition_Table[q1][b] = q0;
}
int DFA(char current_symbol)
{
int i,pos;
    for(pos=0;pos<ALPHABET_CHARCTERS; pos++)
        if(current_symbol==alphabet[pos])   
            break;//stops if any character other than a or b
    if(pos==ALPHABET_CHARCTERS)
         return UNKNOWN_SYMBOL_ERR;
    for(i=0;i<FINAL_STATES;i++)
 if((Current_state=Transition_Table[Current_state][pos])
==Accepted_states[i])
            return REACHED_FINAL_STATE;
    return NOT_REACHED_FINAL_STATE;
}
int main(void)
{
    char current_symbol;
    int result;
 
    DefineDFA();    //Fill transition table
 
    printf("Enter a string with 'a' s and 'b's:\n
                Press Enter Key to stop\n");
 
 
    while((current_symbol=getchar())!= '\n')
        if((result= DFA(current_symbol))==UNKNOWN_SYMBOL_ERR)
            break;
    switch (result) {
    case UNKNOWN_SYMBOL_ERR:printf("Unknown Symbol %c",
  current_symbol); 
 break;
    case NOT_REACHED_FINAL_STATE:printf("Not accepted"); break;
    case REACHED_FINAL_STATE:printf("Accepted");break;
    default: printf("Unknown Error");
    }
    printf("\n\n\n");
    return 0;
}


OUTPUT
========

String with only "a"s and "b"s and last symbol is "a".     ACCEPTED..!!



String with only "a"s and "b"s but  last symbol is not  "a".     REJECTED..!!



Unknown symbol "y" in input. REJECTED.!!!!



16 comments :

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous
Thank you for ur feedback..!!!!! :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

thnk yar...

zaheer said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Dear,i need some help to impliment DFA through a file to check the input string is given at run time is accepted by this DFA

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

CAN I KNOW SOME APPLICATIONS OF DFA

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

thank u

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous

%e3cscript%3calert(9)%3cscript%3e

vijai krishnan said... Best Blogger Tips [Reply to comment] Best Blogger Templates

great work my friend

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Thank You vijai krishnan :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

http://www.cs.umbc.edu/~squire/images/451-f1.jpg

can u give me the regular expression for this dfa..pls reply 2dae itself..its very urgent.thank you

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hi @neha a,

01*0 + 10*1 can be the regular expression of the DFA you asked.!!

Vrajesh said... Best Blogger Tips [Reply to comment] Best Blogger Templates

sir plzzz tell the input which is u r entered at code testing time................plzzz

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hello @Vrajesh Pandya,

code is updated.!! Logic is more comprehensive now..!!
Screenshots of sample output is also included. Please check it out!!

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hey @Junaid Iqbal,
Nice Question..!!
DFA is used in many areas like COMPILER CONSTRUCTION, Speech recognition programs.!!!

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

what could be the program of (a+b)*aba(a+b)*

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

#include
#include
#include
int main()
{
char s[100];
int l,c=0, state=1;
gets(s);
l=strlen(s);
printf("%s %d",s,l);
while(c!=l+1)
{
switch(state)
{
case 1: if(s[c]=='a')
state=2;
break;
case 2: if(s[c]=='b')
state=1;
break;
}
c++;
}
// printf("%s %d",s,c);
if(state==2)
printf("\nAccepted");
else
printf("\nRejected");
}

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

can you please give the program for checking substring 01

Post a Comment