COMP-330 Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lec. 10 : Context-Free Grammars
COMP 330 Fall 2017: Lectures Schedule 1-2. Introduction 1.5. Some basic mathematics 2-3. Deterministic finite automata +Closure properties, 4. Nondeterministic finite automata 5. Minimization 6. Determinization+Kleene s theorem 7. Regular Expressions+GNFA 8. The pumping lemma 9. Myhill-Nerode theorem 10. Context-Free Grammar 11. PushDown automata 12.CFG - PDA equivalence 13. CFG Pumping lemma and applications 14. MIDTERM 15. 16. 17. 18. Introduction to computability 19. Models of computation Basic computability theory 20. Reducibility, undecidability and Rice s theorem 21. Undecidable problems about CFGs 22. Post Correspondence Problem 23. Validity of FOL is RE / Gödel s and Tarski s thms 24. Universality / The recursion theorem 25. Degrees of undecidability 26. Introduction to complexity
Context-Free Grammars Let s call the following grammar G 1 : A 0A1 A B B # Derivation of a string 000#111 : A 0A1 00A11 000A111 000B111 000#111.
Definition of CFG Variables A, B, C, TERM, EXPR Alphabet (of terminals) 0, 1, # Substitution Rules A 0A1 EXPR TERM Start Variable A (left-hand side of the first substitution rule)
Definition of CFG
Parse Tree
Definition of CFL If u, v and w are strings of variables and terminals, and A w is a rule of the grammar, we say that uav yields uwv, written uav uwv. We say that u derives v ( u * v ) if u=v or if u u 1 u 2... u k v, k 0. The language of G is { w * S * w }.
Context-Free Grammars Formally, grammar G 1 : V = {A,B} = {0,1,#} R = {A 0A1 B, B #} S = A L(G 1 ) = { 0 n #1 n n 0 }.
Example of CFG Rules of grammar G 2 :
Example of CFG ARTICLE a the means Rules of grammar G 2 : ARTICLE a ARTICLE the
Regular Operations : Kleene s theorem (CFG)
Regular Operations : Kleene s theorem (CFL) CFLs
Kleene s theorem (CFL) Let G A =(V A,,R A,S A ) be a CFG generating L A and G B =(V B,,R B,S B ) be a CFG generating L B (V A V B = ). Consider G U =( {S U } V A V B,,{S U S A S B } R A R B,S U ). L U = L A L B.
V = {A,B} = {0,1,#} R 1 = {A 0A1 B, B #} S = A R 2 : Let G 1 =({A,B},{0,1,#},R 1,A) be a CFG generating L 1 and G 2 =({ SENTENCE, NOUN-PHRASE, VERB-PHRASE, PREP-PHRASE, CMPLX-NOUN, CMPLX-VERB, ARTICLE, NOUN, VERB, PREP }, {a,b,c,,z," "}, R 2, SENTENCE ) be a CFG generating L 2. Let G U =( {S U,A,B, SENTENCE, NOUN-PHRASE, VERB-PHRASE, PREP-PHRASE, CMPLX-NOUN, CMPLX-VERB, ARTICLE, NOUN, VERB, PREP }, {0,1,#,a,b,c,,z," "}, {S U A SENTENCE } R 1 R 2,S U ). L U = L 1 L 2.
Regular Operations : Kleene s theorem (CFL) CFLs
Kleene s theorem (CFL) Let G A =(V A,,R A,S A ) be a CFG generating L A and G B =(V B,,R B,S B ) be a CFG generating L B (V A V B = ). Consider G C =( {S C } V A V B,,{S C S A S B } R A R B,S C ). L C = L A L B.
V = {A,B} = {0,1,#} R 1 = {A 0A1 B, B #} S = A R 2 : Let G 1 =({A,B},{0,1,#},R 1,A) be a CFG generating L 1 and G 2 =({ SENTENCE, NOUN-PHRASE, VERB-PHRASE, PREP-PHRASE, CMPLX-NOUN, CMPLX-VERB, ARTICLE, NOUN, VERB, PREP }, {a,b,c,,z," "}, R 2, SENTENCE ) be a CFG generating L 2. Let G C =( {S C,A,B, SENTENCE, NOUN-PHRASE, VERB-PHRASE, PREP-PHRASE, CMPLX-NOUN, CMPLX-VERB, ARTICLE, NOUN, VERB, PREP }, {0,1,#,a,b,c,,z," "}, { S C A SENTENCE } R 1 R 2,S C ). L C = L 1 L 2.
Regular Operations : Kleene s theorem (CFL) CFLs
Kleene s theorem (CFL) Let G A =(V A,,R A,S A ) be a CFG generating L A. Consider G S =( {S S } V A,,{S S ε S A S S } R A,S S ). L S = (L A )*.
V = {A,B} = {0,1,#} R 1 = {A 0A1 B, B #} S = A Let G 1 =({A,B},{0,1,#},R 1,A) be a CFG generating L 1. Let G S =( {S S,A,B}, {0,1,#}, { S S ε AS S, A 0A1 B, B #}, S S ). L S = (L 1 )*.
Construction tools (and Reductions) CFLs are closed under union, concatenation and star. If there exists a CFL C s. t. either A*=A, A C=A, A C=A (but not complement nor intersection) or any combinations of these operations then A is a CFL as long as A is. ( If A is NON-CFL then so is A. )
Construction tools Constructing a CFG for a regular language L: M = (Q={q 0,q 1,...,q k },,δ,q 0,F) is converted to G = (V={R 0,R 1,...,R k },,R,S=R 0 ) where R contains rule R i ar j for each δ(q i,a) = q j in M, and rule R i ε for each accept-state q i F. R 0 is the start variable.
extra EXAMPLE of CFG
extra EXAMPLE of CFG
Ambiguity in CFGs
Leftmost Derivation A derivation is Leftmost if every time a variable is substituted, it is always the leftmost variable. E X A M P L E
Ambiguity A string w is derived ambiguously by a CFG G if it has two or more distinct leftmost derivations. Grammar G is ambigious if it generates some string ambiguously.
Ambiguity Ambiguity is not desirable in CFG because it may lead to unexpected interpretations of a string, for instance in the context of arithmetic expressions or programming languages. However, some languages are inherently ambiguous, meaning that all grammars generating this language must be ambiguous. example : {a i b j c k i=j or j=k}
Ambiguous version of example 2.4 G 5
Ambiguous CFG
Noam Chomsky Chomsky Normal Form
Chomsky Normal Form
Chomsky Normal Form
157
Proof: First, we add a new start variable S 0 and the rule S 0 S, where S was the original start variable.
Chomsky Normal Form
Second, we take care of all ε-rules. We remove an ε-rule "A ε", where A is not the start variable. Then for each occurrence of A on the righthand side of a rule we add a new rule with that occurrence deleted. Accordingly, each rule "R A" is replaced by "R ε" unless it has been already removed.
Chomsky Normal Form
Third, we handle all unit rules by removing each unit rule A B. In consequence whenever B u appears, we add the rule A u unless this is a unit rule previously removed.
Chomsky Normal Form
Finally, we convert all remaining rules as follows: A u 1 u 2...u k for k>2, where each u i is a variable or terminal with a series of rules A u 1 A 1, A 1 u 2 A 2,..., A k-2 u k-1 u k where each A i is a new variable. When k=2, and A u 1 u 2, we may replace any terminal u i by a variable U i and the rule U i u i.
Chomsky Normal Form
Chomsky Normal Form
COMP-330 Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lec. 10 : Context-Free Grammars