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

Q2 V 1

Uploaded by

workerchegg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Q2 V 1

Uploaded by

workerchegg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
#include<stdlib.h>

struct node{
int st;
struct node * link;
};
struct node1
{
int nst[20];
};

int findalpha(char c);


int compare(struct node1 a,struct node1 b);

int noalpha,nostate,start,nofinal,notransitions;
char alphabets[20];
int finalstate[20];
struct node * tranition[20][20]={NULL}; //Linked List
struct node1 hash[20];
int set[20];
int complete;

void insert(int r,char ch,int s)


{
struct node* temp;
int j=findalpha(ch);
if(j==999)
{
printf("Error");
exit(0);
}
temp=(struct node *)malloc(sizeof(struct node));
temp->st=s;
temp->link=tranition[r][j];
tranition[r][j]=temp;

int findalpha(char c)
{
for(int i=0;i<noalpha;i++)
{
if(alphabets[i]==c)
{
return i;
}
}
return 999;
}

int insertstate(struct node1 newstate)


{
for(int i=0;i<=complete;i++)
{
if(compare(hash[i],newstate))
return 0;

}
complete++;
hash[complete]=newstate;
return 1;
}

int compare(struct node1 a,struct node1 b)


{
for(int i=0;i<=nostate;i++)
{
if(a.nst[i]!=b.nst[i])
return 0;
}
return 1;
}

void printstate(struct node1 state)


{
printf("{");
for(int j=1;j<=nostate;j++)
{
if(state.nst[j]!=0)
{
printf("q%d,",state.nst[j]);
}
}
printf("\n");
}

int main()
{
struct node *temp;
struct node1 newstate={0},tempstate={0};

printf("Enter number of alphabets :");


scanf("%d",&noalpha);
printf("Enter alphabets: ");
for (int i = 0; i < noalpha; i++) {
scanf(" %c", &alphabets[i]); // Note the space before %c
}

printf("Enter Number of states : ");


scanf("%d",&nostate);
getchar();
printf("Enter start state : ");
scanf("%d",&start);
printf("Enter number of final states: ");
scanf("%d",&nofinal);
printf("Enter final states");
for(int i=0;i<nofinal;i++)
scanf("%d",&finalstate[i]);
printf("Enter number of Transitions : ");
scanf("%d",&notransitions);
int r,s;
char ch;
for(int i=0;i<notransitions;i++)
{
scanf("%d %c %d",&r,&ch,&s);
insert(r,ch,s);
}

//initialising hash
//first node1
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
hash[i].nst[j]=0;
}
}

//difficult part
complete=-1;
int i=-1;
printf("\nEquivalent DFA .... \n");
printf(" ...................... \n");
printf("Trnsitions of DFA\n");
//second node1 = newstate, 3rd =tempsate init with {0} in main
newstate.nst[start]=start;
insertstate(newstate);

//while loop

while (i!=complete)
{
i++;
newstate=hash[i];

//loops
for(int k=0;k<noalpha;k++)
{
int c=0;
for(int j=1;j<=nostate;j++)
{
//int array
set[j]=0;
}
for(int j=1;j<=nostate;j++)
{
int l=newstate.nst[j];
if(l!=0)
{
//node pointer in main
temp=tranition[l][k];
while (temp!=NULL)
{
if(set[temp->st]==0)
{
c++;
set[temp->st]=temp->st;
}
temp=temp->link;
}

}
}
printf("\n");
if(c!=0)
{
for(int m=1;m<=nostate;m++)
{
tempstate.nst[m]=set[m];
}
insertstate(tempstate);
printstate(newstate);
printf("%c\t",alphabets[k]);
printstate(tempstate);
printf("\n");
}
else
{
printstate(newstate);
printf("%c\t",alphabets[k]);
//printstate(tempstate);
printf("NULL\n");

}
}
}

You might also like