context free languages and
play

Context Free Languages and 1 constant memory computation. Grammars - PowerPoint PPT Presentation

Algorithms & Models of Computation What stack got to do with it? CS/ECE 374, Fall 2017 Whats a stack but a second hand memory? DFA / NFA /Regular expressions. Context Free Languages and 1 constant memory computation. Grammars NFA +


  1. Algorithms & Models of Computation What stack got to do with it? CS/ECE 374, Fall 2017 What’s a stack but a second hand memory? DFA / NFA /Regular expressions. Context Free Languages and 1 ≡ constant memory computation. Grammars NFA + stack 2 ≡ context free grammars ( CFG ). Lecture 7 Turing machines DFA / NFA + unbounded memory. 3 ≡ a standard computer/program. Tuesday, September 19, 2017 ≡ NFA with two stacks. Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 36 Sariel Har-Peled (UIUC) CS374 2 Fall 2017 2 / 36 Context Free Languages and Grammars Programming Languages Programming Language Specification Parsing Natural language understanding Generative model giving structure . . . Sariel Har-Peled (UIUC) CS374 3 Fall 2017 3 / 36 Sariel Har-Peled (UIUC) CS374 4 Fall 2017 4 / 36

  2. Natural Language Processing Models of Growth L -systems http://www.kevs3d.co.uk/dev/lsystems/ Sariel Har-Peled (UIUC) CS374 5 Fall 2017 5 / 36 Sariel Har-Peled (UIUC) CS374 6 Fall 2017 6 / 36 Kolam drawing generated by grammar Context Free Grammar ( CFG ) Definition Definition A CFG is a quadruple G = ( V , T , P , S ) V is a finite set of non-terminal symbols T is a finite set of terminal symbols (alphabet) P is a finite set of productions, each of the form A → α where A ∈ V and α is a string in ( V ∪ T ) ∗ . Formally, P ⊂ V × ( V ∪ T ) ∗ . S ∈ V is a start symbol � � G = Variables , Terminals , Productions , Start var Sariel Har-Peled (UIUC) CS374 7 Fall 2017 7 / 36 Sariel Har-Peled (UIUC) CS374 8 Fall 2017 8 / 36

  3. Example Example formally... V = { S } V = { S } T = { a , b } T = { a , b } P = { S → ǫ | a | b | aSa | bSb } P = { S → ǫ | a | b | aSa | bSb } (abbrev. for S → ǫ, S → a , S → b , S → aSa , S → bSb ) (abbrev. for S → ǫ, S → a , S → b , S → aSa , S → bSb )     S → ǫ, S � aSa � abSba � abbSbba � abb b bba     S → a ,           G = { S } , { a , b } , S → b S     S → aSa What strings can S generate like this?           S → bSb   Sariel Har-Peled (UIUC) CS374 9 Fall 2017 9 / 36 Sariel Har-Peled (UIUC) CS374 10 Fall 2017 10 / 36 Palindromes Examples L = { 0 n 1 n | n ≥ 0 } Madam in Eden I’m Adam Dog doo? Good God! S → ǫ | 0 S 1 Dogma: I am God. A man, a plan, a canal, Panama Are we not drawn onward, we few, drawn onward to new era? Doc, note: I dissent. A fast never prevents a fatness. I diet on cod. http://www.palindromelist.net Sariel Har-Peled (UIUC) CS374 11 Fall 2017 11 / 36 Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 36

  4. Notation and Convention “Derives” relation Let G = ( V , T , P , S ) then Formalism for how strings are derived/generated a , b , c , d , . . . , in T (terminals) Definition A , B , C , D , . . . , in V (non-terminals) Let G = ( V , T , P , S ) be a CFG . For strings α 1 , α 2 ∈ ( V ∪ T ) ∗ u , v , w , x , y , . . . in T ∗ for strings of terminals we say α 1 derives α 2 denoted by α 1 � G α 2 if there exist strings β, γ, δ in ( V ∪ T ) ∗ such that α, β, γ, . . . in ( V ∪ T ) ∗ X , Y , X in V ∪ T α 1 = β A δ α 2 = βγδ A → γ is in P . Examples: S � ǫ , S � 0 S 1 , 0 S 1 � 00 S 11 , 0 S 1 � 01 . Sariel Har-Peled (UIUC) CS374 13 Fall 2017 13 / 36 Sariel Har-Peled (UIUC) CS374 14 Fall 2017 14 / 36 “Derives” relation continued Context Free Languages Definition Definition For integer k ≥ 0 , α 1 � k α 2 inductive defined: The language generated by CFG G = ( V , T , P , S ) is denoted by L ( G ) where L ( G ) = { w ∈ T ∗ | S � ∗ w } . α 1 � 0 α 2 if α 1 = α 2 α 1 � k α 2 if α 1 � β 1 and β 1 � k − 1 α 2 . Definition Alternative definition: α 1 � k α 2 if α 1 � k − 1 β 1 and β 1 � α 2 A language L is context free ( CFL ) if it is generated by a context free grammar. That is, there is a CFG G such that L = L ( G ) . ∗ is the reflexive and transitive closure of � . � ∗ α 2 if α 1 � k α 2 for some k . α 1 � ∗ 0000011111 . ∗ ǫ , 0 S 1 � Examples: S � Sariel Har-Peled (UIUC) CS374 15 Fall 2017 15 / 36 Sariel Har-Peled (UIUC) CS374 16 Fall 2017 16 / 36

  5. Example Closure Properties of CFLs L = { 0 n 1 n | n ≥ 0 } G 1 = ( V 1 , T , P 1 , S 1 ) and G 2 = ( V 2 , T , P 2 , S 2 ) Assumption: V 1 ∩ V 2 = ∅ , that is, non-terminals are not shared S → ǫ | 0 S 1 Theorem L = { 0 n 1 m | m > n } CFLs are closed under union. L 1 , L 2 CFLs implies L 1 ∪ L 2 is a CFL . � � ∗ � � � L = w ∈ ( , ) � w is properly nested string of parenthesis � Theorem CFLs are closed under concatenation. L 1 , L 2 CFLs implies L 1 · L 2 is a CFL . Theorem CFLs are closed under Kleene star. ⇒ L ∗ is a CFL . If L is a CFL = Sariel Har-Peled (UIUC) CS374 17 Fall 2017 17 / 36 Sariel Har-Peled (UIUC) CS374 18 Fall 2017 18 / 36 Closure Properties of CFLs Closure Properties of CFLs Union Concatenation G 1 = ( V 1 , T , P 1 , S 1 ) and G 2 = ( V 2 , T , P 2 , S 2 ) Theorem Assumption: V 1 ∩ V 2 = ∅ , that is, non-terminals are not shared. CFLs are closed under concatenation. L 1 , L 2 CFLs implies L 1 · L 2 Theorem is a CFL . CFLs are closed under union. L 1 , L 2 CFLs implies L 1 ∪ L 2 is a CFL . Sariel Har-Peled (UIUC) CS374 19 Fall 2017 19 / 36 Sariel Har-Peled (UIUC) CS374 20 Fall 2017 20 / 36

  6. Closure Properties of CFLs Exercise Stardom (i.e, Kleene star) Prove that every regular language is context-free using previous Theorem closure properties. CFLs are closed under Kleene star. Prove the set of regular expressions over an alphabet Σ forms a ⇒ L ∗ is a CFL . If L is a CFL = non-regular language which is context-free. Sariel Har-Peled (UIUC) CS374 21 Fall 2017 21 / 36 Sariel Har-Peled (UIUC) CS374 22 Fall 2017 22 / 36 Closure Properties of CFLs continued Canonical non- CFL Theorem Theorem L = { a n b n c n | n ≥ 0 } is not context-free. CFLs are not closed under complement or intersection. Proof based on pumping lemma for CFLs . Technical and outside Theorem the scope of this class. If L 1 is a CFL and L 2 is regular then L 1 ∩ L 2 is a CFL . Sariel Har-Peled (UIUC) CS374 23 Fall 2017 23 / 36 Sariel Har-Peled (UIUC) CS374 24 Fall 2017 24 / 36

  7. Parse Trees or Derivation Trees Example ∗ w . A tree to represent the derivation S � S A derivation tree for abbaab Rooted tree with root labeled S (also called “parse tree”) Non-terminals at each internal node of tree a S b Terminals at leaves Children of internal node indicate how non-terminal was b S a S à aSb | bSa | SS | ab | ba | ε expanded using a production rule A picture is worth a thousand words S S A corresponding derivation of abbaab b a ε S è aSb è abSab è abSSab è abbaSab è abbaab Sariel Har-Peled (UIUC) CS374 25 Fall 2017 25 / 36 Sariel Har-Peled (UIUC) CS374 26 Fall 2017 26 / 36 Ambiguity in CFLs Ambiguity in CFLs Definition Original grammar: S → S − S | 1 | 2 | 3 Unambiguous grammar: A CFG G is ambiguous if there is a string w ∈ L ( G ) with two S → S − C | 1 | 2 | 3 different parse trees. If there is no such string then G is C → 1 | 2 | 3 unambiguous. Example: S → S − S | 1 | 2 | 3 S S S – S C – The grammar forces a parse S S – S S corresponding to left-to-right S – C 1 evaluation. S – S S – S 3 1 3 2 1 3 2 2 (3–2)–1 3–(2–1) (3–2)–1 Sariel Har-Peled (UIUC) CS374 27 Fall 2017 27 / 36 Sariel Har-Peled (UIUC) CS374 28 Fall 2017 28 / 36

  8. Inherently ambiguous languages Inductive proofs for CFGs Question: How do we formally prove that a CFG L ( G ) = L ? Definition A CFL L is inherently ambiguous if there is no unambiguous CFG Example: S → ǫ | a | b | aSa | bSb G such that L = L ( G ) . Theorem There exist inherently ambiguous CFLs . Example: L = { a n b m c k | n = m or m = k } L ( G ) = { palindromes } = { w | w = w R } Given a grammar G it is undecidable to check whether L ( G ) is Two directions: inherently ambiguous. No algorithm! ∗ w then w = w R L ( G ) ⊆ L , that is, S � L ⊆ L ( G ) , that is, w = w R then S � ∗ w Sariel Har-Peled (UIUC) CS374 29 Fall 2017 29 / 36 Sariel Har-Peled (UIUC) CS374 30 Fall 2017 30 / 36 L(G) ⊆ L L ⊆ L(G) ∗ w then w = w R Show that if w = w R then S � ∗ w . Show that if S � By induction on length of derivation, meaning By induction on | w | ∗ k w implies w = w R . That is, for all k ≥ 0 , | w | = k and w = w R implies S � ∗ w . For all k ≥ 1 , S � If S � 1 w then w = ǫ or w = a or w = b . Each case Exercise: Fill in proof. w = w R . Assume that for all k < n , that if S → k w then w = w R Let S � n w (with n > 1 ). Wlog w begin with a . ◮ Then S → aSa � k − 1 aua where w = aua . ◮ And S � n − 1 u and hence IH, u = u R . ◮ Therefore w r = ( aua ) R = ( ua ) R a = au R a = aua = w . Sariel Har-Peled (UIUC) CS374 31 Fall 2017 31 / 36 Sariel Har-Peled (UIUC) CS374 32 Fall 2017 32 / 36

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend