Dfa that accept a regular expression (a+ ) C code
Regular expression=a+
String accepted by regular expression
={a,aa,aaa,aaaa,aaaaa,aaaaaa,aaaaaaa..................................}
#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
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==1)
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");
}
}
0 Comments