context free languages and grammars
play

Context Free Languages and Grammars Lecture 7 Tuesday, February 5, - PowerPoint PPT Presentation

Algorithms & Models of Computation CS/ECE 374, Spring 2019 Context Free Languages and Grammars Lecture 7 Tuesday, February 5, 2019 L A T EXed: December 27, 2018 08:25 Chan, Har-Peled, Hassanieh (UIUC) CS374 1 Spring 2019 1 / 36


  1. Algorithms & Models of Computation CS/ECE 374, Spring 2019 Context Free Languages and Grammars Lecture 7 Tuesday, February 5, 2019 L A T EXed: December 27, 2018 08:25 Chan, Har-Peled, Hassanieh (UIUC) CS374 1 Spring 2019 1 / 36

  2. What stack got to do with it? What’s a stack but a second hand memory? DFA / NFA /Regular expressions. 1 ≡ constant memory computation. Turing machines DFA / NFA + unbounded memory. 2 ≡ a standard computer/program. Chan, Har-Peled, Hassanieh (UIUC) CS374 2 Spring 2019 2 / 36

  3. What stack got to do with it? What’s a stack but a second hand memory? DFA / NFA /Regular expressions. 1 ≡ constant memory computation. NFA + stack 2 ≡ context free grammars ( CFG ). Turing machines DFA / NFA + unbounded memory. 3 ≡ a standard computer/program. Chan, Har-Peled, Hassanieh (UIUC) CS374 2 Spring 2019 2 / 36

  4. What stack got to do with it? What’s a stack but a second hand memory? DFA / NFA /Regular expressions. 1 ≡ constant memory computation. NFA + stack 2 ≡ context free grammars ( CFG ). Turing machines DFA / NFA + unbounded memory. 3 ≡ a standard computer/program. ≡ NFA with two stacks. Chan, Har-Peled, Hassanieh (UIUC) CS374 2 Spring 2019 2 / 36

  5. Context Free Languages and Grammars Programming Language Specification Parsing Natural language understanding Generative model giving structure . . . Chan, Har-Peled, Hassanieh (UIUC) CS374 3 Spring 2019 3 / 36

  6. Programming Languages Chan, Har-Peled, Hassanieh (UIUC) CS374 4 Spring 2019 4 / 36

  7. Natural Language Processing Chan, Har-Peled, Hassanieh (UIUC) CS374 5 Spring 2019 5 / 36

  8. Models of Growth L -systems http://www.kevs3d.co.uk/dev/lsystems/ Chan, Har-Peled, Hassanieh (UIUC) CS374 6 Spring 2019 6 / 36

  9. Kolam drawing generated by grammar Chan, Har-Peled, Hassanieh (UIUC) CS374 7 Spring 2019 7 / 36

  10. 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 Chan, Har-Peled, Hassanieh (UIUC) CS374 8 Spring 2019 8 / 36

  11. 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 Chan, Har-Peled, Hassanieh (UIUC) CS374 8 Spring 2019 8 / 36

  12. 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 Chan, Har-Peled, Hassanieh (UIUC) CS374 8 Spring 2019 8 / 36

  13. 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 Chan, Har-Peled, Hassanieh (UIUC) CS374 8 Spring 2019 8 / 36

  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 � abb b bba What strings can S generate like this? Chan, Har-Peled, Hassanieh (UIUC) CS374 9 Spring 2019 9 / 36

  15. 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 � abb b bba What strings can S generate like this? Chan, Har-Peled, Hassanieh (UIUC) CS374 9 Spring 2019 9 / 36

  16. 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 � abb b bba What strings can S generate like this? Chan, Har-Peled, Hassanieh (UIUC) CS374 9 Spring 2019 9 / 36

  17. Example formally... V = { S } T = { a , b } P = { S → ǫ | a | b | aSa | bSb } (abbrev. for S → ǫ, S → a , S → b , S → aSa , S → bSb )     S → ǫ,     S → a ,           G = { S } , { a , b } , S → b S     S → aSa           S → bSb   Chan, Har-Peled, Hassanieh (UIUC) CS374 10 Spring 2019 10 / 36

  18. 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 Chan, Har-Peled, Hassanieh (UIUC) CS374 11 Spring 2019 11 / 36

  19. Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Chan, Har-Peled, Hassanieh (UIUC) CS374 12 Spring 2019 12 / 36

  20. Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Chan, Har-Peled, Hassanieh (UIUC) CS374 12 Spring 2019 12 / 36

  21. 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 Chan, Har-Peled, Hassanieh (UIUC) CS374 13 Spring 2019 13 / 36

  22. “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 . Chan, Har-Peled, Hassanieh (UIUC) CS374 14 Spring 2019 14 / 36

  23. “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 definition: α 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 � Chan, Har-Peled, Hassanieh (UIUC) CS374 15 Spring 2019 15 / 36

  24. “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 definition: α 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 � Chan, Har-Peled, Hassanieh (UIUC) CS374 15 Spring 2019 15 / 36

  25. “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 definition: α 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 � Chan, Har-Peled, Hassanieh (UIUC) CS374 15 Spring 2019 15 / 36

  26. 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 ) . Chan, Har-Peled, Hassanieh (UIUC) CS374 16 Spring 2019 16 / 36

  27. 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 ) . Chan, Har-Peled, Hassanieh (UIUC) CS374 16 Spring 2019 16 / 36

  28. 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 � Chan, Har-Peled, Hassanieh (UIUC) CS374 17 Spring 2019 17 / 36

  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 ∗ is a CFL . If L is a CFL = Chan, Har-Peled, Hassanieh (UIUC) CS374 18 Spring 2019 18 / 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