250 likes | 414 Views
Pushdown Automata – Equivalence to CFGs. Lecture 19 Section 2.2 Wed, Oct 3, 2007. Equivalence of PDAs and CFGs. Theorem : A language is context-free if and only if it is accepted by some PDA. Equivalence of PDAs and CFGs. Proof ( ): We are given a CFG G and we will construct a PDA M .
E N D
Pushdown Automata – Equivalence to CFGs Lecture 19 Section 2.2 Wed, Oct 3, 2007
Equivalence of PDAs and CFGs • Theorem: A language is context-free if and only if it is accepted by some PDA.
Equivalence of PDAs and CFGs • Proof (): • We are given a CFG G and we will construct a PDA M. • Let the PDA have three states: Q = {q0, q1, q2}. • q0 is the start state. • q2 is the accept state.
Equivalence of PDAs and CFGs • The transitions are • (q0, , ) = {(q1, $)} • (q1, , $) = {(q2, )} • (q1, , A) = {(q1, w)}, where A w is a rule. • (q1, a, a) = {(q1, )}, where a .
Equivalence of PDAs and CFGs • Proof (): • Given a PDA M, we must construct a grammar G that generates L(M). • Modify M so that • M has a single accept state. • M empties its stack before accepting. • Each transition either pushes one symbol or pops one symbol, but not both.
The Variables • For every pair of states p and q in M, we create a variable Apq. • The variable Apq is interpreted to mean “get from state p with an empty stack to state q with an empty stack.” • Since the stack starts and ends empty, we must push a symbol at the beginning and pop a symbol at the end.
The Grammar Rules • There are two possibilities in going from p to q. • The first symbol pushed does not match the last symbol popped. • The first symbol pushed matches the last symbol popped.
The Grammar Rules • In the first case, we write the grammar rule ApqAprArq where r is an intermediate state, reached when the stack is empty.
The Grammar Rules • In the second case, we write the grammar rule ApqaArsb where • a is the first symbol read, • b is the last symbol read, • r is the next state after p, • s is the last state before q.
The Grammar Rules • Finally, we add the rules App for every state p.
The Start Symbol • Let qstart be the start state and qaccept be the accept state. • Then the start symbol is Aqstartqaccept. • If we can eventually replace the start symbol with all terminals, then the string of terminals must be in the language.
a, ; a , ; $ , $; p q r b, a; Example • Find a grammar for the PDA
Example • The variables are App, Apq, Apr, Aqp, Aqq, Aqr, Arp, Arq, and Arr.
The Grammar Rules (Group 1) • We get the first group of rules: • AppAppApp • AppApqAqp • AppAprArp • ApqAppApq • ApqApqAqq • ApqAprArq • AprAppApr • AprApqAqr • AprAprArr
The Grammar Rules (Group 1) • AqpAqpApp • AqpAqqAqp • AqpAqrArp • AqqAqpApq • AqqAqqAqq • AqqAqrArq • AqrAqpApr • AqrAqqAqr • AqrAqrArr
The Grammar Rules (Group 1) • ArpArpApp • ArpArqAqp • ArpArrArp • ArqArpApq • ArqArqAqq • ArqArrArq • ArrArpApr • ArrArqAqr • ArrArrArr
The Grammar Rules (Group 2) • Then we get the second group of grammar rules, based on the transitions. • (p, , ) = (q, $) • (q, a, ) = (q, a) • (q, b, a) = (q, ) • (q, , $) = (r, )
The Grammar Rules (Group 2) • For every stack symbol s and for every pair of transitions, one of which pushes s and the other of which pops s, we write a grammar rule AxyaAzwb where a is the symbol read when s is pushed and b is the symbol read when s is popped.
The Grammar Rules (Group 2) • The pair (p, , ) = (q, $) (q, , $) = (r, ) gives the rule AprAqq = Aqq
The Grammar Rules (Group 2) • The pair (q, a, ) = (q, a) (q, b, a) = (q, ) gives the rule AqqaAqqb
The Grammar Rules (Group 3) • Finally, add the rules • App • Aqq • Arr
The Grammar Rules • This gives us the grammar rules • AppAppApp • ApqAppApq • ApqApqAqq • AprAppApr • AprApqAqr • AprAprArr • AqqAqqAqq
The Grammar Rules • AqrAqqAqr • AqrAqrArr • ArrArrArr • AprAqq • AqqaAqqb • App • Aqq • Arr
The Grammar • The start symbol is Apr.