0% found this document useful (0 votes)
37 views4 pages

Program

The program takes as input the number of states in a non-deterministic finite automaton (NFA) and its transition function. It constructs the epsilon-closure of each state to obtain the deterministic finite automaton (DFA) equivalent to the given NFA. The epsilon-closure of a state contains all states reachable from it through epsilon transitions. The DFA states are represented by the epsilon-closures, and the transition function is obtained by applying the NFA transitions to each symbol in the epsilon-closures. The resulting DFA is printed.

Uploaded by

murumasi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views4 pages

Program

The program takes as input the number of states in a non-deterministic finite automaton (NFA) and its transition function. It constructs the epsilon-closure of each state to obtain the deterministic finite automaton (DFA) equivalent to the given NFA. The epsilon-closure of a state contains all states reachable from it through epsilon transitions. The DFA states are represented by the epsilon-closures, and the transition function is obtained by applying the NFA transitions to each symbol in the epsilon-closures. The resulting DFA is printed.

Uploaded by

murumasi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM:

#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char nfa[50][50],s[20],st[10][20],eclos[20],input[20];
int x,e,top=0,topd=0,n=0,ns,nos,in;
int checke(char a)
{
int i;
for(i=0;i<e;i++)
{
if(eclos[i]==a)
return i;
}
return -1;
}
int check(char a)
{
int i;
for(i=0;i<in;i++)
{
if(input[i]==a)
return i;
}
return -1;
}
void push(char a)
{
s[top]=a;
top++;
}
char pop()
{
top--;
return s[top];
}
void pushd(char *a)
{
strcpy(st[topd],a);
topd++;
}
char *popd()
{
topd--;
return st[topd];
}
int ctoi(char a)

{
int i=a-48;
return i;
}
char itoc(int a)
{
char i=a+48;
return i;
}
char *eclosure(char *a)
{
int i,j;
char c;
for(i=0;i<strlen(a);i++)
push(a[i]);
e=strlen(a);
strcpy(eclos,a);
while(top!=0)
{
c=pop();
for(j=0;j<ns;j++)
{
if(nfa[ctoi(c)][j]=='e')
{
if(check(itoc(j))==-1)
{
eclos[e]=itoc(j);
push(eclos[e]);
e++;
}
}}}
eclos[e]='\0';
return eclos;
}
int main()
{
int i,j,k,count;
char ec[20],a[20],b[20],c[20],dstates[10][10];
printf("\n\nEnter the number of states\n");
scanf("%d",&ns);
for(i=0;i<ns;i++)
{
for(j=0;j<ns;j++)
{
printf("Move[%d][%d]",i,j);
nfa[i][j]=getche();
printf("\n");
if(nfa[i][j]!='-'&&nfa[i][j]!='e')
{
if((check(nfa[i][j]))==-1)

input[in++]=nfa[i][j];
}
}
}
topd=0;
nos=0;
c[0]=itoc(0);
c[1]='\0';
pushd(eclosure(c));
strcpy(dstates[nos],eclosure(c));
printf("\nDFA is:\n");
for(x=0;x<in;x++)
printf("\t%c",input[x]);
printf("\n");
while(topd>0)
{
strcpy(a,popd());
printf("%s",a);
for(i=0;i<in;i++)
{
int len=0;
for(j=0;j<strlen(a);j++)
{
int x=ctoi(a[j]);
for(k=0;k<ns;k++)
{
if(nfa[x][k]==input[i])
ec[len++]=itoc(k);
}
}
ec[len]='\0';
strcpy(b,eclosure(ec));
count=0;
for(j=0;j<=nos;j++)
{
if(strcmp(dstates[j],b)==0)
count++;
}
if(count==0)
{
if(b[0]!='\0')
{
nos++;
pushd(b);
strcpy(dstates[nos],b);}}
printf("\t%s",b);}
printf("\n");
}
getch();
}

OUTPUT:
Enter the number of states:3
Move[0][0]1
Move[0][1]2
Move[0][2]3
Move[1][0]1
Move[1][1]2
Move[1][2]3
Move[2][0]3
Move[2][1]2
Move[2][2]1
DFA is:
1 2
0 0 1
2 2 1
1 0 1

3
2
0
2

You might also like