ambiguity - liacs.leidenuniv.nl
TRANSCRIPT
Ambiguity
From lecture 8:
Definition
A context-free grammar G is ambiguous, if for at least one x β L(G ), xhas more than one derivation tree (or, equivalently, more than oneleftmost derivation).
Otherwise: unambiguous [M] D 4.18
Automata Theory Context-Free Languages Derivation trees and ambiguity 257 / 285
Ambiguous
Some cf languages are inherently ambiguous
Ambiguity is undecidable[M] Theorem 9.20
Automata Theory Context-Free Languages Derivation trees and ambiguity 258 / 285
Normalform
unwanted in CFG:β variables not used in successful derivations S ββ x β Ξ£β
β A β Ξ A variable Ξ-productionsβ A β B A,B variables unit productions [chain rules]
Automata Theory Context-Free Languages Normalform 259 / 285
Normalform
unwanted in CFG:β variables not used in successful derivations S ββ x β Ξ£β
β A β Ξ A variable Ξ-productionsβ A β B A,B variables unit productions [chain rules]
restricted CFG, with βniceβ formChomsky normalform A β BC , A β Ο
Greibach normalform (β ) A β ΟB1 . . .Bk
Automata Theory Context-Free Languages Normalform 260 / 285
Useful etc.
CFG G = (V ,Ξ£, S ,P)
Definition
variable A is live if A ββ x for some x β Ξ£β.
variable A is reachable if S ββ Ξ±AΞ² for some Ξ±,Ξ² β (Ξ£ βͺ V β).
variable A is useful if there is a derivation of the form S ββ Ξ±AΞ² ββ x forsome string x β Ξ£β.
useful implies live and reachable.For S β AB | b and A β a, variable A is live and reachable, not useful.[M] Exercise 4.51, 4.52, 4.53
Automata Theory Context-Free Languages Normalform 261 / 285
Recursion, and an algorithm
Live variables
Construction
β N0 = β
β Ni+1 = Ni βͺ { A β V | A β Ξ± in P , with Ξ± β (Ni βͺ Ξ£)β }
N1 = { A β V | A β x in P , with x β Ξ£β }
N0 β N1 β N2 β Β· Β· Β· β V
there exists a k such that Nk = Nk+1
A is live iff A ββ
i>0 Ni = Nk
(minimal) depth of derivation tree A ββ x
Automata Theory Context-Free Languages Normalform 262 / 285
Recursion, and an algorithm
Live variables
Construction
β N0 = β
β Ni+1 = Ni βͺ { A β V | A β Ξ± in P , with Ξ± β (Ni βͺ Ξ£)β }
Exercise 4.53(c i).S β ABC | BaB A β aA | BaC | aaa
B β bBb | a C β CA | AC
Automata Theory Context-Free Languages Normalform 263 / 285
Algorithm, ctd.
Reachable variables
Construction
β N0 = {S}
β Ni+1 = Ni βͺ { A β V | B β Ξ±1AΞ±2 in P , with B β Ni }
N0 β N1 β N2 β Β· Β· Β· β V
there exists a k such that Nk = Nk+1
A is reachable iff A ββ
i>0 Ni = Nk
(minimal) length of derivation S ββ Ξ±AΞ²
Automata Theory Context-Free Languages Normalform 264 / 285
Algorithm, ctd.
Reachable variables
Construction
β N0 = {S}
β Ni+1 = Ni βͺ { A β V | B β Ξ±1AΞ±2 in P , with B β Ni }
N0 β N1 β N2 β Β· Β· Β· β V
there exists a k such that Nk = Nk+1
A is reachable iff A ββ
i>0 Ni = Nk
(minimal) length of derivation S ββ Ξ±AΞ²
β remove all non-live variables (and productions that contain them)β remove all unreachable variables (and their productions)
then all variables are useful
does not work the other way around . . .
Automata Theory Context-Free Languages Normalform 265 / 285
Algorithm, ctd.
Reachable variables
Construction
β N0 = {S}
β Ni+1 = Ni βͺ { A β V | B β Ξ±1AΞ±2 in P , with B β Ni }
Exercise 4.53(c i)., ctdS β BaB A β aA | aaa B β bBb | a
Automata Theory Context-Free Languages Normalform 266 / 285
Algorithm, ctd.
β remove all non-live variables (and productions that contain them)β remove all unreachable variables (and productions)
then all variables are useful
does not work the other way around . . .
Exercise 4.53(c i)., revisitedS β ABC | BaB A β aA | BaC | aaa
B β bBb | a C β CA | AC
Automata Theory Context-Free Languages Normalform 267 / 285
Removing Ξ-productions
Idea:
Example
A β BCDCB
B β b | Ξ
C β c | Ξ
D β d
Automata Theory Context-Free Languages Normalform 268 / 285
Definition
variable A is nullable iff A ββ Ξ
Theorem
β if A β Ξ then A is nullable
β if A β B1B2 . . .Bk and all Bi are nullable, then A is nullable
[M] Def 4.26 / Exercise 4.48
Construction
β N0 = β
β Ni+1 = Ni βͺ { A β V | A β Ξ± in P , with Ξ± β Nβ
i }
N1 = { A β V | A β Ξ in P }
N0 β N1 β N2 β Β· Β· Β· β V
there exists a k such that Nk = Nk+1
A is nullable iff A ββ
i>0 Ni = Nk
Automata Theory Context-Free Languages Normalform 269 / 285
Construction
β identify nullable variablesβ for every production A β Ξ± add A β Ξ²,
where Ξ² is obtained from Ξ± by removing one or more nullable variablesβ remove all Ξ-productions (and all productions A β A)
Grammar for { aibjck | i = j or i = k }
S β TU | V
T β aTb | Ξ
U β cU | Ξ
V β aVc | W
W β bW | Ξ
Automata Theory Context-Free Languages Normalform 270 / 285
Example nullable
Grammar for { aibjck | i = j or i = k }
S β TU | V
T β aTb | Ξ
U β cU | Ξ
V β aVc | W
W β bW | Ξ
N1 = {T ,U,W }, variables with Ξ at right-hand side productions
N2 = {T ,U,W } βͺ {S ,V }, variables with {T ,U,W }β at rhs productions
N3 = N2 = {T ,U,W , S ,V }, all productions found, no new
Automata Theory Context-Free Languages Normalform 271 / 285
Example nullable, ctd
add all productions, where (any number of) nullable variables areremoved. . .
S β TU | V
T β aTb | Ξ
U β cU | Ξ
V β aVc | W
W β bW | Ξ
[M] Ex. 4.31
Automata Theory Context-Free Languages Normalform 272 / 285
Example nullable, ctd
add all productions, where (any number of) nullable variables are removedS β TU | V S β T | U | Ξ
T β aTb | Ξ T β ab
U β cU | Ξ U β c
V β aVc | W V β ac | Ξ
W β bW | Ξ W β b
remove all Ξ-productions. . .
[M] Ex. 4.31
Automata Theory Context-Free Languages Normalform 273 / 285
Example nullable, ctd
add all productions, where (any number of) nullable variables are removedS β TU | V S β T | U | Ξ
T β aTb | Ξ T β ab
U β cU | Ξ U β c
V β aVc | W V β ac | Ξ
W β bW | Ξ W β b
remove all Ξ-productionsS β TU | V | T | U
T β aTb | ab
U β cU | c
V β aVc | W | ac
W β bW | b
[M] Ex. 4.31
Automata Theory Context-Free Languages Normalform 274 / 285
Removing Ξ-productions
Theorem
For every CFG G there is CFG G1 without Ξ-productions such that
L(G1) = L(G ) β {Ξ}.
Proof. . .[M] Thm 4.27
Automata Theory Context-Free Languages Normalform 275 / 285
Removing unit productions
Idea:
Example
A β B | aCb
B β C | Bb | Bc
C β c | ABC
Automata Theory Context-Free Languages Normalform 276 / 285
Removing unit productions
Assume Ξ-productions have been removed
Variable B is A-derivable, ifβ B 6= A, andβ A ββ B (using only unit productions)
Construction
β N1 = { B β V | B 6= A and A β B in P }
β Ni+1 = Ni βͺ { C β V | C 6= A and B β C in P , with B β Ni }
N1 β N2 β Β· Β· Β· β V
there exists a k such that Nk = Nk+1
B is A-derivable iff B ββ
i>0 Ni = Nk
Automata Theory Context-Free Languages Normalform 277 / 285
Removing unit productions
Construction
β for each A β V , identify A-derivable variablesβ for every pair (A,B) where B is A-derivable,
and every production B β Ξ± add A β Ξ±
β remove all unit productions
Grammar for { aibjck | i = j or i = k }
S β TU | V | T | U
T β aTb | ab
U β cU | c
V β aVc | W | ac
W β bW | b
Automata Theory Context-Free Languages Normalform 278 / 285
Example unit productions
S β TU | V | T | U
T β aTb | ab
U β cU | c
V β aVc | W | ac
W β bW | b
S-derivable: {V ,T ,U}, {V ,T ,U,W } V -derivable: {W }
New productions:S β aTb | ab S β cU | c S β aVc | W | ac S β bW | b
V β bW | b
Remove unit productions:S β TU | aTb | ab | cU | c | aVc | ac | bW | b
T β aTb | ab
U β cU | c
V β aVc | ac | bW | b
W β bW | b
Automata Theory Context-Free Languages Normalform 279 / 285
Definition
CFG in Chomsky normal form
productions are of the formβ A β BC variables A,B ,Cβ A β Ο variable A, terminal Ο
Theorem
For every CFG G there is CFG G1 in CNF such that L(G1) = L(G ) β {Ξ}.
[M] Def 4.29, Thm 4.30
Automata Theory Context-Free Languages Chomsky normalform 280 / 285
Construction ChNF
Construction
1 remove Ξ-productions
2 remove unit productions
3 introduce variables for terminals XΟ β Ο
4 split long productions
A β aBabA
is replaced byXa β a Xb β b A β XaBXaXbA
A β ACBA
is replaced byA β AY1 Y1 β CY2 Y2 β BA
Automata Theory Context-Free Languages Chomsky normalform 281 / 285
ChNF, example
Grammar for { aibjck | i = j or i = k }
S β TU | V
T β aTb | Ξ U β cU | Ξ
V β aVc | W W β bW | Ξ
After removing Ξ-productions and unit productions, we obtain (see before)S β TU | aTb | ab | cU | c | aVc | ac | bW | b
T β aTb | ab U β cU | c
V β aVc | ac | bW | b W β bW | b
Now introduce productions for the terminals. . .
Automata Theory Context-Free Languages Chomsky normalform 282 / 285
ChNF, example
Grammar for { aibjck | i = j or i = k }
S β TU | V
T β aTb | Ξ U β cU | Ξ
V β aVc | W W β bW | Ξ
After removing Ξ-productions and unit productions, we obtain (see before)S β TU | aTb | ab | cU | c | aVc | ac | bW | b
T β aTb | ab U β cU | c
V β aVc | ac | bW | b W β bW | b
Now introduce productions for the terminals:Xa β a Xb β b Xc β c
S β TU | XaTXb | XaXb | XcU | c | XaVXc | XaXc | XbW | b
T β XaTXb | XaXb
U β XcU | c
V β XaVXc | XaXc | XbW | b
W β XbW | b
Automata Theory Context-Free Languages Chomsky normalform 283 / 285
ChNF, example ctd.
Only a few productions that are too long:S β XaTXb | XaVXc
T β XaTXb
V β XaVXc
Split these long productions. . .
Automata Theory Context-Free Languages Chomsky normalform 284 / 285
ChNF, example ctd.
Only a few productions that are too long:S β XaTXb | XaVXc
T β XaTXb
V β XaVXc
Split these long productions:S β XaY1 | XaY2
Y1 β TXb Y2 β VXc
T β XaY1
V β XaY2
Note that we can reuse Y1,Y2 for two productions
Automata Theory Context-Free Languages Chomsky normalform 285 / 285