Created
October 11, 2018 10:01
-
-
Save AmalJossy/140a2944c56c9ccca8d105405d76f936 to your computer and use it in GitHub Desktop.
Convert given E-NFA to NFA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<malloc.h> | |
/****************/ | |
//Author: Amal Jossy | |
//Description: Convert given E-NFA to NFA | |
//input format - | |
//row 1 : no of transitions | |
//rows>1: transitions "qi input qj" | |
/*****************/ | |
typedef struct trans TRANS; | |
struct trans{ | |
int from; | |
char input; | |
int to; | |
TRANS *ptr; | |
}; | |
TRANS* newTRANS(TRANS* prev,int from,char input,int to){ | |
TRANS* new=(TRANS*)malloc(sizeof(TRANS)); | |
prev->ptr=new; | |
new->from=from; | |
new->input=input; | |
new->to=to; | |
new->ptr=NULL; | |
//printf("ADD (%d,%c)=>%d \n",from,input,to); | |
return new; | |
} | |
void searchAndReplicate(TRANS* head,int qi,int qj){ | |
TRANS *tail,*prev,*curr; | |
tail=head; | |
while(tail->ptr!=NULL){ | |
tail=tail->ptr; | |
} | |
//printf("DELETE (%d,E) => %d \n",qi,qj); | |
prev=head; | |
while(prev->ptr!=NULL){ | |
curr=prev->ptr; | |
if(curr->to==qi){ | |
tail=newTRANS(tail,curr->from,curr->input,qj); | |
} | |
prev=prev->ptr; | |
} | |
} | |
int main(){ | |
TRANS *head,*prev,*new,*curr; | |
int Nt,a,c,i; | |
char b; | |
scanf("%d",&Nt); | |
head=(TRANS*)malloc(sizeof(TRANS)); | |
prev=head; | |
prev->ptr=NULL; | |
for(i=0;i<Nt;i++){ | |
scanf("%d %c %d",&a,&b,&c); | |
prev=newTRANS(prev,a,b,c); | |
} | |
int qi,qj; | |
prev=head; | |
while(prev->ptr!=NULL){ | |
curr=prev->ptr; | |
if(curr->input=='E'){ | |
qi=curr->from; | |
qj=curr->to; | |
if(qi!=qj){ | |
searchAndReplicate(head,qi,qj); | |
} | |
prev->ptr=curr->ptr; | |
free(curr); | |
} | |
else{ | |
prev=prev->ptr; | |
} | |
} | |
prev=head; | |
printf("NFA Transitions \n----------\n"); | |
while(prev->ptr!=NULL){ | |
curr=prev->ptr; | |
printf("%d %c %d\n",curr->from,curr->input,curr->to); | |
prev=prev->ptr; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment