How can I convert the next CFG to a PDA?
S-> abScB | e
B-> bB | b
How can I convert the next CFG to a PDA?
S-> abScB | e
B-> bB | b
I would first look at the language the CFG is generating. It's a pretty complex language actually. With only one expansion of S it'll be abc(b(b*))
. For two, you'll get ab[abc(b(b*))]c(b(b*))
, three will be ab[ab[abc(b(b*))]c(b(b*))]c(b(b*))
and so on. I've put in square braces the part that's added from the S transition. This language appears to be {(ab)^n (c(b(b^x_i)))^m}
where x_i
is an integer greater than or equal to 0, and each x_1, x_2, ... , x_i
can be different. n and m are both integers greater than or equal to 0.
To convert to a PDA, we can start with the easy parts. Your alphabet is {a,b,c}, you'll need a state for the "ab" section, and one for the "c(b(b^x_i)" part. Let's call the first one state p and the second q. Your stack alphabet will be {bottom,A,C}. Bottom is simply the symbol that indicates we've reached the bottom of the stack. Now the hard part, the delta transitions, assuming acceptance by empty stack:
(p,e,bottom),(p,e) - this is for "m" = 0, accepting the empty string "e" or the case (ab)^n (this is an accepting state)
(p,a,bottom),(p, A bottom) - if we read an "a" and the stack is empty, push an A to the stack
(p,b,A),(p,e) - if we get a "b" and there was already an A on the stack, remove it
(p,c,bottom),(q,C bottom) - if we get a "c" and the stack was empty (i.e. we've finished the (ab)^n section), go to state q and push a C onto the stack
(q,b,C),(q,e) - if we get a "b" after a c, remove the C from the stack
(q,b,bottom),(q,bottom) - if we get a "b" in the b^x_i section, pop then immediately push bottom onto the stack
(q,c,bottom),(q,C bottom) - if we get a "c" after the b^x_i section, push a C onto the stack
(q,e,bottom),(q,e) - if we finish our string and our stack is empty (as it should be), then this is an accepting state
The above transitions that read an empty string and produce an empty stack are the accepting states. All the transitions that aren't allowed (e.g. getting a "c" when there's already a C on the stack) are not included.
Hope that helps!