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 Languages and Grammars Lecture 7 February 7, 2017 Chandra Chekuri (UIUC) CS374 2 Spring 2017 2 / 31
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
Programming Languages Chandra Chekuri (UIUC) CS374 4 Spring 2017 4 / 31
Natural Language Processing Chandra Chekuri (UIUC) CS374 5 Spring 2017 5 / 31
Models of Growth L -systems http://www.kevs3d.co.uk/dev/lsystems/ Chandra Chekuri (UIUC) CS374 6 Spring 2017 6 / 31
Kolam drawing generated by grammar Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 31
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
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
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
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
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
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
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
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
Examples L = { 0 n 1 n | n ≥ 0 } Chandra Chekuri (UIUC) CS374 11 Spring 2017 11 / 31
Examples L = { 0 n 1 n | n ≥ 0 } S → ǫ | 0 S 1 Chandra Chekuri (UIUC) CS374 11 Spring 2017 11 / 31
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
“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
“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
“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
“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
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
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
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
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
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
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
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
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
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
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
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
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
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
Recommend
More recommend