CS 301 Lecture 13 – Closure properties of context-free languages Stephen Checkoway March 5, 2018 1 / 18
CFLs and PDAs Theorem Every context-free language can be recognized by some PDA. Proof idea. 1 Use the PDA’s stack to perform a left-most derivation of a word in the language 2 Match the PDA’s input symbols against the stack, popping each one 3 Accept if stack is empty when there’s no more input 2 / 18
What we’d like to do Consider the language A = { w ∣ w ∈ { a , b } ∗ and w is not a palindrome } What CFG generates that language? 3 / 18
What we’d like to do Consider the language A = { w ∣ w ∈ { a , b } ∗ and w is not a palindrome } What CFG generates that language? S → aSa ∣ bSb ∣ aTb ∣ bTa T → aT ∣ bT ∣ ε 3 / 18
What we’d like to do Consider the language A = { w ∣ w ∈ { a , b } ∗ and w is not a palindrome } What CFG generates that language? S → aSa ∣ bSb ∣ aTb ∣ bTa T → aT ∣ bT ∣ ε A left-most derivation of the string abaaa is S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa. We want to start by pushing S on the stack, then performing the derivation step by step so that abaaa ends on the stack, and then match the input 3 / 18
What we’d like to do Consider the language A = { w ∣ w ∈ { a , b } ∗ and w is not a palindrome } What CFG generates that language? S → aSa ∣ bSb ∣ aTb ∣ bTa T → aT ∣ bT ∣ ε A left-most derivation of the string abaaa is S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa. We want to start by pushing S on the stack, then performing the derivation step by step so that abaaa ends on the stack, and then match the input There are two complications 1 The first step in the derivation S ⇒ aSa requires popping one symbol and pushing three 2 We can only replace symbols at the top of the stack 3 / 18
Pushing multiple symbols ε, S → a T b We would like to write a transition like q r but δ ∶ Q × Σ ε × Γ ε → P ( Q × Γ ε ) doesn’t allow that 4 / 18
Pushing multiple symbols ε, S → a T b We would like to write a transition like q r but δ ∶ Q × Σ ε × Γ ε → P ( Q × Γ ε ) doesn’t allow that ε, S → b ε, ε → T ε, ε → a Instead, use multiple transitions q r Note that the symbols are pushed on in reverse order 4 / 18
We can only replace symbols at the top of the stack Rather than first deriving the whole string on the stack and then matching the input, • If the top of the stack is a terminal, match it to the next input symbol t, t → ε for each t ∈ Σ • If the top of the stack is a variable, replace it with the RHS of a corresponding rule 5 / 18
We can only replace symbols at the top of the stack Rather than first deriving the whole string on the stack and then matching the input, • If the top of the stack is a terminal, match it to the next input symbol t, t → ε for each t ∈ Σ • If the top of the stack is a variable, replace it with the RHS of a corresponding rule In fact, we only need four main states plus q 0 any additional states necessary to push mul- ε, ε → $ tiple symbols q 1 The q loop state is where all the real work ε, ε → S happens q loop ... ... ε, $ → ε q a 5 / 18
Example q 0 S → a S a ∣ b S b ∣ a T b ∣ b T a T → a T ∣ b T ∣ ε ε, ε → $ 1 For each t ∈ Σ , add the transition q 1 t, t → ε from q loop to q loop ε, S → a S a 2 For each rule A → u 1 u 2 ⋯ u n for ε, ε → S ε, S → b S b u i ∈ V ∪ Σ , add n − 1 new states (if ε, S → a T b a , a → ε n > 1 ) and transitions to pop A and q loop ε, S → b T a b , b → ε push u 1 , u 2 , . . . , u n on in reverse order ε, T → a T ε, T → b T ε, $ → ε ε, T → ε q a [The rules on the right need 10 extra states to make this a proper PDA] 6 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ q 1 ε, S → a S a ε, ε → S ε, S → b S b ε, S → a T b a , a → ε q loop ε, S → b T a b , b → ε ε, T → a T ε, T → b T ε, $ → ε ε, T → ε q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 ε, S → a S a ε, ε → S ε, S → b S b ε, S → a T b a , a → ε q loop ε, S → b T a b , b → ε ε, T → a T ε, T → b T ε, $ → ε ε, T → ε q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a ε, ε → S ε, S → b S b ε, S → a T b a , a → ε q loop ε, S → b T a b , b → ε ε, T → a T ε, T → b T ε, $ → ε ε, T → ε q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b ε, S → a T b a , a → ε q loop ε, S → b T a b , b → ε ε, T → a T ε, T → b T ε, $ → ε ε, T → ε q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b 5 pop S , push b T a ; b T aa $ ε, S → a T b a , a → ε q loop ε, S → b T a b , b → ε ε, T → a T ε, T → b T ε, $ → ε ε, T → ε q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b 5 pop S , push b T a ; b T aa $ ε, S → a T b a , a → ε q loop ε, S → b T a 6 read and pop b ; T aa $ b , b → ε ε, T → a T ε, T → b T ε, $ → ε ε, T → ε q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b 5 pop S , push b T a ; b T aa $ ε, S → a T b a , a → ε q loop ε, S → b T a 6 read and pop b ; T aa $ b , b → ε ε, T → a T 7 pop T , push a T ; a T aa $ ε, T → b T ε, $ → ε ε, T → ε q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b 5 pop S , push b T a ; b T aa $ ε, S → a T b a , a → ε q loop ε, S → b T a 6 read and pop b ; T aa $ b , b → ε ε, T → a T 7 pop T , push a T ; a T aa $ ε, T → b T ε, $ → ε 8 read and pop a ; T aa $ ε, T → ε q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b 5 pop S , push b T a ; b T aa $ ε, S → a T b a , a → ε q loop ε, S → b T a 6 read and pop b ; T aa $ b , b → ε ε, T → a T 7 pop T , push a T ; a T aa $ ε, T → b T ε, $ → ε 8 read and pop a ; T aa $ ε, T → ε 9 pop T , push ε ; aa $ q a 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b 5 pop S , push b T a ; b T aa $ ε, S → a T b a , a → ε q loop ε, S → b T a 6 read and pop b ; T aa $ b , b → ε ε, T → a T 7 pop T , push a T ; a T aa $ ε, T → b T ε, $ → ε 8 read and pop a ; T aa $ ε, T → ε 9 pop T , push ε ; aa $ q a 10 read and pop a ; a $ 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b 5 pop S , push b T a ; b T aa $ ε, S → a T b a , a → ε q loop ε, S → b T a 6 read and pop b ; T aa $ b , b → ε ε, T → a T 7 pop T , push a T ; a T aa $ ε, T → b T ε, $ → ε 8 read and pop a ; T aa $ ε, T → ε 9 pop T , push ε ; aa $ q a 10 read and pop a ; a $ 11 read and pop a ; $ 7 / 18
Running the PDA on some input Consider running the PDA on the input q 0 abaaa . The stack is shown on the right after each step ε, ε → $ 1 push $; $ 2 push S ; S $ q 1 3 pop S , push a S a ; a S a $ ε, S → a S a 4 read and pop a ; S a $ ε, ε → S ε, S → b S b 5 pop S , push b T a ; b T aa $ ε, S → a T b a , a → ε q loop ε, S → b T a 6 read and pop b ; T aa $ b , b → ε ε, T → a T 7 pop T , push a T ; a T aa $ ε, T → b T ε, $ → ε 8 read and pop a ; T aa $ ε, T → ε 9 pop T , push ε ; aa $ q a 10 read and pop a ; a $ 11 read and pop a ; $ 12 pop $ and accept; ε 7 / 18
Proving that every CFL is recognized by a PDA Proof. Let A be a CFL generated by a CFG G = ( V, Σ , R, S ) . 8 / 18
Recommend
More recommend