1 / 24

Pushdown Automata – Equivalence to CFGs

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 .

cara
Download Presentation

Pushdown Automata – Equivalence to CFGs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pushdown Automata – Equivalence to CFGs Lecture 19 Section 2.2 Wed, Oct 3, 2007

  2. Equivalence of PDAs and CFGs • Theorem: A language is context-free if and only if it is accepted by some PDA.

  3. 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.

  4. 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  .

  5. 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.

  6. 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.

  7. 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.

  8. The Grammar Rules • In the first case, we write the grammar rule ApqAprArq where r is an intermediate state, reached when the stack is empty.

  9. The Grammar Rules • In the second case, we write the grammar rule ApqaArsb 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.

  10. The Grammar Rules • Finally, we add the rules App for every state p.

  11. 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.

  12. a, ; a , ; $ , $;  p q r b, a;  Example • Find a grammar for the PDA

  13. Example • The variables are App, Apq, Apr, Aqp, Aqq, Aqr, Arp, Arq, and Arr.

  14. The Grammar Rules (Group 1) • We get the first group of rules: • AppAppApp • AppApqAqp • AppAprArp • ApqAppApq • ApqApqAqq • ApqAprArq • AprAppApr • AprApqAqr • AprAprArr

  15. The Grammar Rules (Group 1) • AqpAqpApp • AqpAqqAqp • AqpAqrArp • AqqAqpApq • AqqAqqAqq • AqqAqrArq • AqrAqpApr • AqrAqqAqr • AqrAqrArr

  16. The Grammar Rules (Group 1) • ArpArpApp • ArpArqAqp • ArpArrArp • ArqArpApq • ArqArqAqq • ArqArrArq • ArrArpApr • ArrArqAqr • ArrArrArr

  17. 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, )

  18. 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 AxyaAzwb where a is the symbol read when s is pushed and b is the symbol read when s is popped.

  19. The Grammar Rules (Group 2) • The pair (p, , ) = (q, $) (q, , $) = (r, ) gives the rule AprAqq = Aqq

  20. The Grammar Rules (Group 2) • The pair (q, a, ) = (q, a) (q, b, a) = (q, ) gives the rule AqqaAqqb

  21. The Grammar Rules (Group 3) • Finally, add the rules • App • Aqq • Arr

  22. The Grammar Rules • This gives us the grammar rules • AppAppApp • ApqAppApq • ApqApqAqq • AprAppApr • AprApqAqr • AprAprArr • AqqAqqAqq

  23. The Grammar Rules • AqrAqqAqr • AqrAqrArr • ArrArrArr • AprAqq • AqqaAqqb • App • Aqq • Arr

  24. The Grammar • The start symbol is Apr.

More Related