  # = Languages Accepted by NPDAs Accept (Grammars) NPDAs - PDF document

## Review Languages and Grammars Alphabets, strings, languages Regular Languages CS 301 - Lecture 13 Deterministic Finite and Nondeterministic Automata Equivalence of NFA and DFA Converting NPDAs to Grammars Regular

1. Review • Languages and Grammars – Alphabets, strings, languages • Regular Languages CS 301 - Lecture 13 – Deterministic Finite and Nondeterministic Automata – Equivalence of NFA and DFA Converting NPDAs to Grammars – Regular Expressions – Regular Grammars and – Properties of Regular Languages – Languages that are not regular and the pumping lemma • Context Free Languages Deterministic Pushdown Automata – Context Free Grammars – Derivations: leftmost, rightmost and derivation trees Fall 2008 – Parsing and ambiguity – Simplifications and Normal Forms – Nondeterministic Pushdown Automata – Converting Context Free Grammars into NPDAs • Today: – Converting NPDAs to Context Free Grammars – Deterministic Pushdown Automata Theorem: Context-Free Languages = Languages Accepted by NPDAs Accept (Grammars) NPDAs Context-Free Languages 1

2. Proof - Step 1: Proof - Step 2: Context-Free Languages Context-Free Languages Languages Accepted by Languages Accepted by (Grammars) NPDAs (Grammars) NPDAs M Convert any context-free grammar G Convert any NPDA to a context-free L ( G ) L ( M ) L ( G ) L ( M ) to a NPDA with: M grammar with: G = = Last Time we showed how to convert A grammar to an NPDA….. For any NPDA M Proof - step 2 Converting we will construct NPDAs a context-free grammar with G to Context-Free Grammars L ( M ) L ( G ) = 2

3. Some Necessary 1) Modify the NPDA so that the stack is never empty Modifications Modify (if necessary) the NPDA so that: Stack 1) The stack is never empty a 2) It has a single final state \$ \$ and empties the stack when it accepts a string OK OK NOT OK 3) Has transitions in a special form # Introduce the new symbol to denote # At the beginning push into the stack the bottom of the stack a Original NPDA \$ \$ new initial state , # λ λ → a # # \$ \$ # original initial state 3

4. In transitions: # \$ replace every instance of with Convert all transitions so that: Example: a , \$ b → q q if the automaton attempts to pop j i \$ or replace it will halt a , # b → q q j i Convert transitions as follows: 2) Modify the NPDA so that it empties the stack σ , s ξ → q q j i and has a unique final state Empty the stack NPDA , x ∀ x {\$} λ → λ ∈ Γ − ∀ x {\$} ∈ Γ − λ , λ → σ , s , x → x λ λ ξ → q q λ , λ j → i λ , \$ λ → λ q λ , f λ \$ → → λ λ , \$ halting state Old final states 4

5. 3) modify the NPDA so that Convert: transitions have the following forms: σ , y λ → q q j i , B σ → λ q q j i OR , y σ τ → τ q q , B → CD σ j q i q j i {\$} ∀ τ ∈ Γ − B , C , D : stack symbols | y | 2 Convert: ≥ Convert: symbols symbols , A → B , A → By σ σ q q q q j j i i Convert recursively , A → XB , X , A → y , X → BX σ σ → λ σ σ q q q q j j i i ∀ X {\$} ∈ Γ − X {\$} ∈ Γ − 5

6. Example of a NPDA in correct form: The Grammar Construction L ( M ) { w : n n } = = a b Stack symbol In grammar : G \$ : initial stack symbol Variables: ( q i Bq ) j a , \$ → 0\$ b , \$ → 1\$ states a , 0 → 00 b ,1 → 11 a ,1 → λ b , 0 → λ Terminals: , \$ λ → λ q q Input symbols of NPDA f 0 For each transition a , B → λ For each transition a , B CD q q → q j q i j i We add productions ( q Bq ) a ( q Cq )( q Dq ) ( q Bq ) a → We add production → i k j l l k i j q , k q For all possible states l in the automaton 6

7. Example: a , \$ → 0\$ b , \$ → 1\$ Stack bottom symbol a , 0 → 00 b ,1 → 11 a ,1 → λ b , 0 → λ ( q o q \$ ) Start Variable: f , \$ λ → λ q q f 0 Start state final state ( q 1 q ) a Grammar production: → 0 0 Example: Example: a , \$ → 0\$ b , \$ → 1\$ a , \$ → 0\$ b , \$ → 1\$ a , 0 → 00 b ,1 → 11 a , 0 → 00 b ,1 → 11 a ,1 → λ b , 0 → λ a ,1 → λ b , 0 → λ , \$ , \$ λ → λ λ → λ q q q q f f 0 0 Grammar productions: ( q \$ q ) b ( q 1 q )( q \$ q ) | b ( q 1 q )( q \$ q ) → ( 0 q \$ q ) Grammar production: → λ 0 0 0 0 0 0 0 f f 0 f ( q \$ q ) b ( q 1 q )( q \$ q ) | b ( q 1 q )( q \$ q ) → 0 f 0 0 0 f 0 f f f 7

