lecture-7.md (3361B)
1 +++ 2 title = "Lecture 7" 3 template = "page-math.html" 4 +++ 5 6 # Pushdown automata 7 8 Unlike NFA, the PDA has memory -- a stack of unlimited size. 9 10 Stack alphabet represented by Γ. Elements can be pushed/popped from the 11 stack. Initially, stack contains stack start symbol z ∈ Γ. 12 13 On transition, read topmost element of stack, and exchange with zero or 14 more new elements. 15 16 When NPDA reads words, need to keep track of current state, remaining 17 input, current stack. 18 19 Transition (q', v) ∈ δ(q, α, b) means that from state q with input α w 20 and stack bu, the automaton can go to state q' with input w and stack 21 vu. In other words, $(q, \alpha w, bu) \vdash (q', w, vu)$. 22 23 Transition graph for NPDA contains, for every (q', v) ∈ δ(q, α, b), an 24 arrow $q \xrightarrow{\alpha[b/v]} q'$. This reads as "if b is on the top of the 25 stack, transition from state q to state q', reading an α of input, and replacing 26 the b on the top of the stack with v". 27 28 Deterministic PDA: 29 30 - δ(q, α, b) contains max one element 31 - if δ(q, λ, b) ≠ ∅, then δ(q, a, b) = ∅ for every a ∈ Σ 32 33 Deterministic context-free language allows for efficient parsing. 34 35 Not all context-free languages are deterministic context-free. 36 37 It is decidable if two DPDAs generate same language. 38 39 Language L is context free ⇔ there exists NPDA for that language. 40 41 ## CFG to NPDA 42 43 Simulate leftmost derivations on stack. 44 45 Construct NPDA with: 46 47 - 3 states, one of which is final 48 - the stack alphabet containing variables, terminals, and z (stack 49 start symbol) 50 51 Construct the PDA: 52 53 1. Start with lambda transition that takes z on top of stack, and 54 replaces it with the symbol. 55 2. Simulate leftmost reductions for every rule. A rule is a λ 56 transition that takes a a symbol on the stack, and replaces it with 57 the expansion of the symbol. 58 - e.g. rules S → aSa \| aa 59 - two lambda transitions (loops): 60 - q<sub>1</sub> to q<sub>1</sub>, taking an S on the stack, and replacing it 61 with aSa 62 - q<sub>1</sub> to q<sub>1</sub>, taking an S on the stack, and replacing it 63 with aa 64 3. Add transitions for consuming terminals. For every terminal, add 65 transition that consumes the terminal, and replaces the terminal on 66 the stack with λ. 67 4. Add a lambda transition to final state, that takes z on the top of 68 the stack and replaces with λ. 69 70 ## NPDA to CFG 71 72 Assume there is one final state, which is reachable only with empty 73 stack. If not, make it so. 74 75 Define CFG with: 76 77 - terminals being the alphabet 78 - variables being (q b q') where q,q' are states and b ∈ Γ 79 - $(q b q') \Rightarrow<sup>{+} w \Leftrightarrow (q, w, b) \vdash</sup>{+} (q', \lambda, \lambda)$ 80 - build the productions with rules: 81 - if there's an arrow $q \xrightarrow{\alpha [b/\lambda]} q'$ 82 - add production (q b q') → α 83 - in Joerg terms: if $(q, \alpha w, b) \vdash (q', w, \lambda)$, or if (q', λ) ∈ δ(q, α, b) 84 - if there's an arrow $q \xrightarrow{\alpha[b/c_{1}...c_{n}]} q'$ 85 - add production $(q b r_{n}) \rightarrow \alpha (q' c_{1} r_{1})(r_{1} c_{2}r_{2})...(r_{n-1}c_{n}r_{n})$ 86 - for all states r<sub>1</sub>...r<sub>n</sub> 87 - in Joerg terms: if $(q, \alpha w, b) \vdash (q', w, c_{1}...c_{n})$, or if (q', c<sub>1</sub>...c<sub>n</sub>) ∈ δ (q, α, b)