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
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
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
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
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
Programming Languages Chan, Har-Peled, Hassanieh (UIUC) CS374 4 Spring 2019 4 / 36
Natural Language Processing Chan, Har-Peled, Hassanieh (UIUC) CS374 5 Spring 2019 5 / 36
Models of Growth L -systems http://www.kevs3d.co.uk/dev/lsystems/ Chan, Har-Peled, Hassanieh (UIUC) CS374 6 Spring 2019 6 / 36
Kolam drawing generated by grammar Chan, Har-Peled, Hassanieh (UIUC) CS374 7 Spring 2019 7 / 36
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
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
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
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
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
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
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
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
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
Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Chan, Har-Peled, Hassanieh (UIUC) CS374 12 Spring 2019 12 / 36
Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Chan, Har-Peled, Hassanieh (UIUC) CS374 12 Spring 2019 12 / 36
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
“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
“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
“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
“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
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
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
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
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
Recommend
More recommend