Pushdown automata(pda) that accept a language a^nb^2n(L=a^nb^2n where n>0) c code
L={a^nb^2n where n>0}
String accept by language
={abb,aabbbb,aaabbbbbb.....................................}
#include<stdio.h>
#include<string.h>
int dfa=0;
char s[10];
int top=-1;
char item;
void push(char item)
{
top=top+1;
s[top]=item;
}
void pop()
{
top=top-1;
}
void trasition0(char c)
{
if(c=='a')
{
push(c);
dfa=0;
}
else if(c=='b')
{
dfa=1;
}
else
dfa=-1;
}
void trasition1(char c)
{
if(c=='b'&& s[top]=='a')
{
pop();
dfa=2;
}
else
dfa=-1;
}
void trasition2(char c)
{
if(c=='b'&&s[top]=='a')
{
dfa=1;
}
else if(c=='\0'&&s[top]=='$')
dfa=3;
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 if(dfa==2)
trasition2(str[i]);
else
return 0;
}
if(dfa==3)
return 1;
else
return 0;
}
int main()
{
char str[100];
printf("enter string");
scanf("%s",str);
push('$');
if(isaccepted(str))
{
printf("accepted");
}
else
{
printf("not accepted");
}
}
Output
enter stringaabbbb
accepted
enter stringabbbbb
not accepted
0 Comments