Fall 2012 -- Prof. Claude Crépeau
Fall 2012 -- Prof. Claude Crépeau Fall 2012 -- Prof. Claude Crépeau
COMP-330 Theory of Computation Fall 2012 -- Prof. Claude Crépeau Lec. 9 : Context-Free Grammars 1
- Page 2 and 3: COMP 330 Fall 2012: Lectures Schedu
- Page 4 and 5: Definition of CFG Variables A, B, C
- Page 6 and 7: Parse Tree 6
- Page 8 and 9: Context-Free Grammars Formally, gra
- Page 10: Example of CFG ⟨ARTICLE⟩ a | t
- Page 20 and 21: Regular Operations : Kleene’s the
- Page 22 and 23: Kleene’s theorem (CFL) Let G A =(
- Page 24 and 25: Kleene’s theorem (CFL) Let G A =(
- Page 26 and 27: Kleene’s theorem (CFL) 22
- Page 28 and 29: Kleene’s theorem (CFL) Let G A =(
- Page 30 and 31: Regular Operations : Kleene’s the
- Page 32 and 33: Kleene’s theorem (CFL) Let G A =(
- Page 34 and 35: Kleene’s theorem (CFL) Let G A =(
- Page 36 and 37: Construction tools Constructing a C
- Page 38 and 39: extra EXAMPLE of CFG 28
- Page 40 and 41: Leftmost Derivation A derivation is
- Page 42 and 43: Ambiguity Ambiguity is not desirabl
- Page 44 and 45: Ambiguous CFG 34
- Page 46 and 47: Chomsky Normal Form 36
- Page 48: 157 38
- Page 51 and 52: Proof: First, we add a new start va
COMP-330<br />
Theory of Computation<br />
<strong>Fall</strong> <strong>2012</strong> -- <strong>Prof</strong>. <strong>Claude</strong> <strong>Crépeau</strong><br />
Lec. 9 : Context-Free<br />
Grammars<br />
1
COMP 330 <strong>Fall</strong> <strong>2012</strong>:<br />
Lectures Schedule<br />
1. Introduction<br />
1.5. Some basic mathematics<br />
2. Deterministic finite automata<br />
+Closure properties,<br />
3. Nondeterministic finite automata<br />
4. Determinization+Kleene’s theorem<br />
5-6. Regular Expressions, GNFA and<br />
Regular Languages<br />
6-7. The pumping lemma<br />
8. Minimization+Myhill-Nerode theorem<br />
9. Context-free languages<br />
13. MIDTERM<br />
15. Pushdown automata<br />
16. Parsing<br />
17. The pumping lemma for CFLs<br />
18. Introduction to computability<br />
19. Models of computation<br />
Basic computability theory<br />
20. Reducibility, undecidability and Rice’s theorem<br />
21. Undecidable problems about CFGs<br />
22. Post Correspondence Problem<br />
23. Validity of FOL is RE / Gödel’s and Tarski’s thms<br />
24. Universality / The recursion theorem<br />
25. Degrees of undecidability<br />
26. Introduction to complexity<br />
2
Context-Free Grammars<br />
Let’s call the following grammar G 1 :<br />
A 0A1<br />
A B<br />
B #<br />
Derivation of a string “000#111” :<br />
A0A100A11000A111000B111000#111.<br />
3
Definition of CFG<br />
Variables<br />
A, B, C, ⟨TERM⟩, ⟨EXPR⟩<br />
Alphabet (of terminals)<br />
0,1,#<br />
Substitution Rules<br />
Start Variable<br />
A 0A1<br />
⟨EXPR⟩ ⟨TERM⟩<br />
A<br />
(left-hand side of the first substitution rule)<br />
4
Definition of CFG<br />
5
Parse Tree<br />
6
Definition of CFL<br />
If u, v and w are strings of variables and<br />
terminals, and A w is a rule of the<br />
grammar, we say that uAv yields uwv, written<br />
uAvuwv.<br />
We say that u derives v ( u * v ) if u=v or if<br />
uu 1 u 2 ...u k v, k≥0.<br />
The language of G is { w∈∑* | S * w }.<br />
7
Context-Free Grammars<br />
Formally, grammar G 1 :<br />
V = {A,B}<br />
∑ = {0,1,#}<br />
R = {A 0A1, A B, B #}<br />
S = A<br />
L(G 1 ) = { 0 n #1 n | n≥0 }.<br />
8
Example of CFG<br />
9
Example of CFG<br />
⟨ARTICLE⟩ a | the<br />
means<br />
⟨ARTICLE⟩ a<br />
⟨ARTICLE⟩ the<br />
9
Regular Operations :<br />
Kleene’s theorem (CFG)<br />
18
Regular Operations :<br />
Kleene’s theorem (CFL)<br />
CFLs<br />
19
Kleene’s<br />
theorem (CFL)<br />
20
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A and<br />
G B =(V B ,∑,R B ,S B ) be a CFG generating L B (V A ∩V B =∅).<br />
20
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A and<br />
G B =(V B ,∑,R B ,S B ) be a CFG generating L B (V A ∩V B =∅).<br />
Consider<br />
G U =( {S U }∪V A ∪V B ,∑,{S U S A | S B }∪R A ∪R B ,S U ).<br />
20
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A and<br />
G B =(V B ,∑,R B ,S B ) be a CFG generating L B (V A ∩V B =∅).<br />
Consider<br />
G U =( {S U }∪V A ∪V B ,∑,{S U S A | S B }∪R A ∪R B ,S U ).<br />
L U = L A ∪ L B .<br />
20
Regular Operations :<br />
Kleene’s theorem (CFL)<br />
CFLs<br />
21
Kleene’s<br />
theorem (CFL)<br />
22
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A and<br />
G B =(V B ,∑,R B ,S B ) be a CFG generating L B (V A ∩V B =∅).<br />
22
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A and<br />
G B =(V B ,∑,R B ,S B ) be a CFG generating L B (V A ∩V B =∅).<br />
Consider<br />
G C =( {S C }∪V A ∪V B ,∑,{S C S A S B }∪R A ∪R B ,S C ).<br />
22
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A and<br />
G B =(V B ,∑,R B ,S B ) be a CFG generating L B (V A ∩V B =∅).<br />
Consider<br />
G C =( {S C }∪V A ∪V B ,∑,{S C S A S B }∪R A ∪R B ,S C ).<br />
L C = L A ∘L B .<br />
22
Regular Operations :<br />
Kleene’s theorem (CFL)<br />
CFLs<br />
23
Kleene’s<br />
theorem (CFL)<br />
24
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A .<br />
24
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A .<br />
Consider<br />
G S =( {S S }∪V A ,∑,{S S E | S A S S }∪R A ,S S ).<br />
24
Kleene’s<br />
theorem (CFL)<br />
Let G A =(V A ,∑,R A ,S A ) be a CFG generating L A .<br />
Consider<br />
G S =( {S S }∪V A ,∑,{S S E | S A S S }∪R A ,S S ).<br />
L S = (L A )*.<br />
24
Construction tools<br />
(and Reductions)<br />
CFLs are closed under union, concatenation and<br />
star. If there exists a CFL C s. t. either A*=A’,<br />
A∪C=A’, A∘C=A’ (but not complement nor<br />
intersection) or any combinations of these<br />
operations then A’ is a CFL as long as A is.<br />
( If A’ is NON-CFL then so is A. )<br />
25
Construction tools<br />
Constructing a CFG for a regular language L:<br />
M = (Q={q 0 ,q 1 ,...,q k },∑,δ,q 0 ,F) is converted to<br />
G = (V={R 0 ,R 1 ,...,R k },∑,R,S=R 0 ) where<br />
R contains rule R i aR j for each δ(q i ,a) = q j<br />
in M, and rule R i E for each accept-state<br />
q i ∈F.<br />
R 0 is the start variable.<br />
26
extra EXAMPLE of CFG<br />
27
extra EXAMPLE of CFG<br />
28
Ambiguity in CFGs<br />
29
Leftmost Derivation<br />
A derivation is Leftmost if every time a variable<br />
is substituted, it is always the leftmost variable.<br />
E<br />
X<br />
A<br />
M<br />
PLE<br />
30
Ambiguity<br />
A string w is derived ambiguously by a CFG<br />
G if it has two or more distinct leftmost<br />
derivations. Grammar G is ambigious if it<br />
generates some string ambiguously.<br />
31
Ambiguity<br />
Ambiguity is not desirable in CFG because it<br />
may lead to unexpected interpretations of a<br />
string, for instance in the context of arithmetic<br />
expressions or programming languages.<br />
However, some languages are inherently<br />
ambiguous, meaning that all grammars<br />
genrating this language must be ambiguous.<br />
example : {a i b j c k | i=j or j=k}<br />
32
Ambiguous version of<br />
example 2.4<br />
G 5<br />
33
Ambiguous CFG<br />
34
Noam Chomsky<br />
Chomsky Normal Form<br />
35
Chomsky Normal Form<br />
36
Chomsky Normal Form<br />
37
157<br />
38
Proof:<br />
39
Proof:<br />
First, we add a new start variable S 0 and the<br />
rule S 0 S, where S was the origial start<br />
variable.<br />
39
Chomsky Normal Form<br />
40
Second, we take care of all E-rules. We<br />
remove an E-rule A E, where A is not the<br />
start variable.<br />
41
Second, we take care of all E-rules. We<br />
remove an E-rule A E, where A is not the<br />
start variable.<br />
Then for each occurrence of A on the righthand<br />
side of a rule we add a new rule with<br />
that occurrence deleted.<br />
41
Second, we take care of all E-rules. We<br />
remove an E-rule A E, where A is not the<br />
start variable.<br />
Then for each occurrence of A on the righthand<br />
side of a rule we add a new rule with<br />
that occurrence deleted.<br />
Accordingly, each rule R A is replaced by<br />
R E unless it has been already removed.<br />
41
Chomsky Normal Form<br />
42
Third, we handle all unit rules by removing<br />
each unit rule A B.<br />
43
Third, we handle all unit rules by removing<br />
each unit rule A B.<br />
In consequence whenever B u appears, we<br />
add the rule A u unless this is a unit rule<br />
previously removed.<br />
43
Chomsky Normal Form<br />
44
Finally, we convert all remaining rules as<br />
follows: A u 1 u 2 ...u k for k>2, where each u i<br />
is a variable or terminal with a series of<br />
rules A u 1 A 1 , A 1 u 2 A 2 ,..., A k-2 u k-1 u k<br />
where each A i is a new variable.<br />
45
Finally, we convert all remaining rules as<br />
follows: A u 1 u 2 ...u k for k>2, where each u i<br />
is a variable or terminal with a series of<br />
rules A u 1 A 1 , A 1 u 2 A 2 ,..., A k-2 u k-1 u k<br />
where each A i is a new variable.<br />
When k=2, and A u 1 u 2 , we may replace<br />
any terminal u i by a variable U i and the rule<br />
U i u i.<br />
45
Chomsky Normal Form<br />
46
COMP-330<br />
Theory of Computation<br />
<strong>Fall</strong> <strong>2012</strong> -- <strong>Prof</strong>. <strong>Claude</strong> <strong>Crépeau</strong><br />
Lec. 9 : Context-Free<br />
Grammars<br />
47