context free languages and grammars
play

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

Algorithms & Models of Computation CS/ECE 374, Fall 2017 Context Free Languages and Grammars Lecture 7 Tuesday, September 19, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 36 What stack got to do with it? Whats a stack but a


  1. Algorithms & Models of Computation CS/ECE 374, Fall 2017 Context Free Languages and Grammars Lecture 7 Tuesday, September 19, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 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. Sariel Har-Peled (UIUC) CS374 2 Fall 2017 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. Sariel Har-Peled (UIUC) CS374 2 Fall 2017 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. Sariel Har-Peled (UIUC) CS374 2 Fall 2017 2 / 36

  5. Context Free Languages and Grammars Programming Language Specification Parsing Natural language understanding Generative model giving structure . . . Sariel Har-Peled (UIUC) CS374 3 Fall 2017 3 / 36

  6. Programming Languages Sariel Har-Peled (UIUC) CS374 4 Fall 2017 4 / 36

  7. Natural Language Processing Sariel Har-Peled (UIUC) CS374 5 Fall 2017 5 / 36

  8. Models of Growth L -systems http://www.kevs3d.co.uk/dev/lsystems/ Sariel Har-Peled (UIUC) CS374 6 Fall 2017 6 / 36

  9. Kolam drawing generated by grammar Sariel Har-Peled (UIUC) CS374 7 Fall 2017 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 Sariel Har-Peled (UIUC) CS374 8 Fall 2017 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 Sariel Har-Peled (UIUC) CS374 8 Fall 2017 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 Sariel Har-Peled (UIUC) CS374 8 Fall 2017 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 Sariel Har-Peled (UIUC) CS374 8 Fall 2017 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? Sariel Har-Peled (UIUC) CS374 9 Fall 2017 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? Sariel Har-Peled (UIUC) CS374 9 Fall 2017 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? Sariel Har-Peled (UIUC) CS374 9 Fall 2017 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   Sariel Har-Peled (UIUC) CS374 10 Fall 2017 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 Sariel Har-Peled (UIUC) CS374 11 Fall 2017 11 / 36

  19. Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 36

  20. Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Sariel Har-Peled (UIUC) CS374 12 Fall 2017 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 Sariel Har-Peled (UIUC) CS374 13 Fall 2017 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 . Sariel Har-Peled (UIUC) CS374 14 Fall 2017 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 � Sariel Har-Peled (UIUC) CS374 15 Fall 2017 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 � Sariel Har-Peled (UIUC) CS374 15 Fall 2017 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 � Sariel Har-Peled (UIUC) CS374 15 Fall 2017 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 ) . Sariel Har-Peled (UIUC) CS374 16 Fall 2017 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 ) . Sariel Har-Peled (UIUC) CS374 16 Fall 2017 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 � Sariel Har-Peled (UIUC) CS374 17 Fall 2017 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 = Sariel Har-Peled (UIUC) CS374 18 Fall 2017 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