1 / 49

Pushdown Automata

Pushdown Automata. Lecture 9 Section 2.2 Wed, Oct 4, 2006. Grammars vs. Machines. Given a regular language, we can describe it using A regular grammar, or A DFA. A context-free language can be described by a context-free grammar. Can it be described by a machine?. Machines for CFLs.

thomasmatos
Download Presentation

Pushdown Automata

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 Lecture 9 Section 2.2 Wed, Oct 4, 2006

  2. Grammars vs. Machines • Given a regular language, we can describe it using • A regular grammar, or • A DFA. • A context-free language can be described by a context-free grammar. • Can it be described by a machine?

  3. Machines for CFLs • If a machine is to process the string anbn, then it must be able to “remember” the number of a’s. • We will use a stack to do this. • As each a is read, push it onto the stack. • When b is read, pop an a.

  4. Machines for CFLs • Each transition will include three parts: • The symbol read. • The symbol popped. • The symbol pushed. • (Each of these could be .)

  5. a,; a b, a;  , ;  Machine for {anbn | n 0} • First attempt:

  6. Machine for {anbn | n 0} • This machine has one shortcoming. • There are several we could define acceptance of a string. • They all involve the final “state” of the machine.

  7. Acceptance of Strings • Acceptance by final state: • Accept w if the final state is in F. • Acceptance by empty stack: • Accept w if the stack is empty. • Acceptance by final state and empty stack: • Accept w if the final state is in F and the stack is empty.

  8. Acceptance of Strings • We will use acceptance by final state. • Thus, our machine actually accepts {anbm | n m  0}.

  9. Testing for an Empty Stack • We need to check that the stack is empty after reading the last symbol. • To do this, we will use a bottom marker on the stack. • Let $ be the bottom marker.

  10. Testing for an Empty Stack • At the beginning, push $ onto the stack. • At the end, pop $ off the stack. • That guarantees that the stack is empty.

  11. a,; a b, a;  , ;  , ; $ , $;  Machine for {anbn | n 0}

  12. Pushdown Automata • Such a machine is called a pushdown automaton, or PDA.

  13. Definition of a PDA • A pushdown automaton is a 6-tuple {Q, , , , q0, F}, where • Q is a finite set of states. •  is a finite input alphabet. •  is a finite stack alphabet. •  : Q(Q) is the transition function. • q0Q is the start state. • F Q is the set of accept states.

  14. Example • The PDA that accepts {anbn | n 0} is • Q = {q0, q1, q2, q3} •  = {a, b} • = {a, $} • F = {q3}

  15. Example and  is given by • (q0, , ) = {(q1, $)} • (q1, a, ) = {(q1, a)} • (q1, , ) = {(q2, )} • (q2, b, b) = {(q2, )} • (q2, , $) = {(q3, )}

  16. Examples • Design a PDA that accept the following language. • {w | w contains an equal number of a’s and b’s}

  17. Examples • Let  = {a, b, c, +, *, (, )}. • Design a PDA that accepts {w | w contains balanced parentheses} • Design a PDA that accepts {w | w is a valid algebraic expression}

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

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

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

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

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

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

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

  25. 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 state just before q.

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

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

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

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

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

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

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

  33. 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) = (a, ) • (q, , $) = (r, )

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

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

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

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

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

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

  40. The Grammar • The start symbol is Apr.

  41. Simplifying the Grammar • We may simplify these grammar rules considerably. • Eliminate any rule involving a variable that never appears on the left. • Eliminate any rule involving a variable, other than the start symbol, that never appears on the right.

  42. Simplifying the Grammar • Eliminate any rule involving a variable that is purely recursive. • Eliminate any unnecessary unit rules. • Use common sense to eliminate any other rules that are useless.

  43. Simplifying the Grammar • The grammar rules are reduced to • AppAppApp |  • AprAppApr | AprArr | Aqq • AqqAqqAqq | aAqqb |  • ArrArrArr | 

  44. Simplifying the Grammar • Then we notice that the rules • AppAppApp |  • ArrArrArr |  are useless since the only other choice for App and Arr is . • We should eliminate them and replace App and Arr with  everywhere they occur.

  45. Simplifying the Grammar • Now the grammar rules are reduced to • AprApr | Apr | Aqq • AqqAqqAqq | aAqqb | 

  46. Simplifying the Grammar • Now it is obvious that the rule • AprApr is pointless, so eliminate it. • We now have • AprAqq • AqqAqqAqq | aAqqb | 

  47. Simplifying the Grammar • Now it is obvious that the unit rule • AprAqq serves no purpose. • Eliminate it, making Aqq the new start symbol. • We now have • AqqAqqAqq | aAqqb | 

  48. Simplifying the Grammar • We now have • AqqAqqAqq | aAqqb |  or, more simply, • SSS | aSb | 

  49. a,; a b, a;  , ;  , ; $ , $;  p q r s Exercise • Find a context-free grammar for the language of the PDA

More Related