Pushdownautomata(PDA) that accept a language L={WcW^r)where W={a,b} C code
String accepted by language L={WcW^r)where W={a,b}
={abcba,aca,bca,bcb,bacab,bbcbb,abcba.......................................}
#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'||c=='b')
{
push(c);
dfa=0;
}
else if(c=='c')
{
dfa=1;
}
else
dfa=-1;
}
void trasition1(char c)
{
if(c=='a'&& s[top]=='a')
{
pop();
dfa=1;
}
else if(c=='b'&&s[top]=='b')
{
pop();
dfa=1;
}
else if(c=='\0'&&s[top]=='$')
{
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);
push('$');
if(isaccepted(str))
{
printf("accepted");
}
else
{
printf("not accepted");
}
}
Output
enter stringabcba
accepted
enter stringabcab
not accepted
0 Comments