context free languages and grammars
play

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

Algorithms & Models of Computation CS/ECE 374 B, Spring 2020 Context Free Languages and Grammars Lecture 7 Wednesday, February 12, 2020 L A T EXed: January 19, 2020 04:15 Miller, Hassanieh (UIUC) CS374 1 Spring 2020 1 / 44 Regular


  1. Algorithms & Models of Computation CS/ECE 374 B, Spring 2020 Context Free Languages and Grammars Lecture 7 Wednesday, February 12, 2020 L A T EXed: January 19, 2020 04:15 Miller, Hassanieh (UIUC) CS374 1 Spring 2020 1 / 44

  2. Regular Languages Regular expressions allow us to describe/express a class of languages compactly and precisely. Equivalence with DFAs show the following: given any regular expression r there is a very efficient algorithm for solving the language recognition problem for L ( r ) : given w ∈ Σ ∗ is w ∈ L ( r ) ? Miller, Hassanieh (UIUC) CS374 2 Spring 2020 2 / 44

  3. Regular Languages Regular expressions allow us to describe/express a class of languages compactly and precisely. Equivalence with DFAs show the following: given any regular expression r there is a very efficient algorithm for solving the language recognition problem for L ( r ) : given w ∈ Σ ∗ is w ∈ L ( r ) ? In fact the running time of the algorithm is linear in | w | . Miller, Hassanieh (UIUC) CS374 2 Spring 2020 2 / 44

  4. Regular Languages Regular expressions allow us to describe/express a class of languages compactly and precisely. Equivalence with DFAs show the following: given any regular expression r there is a very efficient algorithm for solving the language recognition problem for L ( r ) : given w ∈ Σ ∗ is w ∈ L ( r ) ? In fact the running time of the algorithm is linear in | w | . Disadvantage of regular expressions/languages: Miller, Hassanieh (UIUC) CS374 2 Spring 2020 2 / 44

  5. Regular Languages Regular expressions allow us to describe/express a class of languages compactly and precisely. Equivalence with DFAs show the following: given any regular expression r there is a very efficient algorithm for solving the language recognition problem for L ( r ) : given w ∈ Σ ∗ is w ∈ L ( r ) ? In fact the running time of the algorithm is linear in | w | . Disadvantage of regular expressions/languages: too simple and cannot express interesting features such as balanced parenthesis that we need in programming languages. No recursion allowed even in limited form. Miller, Hassanieh (UIUC) CS374 2 Spring 2020 2 / 44

  6. Language classes: Chomsky Hierarchy Generative models for languages based on grammars. All Recursively Enumerable Context Sensitive Context Free Regular Miller, Hassanieh (UIUC) CS374 3 Spring 2020 3 / 44

  7. Chomsky Hierarchy and Machines For each class one can define a corresponding class of machines. All Recursively Enumerable TM Context Sensitive LBA Context Free PDA Regular DFA Miller, Hassanieh (UIUC) CS374 4 Spring 2020 4 / 44

  8. Regular vs. Context Free Languages Regular Languages: Built from strings using: Sequencing 1 Branching 2 Repetition 3 Miller, Hassanieh (UIUC) CS374 5 Spring 2020 5 / 44

  9. Regular vs. Context Free Languages Regular Languages: Built from strings using: Sequencing 1 Branching 2 Repetition 3 Context Free Languages: Built from strings using: Sequencing 1 Branching 2 Recursion 3 Miller, Hassanieh (UIUC) CS374 5 Spring 2020 5 / 44

  10. 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. Miller, Hassanieh (UIUC) CS374 6 Spring 2020 6 / 44

  11. 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. Miller, Hassanieh (UIUC) CS374 6 Spring 2020 6 / 44

  12. 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. Miller, Hassanieh (UIUC) CS374 6 Spring 2020 6 / 44

  13. Programming Language Design Question: What is a valid C program? Or a Python program? Question: Given a string w what is an algorithm to check whether w is a valid C program? The parsing problem. Miller, Hassanieh (UIUC) CS374 7 Spring 2020 7 / 44

  14. Context Free Languages and Grammars Programming Language Specification Parsing Natural language understanding Generative model giving structure . . . CFLs provide a good balance between expressivity and tractability. Limited form of recursion. Miller, Hassanieh (UIUC) CS374 8 Spring 2020 8 / 44

  15. Programming Languages Miller, Hassanieh (UIUC) CS374 9 Spring 2020 9 / 44

  16. Natural Language Processing Miller, Hassanieh (UIUC) CS374 10 Spring 2020 10 / 44

  17. Models of Growth L -systems http://www.kevs3d.co.uk/dev/lsystems/ Miller, Hassanieh (UIUC) CS374 11 Spring 2020 11 / 44

  18. Kolam drawing generated by grammar Miller, Hassanieh (UIUC) CS374 12 Spring 2020 12 / 44

  19. 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 � � G = Variables , Terminals , Productions , Start var Miller, Hassanieh (UIUC) CS374 13 Spring 2020 13 / 44

  20. 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) � � G = Variables , Terminals , Productions , Start var Miller, Hassanieh (UIUC) CS374 13 Spring 2020 13 / 44

  21. 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 ) ∗ . � � G = Variables , Terminals , Productions , Start var Miller, Hassanieh (UIUC) CS374 13 Spring 2020 13 / 44

  22. 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 Miller, Hassanieh (UIUC) CS374 13 Spring 2020 13 / 44

  23. Example V = { S } T = { a , b } P = { S → ǫ | a | b | aSa | bSb } (abbrev. for S → ǫ, S → a , S → b , S → aSa , S → bSb ) Miller, Hassanieh (UIUC) CS374 14 Spring 2020 14 / 44

  24. 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 Miller, Hassanieh (UIUC) CS374 14 Spring 2020 14 / 44

  25. 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? Miller, Hassanieh (UIUC) CS374 14 Spring 2020 14 / 44

  26. 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   Miller, Hassanieh (UIUC) CS374 15 Spring 2020 15 / 44

  27. 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 Miller, Hassanieh (UIUC) CS374 16 Spring 2020 16 / 44

  28. Examples L = { 0 n 1 n | n ≥ 0 } Miller, Hassanieh (UIUC) CS374 17 Spring 2020 17 / 44

  29. Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Miller, Hassanieh (UIUC) CS374 17 Spring 2020 17 / 44

  30. 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 Miller, Hassanieh (UIUC) CS374 18 Spring 2020 18 / 44

  31. “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 . Miller, Hassanieh (UIUC) CS374 19 Spring 2020 19 / 44

  32. “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 . Miller, Hassanieh (UIUC) CS374 20 Spring 2020 20 / 44

Recommend


More recommend