Ticker

6/recent/ticker-posts

2PDA that accept a language {L=a^nb^nc^n where n>0}C code

 

2PDA that accept a language {L=a^nb^nc^n where n>0}C code

L=a^nb^nc^n

String accepted by this language is

=[abc,aabbcc,aaabbbccc,aaaabbbbcccc.................................]


#include<stdio.h>

#include<string.h>

int dfa=0;

char s1[10];

char s2[10];

int top=-1;

int t=-1;

char item;

void push(char item)

{

    top=top+1;

    s1[top]=item;

}

void pop()

{

    top=top-1;

}

void insert(char item)

{

    t=t+1;

    s2[t]=item;

}

void delete()

{

    t=t-1;

}

void trasition0(char c)

{

if(c=='a')

{

push(c);    

dfa=0;

}

else if(c=='b')

{

if(s1[top]=='a')

{

    pop();

    insert(c);

dfa=1;

}

}

else

dfa=-1;

}

void trasition1(char c)

{

if(c=='b'&& s1[top]=='a')

{

pop();

insert(c);

dfa=1;

}

else if(c=='c')

{

    delete();

    dfa=2;

}

else

dfa=-1;

}

void trasition2(char c)

{

    if(c=='c')

    {

        delete();

        dfa=2;

    }

    else if(c=='\0'&&s2[t]=='$')

    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('$');

insert('$');

if(isaccepted(str))

{

printf("accepted");

}

else

{

printf("not accepted");

}


Post a Comment

0 Comments