# Context-free grammar to a pushdown automaton

I'm trying to convert a context free grammar to a pushdown automaton (PDA); I'm not sure how I'm gonna get an answer or show you my progress as it's a diagram... Anyway this is the last problem I have on a homework that's due later today, so I'd appreciate some kind of help, even if it's just an explanation of the correct answers diagram. I need a PDA corresponding to this CFG:

$$S \rightarrow aSa | bSb | B$$ $$B \rightarrow bB | \epsilon$$

I know it will have to push X every time 'a' is read before a 'b', and pop X every time 'a' is read after a 'b'. But I'm not sure how to arrange the PDA in order to tell which a's came after b's. Also, I'm unsure of how to deal with the b's in terms of the stack, as there can be as many in the middle of the string as you want. Help appreciated.

Thanks, Pachun

• The singular form of "automata" is "automaton". Jul 15 '12 at 2:42
• You ask us to solve your homework in time for the hand-in, seriously? Anyway, there are simple standard algorithms for this conversion which were either presented in lecture or can be found in your textbook; check the proof(s) that CFG and NPDA have the same power.
– Raphael
Jul 18 '12 at 0:49

This is basically the language of all palindromes over $\{a, b\}$ which can contain an arbitrary number of $b$'s in the middle. First, you could build a PDA for palindromes. This is a fairly standard language, so you should be able to find plenty to get you started. Then, you need to add a way to read an arbitrary number of $b$'s after you're done pushing new symbols to the stack, and before you start popping symbols. So the basic steps here:
1. Read $a$'s and $b$'s and push to the stack;
2. Read any number of $b$'s;
3. Read $a$'s and $b$'s and pop from the stack.