Dfa that accept a regular expression (aa*b ) C code
Regular expression=aa*b
String accepted by regular expression
={ab,aab,aaab,aaaab,aaaaab..................................................}
#include<stdio.h>
#include<string.h>
int dfa=0;
char item;
void trasition0(char c)
{
if(c=='a')
{
dfa=1;
}
else
dfa=-1;
}
void trasition1(char c)
{
if(c=='a')
dfa=1;
else if(c=='b')
dfa=2;
else
dfa=-1;
}
int isaccepted(char str[])
{
int i;
int len=strlen(str);
for(i=0;i<len;i++)
{
if(dfa==0)
trasition0(str[i]);
else if(dfa==1)
trasition1(str[i]);
else
return 0;
}
if(dfa==2)
return 1;
else
return 0;
}
int main()
{
char str[100];
printf("enter string");
scanf("%s",str);
if(isaccepted(str))
{
printf("accepted");
}
else
{
printf("not accepted");
}
}
enter stringab
accepted
enter stringbaaaaaab
not accepted
0 Comments