8. ( 0 q \$ q ) : start vari able Resulting Grammar: f ( q 0 q ) a ( q 0 q )( q 0 q ) | a ( q 0 q )( q 0 q ) → 0 0 0 0 0 0 0 f f 0 ( q 0 q ) a ( q 0 q )( q 0 q ) | a ( q 0 q )( q 0 q ) → 0 f 0 0 0 f 0 f f f ( q \$ q ) b ( q 1 q )( q \$ q ) | b ( q 1 q )( q \$ q ) → 0 0 0 0 0 0 0 f f 0 ( q \$ q ) b ( q 1 q )( q \$ q ) | b ( q 1 q )( q \$ q ) → ( q 1 q ) a 0 f 0 0 0 f 0 f f f → 0 0 ( q 0 q ) b → ( q 1 q ) b ( q 1 q )( q 1 q ) | b ( q 1 q )( q 1 q ) 0 0 → 0 0 0 0 0 0 0 f f 0 ( q 1 q ) b ( q 1 q )( q 1 q ) | b ( q 1 q )( q 1 q ) → 0 f 0 0 0 f 0 f f f ( 0 q \$ q ) → λ f ( q \$ q ) a ( q 0 q )( q \$ q ) | a ( q 0 q )( q \$ q ) → 0 0 0 0 0 0 0 f f 0 ( q \$ q ) a ( q 0 q )( q \$ q ) | a ( q 0 q )( q \$ q ) → 0 f 0 0 0 f 0 f f f Derivation of string abba In general: ∗ ( q Aq ) w ( 0 q \$ q ) a ( q 0 q )( q \$ q ) ⇒ ⇒ ⇒ i j f 0 0 0 f if and only if ab ( 0 q \$ q ) ⇒ f q q abb ( q 1 q )( q \$ q ) the NPDA goes from to ⇒ i 0 0 0 f j w by reading string and abba abba ( 0 q \$ q ) A ⇒ the stack doesn’t change below f and then is removed from stack A 8

9. Therefore: Therefore: For any NPDA ∗ ( 0 q \$ q ) w ⇒ there is a context-free grammar f that accepts the same language if and only if Context-Free Languages w is accepted by the NPDA Languages Accepted by (Grammars) NPDAs Deterministic PDA: DPDA Allowed transitions: Deterministic PDA a , b w → q 1 q 2 DPDA , b → w λ q q 1 2 (deterministic choices) 9

10. Not allowed: Allowed transitions: q 2 q 2 a , b w , b → w → λ 1 1 q 2 q 2 a , b w , b → w → λ 1 1 q 1 q 1 q 1 q 1 a , b w a , b w → → 2 2 q 3 q 3 a , c w , c → w → λ 2 2 q 3 q 3 (non deterministic choices) (deterministic choices) DPDA example n n n n L ( M ) { a b : n 0 } The language L ( M ) { a b : n 0 } = ≥ = ≥ is deterministic context-free a , λ → a b , a → λ a , λ → a b , a → λ λ , \$ → \$ q 0 q 1 q 2 q 3 10

11. Example of Non-DPDA (NPDA) Definition: R L ( M ) { ww } = L A language is deterministic context-free if there exists some DPDA that accepts it a , a a , a λ → → λ b , b λ → b , b → λ λ , λ → λ λ , \$ → \$ q 2 q 0 q 1 NPDAs Not allowed in DPDAs Have More Power than a , a λ → a , a → λ b , b λ → b , b → λ DPDAs λ , λ → λ λ , \$ → \$ q 2 q 0 q 1 11

12. It holds that: We will actually show: Deterministic Deterministic Context-Free Context-Free Context-Free Context-Free Languages Languages Languages Languages NPDAs (NPDA) (DPDA) (DPDA) Since every DPDA is also a NPDA We will show that there exists L a context-free language which is not accepted by any DPDA The language is: n n n 2 n L { a b } { a b } = ∪ n n n 2 n L { a b } { a b } n 0 = ∪ L ≥ Language is context-free Context-free grammar for : L We will show: S → S 1 | S n n n 2 n { a b } { a b } ∪ 2 L • is context-free n b n S → aS b | { a } λ 1 1 L • is not deterministic context-free n b 2 n S → aS bb | { a } λ 2 2 12

13. Theorem: Proof: Assume for contradiction that n n n 2 n n n n 2 n L { a b } { a b } The language L { a b } { a b } = ∪ = ∪ is not deterministic context-free is deterministic context free (there is no DPDA that accepts ) L Therefore: M L there is a DPDA that accepts n n n 2 n n n n 2 n L ( M ) { a b } { a b } L ( M ) { a b } { a b } DPDA with M DPDA with M = ∪ = ∪ n b n a accepts Such a path exists because of the determinism M n b n n a b n b n n a b n b 2 n a accepts 13

14. n n n n n n Fact 1: Fact 2: { a b c } L ∪ { a b c } The language The language is not context-free is not context-free Context-free languages n b n n n n 2 n a ( L { a b } { a b }) = ∪ Regular languages a * b * (we can prove this using pumping lemma (we will prove this at a later class using for context-free languages - next lecture) pumping lemma for context-free languages) n n n 2 n M L ( M ) { a b } { a b } = ∪ We will construct a NPDA that accepts: n b n n a b n n n L ∪ { a b c } Replace b Modify M n n n 2 n with c ( L { a b } { a b }) = ∪ n n n 2 n M ′ L ( M ) { a c } { a c } ′ = ∪ which is a contradiction! n c n n a c 14

More recommend