Document Sample

PDAs Accept Context-Free Languages 1 Theorem: Context-Free Languages Languages Accepted by (Grammars) PDAs 2 Proof - Step 1: Context-Free Languages Languages Accepted by (Grammars) PDAs Convert any context-free grammar G to a PDA M with: L (G ) L( M ) 3 Proof - Step 2: Context-Free Languages Languages Accepted by (Grammars) PDAs Convert any PDA M to a context-free grammar G with: L (G ) L( M ) 4 Proof - step 1 Converting Context-Free Grammars to PDAs 5 Context-Free Languages Languages Accepted by (Grammars) PDAs Convert any context-free grammar G to a PDA M with: L (G ) L( M ) 6 We will convert grammar G to a PDA M such that: M simulates leftmost derivations of G 7 Convert grammar G to PDA M Production in G Terminal in G Aw a , A w a, a q0 , S q1 , $ $ q2 8 Example Grammar S aSTb PDA S b T Ta , S aSTb T , S b , T Ta a, a , T b, b q0 , S q1 , $ $ q2 9 Derivation: Input a b a b , S aSTb $ Time 0 , S b Stack , T Ta a, a , T b, b , S , $ $ q0 q1 q2 10 Derivation: S Input a b a b S , S aSTb $ Time 0 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 11 Derivation: S aSTb a S Input a b a b T b , S aSTb $ Time 1 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 12 Derivation: S aSTb a S Input a b a b T b , S aSTb $ Time 2 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 13 Derivation: S aSTb abTb b Input a b a b T b , S aSTb $ Time 3 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 14 Derivation: S aSTb abTb b Input a b a b T b , S aSTb $ Time 4 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 15 Derivation: S aSTb abTb abTab T Input a b a b a b , S aSTb $ Time 5 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 16 Derivation: S aSTb abTb abTab abab T Input a b a b a b , S aSTb $ Time 6 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 17 Derivation: S aSTb abTb abTab abab Input a b a b a b , S aSTb $ Time 7 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 18 Derivation: S aSTb abTb abTab abab Input a b a b b , S aSTb $ Time 8 , S b Stack , T Ta a, a , T b, b q0 , S q1 , $ $ q2 19 Derivation: S aSTb abTb abTab abab Input a b a b , S aSTb $ Time 9 , S b Stack , T Ta a, a , T b, b accept q0 , S q1 , $ $ q2 20 Grammar PDA computation derivation ( q0 , abab,$) S ( q1 , abab, S $) aSTb ( q1 , bab, STb$) ( q1 , bab, bTb$) abTb ( q1 , ab, Tb$) abTab ( q1 , ab, Tab$) abab ( q1 , ab, ab$) ( q1 , b, b$) ( q1 , ,$) ( q2 , ,$) 21 In general, it can be shown that: Grammar G generates PDA M If and accepts w string w Only if * (q0 , w,$) (q2 , ,$) S w Therefore L(G) L( M ) 22 Therefore: For any context-free language L there is a PDA that accepts L Context-Free Languages Languages Accepted by (Grammars) PDAs 23 Proof - step 2 Converting PDAs to Context-Free Grammars 24 Context-Free Languages Languages Accepted by (Grammars) PDAs Convert any PDA M to a context-free grammar G with: L (G ) L( M ) 25 We can convert PDA M to a context-free grammar G such that: G simulates computations of M with leftmost derivations 26 Modify the PDA so that at end it empties stack and has a unique accept state Empty stack PDA x {$} , x , $ q f New accept Old accept states state 27 Deterministic PDAs - DPDAs 28 Deterministic PDA: DPDA Allowed transitions: a, b w q1 q2 , b w q1 q2 (deterministic choices) 29 Allowed transitions: a, b w1 q2 , b w1 q2 q1 q1 a , c w2 q3 , c w2 q3 (deterministic choices) 30 Not allowed: a, b w1 q2 , b w1 q2 q1 q1 a , b w2 q3 a , b w2 q3 (non deterministic choices) 31 DPDA example L( M ) {a b : n 0} n n a, a b, a q0 a, a q b, a q , $ $ q 1 2 3 32 Definition: A language L is deterministic context-free if there exists some DPDA that accepts it Example: The language L( M ) {a b : n 0} n n is deterministic context-free 33 Example of Non-DPDA (PDA) L( M ) {vv : v {a, b} } R * a, a a, a b, b b, b q0 , q1 , $ $ q2 34 Not allowed in DPDAs a, a a, a b, b b, b q0 , q1 , $ $ q2 35 PDAs Have More Power than DPDAs 36 It holds that: Deterministic Context-Free Languages Context-Free Languages PDAs (DPDA) Since every DPDA is also a PDA 37 We will actually show: Deterministic Context-Free Context-Free Languages Languages (PDA) (DPDA) L L We will show that there exists a context-free language L which is not accepted by any DPDA 38 The language is: L {a b } {a b } n n n 2n n0 We will show: • L is context-free • L is not deterministic context-free 39 L {a b } {a b } n n n 2n Language L is context-free Context-free grammar for L: S S1 | S2 {a nbn } {a nb2n } S1 aS1b | n n {a b } S2 aS2bb | n 2n {a b } 40 Theorem: The language L {a b } {a b } n n n 2n is not deterministic context-free (there is no DPDA that accepts L) 41 Proof: Assume for contradiction that L {a b } {a b } n n n 2n is deterministic context free Therefore: there is a DPDA M that accepts L 42 DPDA M with L( M ) {a b } {a b } n n n 2n n n accepts a b n n n a b b n 2n accepts a b 43 DPDA M with L( M ) {a b } {a b } n n n 2n Such a path exists due to determinism M n n n a b b 44 Fact 1: The language {a b n n n c } is not context-free Context-free languages a nb n Regular languages a *b * (we will prove this at a later class using pumping lemma for context-free languages) 45 Fact 2: The language L {a n n n b c } is not context-free ( L {a b } {a b }) n n n 2n (we can prove this using pumping lemma for context-free languages) 46 We will construct a PDA that accepts: L {a b c } n n n ( L {a b } {a b }) n n n 2n which is a contradiction! 47 DPDA M L( M ) {a b } {a b } n n n 2n a nb n bn Modify M Replace b with c DPDA M L( M ) {a nc n } {a nc 2n } a nc n cn 48 A PDA that accepts L {a b c } n n n Connect the final states of M with the final states of M M n n n a b b M a nc n cn 49 Since L {a b c } is accepted by a PDA n n n it is context-free Contradiction! (since L {a b c } is not context-free) n n n 50 Therefore: L {a b } {a b } n n n 2n Is not deterministic context free There is no DPDA that accepts it End of Proof 51 Positive Properties of Context-Free languages 52 Union Context-free languages are closed under: Union L1 is context free L1 L2 L2 is context free is context-free 53 Example Language Grammar L1 {a b } n n S1 aS1b | L2 {ww } R S2 aS2a | bS2b | Union L {a b } {ww } n n R S S1 | S2 54 In general: For context-free languages L1, L2 with context-free grammars G1, G2 and start variables S1, S2 The grammar of the union L1 L2 has new start variable S and additional production S S1 | S2 55 Concatenation Context-free languages are closed under: Concatenation L1 is context free L1L2 L2 is context free is context-free 56 Example Language Grammar L1 {a b } n n S1 aS1b | L2 {ww } R S2 aS2a | bS2b | Concatenation L {a b }{ww } n n R S S1S2 57 In general: For context-free languages L1, L2 with context-free grammars G1, G2 and start variables S1, S2 The grammar of the concatenation L1L2 has new start variable S and additional production S S1S2 58 Star Operation Context-free languages are closed under: Star-operation L is context free * L is context-free 59 Example Language Grammar L {a b } n n S aSb | Star Operation L {a b } * n n S1 SS1 | 60 In general: For context-free language L with context-free grammar G and start variable S The grammar of the star operation L * has new start variable S1 and additional production S1 SS1 | 61 Negative Properties of Context-Free Languages 62 Intersection Context-free languages are not closed under: intersection L1 is context free L1 L2 L2 is context free not necessarily context-free 63 Example L1 {a b c } n n m L2 {a b c } n m m Context-free: Context-free: S AC S AB A aAb | A aA | C cC | B bBc | Intersection L1 L2 {a b c } NOT context-free n n n 64 Complement Context-free languages are not closed under: complement L is context free L not necessarily context-free 65 Example L1 {a b c } n n m L2 {a b c } n m m Context-free: Context-free: S AC S AB A aAb | A aA | C cC | B bBc | Complement L1 L2 L1 L2 {a b c } n n n NOT context-free 66 Intersection of Context-free languages and Regular Languages 67 The intersection of a context-free language and a regular language is a context-free language L1 context free L1 L2 L2 regular context-free 68 Machine M1 Machine M2 NPDA for L1 DFA for L2 context-free regular Construct a new NPDA machine M that accepts L1 L2 M simulates in parallel M1 and M 2 69 NPDA M1 DFA M2 a, b c a q1 q2 p1 p2 transition transition NPDA M q1, p1 a, b c q2 , p2 transition 70 NPDA M1 DFA M2 , b c q1 q2 p1 transition NPDA M q1, p1 , b c q2 , p1 transition 71 NPDA M1 DFA M2 q0 p0 initial state initial state NPDA M q0 , p0 Initial state 72 NPDA M1 DFA M2 q1 p1 p2 final state final states NPDA M q1, p1 q1, p2 final states 73 Example: context-free L1 {w1w2 : | w1 || w2 |, w1 {a, b}*, w2 {c, d }*} NPDA M1 a, 1 c,1 b, 1 d ,1 q0 , q1 , q2 ,$ $ q3 74 regular * L2 {a, c} DFA M2 a, c p0 75 context-free Automaton for: L1 L2 {a nc n : n 0} NPDA M a, 1 c,1 q0 , p0 , q1, p0 , q2 , p0 ,$ $ q3 , p0 76 In General: M simulates in parallel M1 and M 2 M accepts string w if and only if M1 accepts string w and M2 accepts string w L( M ) L( M1) L( M 2 ) 77 Therefore: M is NPDA L ( M1 ) L ( M 2 ) is context-free L1 L2 is context-free 78 Applications of Regular Closure 79 The intersection of a context-free language and a regular language is a context-free language Regular Closure L1 context free L1 L2 L2 regular context-free 80 An Application of Regular Closure n n Prove that: L {a b : n 100, n 0} is context-free 81 We know: n n {a b : n 0} is context-free 82 We also know: L1 {a100 100 b } is regular L1 {(a b) } {a * 100 100 b } is regular 83 n n {a b } L1 {(a b) } {a * 100 100 b } context-free regular (regular closure) {a b } L1 n n context-free n n n n {a b } L1 {a b : n 100, n 0} L is context-free 84 Another Application of Regular Closure Prove that: L {w : na nb nc } is not context-free 85 If L {w : na nb nc } is context-free (regular closure) Then L {a * b * c*} {a b c } n n n context-free regular context-free Impossible!!! Therefore, L is not context free 86 Decidable Properties of Context-Free Languages 87 Membership Question: for context-free grammar G find if string w L (G ) Membership Algorithms: Parsers • Exhaustive search parser • CYK parsing algorithm 88 Empty Language Question: for context-free grammar G find if L (G ) Algorithm: 1. Remove useless variables 2. Check if start variable S is useless 89 Infinite Language Question: for context-free grammar G find if L (G ) is infinite Algorithm: 1. Remove useless variables 2. Remove unit and productions 3. Create dependency graph for variables 4. If there is a loop in the dependency graph then the language is infinite 90 Example: S AB A aCb | a B bB | bb C cBS Dependency graph Infinite language A C S B 91 S AB A aCb | a B bB | bb C cBS S AB aCbB acBSbB acbbSbbb S acbbSbbb(acbb) S (bbb) 2 2 (acbb) S (bbb) i i 92

DOCUMENT INFO

Shared By:

Categories:

Tags:

Stats:

views: | 2 |

posted: | 9/2/2011 |

language: | English |

pages: | 92 |

OTHER DOCS BY yaofenji

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.