cs 374 algorithms models of computation
play

CS 374: Algorithms & Models of Computation Chandra Chekuri - PowerPoint PPT Presentation

CS 374: Algorithms & Models of Computation Chandra Chekuri University of Illinois, Urbana-Champaign Spring 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 31 CS 374: Algorithms & Models of Computation, Spring 2017 Context Free


  1. CS 374: Algorithms & Models of Computation Chandra Chekuri University of Illinois, Urbana-Champaign Spring 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 31

  2. CS 374: Algorithms & Models of Computation, Spring 2017 Context Free Languages and Grammars Lecture 7 February 7, 2017 Chandra Chekuri (UIUC) CS374 2 Spring 2017 2 / 31

  3. Context Free Languages and Grammars Programming Language Specification Parsing Natural language understanding Generative model giving structure . . . Chandra Chekuri (UIUC) CS374 3 Spring 2017 3 / 31

  4. Programming Languages Chandra Chekuri (UIUC) CS374 4 Spring 2017 4 / 31

  5. Natural Language Processing Chandra Chekuri (UIUC) CS374 5 Spring 2017 5 / 31

  6. Models of Growth L -systems http://www.kevs3d.co.uk/dev/lsystems/ Chandra Chekuri (UIUC) CS374 6 Spring 2017 6 / 31

  7. Kolam drawing generated by grammar Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 31

  8. Context Free Grammar (CFG) Definition Definition A CFG is is a quadruple G = ( V , T , P , S ) V is a finite set of non-terminal symbols Chandra Chekuri (UIUC) CS374 8 Spring 2017 8 / 31

  9. Context Free Grammar (CFG) Definition Definition A CFG is 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) Chandra Chekuri (UIUC) CS374 8 Spring 2017 8 / 31

  10. Context Free Grammar (CFG) Definition Definition A CFG is 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 ) ∗ . Chandra Chekuri (UIUC) CS374 8 Spring 2017 8 / 31

  11. Context Free Grammar (CFG) Definition Definition A CFG is 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 Chandra Chekuri (UIUC) CS374 8 Spring 2017 8 / 31

  12. Example V = { S } T = { a , b } P = { S → ǫ | a | b | aSa | bSb } (abbrev. for S → ǫ, S → a , S → b , S → aSa , S → bSb ) Chandra Chekuri (UIUC) CS374 9 Spring 2017 9 / 31

  13. Example V = { S } T = { a , b } P = { S → ǫ | a | b | aSa | bSb } (abbrev. for S → ǫ, S → a , S → b , S → aSa , S → bSb ) S � aSA � abSba � abbSBba � abbba Chandra Chekuri (UIUC) CS374 9 Spring 2017 9 / 31

  14. Example V = { S } T = { a , b } P = { S → ǫ | a | b | aSa | bSb } (abbrev. for S → ǫ, S → a , S → b , S → aSa , S → bSb ) S � aSA � abSba � abbSBba � abbba What strings can S generate like this? Chandra Chekuri (UIUC) CS374 9 Spring 2017 9 / 31

  15. Palindromes Madam in Eden I’m Adam Dog doo? Good God! 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 Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 31

  16. Examples L = { 0 n 1 n | n ≥ 0 } Chandra Chekuri (UIUC) CS374 11 Spring 2017 11 / 31

  17. Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Chandra Chekuri (UIUC) CS374 11 Spring 2017 11 / 31

  18. Notation and Convention Let G = ( V , T , P , S ) then a , b , c , d , . . . , in T (terminals) A , B , C , D , . . . , in V (non-terminals) u , v , w , x , y , . . . in T ∗ for strings of terminals α, β, γ, . . . in ( V ∪ T ) ∗ X , Y , X in V ∪ T Chandra Chekuri (UIUC) CS374 12 Spring 2017 12 / 31

  19. “Derives” relation Formalism for how strings are derived/generated Definition Let G = ( V , T , P , S ) be a CFG. For strings α 1 , α 2 ∈ ( V ∪ T ) ∗ we say α 1 derives α 2 denoted by α 1 � G α 2 if there exist strings β, γ, δ in ( V ∪ T ) ∗ such that α 1 = β A δ α 2 = βγδ A → γ is in P . Examples: S � ǫ , S � 0 S 1 , 0 S 1 � 00 S 11 , 0 S 1 � 01 . Chandra Chekuri (UIUC) CS374 13 Spring 2017 13 / 31

  20. “Derives” relation continued Definition For integer k ≥ 0 , α 1 � k α 2 inductive defined: α 1 � 0 α 2 if α 1 = α 2 α 1 � k α 2 if α 1 � β 1 and β 1 � k − 1 α 2 . Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 31

  21. “Derives” relation continued Definition For integer k ≥ 0 , α 1 � k α 2 inductive defined: α 1 � 0 α 2 if α 1 = α 2 α 1 � k α 2 if α 1 � β 1 and β 1 � k − 1 α 2 . Alternative defn: α 1 � k α 2 if α 1 � k − 1 β 1 and β 1 � α 2 Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 31

  22. “Derives” relation continued Definition For integer k ≥ 0 , α 1 � k α 2 inductive defined: α 1 � 0 α 2 if α 1 = α 2 α 1 � k α 2 if α 1 � β 1 and β 1 � k − 1 α 2 . Alternative defn: α 1 � k α 2 if α 1 � k − 1 β 1 and β 1 � α 2 ∗ is the reflexive and transitive closure of � . � ∗ α 2 if α 1 � k α 2 for some k . α 1 � ∗ 0000011111 . ∗ ǫ , 0 S 1 � Examples: S � Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 31

  23. Context Free Languages Definition The language generated by CFG G = ( V , T , P , S ) is denoted by L ( G ) where L ( G ) = { w ∈ T ∗ | S � ∗ w } . Chandra Chekuri (UIUC) CS374 15 Spring 2017 15 / 31

  24. Context Free Languages Definition The language generated by CFG G = ( V , T , P , S ) is denoted by L ( G ) where L ( G ) = { w ∈ T ∗ | S � ∗ w } . Definition 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 ) . Chandra Chekuri (UIUC) CS374 15 Spring 2017 15 / 31

  25. Example L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 L = { 0 n 1 m | m > n } L = { w ∈ { ( , ) } ∗ | w is properly nested string of parenthesis } Chandra Chekuri (UIUC) CS374 16 Spring 2017 16 / 31

  26. Closure Properties of CFLs 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 Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 31

  27. Closure Properties of CFLs 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 Theorem CFLs are closed under union. L 1 , L 2 CFLs implies L 1 ∪ L 2 is a CFL. Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 31

  28. Closure Properties of CFLs 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 Theorem CFLs are closed under union. L 1 , L 2 CFLs implies L 1 ∪ L 2 is a CFL. Theorem CFLs are closed under concatenation. L 1 , L 2 CFLs implies L 1 · L 2 is a CFL. Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 31

  29. Closure Properties of CFLs 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 Theorem CFLs are closed under union. L 1 , L 2 CFLs implies L 1 ∪ L 2 is a CFL. 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 CFL implies L ∗ is a CFL. Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 31

  30. Closure Properties of CFLs 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 Theorem CFLs are closed under union. L 1 , L 2 CFLs implies L 1 ∪ L 2 is a CFL. 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 CFL implies L ∗ is a CFL. Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 31

  31. Exercise Prove that every regular language is context-free using previous closure properties. Prove the set of regular expressions over an alphabet Σ forms a non-regular language which is context-free. Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 31

  32. Closure Properties of CFLs continued Theorem CFLs are not closed under complement or intersection. Theorem If L 1 is a CFL and L 2 is regular then L 1 ∩ L 2 is a CFL. Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 31

  33. Canonical non-CFL Theorem L = { a n b n c n | n ≥ 0 } is not context-free. Proof based on pumping lemma for CFLs. Technical and outside the scope of this class. Chandra Chekuri (UIUC) CS374 20 Spring 2017 20 / 31

  34. Parse Trees or Derivation Trees ∗ w . A tree to represent the derivation S � Rooted tree with root labeled S Non-terminals at each internal node of tree Terminals at leaves Children of internal node indicate how non-terminal was expanded using a production rule Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 31

  35. Parse Trees or Derivation Trees ∗ w . A tree to represent the derivation S � Rooted tree with root labeled S Non-terminals at each internal node of tree Terminals at leaves Children of internal node indicate how non-terminal was expanded using a production rule A picture is worth a thousand words Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 31

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