Equivalence of CFGs and PDAs What does is mean to say that Context-Free Grammars and Pushdown Automata are equivalent? they recognize the same class of languages (context-free) How do you show that Context-Free Grammars and Pushdown Automata are equivalent? given a CFG, construct a PDA given a PDA, construct a CFG (we'll do the first construction, skip the second) How do you convert a Context-Free Grammar to an equivalent Pushdown Automaton? build a machine that simulates derivations Suppose one of the steps in a leftmost derivation is uAv => uwv, where A is a nonterminal and u, v, and w are strings of symbols. What is stored on the PDA stack? the string uAv What does the machine do with the string u? What does the machine do when a terminal is on top of the stack? pop the terminal and check that it matches the input (u contains only terminals because the derivation is leftmost) Once the symbol A is on the top of the stack, how does the machine simulate the application of the production (A -> w)? What does the machine do when a nonterminal is on top of the stack? pop the symbol A off the stack push the symbols in w on the stack What does the machine initially push on the stack? the start symbol When does the machine accept? when the stack is empty and the input is consumed The machine can simulate many derivations that don't match the input. How does the machine know which derivation to simulate so that the string generated by the derivation matches the machines input? the machine uses nondeterminism How does this type of PDA compare to a table-driven parser? Convert the grammar to an equivalent PDA. S -> a A A -> a A | b B B -> b B | b What are the states in the machine? start loop accept What are the transitions in the machine? push the start symbol (start, e, e) -> (loop, S$) check for empty stack (loop, e, $) -> (accept, e) for each rule A -> w (loop, e, A) -> (loop, w) for each terminal a (loop, a, a) -> (loop, e) But wait! What's wrong with this machine? How can you justify that this is equivalent to a standard PDA? a single transition pushing multiple symbols can be implemented with a sequence of states pushing one symbol Classwork (You may work with a partner.) Convert the grammar to an equivalent PDA. S -> aSb | SS | epsilon