Before We Start • Any questions? Context Free Languages PDAs and CFLs Languages Context Free Languages • Recall. • Context Free Languages(CFL) is the next class of languages outside of Regular – What is a language? Languages: – What is a class of languages? – Means for defining: Context Free Grammar – Machine for accepting: Pushdown Automata Now our picture looks like Context Free Grammars • Let’s formalize this a bit: Context Free Languages – A context free grammar (CFG) is a 4-tuple: (V, Deterministic Context Free Languages T, S, P) where • V is a set of variables Regular Languages • T is a set of terminals Finite • V and Σ are disjoint (I.e. V ∩ Σ = ∅ ) Languages • S ∈ V, is your start symbol
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 4 – γ = α 1 β α 2 – α ⇒ γ State machine Stack Pushdown Automata Pushdown Automata • About this transition function δ : • Let’s formalize this: – During a move of a PDA: – A pushdown automata (PDA) is a 7-tuple: • 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 • The machine will move to a new state based on: – Γ = stack alphabet (may have symbols in common 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 onto the – δ = transition function stack. Plan for today Equivalence of CFG and PDA • Show that PDAs and CFGs are equivalent. 1. Given a CFG, G, construct a PDA M, such that L(M) = L(G) 2. Given a PDA, M, define a CGF, G such • Questions? that L(G) = L(M)
Step 1: CFG → PDA Step 1: CFG → PDA • Basic idea • Given: A context free grammar G – 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 – Language generated by G is the same as • From this point on, there are two moves the PDA can make: – Language accepted by M. 1. 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. 2. 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. Step 1: CFG → PDA Step 1: CFG → PDA • Observations: • More observations: – There can be many productions that have a – A string will only be accepted if: 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. 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 grammar. – Q = { q 0 , q 1 , q 2 } – We define a pushdown automata • q o will be the start state • q 1 will be where all the work gets done • M = (Q, Σ , Γ , δ , q 0 , Z 0 ,F) • q 2 will be the accepting state – Such that – Γ = V ∪ ∑ ∪ { Z 0 } Z 0 ∉ (V ∪ ∑ ) • L(M) = L(G) – A = { q 2 }
Step 1: CFG → PDA Step 1: CFG → PDA • Transition function δ is defined as follows: • Transition function δ is defined as follows: – δ (q 0 , ε , Z o ) = { (q 1 , SZ o ) } – δ (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 0 ) = { (q 2 , Z 0 ) } – δ (q 1, ε , A) = { (q 1 , α ) | A → α is a production • After all reading is done, accept only if stack is of G} for all variables A empty. • Pop and replace variable. – No other transitions exist for M Step 1: CFG → PDA Step 1: CFG → PDA • Let’s look at an example: • Example: – M = (Q, Σ , Γ , δ , q 0 , Z 0 ,F) – Remember the CFG for odd length palindromes: – Q = { q 0 , q 1 , q 2 } • S → a | b – Σ = { a, b } • S → a S a | b S b – Γ = { a, b, S, Z 0 } – F = { q 2 } – Let’s convert this to a PDA. Step 1: CFG → PDA Step 1: CFG → PDA State Tape input Stack Move(s) q 1 ε S (q 1 , a) ε , Z 0 / SZ 0 ε , Z 0 / Z 0 (q 1 , b) 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) 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 Step 2: PDA → CFG Pushdown Automata • Strings accepted by a PDA by Final State • Given: A pushdown automata M – Start at (q 0 , x, Z 0 ) • Define: A context free grammar G • Start state q 0 • X on the input tape • Such that: • Empty stack – 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). Pushdown Automata Final State vs. Empty Stack • Strings accepted by a PDA by Empty Stack • The two means by which a PDA can accept – Start at (q 0 , x, Z 0 ) are equivalent wrt the class of languages • Start state q 0 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, ε , ε ) – Given a PDA M such that L = N(M), there • End in any state exists a PDA M’ such that L = L(M’) • All characters of x have been read • Stack is empty
Final State → Empty Stack Accept by Empty Stack • Final State → Empty Stack • Final State → Empty Stack – Given a PDA P F = (Q, Σ , Γ , δ F , q 0 , Z 0 ,F) and – Basic idea L = L (P F ) then there exists a PDA P N such that • Transitions of P N will mimic those of P F L = N (P N ) • Create a new state in P N that will empty the stack. • The machine can move into this new state whenever the machine is in an accepting state of P F – We will build such a PDA Accept by Empty Stack Accept by Empty Stack • Final State → Empty Stack • Final State → Empty Stack – We must be careful though • P F may crash when the stack is empty. • In those cases we need to assure that P N does not accept • To solve this: – Create a new empty stack symbol X 0 which is placed on the stack before P F s empty stack marker (Z 0 ) – Z 0 will only be popped by the new “stack emptying state • The first move of P N will be to place Z 0 X 0 on P N stack. Empty Stack → Final State Accept by Empty Stack • Final State → Empty Stack • Must show: – A string x is accepted by P N (by empty stack) iff it is accepted by P F (by final state) – If x is accepted by P N (empty stack) then it is accepted by P F (final state) – If x accepted by P F (final state) then it is accepted by P N (empty stack)
Recommend
More recommend