PUSH DOWN AUTOMATA PDA By Mr R Karunamoorthi
PUSH DOWN AUTOMATA (PDA) By, Mr. R. Karunamoorthi, AP / CSE
Pushdown automata are machines to accept context-free languages. For a context-free grammar G, there is an equivalent pushdown automaton M to recognize the language generated by the grammar G.
Description of pushdown automaton (PDA) A PDA has an input tape, a finite control, and a stack. 0 0 0 1 0 0 0 (input tape) step 1: read 0, push 0, and move to right q 1 q 1 (finite control) 0 0 (stack) step 2: read 0, push 0, and move to right
There are 2 types of moves (Non-determinstic): 1. According to an input symbol, current state, and top symbol of the stack, a decision is made, then the input head is moved ahead one symbol. 2. Next move is decided by the current state and the top symbol of the stack only. The input symbol is not used. Hence the input head is not moved at all after the decision is made. This type of move is called ε-move that allows the PDA to manipulate the stack without reading input symbol.
Languages accepted by PDA's There are two ways to accept inputs: 1. The PDA accepts an input if after reading the input and the machine empty its stack. The set of inputs accepted by the PDA is the language accepted by empty stack. 2. Some states of the PDA are final states. The PDA accepts an input if the machine enters a final state. The set of inputs accepted by the PDA is the language accepted by final state.
Definition of PDA's A pushdown automaton M is a system (Q, Σ, Γ, δ, q 0, Z 0 , F), where 1. Q is a finite set of states; 2. Σ is an alphabet called the input alphabet; 3. Γ is an alphabet, called the stack alphabet; 4. q 0 in Q is the initial state; 5. Z 0 in Γ is a particular stack symbol called the start symbol; 6. F, a subset of Q, is the set of final states; 7. mapping δ: Q×(Σ∪{ε}) ×Γ → finite subset of Q×Γ*.
Moves: δ(q, a, Z) = {(p 1, r 1), (p 2, r 2), . . . , (pm, rm)}, where q and pi, 1≦i ≦ m, are states, a is in , Z is a stack symbol, and ri is in Γ*, 1≦i ≦ m, means PDA in state q, reading input symbol a with top symbol Z on the stack, can enter any of the state pi and replaces top symbol Z by ri, and advances reading head one symbol. Example: δ(q 1, 0, G) = {(q 1, BG)} 0 0 q 1 1 0 δ G R 0 0 1 q 1 0 B G R
-Moves: δ(q, , Z) = {(p 1, r 1), (p 2, r 2), . . . , (pm, rm)}, where q and pi, 1≦i ≦ m, are states, Z is a stack symbol, and ri is in Γ*, 1≦i ≦ m, means PDA in state q, without reading any input symbol with top symbol Z on the stack, can enter any of the state pi and replaces top symbol Z by ri, and the reading head remains at the same place. Example: δ(q 1, , G) = {(q 1, BG)} 0 0 q 1 1 0 δ G R 0 0 q 1 1 0 B G R
Let Lwwr = {ww. R | w is in (0+1)*} • CFG for Lwwr : S==> 0 S 0 | 1 S 1 | • PDA for Lwwr : • P : = ( Q, ∑, , δ, q 0, Z 0, F ) = ( {q 0, q 1, q 2}, {0, 1, Z 0}, δ, q 0, Z 0, {q 2})
Initial state of the PDA: PDA for Lwwr 1. 2. δ(q 0, 0, Z 0)={(q 0, 0 Z 0)} δ(q 0, 1, Z 0)={(q 0, 1 Z 0)} 3. 4. 5. 6. δ(q 0, 0, 0)={(q 0, 00)} δ(q 0, 0, 1)={(q 0, 01)} δ(q 0, 1, 0)={(q 0, 10)} δ(q 0, 1, 1)={(q 0, 11)} 7. 8. 9. δ(q 0, , 0)={(q 1, 0)} δ(q 0, , 1)={(q 1, 1)} δ(q 0, , Z 0)={(q 1, Z 0)} 10. 11. δ(q 1, 0, 0)={(q 1, )} δ(q 1, 1, 1)={(q 1, )} 12. δ(q 1, , Z 0)={(q 2, Z 0)} Stack top q 0 Z 0 First symbol push on stack Grow the stack by pushing new symbols on top of old (w-part) Switch to popping mode (boundary between w and w. R) Shrink the stack by popping matching symbols (w. R-part) Enter acceptance state 11
PDA as a state diagram δ(qi, a, X)={(qj, Y)} Current state Next input symbol qi Current stack top a, X / Y Stack Top Replacement (w/ string Y) qj Next state 12
PDA for Lwwr: Transition Diagram Grow stack ∑ = {0, 1} = {Z 0, 0, 1} Q = {q 0, q 1, q 2} 0, Z 0/0 Z 0 1, Z 0/1 Z 0 0, 0/00 0, 1/01 1, 0/10 1, 1/11 , Z 0/Z 0 q 0 Pop stack for matching symbols 0, 0/ 1, 1/ , Z 0/Z 0 , 0/0 , 1/1 q 1 , Z 0/Z 0 q 2 Go to acceptance Switch to popping mode This would be a non-deterministic PDA 13
Instantaneous descriptions: If δ(q, a, Z) = contains (p, ), we say (q, a , Z ) M(p, , ). Example: δ(q 1, 0, G) = {(q 1, BG)} 0 0 q 1 1 0 δ 0 G R 0 1 0 B G q 1 R For the above case, we have that (q 1, 010, GR) M(q 1, 01, BGR). Let be the reflexive and transitive closure of M.
PDA’s Instantaneous Description (ID) A PDA has a configuration at any given instance: (q, w, y) – q - current state – w - remainder of the input (i. e. , unconsumed part) – y - current stack contents as a string from top to bottom of stack If δ(q, a, X)={(p, A)} is a transition, then the following are also true: – (q, a, X ) |--- (p, , A) – (q, aw, XB ) |--- (p, w, AB) |--- sign is called a “turnstile notation” and represents one move |---* sign represents a sequence of moves 15
How does the PDA for Lwwr work on input “ 1111”? All moves made by the non-deterministic PDA (q 0, 1111, Z 0) (q 0, 111, 1 Z 0) (q 1, 1111, Z 0) Path dies… (q 0, 11 Z 0) (q 1, 111, 1 Z 0) (q 0, 1, 111 Z 0) (q 1, 11 Z 0) (q 0, , 1111 Z 0) (q 1, 1, 1 Z 0) (q 1, , 1111 Z 0) Path dies… (q 1, , 11 Z 0) (q 1, , Z 0) Path dies… (q 2, , Z 0) Acceptance by final state: = empty input AND final state 16
There are two types of PDAs that one can design: those that accept by final state or by empty stack Acceptance by… • PDAs that accept by final state: – For a PDA P, the language accepted by P, denoted by L(P) by final state, is: • {w | (q 0, w, Z 0) |---* (q, , A) }, s. t. , q F Checklist: - input exhausted? - in a final state? • PDAs that accept by empty stack: – For a PDA P, the language accepted by P, denoted by N(P) by empty stack, is: • {w | (q 0, w, Z 0) |---* (q, , ) }, for any q Q. Q) Does a PDA that accepts by empty stack need any final state specified in the design? Checklist: - input exhausted? - is the stack empty? 18
Example 2: language of balanced paranthesis Pop stack for matching symbols Grow stack (, Z 0 / ( Z 0 (, ( / ( ( , Z 0 / Z 0 q 0 ∑ = { (, ) } = {Z 0, ( } Q = {q 0, q 1, q 2} ), ( / , Z 0 / Z 0 ), ( / q 1 Switch to (, ( / ( ( popping mode (, Z / ( Z 0 0 , Z 0 / Z 0 q 2 Go to acceptance (by final state) when you see the stack bottom symbol To allow adjacent 19 blocks of nested paranthesis
Example 2: language of balanced paranthesis (another design) ∑ = { (, ) } = {Z 0, ( } Q = {q 0, q 1} (, Z 0 / ( Z 0 (, ( / ( ( ), ( / start , Z 0/ Z 0 q 0 , Z 0/ Z 0 q 1 20
Example: L of balanced parenthesis PDA that accepts by final state PF : (, Z 0 / ( Z 0 (, ( / ( ( ), ( / start q 0 , Z 0/ Z 0 q 1 An equivalent PDA that accepts by empty stack (, Z 0 / ( Z 0 PN : (, ( / ( ( ), ( / , Z 0 / start , Z 0/ Z 0 q 0 How will these two PDAs work on the input: ( ( ( ) ) ( ) 21
PDAs accepting by final state and empty stack are equivalent • PF <= PDA accepting by final state – PF = (QF, ∑, , δF, q 0, Z 0, F) • PN <= PDA accepting by empty stack – PN = (QN, ∑, , δN, q 0, Z 0) • Theorem: – (PN==> PF) For every PN, there exists a PF s. t. L(PF)=L(PN) – (PF==> PN) For every PF, there exists a PN s. t. L(PF)=L(PN) 23
How to convert an empty stack PDA into a final state PDA? PN==> PF construction • Whenever PN’s stack becomes empty, make PF go to a final state without consuming any addition symbol. new state pf is needed. • To detect empty stack in PN: PF pushes a new stack symbol X 0 (not in of PN) initially before simulating PN • New start state p 0 in Pf that pushes Z 0 onto TOS and goes to PN PF : New p 0 start , X 0 / X 0 PN PN: , X 0/Z 0 X 0 q 0 , X 0/ X 0 … , X 0/ X 0 pf , X 0/ X 0 PN = (QN, ∑, , δN, q 0, Z 0) PF = (QN U {p 0, pf}, ∑, U {X 0}, δF, p 0, X 0, {pf}) 24
Example: Matching parenthesis “(” “)” PN: ( {q 0}, {(, )}, {Z 0, Z 1}, δN, q 0, Z 0 ) δN: δN(q 0, (, Z 0) = { (q 0, Z 1 Z 0) } δN(q 0, (, Z 1) = { (q 0, Z 1 Z 1) } δN(q 0, ), Z 1) = { (q 0, ) } δN(q 0, , Z 0) = { (q 0, ) } Pf: ( {p 0, q 0 , pf}, {(, )}, {X 0, Z 1}, δf, p 0, X 0 , pf) δf: δf(p 0, , X 0) = { (q 0, Z 0) } δf(q 0, (, Z 0) = { (q 0, Z 1 Z 0) } δf(q 0, (, Z 1) = { (q 0, Z 1 Z 1) } δf(q 0, ), Z 1) = { (q 0, ) } δf(q 0, , Z 0) = { (q 0, ) } δf(p 0, , X 0) = { (pf, X 0 ) } (, Z 0 /Z 1 Z 0 (, Z 1 /Z 1 Z 1 ), Z 1 / , Z 0 / (, Z 0/Z 1 Z 0 (, Z 1/Z 1 Z 1 ), Z 1/ , Z 0/ start q 0 Accept by empty stack , X 0/Z 0 X 0 q 0 p 0 , X 0/ X 0 Accept by final state pf 25
How to convert an final state PDA into an empty stack PDA? PF==> PN construction • Main idea: – Whenever PF reaches a final state, just make an -transition into a new end state, clear out the stack and accept – Danger: What if PF design is such that it clears the stack midway without entering a final state? to address this, add a new start symbol X 0 (not in of PF) PN = (Q U {p 0, pe}, ∑, U {X 0}, δN, p 0, X 0) PN: New start , X 0/Z 0 X 0 p 0 q 0 … , any/ p e , any/ PF 26
Equivalence of PDAs and CFGs 27
CFGs == PDAs ==> CFLs PDA by final state ≡ PDA by empty stack ? CFG 28
This is same as: “implementing a CFG using a PDA” Converting CFG to PDA Main idea: The PDA simulates the leftmost derivation on a given w, and upon consuming it fully it either arrives at acceptance (by empty stack) or non-acceptance. w PDA (acceptance by empty stack) reject OUTPUT INPUT accept implements CFG 29
Main idea: The PDA simulates the leftmost derivation on a given w, and upon consuming it fully it either arrives at acceptance (by empty stack) or non-acceptance. Steps: 1. Push the right hand side of the production onto the stack, with leftmost symbol at the stack top 2. If stack top is the leftmost variable, then replace it by all its productions (each possible substitution will represent a distinct path taken by the non-deterministic PDA) 3. If stack top has a terminal symbol, and if it matches with the next symbol in the input string, then pop it State is inconsequential (only one state is needed) 30
Formal construction of PDA from CFG Note: Initial stack symbol (S) same as the start variable in the grammar • Given: G= (V, T, P, S) • Output: PN = ({q}, T, V U T, δ, q, S) • δ: Before: – For all A V , add the following transition(s) in the PDA: After: • δ(q, , A) = { (q, ) | “A ==> ” P} A … … – For all a T, add the following transition(s) in the PDA: Before: a a… a pop … … • δ(q, a, a)= { (q, ) } After: 31
Example: CFG to PDA 1, 1 / 0, 0 / , A / 01 , A / A 1 , A / 0 A 1 , S / AS • G = ( {S, A}, {0, 1}, P, S) • P: – S ==> AS | – A ==> 0 A 1 | 01 • PDA = ({q}, {0, 1, A, S}, δ, q, S) • δ: – – δ(q, , S) = { (q, AS), (q, )} δ(q, , A) = { (q, 0 A 1), (q, 01) } δ(q, 0, 0) = { (q, ) } δ(q, 1, 1) = { (q, ) } , S / S q How will this new PDA work? Lets simulate string 0011 32
Simulating string 0011 on the new PDA … Leftmost deriv. : PDA (δ): δ(q, , S) = { (q, AS), (q, )} δ(q, , A) = { (q, 0 A 1), (q, 01) } δ(q, 0, 0) = { (q, ) } δ(q, 1, 1) = { (q, ) } 1, 1 / 0, 0 / , A / 01 , A / A 1 , A / 0 A 1 , S / AS S => AS => 0 A 1 S => 0011 , S / S Stack moves (shows only the successful path): S A S 0 A 1 S 0 S =>AS =>0 A 1 S =>0011 S 0 1 1 S q 1 1 S S 0 1 1 Accept by empty stack => 0011 33
Example 2 L ={anbn | n≧ 1} is a context-free language. L = L(G), G = (V, T, P, S) a CFG in Greiback normal form, where S a. SB, S a. B, B b. Let M = ({q}, T, V, δ, q, S , {}), a PDA, where δ(q, a, S) contains (q, SB), where S a. SB is in P, δ(q, a, S) contains (q, B), where S a. B is in P, δ(q, b, B) contains (q, ), where B b is in P.
a a b b δ(q, a, S) : = (q, SB) a a b b S q q S B δ(q, a, S) : = (q, B) a a b b B q δ(q, b, B) : = (q, ) a b b q B δ(q, b, B) : = (q, ) a a B
Converting a PDA into a CFG Main idea: Reverse engineer the productions from transitions If δ(q, a, Z) => (p, Y 1 Y 2 Y 3…Yk): • 1. State is changed from q to p; 2. Terminal a is consumed; 3. Stack top symbol Z is popped and replaced with a sequence of k variables. – Action: Create a grammar variable called “[q. Zp]” which includes the following production: – – [q. Zp] => a[p. Y 1 q 1] [q 1 Y 2 q 2] [q 2 Y 3 q 3]… [qk-1 Ykqk] Proof discussion (in the book) 37
Example: Bracket matching PDA to CFG • To avoid confusion, we will use b=“(“ and e=“)” PN: ( {q 0}, {b, e}, {Z 0, Z 1}, δ, q 0, Z 0 ) 1. 2. 3. 4. δ(q 0, b, Z 0) = { (q 0, Z 1 Z 0) } δ(q 0, b, Z 1) = { (q 0, Z 1 Z 1) } δ(q 0, e, Z 1) = { (q 0, ) } δ(q 0, , Z 0) = { (q 0, ) } If you were to directly write a CFG: S => b S e S | 0. 1. 2. 3. 4. S => [q 0 Z 0 q 0] => b [q 0 Z 1 q 0] [q 0 Z 0 q 0] [q 0 Z 1 q 0] => b [q 0 Z 1 q 0] => e [q 0 Z 0 q 0] => Let A=[q 0 Z 0 q 0] Let B=[q 0 Z 1 q 0] 0. 1. 2. 3. 4. S => A A => b B A B => b B B B => e A => Simplifying, 0. 1. S => b B S | B => b B B | e 38
Two ways to build a CFG Build a PDA Construct CFG from PDA (direct) Derive CFG directly Similarly… (indirect) Two ways to build a PDA Derive a CFG Design a PDA directly Construct PDA from CFG (indirect) (direct) 39
Deterministic PDAs 40
This PDA for Lwwr is non-deterministic Grow stack 0, Z 0/0 Z 0 1, Z 0/1 Z 0 0, 0/00 0, 1/01 1, 0/10 1, 1/11 q 0 Why does it have to be nondeterministic? Pop stack for matching symbols 0, 0/ 1, 1/ , Z 0/Z 0 , 0/0 , 1/1 Switch to popping mode q 1 , Z 0/Z 0 q 2 Accepts by final state To remove guessing, impose the user to insert c in the middle 41
Example shows that: Nondeterministic PDAs ≠ D-PDAs D-PDA for Lwcwr = {wcw. R | c is some special symbol not in w} Grow stack Pop stack for matching symbols 0, Z 0/0 Z 0 1, Z 0/1 Z 0 0, 0/00 0, 1/01 1, 0/10 1, 1/11 q 0 Note: • all transitions have become deterministic 0, 0/ 1, 1/ c, Z 0/Z 0 c, 0/0 c, 1/1 Switch to popping mode q 1 , Z 0/Z 0 q 2 Accepts by final state 42
Deterministic PDA: Definition • A PDA is deterministic if and only if: δ(q, a, X) has at most one member for any a ∑ U { } If δ(q, a, X) is non-empty for some a ∑, then δ(q, , X) must be empty. 43
PDA vs DPDA vs Regular languages Lwcwr Regular languages Lwwr D-PDA non-deterministic PDA 44
Summary • PDAs for CFLs and CFGs – – • Non-deterministic Deterministic PDA acceptance types 1. By final state 2. By empty stack • PDA – • IDs, Transition diagram Equivalence of CFG and PDA – – CFG => PDA construction PDA => CFG construction 45
- Slides: 42