announcements
play

Announcements Final Exam Dates have been Context Free Languages - PDF document

Announcements Final Exam Dates have been Context Free Languages announced Tuesday, November 11 12:30 2:30 pm PDAs and CFLs Room TBA Conflicts? Let me know. Before We Start Plan for today Exam 1 will be returned in


  1. Announcements  Final Exam Dates have been Context Free Languages announced  Tuesday, November 11  12:30 – 2:30 pm PDAs and CFLs  Room TBA  Conflicts? Let me know. Before We Start Plan for today  Exam 1 will be returned in 2nd half  1st half  PDAs and CFLs  2nd half  PDA homework session / Exam 1  Any questions? Languages The Language Bubble  Recall.  What is a language? Context Free Languages  What is a class of languages? Regular Languages Finite Languages 1

  2. Context Free Languages Context Free Grammars  Context Free Languages(CFL) is the  Let’s formalize this a bit: next class of languages outside of  A context free grammar (CFG) is a 4-tuple: Regular Languages: (V, T, S, P) where  V is a set of variables  Language / grammar: Context Free  T is a set of terminals Grammar  V and Σ are disjoint (I.e. V ∩ Σ = ∅ )  Machine for accepting: Pushdown  S ∈ V, is your start symbol Automata Context Free Grammars Pushdown Automata  Let’s formalize this a bit:  Production rules Input tape  Of the form A → β where  A ∈ V  β ∈ (V ∪ ∑ ) * string with symbols from V and ∑ 1 2  We say that γ can be derived from α in one step:  A → β is a rule 3  α = α 1 A α 2 5  γ = α 1 β α 2 4  α ⇒ γ State machine Stack Pushdown Automata Pushdown Automata  Let’s formalize this:  About this transition function δ :  A pushdown automata (PDA) is a 7-tuple:  During a move of a PDA:  At most one character is read from the input tape  M = (Q, Σ , Γ , q 0 , Z 0 , A, δ ) where  λ transitions are okay  Q = finite set of states  The topmost character is popped from the stack  Σ = tape alphabet  Γ = stack alphabet (may have symbols in common  The machine will move to a new state based on: w/ Σ )  The character read from the tape  q 0 ∈ Q = start state  The character popped off the stack  Z 0 ∈ Γ = initial stack symbol  The current state of the machine  A ⊆ Q = set of accepting states  0 or more symbols from the stack alphabet are pushed  δ = transition function onto the stack. 2

  3. Plan for today Equivalence of CFG and PDA  Show that PDAs and CFGs are Given a CFG, G, construct a PDA M, 1. equivalent. such that L(M) = L(G) Given a PDA, M, define a CGF, G such 2. that L(G) = L(M)  Questions? Step 1: CFG → PDA Step 1: CFG → PDA  Given: A context free grammar G Basic idea  Use the stack of the PDA to simulate the  Construct: A pushdown automata M  derivation of a string in the grammar.  Such that: Push S (start variable of G) on the stack  From this point on, there are two moves the PDA can  Language generated by G is the same as  make:  Language accepted by M. If a variable A is on the top of the stack, pop it and push 1. the right-hand side of a production A → β from G. If a terminal, a is on the top of the stack, pop it and 2. match it with whatever symbol is being read from the tape. Step 1: CFG → PDA Step 1: CFG → PDA  Observations:  More observations:  There can be many productions that have  A string will only be accepted if: a given variable on the left hand side:  After a string is completely read  S → λ | 0S1 | 1S0  The stack is empty  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 non- deterministic. 3

  4. Step 1: CFG → PDA Step 1: CFG → PDA  Let’s formalize this:  Define M as follows:  Let G = (V, T, S, P) be a context free  Q = { q 0 , q 1 , q 2 } grammar.  q o will be the start state  We define a pushdown automata  q 1 will be where all the work gets done  q 2 will be the accepting state  M = (Q, Σ , Γ , δ , q 0 , z ,F)  Γ = V ∪ ∑ ∪ { z } z ∉ (V ∪ ∑ )  Such that  A = { q 2 }  L(M) = L(G) Step 1: CFG → PDA Step 1: CFG → PDA  Transition function δ is defined as follows:  Transition function δ is defined as follows:  δ (q 0 , λ , z ) = { (q 1 , Sz) }  δ (q 1, a, a) = { (q 1 , λ )} for all terminals a  To start things off, push S onto the stack and  Pop and match terminal. immediately go into state 1  δ (q 1, λ , z) = { (q 2 , z) }  δ (q 1, λ , A) = { (q 1 , α ) | A → α is a production of  After all reading is done, accept only if stack is empty. G} for all variables A  No other transitions exist for M  Pop and replace variable. Step 1: CFG → PDA Step 1: CFG → PDA  Example:  Let’s look at an example:  M = (Q, Σ , Γ , δ , q 0 , z ,F)  Remember the CFG for odd length palindromes:  Q = { q 0 , q 1 , q 2 }  Σ = { a, b }  S → a | b  Γ = { a, b, S, z }  S → a S a | b S b  F = { q 2 }  Let’s convert this to a PDA. 4

  5. Step 1: CFG → PDA Step 1: CFG → PDA State Tape input Stack Move(s) q 1 λ S (q 1 , a) (q 1 , b) λ , Z 0 / SZ 0 λ , Z 0 / Z 0 q 1 q 2 q 0 (q 1 , aSa) (q 1 , bSb) q 1 a a (q 1 , λ ) Lots of moves q 1 b b (q 1 , λ ) (see next slide) Step 1: CFG → PDA Step 1: CFG → PDA  Let’s run M on abbba  Questions?  (q 0 , abbba, Z) → (q 1 , abbba, SZ)  → (q 1 , abbba, aSaZ) // push  → (q 1 , bbba, SaZ) // match  → (q 1 , bbba, bSbaZ) // push  → (q 1 , bba, SbaZ) // match  → (q 1 , bba, bbaZ) // push  → (q 1 , ba, baZ) // match  → (q 1 , a, aZ) // match  → (q 1 , ε , Z) // match  → (q 2 , ε , Z ) // accept Step 2: PDA → CFG Pushdown Automata  Given: A pushdown automata M  Strings accepted by a PDA by Final State  Start at (q 0 , x, z)  Define: A context free grammar G  Start state q 0  X on the input tape  Empty stack  Such that:  End with (q, λ , β )  Language accepted by M is the same as  End in an accepting state (q ∈ F)  Language generated by G  All characters of x have been read  Some string on the stack (doesn’t matter what). 5

  6. Pushdown Automata Final State vs. Empty Stack  Strings accepted by a PDA by Empty Stack  The two means by which a PDA can  Start at (q 0 , x, z) accept are equivalent wrt the class of  Start state q 0 languages accepted  X on the input tape  Given a PDA M such that L = L(M), there  Empty stack exists a PDA M’ such that L = N(M’)  End with (q, λ , λ )  End in any state  Given a PDA M such that L = N(M), there  All characters of x have been read exists a PDA M’ such that L = L(M’)  Stack is empty Step 2: PDA → CFG Step 2: PDA → CFG  Basic idea  Given: A pushdown automata M that  We define variables in G to be triplets: accepts by empty stack  [p, A, q] will represent a variable, that can generate all  Define: A context free grammar G 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  Such that:  Note that it may take many moves to get there.  Language accepted by M is the same as  Language generated by G Step 2: PDA → CFG Step 2: PDA → CFG  Productions of G  More Productions of G 1. For all states q in M, add the production 2. For every q, q 1 ∈ Q, a ∈Σ∪ { λ } and A ∈Γ  S → [q 0 zq]  If δ (q, a, A) contains (q 1 , λ ) then add  Following these productions will generate all strings  [qAq 1 ] → a that start at q o , and result in an empty stack. Final state is not important.  Meaning you can get from q to q 1 while  In other words, all strings accepted by M. popping A from the stack by reading an a. 6

  7. a, A / B 1 …B m Step 2: PDA → CFG Step 2: PDA → CFG q q 1  One can show by induction (though we  Even More Productions of G won’t) that 3. For every q, q 1 ∈ Q, a ∈Σ∪ { λ } and A ∈Γ  [qAp] ⇒ * x iff (q, x, A) → * (p, λ , λ )  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  More specifically [q 0 zp] ⇒ * x and since we added  [qAq m+1 ] → a[q 1 B 1 q 2 ] [q 2 B 2 q 3 ] … [q m B m q m+1 ] the productions S → [q 0 zp] for all p, then x ∈ L(G)  Meaning:  On the flip side S → [q 0 zp] will always be the first  one way to pop A off the stack and to get from q to q m+1 is to production of any derivation of G  read an a  (q 0 , x, z) → * (p, λ , λ )  use some input to pop B 1 off the stack (bring you from q 1 to q 2 in the process),  So x is accepted by empty stack  While in q 2 , use some input to pop B 2 off the stack (bringing  x ∈ L(M) you to q 3 in the process)  And so on… Step 2: PDA → CFG Step 2: PDA → CFG  Example  Example  M = (Q, Σ , Γ , δ , q 0 , z ,F) L = { 0 i 1 j | i ≥ j ≥ 1} λ ,X / λ  Q = { q 0 , q 1 } 1,X / λ  Σ = { 0, 1 } 1, X / λ λ ,Z / λ  Γ = { X, Z } q 1 q 0  z = Z 0,Z / XZ  F = ∅ 0,X / XX Step 2: PDA → CFG Step 2: PDA → CFG  Corresponding CFG  Corresponding CFG  Type 2 productions  Type 1 productions  If δ (q, a, A) contains (q 1 , λ ) then add  For all states q, S → [q 0 zq]  [qAq 1 ] → a  S → [q 0 Zq 1 ]  δ (q 0 , 1, X) = (q 1 , λ ) [q 0 Xq 1 ] → 1  S → [q 0 Zq 0 ]  δ (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 ] → λ 7

Recommend


More recommend