Homework Context Free Languages PDAs and CFLs Homework #3 returned Homework #4 due today Homework #5 Pg 169 -- Exercise 4 Pg 183 -- Exercise 4c,e,i (use JFLAP) Pg 184 -- Exercise 10 Pg 184 -- Exercise 12 Pg 185 -- Exercise 17 (use mathematical formal definitions) Due 10 / 17 Announcements Final Exam Dates have been announced Friday, November 17 10:15am -- 12:15pm Room 70-3435 Before We Start Exam 1 will be returned in 2nd half Any questions? Conflicts? Let me know. Plan for today 1st half PDAs and CFLs 2nd half PDA homework session / Exam 1 Languages Recall. What is a language? What is a class of languages? 1
The Language Bubble Context Free Languages Regular Languages Finite Languages Context Free Languages Context Free Languages(CFL) is the next class of languages outside of Regular Languages: Language / grammar: Context Free Grammar Machine for accepting: Pushdown Automata Context Free Grammars Context Free Grammars Let s formalize this a bit: A context free grammar (CFG) is a 4-tuple: (V, T, S, P) where V is a set of variables T is a set of terminals V and Σ are disjoint (I.e. V Σ = ) S V, is your start symbol Let s formalize this a bit: Production rules Of the form A β where A V β (V ) * string with symbols from V and We say that γ can be derived from α in one step: A β is a rule α = α 1 A α 2 γ = α 1 β α 2 α γ Pushdown Automata Input tape 1 2 3 5 4 State machine Stack Pushdown Automata Let s formalize this: A pushdown automata (PDA) is a 7-tuple: M = (Q, Σ, Γ, q 0, Z 0, A, δ) where Q = finite set of states Σ = tape alphabet Γ = stack alphabet (may have symbols in common w/ Σ) q 0 Q = start state Z 0 Γ = initial stack symbol A Q = set of accepting states δ = transition function 2
Pushdown Automata About this transition function δ: During a move of a PDA: At most one character is read from the input tape λ transitions are okay The topmost character is popped from the stack The machine will move to a new state based on: The character read from the tape The character popped off the stack The current state of the machine 0 or more symbols from the stack alphabet are pushed onto the stack. Plan for today Show that PDAs and CFGs are equivalent. Questions? Equivalence of CFG and PDA Given a CFG, G, construct a PDA M, such that L(M) = L(G) Given a PDA, M, define a CGF, G such that L(G) = L(M) Given: A context free grammar G Construct: A pushdown automata M Such that: Language generated by G is the same as Language accepted by M. Basic idea Use the stack of the PDA to simulate the derivation of a string in the grammar. Push S (start variable of G) on the stack From this point on, there are two moves the PDA can make: If a variable A is on the top of the stack, pop it and push the right-hand side of a production A β from G. If a terminal, a is on the top of the stack, pop it and match it with whatever symbol is being read from the tape. Observations: There can be many productions that have a given variable on the left hand side: S λ 0S1 1S0 In these cases, the PDA must choose which string to push onto the stack after pushing a variable. I.e. the PDA being constructed in nondeterministic. 3
More observations: A string will only be accepted if: After a string is completely read The stack is empty Let s formalize this: Let G = (V, T, S, P) be a context free grammar. We define a pushdown automata M = (Q, Σ, Γ,δ, q 0, z,f) Such that L(M) = L(G) Define M as follows: Q = { q 0, q 1, q 2 } q o will be the start state q 1 will be where all the work gets done q 2 will be the accepting state Γ = V { z } z (V ) A = { q 2 } Transition function δ is defined as follows: δ (q 0, λ, z ) = { (q 1, Sz) } To start things off, push S onto the stack and immediately go into state 1 δ (q 1, λ, A) = { (q 1, α ) A α is a production of G} for all variables A Pop and replace variable. Transition function δ is defined as follows: δ (q 1, a, a) = { (q 1, λ )} for all terminals a Pop and match terminal. δ (q 1, λ, z) = { (q 2, z) } After all reading is done, accept only if stack is empty. No other transitions exist for M Let s look at an example: Remember the CFG for odd length palindromes: S a b S a S a b S b Let s convert this to a PDA. 4
Example: M = (Q, Σ, Γ,δ, q 0, z,f) Q = { q 0, q 1, q 2 } Σ = { a, b } Γ = { a, b, S, z } F = { q 2 } λ, Z 0 / SZ 0 λ, Z 0 / Z 0 q 0 q 1 q 2 Lots of moves (see next slide) State Tape input Stack Move(s) q 1 λ S (q 1, a) (q 1, b) (q 1, asa) (q 1, bsb) q 1 a a (q 1, λ) q 1 b b (q 1, λ) Let s run M on abbba (q 0, abbba, Z) a(q 1, abbba, SZ) a(q 1, abbba, asaz) // push a(q 1, bbba, SaZ) // match a(q 1, bbba, bsbaz) // push a(q 1, bba, SbaZ) // match a(q 1, bba, bbaz) // push a(q 1, ba, baz) // match a(q 1, a, az) // match a(q 1, ε, Z) // match a(q 2, ε, Z ) // accept Questions? Given: A pushdown automata M Define: A context free grammar G Such that: Language accepted by M is the same as Language generated by G 5
Pushdown Automata Pushdown Automata Strings accepted by a PDA by Final State Start at (q 0, x, z) Strings accepted by a PDA by Empty Stack Start at (q 0, x, z) Start state q 0 X on the input tape Empty stack End with (q, λ, β) End in an accepting state (q F) All characters of x have been read Some string on the stack (doesn t matter what). Start state q 0 X on the input tape Empty stack End with (q, λ, λ) End in any state All characters of x have been read Stack is empty Final State vs. Empty Stack The two means by which a PDA can accept are equivalent wrt the class of languages accepted Given a PDA M such that L = L(M), there exists a PDA M such that L = N(M ) Given a PDA M such that L = N(M), there exists a PDA M such that L = L(M ) Given: A pushdown automata M that accepts by empty stack Define: A context free grammar G Such that: Language accepted by M is the same as Language generated by G Basic idea We define variables in G to be triplets: [p, A, q] will represent a variable, that can generate all strings x that: Upon reading x on the PDA tape will Take you from state p to state q in the PDA and Have a net result of popping A off the stack Note that it may take many moves to get there. Productions of G 1. For all states q in M, add the production S [q 0 zq] Following these productions will generate all strings that start at q o, and result in an empty stack. Final state is not important. In other words, all strings accepted by M. 6
a, A / B 1 B m q q 1 More Productions of G 2. For every q, q 1 Q, a Σ {λ} and A Γ If δ (q, a, A) contains (q 1, λ) then add [qaq 1 ] a Even More Productions of G 3. For every q, q 1 Q, a Σ {λ} and A Γ If δ (q, a, A) contains (q 1, B 1 B 2 B m ) then For every possible sequence of states q 2, q m+1 Add [qaq m+1 ] a[q 1 B 1 q 2 ] [q 2 B 2 q 3 ] [q m B m q m+1 ] Meaning you can get from q to q 1 while popping A from the stack by reading an a. Meaning: one way to pop A off the stack and to get from q to q m+1 is to read an a use some input to pop B 1 off the stack (bring you from q 1 to q 2 in the process), While in q 2, use some input to pop B 2 off the stack (bringing you to q 3 in the process) And so on One can show by induction (though we won t) that [qap] * x iff (q, x, A) a * (p, λ, λ) More specifically [q 0 zp] * x and since we added the productions S [q 0 zp] for all p, then x L(G) On the flip side S [q 0 zp] will always be the first production of any derivation of G (q 0, x, z) a * (p, λ, λ) So x is accepted by empty stack x L(M) Example L = { 0 i 1 j i j 1} 1, X / λ q q 0 1 0,Z / XZ 0,X / XX λ,x / λ 1,X / λ λ,z / λ Example M = (Q, Σ, Γ,δ, q 0, z,f) Q = { q 0, q 1 } Σ = { 0, 1 } Γ = { X, Z } z = Z F = Corresponding CFG Type 1 productions For all states q, S [q 0 zq] S [q 0 Zq 1 ] S [q 0 Zq 0 ] 7
Corresponding CFG Type 2 productions If δ (q, a, A) contains (q 1, λ) then add [qaq 1 ] a δ (q 0, 1, X) = (q 1, λ) [q 0 Xq 1 ] 1 δ (q 1, 1, X) = (q 1, λ) [q 1 Xq 1 ] 1 δ (q 1, ε, X) = (q 1, λ) [q 1 Xq 1 ] λ δ (q 1, ε, Z) = (q 1, λ) [q 1 Zq 1 ] λ Corresponding CFG Type 3 production If δ (q, a, A) contains (q 1, B 1 B 2 B m ) then [qaq m+1 ] a[q 1 B 1 q 2 ] [q 2 B 2 q 3 ] [q m B m q m+1 ] Transitions to consider: δ (q 0, 0, Z) = (q 0, XZ) δ (q 0, 0, X) = (q 0, XX) q b q 0 q 0 q 1 q 0 q 0 q 1 q 1 q 1 Corresponding CFG Type 3 productions q c δ (q 0, 0, X) = (q 0, XX) [q 0 X q c ] 0 [q 0 Xq b ][q b Xq c ] Corresponding CFG Type 3 productions δ (q 0, 0, X) = (q 0, XX) Add productions [q 0 Xq 0 ] 0[q 0 Xq 0 ][q 0 Xq 0 ] [q 0 Xq 0 ] 0[q 0 Xq 1 ][q 1 Xq 0 ] [q 0 Xq 1 ] 0[q 0 Xq 0 ][q 0 Xq 1 ] [q 0 Xq 1 ] 0[q 0 Xq 1 ][q 1 Xq 1 ] q b q 0 q 0 q 1 q 0 q 0 q 1 q 1 q 1 Corresponding CFG Type 3 productions q c δ (q 0, 0, Z) = (q 0, XZ) [q 0 Z q c ] 0 [q 0 Xq b ][q b Zq c ] Corresponding CFG Type 3 productions δ (q 0, 0, Z) = (q 0, XZ) Add productions [q 0 Zq 0 ] 0[q 0 Xq 0 ][q 0 Zq 0 ] [q 0 Zq 0 ] 0[q 0 Xq 1 ][q 1 Zq 0 ] [q 0 Zq 1 ] 0[q 0 Xq 0 ][q 0 Zq 1 ] [q 0 Zq 1 ] 0[q 0 Xq 1 ][q 1 Zq 1 ] 8
Complete grammar G = (V, Σ, S, P) V = { S, [q 0 Xq 0 ], [q 0 Zq 0 ], [q 0 Xq 1 ], [q 0 Zq 1 ], [q 1 Xq 0 ], [q 1 Zq 0 ], [q 1 Xq 1 ], [q 1 Zq 1 ], [q 1 Xq 1 ] } P = S [q 0 Zq 1 ] (1) [q 0 Xq 0 ] 0[q 0 Xq 0 ][q 0 Xq 0 ] (7) S [q 0 Zq 0 ] (2) [q 0 Xq 0 ] 0[q 0 Xq 1 ][q 1 Xq 0 ] (8) [q 0 Xq 1 ] 1 (3) [q 0 Xq 1 ] 0[q 0 Xq 0 ][q 0 Xq 1 ] (9) [q 1 Xq 1 ] 1 (4) [q 0 Xq 1 ] 0[q 0 Xq 1 ][q 1 Xq 1 ] (10) [q 1 Xq 1 ] λ (5) [q 0 Zq 0 ] 0[q 0 Xq 0 ][q 0 Zq 0 ] (11) [q 1 Zq 1 ] λ (6) [q 0 Zq 0 ] 0[q 0 Xq 1 ][q 1 Zq 0 ] (12) [q 0 Zq 1 ] 0[q 0 Xq 0 ][q 0 Zq 1 ] (13) [q 0 Zq 1 ] 0[q 0 Xq 1 ][q 1 Zq 1 ] (14) Summary Summary What have we learned? (We really don t need to see the CFG corresponding to a PDA, do we?) What we have really learned? Given a CFG, we can build a PDA that accepts the same language generated by the CFG Given a PDA, we can define a CFG that can generate the language accepted by the PDA. Looking for a machine to accept CGLs? The pushdown automata fits the bill! Break 9