Space for google add

DFA Driver

 Assignment 5 :- DFA Driver 


#include<stdio.h>

#include<string.h>

struct DFA

{

int nos;

int nof[10];

char sym[10];

int delta [10][10];

}temp;

int i,j,n,m;


void accept()

{

printf("Enter number of states :\n");

scanf("%d",&temp.nos);

printf("Enter number of symbols:\n");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("Enter %d input symbol \n",i+1);

scanf("\n %c",&temp.sym[i]);

}

printf("Enter number of final states :\n");

scanf("%d",&m);

for(i=0;i<m;i++)

{

printf("Enter %d final state  \n",i+1);

  scanf("%d",&temp.nof[i]);

}

printf("Enter delta %d X %d matrix\n",temp.nos,n);

for(i=0;i<temp.nos;i++)

{

for(j=0;j<n;j++)

{

scanf("%d",&temp.delta[i][j]);

}

}

}



void display()

{

printf("Transition Table \n");

for(i=0;i<temp.nos;i++)

{

for(j=0;j<n;j++)

{

printf("%d \t",temp.delta[i][j]);

}

printf("\n");

}

}

int execute()

{

int curr=0,i,j;

char s[10];

printf("Enter the string\n");

scanf("%s",s);

for(i=0;s[i]!='\0';i++)

{

for(j=0;j<temp.nos;j++)

{

if(temp.sym[j]==s[i])

break;

}

if(j==n)

{

printf("INVALID INPUT %c",s[i]);

return 0;

}

curr=temp.delta[curr][j];

}


for(i=0;i<m;i++)

{

if(temp.nof[i]==curr)

break;

}

if(i==m)

printf("\nString Rejected\n");

else

printf("\n String Accepted\n");

return 1;

}


void main()

{

int choice;

do

{

printf("1.Accept\n2.Print Delta transition\n3.Check String\n4.Exit\n");

scanf("%d",&choice);

switch(choice)

{

case 1:accept();

break;


case 2:display();

break;


case 3:execute();

break;


case 4:exit(0);

default:printf("Invalid Choice\n");

}

}

while(choice!=4);

}

Post a Comment

0 Comments