Context Free Grammars
Ambiguity
CS 373: Theory of Computation
Gul Agha Mahesh Viswanathan
University of Illinois, Urbana-Champaign
Fall 2010
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
V is a finite set of variables/non-terminals.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
V is a finite set of variables/non-terminals.
Σ is a finite set of terminals. Σ is disjoint from V .
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
V is a finite set of variables/non-terminals.
Σ is a finite set of terminals. Σ is disjoint from V .
R is a finite set of rules or productions of the form A → α
where A ∈V and α ∈(V ∪ Σ)∗
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
V is a finite set of variables/non-terminals.
Σ is a finite set of terminals. Σ is disjoint from V .
R is a finite set of rules or productions of the form A → α
where A ∈V and α ∈(V ∪ Σ)∗
S ∈V is the start symbol
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Example: Palindromes
Example
A string w is apalindromeif w = wR .
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Example: Palindromes
Example
A string w is apalindromeif w = wR .
Gpal = ({S}, {0,1},R, S) defines palindromes over {0,1}, where R
is
S → s
S → 0
S → 1
S → 0S0
S → 1S1
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Example: Palindromes
Example
A string w is apalindromeif w = wR .
Gpal = ({S}, {0,1},R, S) defines palindromes over {0,1}, where R
is
S → s
S → 0
S → 1
S → 0S0
S → 1S1
Or more briefly, R = {S → s|0 |1 |0S0 |1S1}
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Language of a CFG
Derivations
Expand the start symbol using one of its rules. Further expand the
resulting string by expanding one of the variables in the string, by
the RHS of one of its rules. Repeat until you get a string of
terminals.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Language of a CFG
Derivations
Expand the start symbol using one of its rules. Further expand the
resulting string by expanding one of the variables in the string, by
the RHS of one of its rules. Repeat until you get a string of
terminals. For the grammar
Gpal = ({S}, {0,1},{S → s|0 |1 |0S0 |1S1}, S) we have
S ⇒ 0S0 ⇒ 00S00 ⇒ 001S100 ⇒ 0010100
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Formal Definition
Definition
Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where
α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G .
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Formal Definition
Definition
Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where
α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G .
We say α ∗
0 1 n⇒G β if either α = β or there are α , α , . . . α such that
α = α0 ⇒G α1 ⇒G α2 ⇒G · · · ⇒G αn = β
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Formal Definition
Definition
Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where
α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G .
We say α ∗
0 1 n⇒G β if either α = β or there are α , α , . . . α such that
α = α0 ⇒G α1 ⇒G α2 ⇒G · · · ⇒G αn = β
Notation
When G is clear from the context, we will write ⇒ and ∗
instead
⇒
of ⇒G and ∗
.
⇒G
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Language
Definition
Thelanguage of CFG G = (V , Σ, R, S), denoted L(G ) is the
collection of strings over the terminals derivable from S using the
rules in R. In other words,
L(G ) = {w ∈Σ∗ |S ∗
w}
⇒
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Language
Definition
Thelanguage of CFG G = (V , Σ, R, S), denoted L(G ) is the
collection of strings over the terminals derivable from S using the
rules in R. In other words,
L(G ) = {w ∈Σ∗ |S ∗
w}
⇒
Definition
A language L is said to becontext-freeif there is a CFG G such
that L = L(G).
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Palindromes Revisited
Recall, Lpal = {w∈{0, 1}∗|w = w R }is the language of
palindromes.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Palindromes Revisited
Recall, Lpal = {w∈{0, 1}∗|w = w R }is the language of
palindromes.
Consider Gpal = ({S}, {0,1},R, S) defines palindromes over
{0, 1}, where R = {S → s|0 |1 |0S0 |1S1}
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Palindromes Revisited
Recall, Lpal = {w∈{0, 1}∗|w = w R }is the language of
palindromes.
Consider Gpal = ({S}, {0,1},R, S) defines palindromes over
{0, 1}, where R = {S → s|0 |1 |0S0 |1S1}
Proposition
L(Gpal) = Lpal
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Induction Step:If |w|≥ 2 and w = w R then it must begin
and with the same symbol.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Induction Step:If |w|≥ 2 and w = w R then it must begin
and with the same symbol. Let w = 0x 0.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Induction Step:If |w|≥ 2 and w = w R then it must begin
and with the same symbol. Let w = 0x 0. Now,
wR = 0xR0 = w = 0x0; thus, xR = x.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Induction Step:If |w|≥ 2 and w = w R then it must begin
and with the same symbol. Let w = 0x 0. Now,
wR = 0xR0 = w = 0x0; thus, xR = x. By induction
hypothesis, S ⇒
∗
x . Hence S ⇒ 0S0 ∗
0x 0. If w = 1x 1 the
⇒
argument is similar. ··→
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal by induction
on the number of derivation steps.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal by induction
on the number of derivation steps.
Base Case:If the derivation has only one step then the
derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0
or 1 and is in LPal.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal by induction
on the number of derivation steps.
Base Case:If the derivation has only one step then the
derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0
or 1 and is in LPal.
Induction Step:Consider an ( n + 1)-step derivation of w . It
must be of the form S ⇒ 0S0 ∗
0x 0 = w or
⇒
S ⇒ 1S1 ∗
1x 1 = w .
⇒
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal by induction
on the number of derivation steps.
Base Case:If the derivation has only one step then the
derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0
or 1 and is in LPal.
Induction Step:Consider an ( n + 1)-step derivation of w . It
must be of the form S ⇒ 0S0 ∗
0x 0 = w or
⇒
S ⇒ 1S1 ∗
1x 1 = w . In either case S
⇒
∗
⇒ x in n-steps. Hence
x ∈LPal and so w = w R. Q
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
Each interior node is labeled by a
variable in V
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
Each interior node is labeled by a
variable in V
Each leaf is labeled by either a
variable, a terminal or s;a leaf
labeled by smust be the only child
of its parent.
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
Each interior node is labeled by a
variable in V
Each leaf is labeled by either a
variable, a terminal or s;a leaf
labeled by smust be the only child
of its parent.
If an interior node labeled by A
with children labeled by
X1, X2, ...Xk (from the left), then
A → X1X2 · · · Xk mustbe a rule.
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
Each interior node is labeled by a
variable in V
Each leaf is labeled by either a
variable, a terminal or s;a leaf
labeled by smust be the only child
of its parent.
If an interior node labeled by A
with children labeled by
X1, X2, ...Xk (from the left), then
A → X1X2 · · · Xk mustbe a rule.
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Yield of a parse treeis the concatenation of leaf labels (left–right)
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees and Derivations
Proposition
A ∗
Let G = (V , Σ, R, S) be a CFG. For any A ∈V and α ∈(V ∪Σ)∗,
⇒ α iffthereis a parse treewith root labeled A and whose yield is
α.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees and Derivations
Proposition
A ∗
Let G = (V , Σ, R, S) be a CFG. For any A ∈V and α ∈(V ∪Σ)∗,
⇒ α iffthereis a parse treewith root labeled A and whose yield is
α.
Proof.
(⇒):Proof by induction on the number of steps in the derivation.
Base Case:If A ⇒ α then A → α
is a rule in G . There is a tree of
height 1, with root A and leaves
the symbols in α. ··→
A
α1 α2 · · · αn
Parse Tree for Base Case
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees for Derivations
Proof (contd).
(⇒):Proof by induction on the number of steps in the derivation.
Induction Step:Let A ∗
α in
⇒
k + 1steps.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees for Derivations
Proof (contd).
(⇒):Proof by induction on the number of steps in the derivation.
Induction Step:Let A ∗
α in
⇒
k + 1steps.
Then A ∗
α1Xα2 ⇒ α1γα2 =α,
⇒
where X → X1 · · · Xn = γ is arule
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees for Derivations
Proof (contd).
(⇒):Proof by induction on the number of steps in the derivation.
Induction Step:Let A ∗
α in
⇒
k + 1steps.
Then A ∗
α1Xα2 ⇒ α1γα2 =α,
⇒
where X → X1 · · · Xn = γ is arule
By ind. hyp., there is a tree with
root A and yield α1Xα2.
A
α1 X α2
Parse Tree for Induction Step
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees for Derivations
Proof (contd).
(⇒):Proof by induction on the number of steps in the derivation.
Induction Step:Let A ∗
α in
⇒
k + 1steps.
Then A ∗
α1Xα2 ⇒ α1γα2 =α,
⇒
where X → X1 · · · Xn = γ is arule
By ind. hyp., there is a tree with
root A and yield α1Xα2.
Add leaves X1, . . . Xn and make
them children of X . New tree isa
parse tree with desired yield. ··→
A
α1 X α2
γ
Parse Tree for Induction Step
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐):Assume that there is a parse tree with root A and yield α.
Need to show that A ∗
α.
⇒
··→
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐):Assume that there is a parse tree with root A and yield α.
Need to show that A ∗
α. Proof by induction on the number of⇒
internal nodes in the tree.
··→
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐):Assume that there is a parse tree with root A and yield α.
Need to show that A ∗
α. Proof by induction on the number of⇒
internal nodes in the tree.
Base Case:If tree has only one
internal node, then it has the form
as in picture
A
α1 α2 · · · αn
Parse Tree with one internal
node
··→
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐):Assume that there is a parse tree with root A and yield α.
Need to show that A ∗
α. Proof by induction on the number of⇒
internal nodes in the tree.
Base Case:If tree has only one
internal node, then it has the form
as in picture
Then, α = X1 ···Xn and A → α is
a rule. Thus, A ∗
α.
⇒
A
α1 α2 · · · αn
Parse Tree with one internal
node
··→
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes. Let X1, X2, . . . Xn be the children of the root
ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn
must be a rule.
A
X1 X2 Xn···
···
α1 α2 αn
Tree with k+1 internal nodes
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes. Let X1, X2, . . . Xn be the children of the root
ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn
must be a rule.
Let αi be the yield of the tree
rooted at Xi ; so Xi is a leaf αi = Xi
A
X1 X2 Xn···
···
α1 α2 αn
Tree with k+1 internal nodes
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes. Let X1, X2, . . . Xn be the children of the root
ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn
must be a rule.
Let αi be the yield of the tree
rooted at Xi ; so Xi is a leaf αi = Xi
Now if j< i then all the
descendents of Xj are to the left of
the descendents of Xi . So
α = α1α2 ···αn. ··→
A
X1 X2 Xn···
···
α1 α2 αn
Tree with k+1 internal nodes
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
α is the yield of a tree with k + 1(⇐) Induction Step:Suppose
interior nodes.
A
X1 X2 Xn···
···
α1 α2 αn
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes.
∗
αi and if Xi is not a leaf
Each subtree rooted at Xi has at
most k internal nodes. So if Xi is a
leaf Xi ⇒
then Xi ⇒∗
αi (ind. hyp.).
A
X1 X2 Xn···
···
α1 α2 αn
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes.
∗
αi and if Xi is not a leaf
∗
αi (ind. hyp.).
Each subtree rooted at Xi has at
most k internal nodes. So if Xi is a
leaf Xi ⇒
then Xi ⇒
Thus
A ⇒ X1X2 ···Xn ⇒
∗∗
α1X2 · · · Xn ⇒
α1α2 ···Xn ⇒∗
α1 ···αn = α Q
A
X1 X2 Xn···
···
α1 α2 αn
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Recap ...
For a CFG G with variable A the following are equivalent
1 A ∗
⇒ w
2 There is a parse tree with root A and yield w
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Recap ...
For a CFG G with variable A the following are equivalent
1 A ∗
⇒ w
2 There is a parse tree with root A and yield w
Context-free-ness
CFGs have the property that if X ⇒
∗
γ then αXβ ∗
αγβ
⇒
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Example: English Sentences
English sentences can be described as
(S) →(NP)(VP)
(NP) → (CN)|(CN)(PP)
(VP) → (CV )|(CV )(PP)
(PP) →(P)(CN)
(CN) →(A)(N)
(CV ) → (V) |(V )(NP)
(A) → a|the
(N) → boy |girl |bat
(V ) → hits |likes |sees
(P) →with
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Multiple Parse Trees
Example 1
The sentence “the girl hits the boy with the bat” has the following
parse tree
(S)
(NP)
(CN)
(A)
the girl
(VP)
(V )
(N) hits
(NP)
the
(CN) (PP)
(A) (N)(P) (CN)
boywith (A) (N)
the bat
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
the girl
the
the girl hits
boywith (A) (N) (A) (N)
the bat the boy
Multiple Parse Trees
Example 1
The sentence “the girl hits the boy with the bat” has the following
parse trees
(S) (S)
(NP) (VP) (NP) (VP)
(CN) (V ) (NP) (CN) (CV ) (PP)
(A) (N) hits (CN) (PP) (A) (N) (V ) (NP) (P) (CN)
(A) (N)(P) (CN) (CN) with (A) (N)
the bat
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Example: Arithmetic Expressions
Consider the language of all arithmetic expressions (E ) built out of
integers (N) and identifiers (I ), using only + and ∗
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Example: Arithmetic Expressions
Consider the language of all arithmetic expressions (E ) built out of
integers (N) and identifiers (I ), using only + and ∗
Gexp = ({E,I,N},{a,b,0,1,(,),+,∗,−},R, E ) where R is
E → I |N | − N |E + E |E ∗E |(E)
I → a |b |Ia |Ib
N → 0 |1 |N0 |N1
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Multiple Parse Trees
Example 2
The parse tree for expression a + b ∗a in the grammar Gexp is
E
E
E +
I E
a I
b
∗ E
I
a
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Multiple Parse Trees
Example 2
The parse trees for expression a + b ∗a in the grammar Gexp is
E
E
E +
I E
a I
b
∗ E
I
a
E
E
I
a
E
+
∗ E
E I
I a
b
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity
Definition
A grammar G = (V ,Σ, R, S) is said to beambiguousif there is
w ∈Σ∗ for which there are two different parse trees.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity
Definition
A grammar G = (V ,Σ, R, S) is said to beambiguousif there is
w ∈Σ∗ for which there are two different parse trees.
Warning!
Existence of two derivations for a string does not mean the
grammar is ambiguous!
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Using the semantics to change the rules.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Using the semantics to change the rules. For example, if we
knew who had the bat (the girl or the boy) from the context,
we would know which is the right interpretation.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Using the semantics to change the rules. For example, if we
knew who had the bat (the girl or the boy) from the context,
we would know which is the right interpretation.
Adding precedence to operators.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Using the semantics to change the rules. For example, if we
knew who had the bat (the girl or the boy) from the context,
we would know which is the right interpretation.
Adding precedence to operators. For example, ∗binds more
tightly than +, or “else” binds with the innermost “if”.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
An Example
Recall, Gexp has the following rules
E → I |N | − N |E + E |E ∗E |(E)
I → a |b |Ia |Ib
N → 0 |1 |N0 |N1
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
An Example
Recall, Gexp has the following rules
E → I |N | − N |E + E |E ∗E |(E)
I → a |b |Ia |Ib
N → 0 |1 |N0 |N1
New CFG Gt
exp has the
rules
I → a |b |Ia |Ib
N → 0 |1 |N0 |N1
F → I |N | − N |(E)
T → F |T ∗F
E → T |E + T
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity: Computational Problems
Removing Ambiguity
Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and
Gt is unambiguous.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity: Computational Problems
Removing Ambiguity
Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and
Gt is unambiguous.
There is no algorithm that can solve the above problem!
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity: Computational Problems
Removing Ambiguity
Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and
Gt is unambiguous.
There is no algorithm that can solve the above problem!
Deciding Ambiguity
Problem:Given CFG G , determine if G is ambiguous.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity: Computational Problems
Removing Ambiguity
Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and
Gt is unambiguous.
There is no algorithm that can solve the above problem!
Deciding Ambiguity
Problem:Given CFG G , determine if G is ambiguous.
The problem is undecidable.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Problem:Is it the case that for every CFG G , there is a grammar
Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt
cannot be constructed algorithmically?
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
Problem:Is it the case that for every CFG G , there is a grammar
Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt
cannot be constructed algorithmically?
No! There are context-free languages L such that every grammar
for L is ambiguous.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
Problem:Is it the case that for every CFG G , there is a grammar
Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt
cannot be constructed algorithmically?
No! There are context-free languages L such that every grammar
for L is ambiguous.
Definition
A context-free language L is said to beinherently ambiguousif
every grammar G for L is ambiguous.
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
An Example
Consider
L = {aibjck |i = j or j = k}
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
An Example
Consider
L = {aibjck |i = j or j = k}
One can show that any CFG G for L will have two parse trees on
anbncn, for all but finitely many values of n
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
An Example
Consider
L = {aibjck |i = j or j = k}
One can show that any CFG G for L will have two parse trees on
anbncn, for all but finitely many values of n
One that checks that number of a’s = number of b’s
Agha-Viswanathan CS373
Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
An Example
Consider
L = {aibjck |i = j or j = k}
One can show that any CFG G for L will have two parse trees on
anbncn, for all but finitely many values of n
One that checks that number of a’s = number of b’s
Another that checks that number of b’s = number of c ’s
Agha-Viswanathan CS373

Ambiguty

  • 1.
    Context Free Grammars Ambiguity CS373: Theory of Computation Gul Agha Mahesh Viswanathan University of Illinois, Urbana-Champaign Fall 2010 Agha-Viswanathan CS373
  • 2.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Context-Free Grammars Definition Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where Agha-Viswanathan CS373
  • 3.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Context-Free Grammars Definition Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where V is a finite set of variables/non-terminals. Agha-Viswanathan CS373
  • 4.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Context-Free Grammars Definition Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where V is a finite set of variables/non-terminals. Σ is a finite set of terminals. Σ is disjoint from V . Agha-Viswanathan CS373
  • 5.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Context-Free Grammars Definition Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where V is a finite set of variables/non-terminals. Σ is a finite set of terminals. Σ is disjoint from V . R is a finite set of rules or productions of the form A → α where A ∈V and α ∈(V ∪ Σ)∗ Agha-Viswanathan CS373
  • 6.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Context-Free Grammars Definition Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where V is a finite set of variables/non-terminals. Σ is a finite set of terminals. Σ is disjoint from V . R is a finite set of rules or productions of the form A → α where A ∈V and α ∈(V ∪ Σ)∗ S ∈V is the start symbol Agha-Viswanathan CS373
  • 7.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Example: Palindromes Example A string w is apalindromeif w = wR . Agha-Viswanathan CS373
  • 8.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Example: Palindromes Example A string w is apalindromeif w = wR . Gpal = ({S}, {0,1},R, S) defines palindromes over {0,1}, where R is S → s S → 0 S → 1 S → 0S0 S → 1S1 Agha-Viswanathan CS373
  • 9.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Example: Palindromes Example A string w is apalindromeif w = wR . Gpal = ({S}, {0,1},R, S) defines palindromes over {0,1}, where R is S → s S → 0 S → 1 S → 0S0 S → 1S1 Or more briefly, R = {S → s|0 |1 |0S0 |1S1} Agha-Viswanathan CS373
  • 10.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Language of a CFG Derivations Expand the start symbol using one of its rules. Further expand the resulting string by expanding one of the variables in the string, by the RHS of one of its rules. Repeat until you get a string of terminals. Agha-Viswanathan CS373
  • 11.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Language of a CFG Derivations Expand the start symbol using one of its rules. Further expand the resulting string by expanding one of the variables in the string, by the RHS of one of its rules. Repeat until you get a string of terminals. For the grammar Gpal = ({S}, {0,1},{S → s|0 |1 |0S0 |1S1}, S) we have S ⇒ 0S0 ⇒ 00S00 ⇒ 001S100 ⇒ 0010100 Agha-Viswanathan CS373
  • 12.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Formal Definition Definition Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G . Agha-Viswanathan CS373
  • 13.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Formal Definition Definition Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G . We say α ∗ 0 1 n⇒G β if either α = β or there are α , α , . . . α such that α = α0 ⇒G α1 ⇒G α2 ⇒G · · · ⇒G αn = β Agha-Viswanathan CS373
  • 14.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Formal Definition Definition Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G . We say α ∗ 0 1 n⇒G β if either α = β or there are α , α , . . . α such that α = α0 ⇒G α1 ⇒G α2 ⇒G · · · ⇒G αn = β Notation When G is clear from the context, we will write ⇒ and ∗ instead ⇒ of ⇒G and ∗ . ⇒G Agha-Viswanathan CS373
  • 15.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Context-Free Language Definition Thelanguage of CFG G = (V , Σ, R, S), denoted L(G ) is the collection of strings over the terminals derivable from S using the rules in R. In other words, L(G ) = {w ∈Σ∗ |S ∗ w} ⇒ Agha-Viswanathan CS373
  • 16.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Context-Free Language Definition Thelanguage of CFG G = (V , Σ, R, S), denoted L(G ) is the collection of strings over the terminals derivable from S using the rules in R. In other words, L(G ) = {w ∈Σ∗ |S ∗ w} ⇒ Definition A language L is said to becontext-freeif there is a CFG G such that L = L(G). Agha-Viswanathan CS373
  • 17.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Palindromes Revisited Recall, Lpal = {w∈{0, 1}∗|w = w R }is the language of palindromes. Agha-Viswanathan CS373
  • 18.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Palindromes Revisited Recall, Lpal = {w∈{0, 1}∗|w = w R }is the language of palindromes. Consider Gpal = ({S}, {0,1},R, S) defines palindromes over {0, 1}, where R = {S → s|0 |1 |0S0 |1S1} Agha-Viswanathan CS373
  • 19.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Palindromes Revisited Recall, Lpal = {w∈{0, 1}∗|w = w R }is the language of palindromes. Consider Gpal = ({S}, {0,1},R, S) defines palindromes over {0, 1}, where R = {S → s|0 |1 |0S0 |1S1} Proposition L(Gpal) = Lpal Agha-Viswanathan CS373
  • 20.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊆ L(Gpal) Proof. Let w ∈Lpal. We prove that S ⇒∗ w Agha-Viswanathan CS373
  • 21.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊆ L(Gpal) Proof. Let w ∈Lpal. We prove that S ⇒∗ w by induction on |w|. Agha-Viswanathan CS373
  • 22.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊆ L(Gpal) Proof. Let w ∈Lpal. We prove that S ⇒∗ w by induction on |w|. Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And S → s|0 |1. Agha-Viswanathan CS373
  • 23.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊆ L(Gpal) Proof. Let w ∈Lpal. We prove that S ⇒∗ w by induction on |w|. Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And S → s|0 |1. Induction Step:If |w|≥ 2 and w = w R then it must begin and with the same symbol. Agha-Viswanathan CS373
  • 24.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊆ L(Gpal) Proof. Let w ∈Lpal. We prove that S ⇒∗ w by induction on |w|. Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And S → s|0 |1. Induction Step:If |w|≥ 2 and w = w R then it must begin and with the same symbol. Let w = 0x 0. Agha-Viswanathan CS373
  • 25.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊆ L(Gpal) Proof. Let w ∈Lpal. We prove that S ⇒∗ w by induction on |w|. Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And S → s|0 |1. Induction Step:If |w|≥ 2 and w = w R then it must begin and with the same symbol. Let w = 0x 0. Now, wR = 0xR0 = w = 0x0; thus, xR = x. Agha-Viswanathan CS373
  • 26.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊆ L(Gpal) Proof. Let w ∈Lpal. We prove that S ⇒∗ w by induction on |w|. Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And S → s|0 |1. Induction Step:If |w|≥ 2 and w = w R then it must begin and with the same symbol. Let w = 0x 0. Now, wR = 0xR0 = w = 0x0; thus, xR = x. By induction hypothesis, S ⇒ ∗ x . Hence S ⇒ 0S0 ∗ 0x 0. If w = 1x 1 the ⇒ argument is similar. ··→ Agha-Viswanathan CS373
  • 27.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊇ L(Gpal) Proof (contd). Let w ∈L(G ), i.e., S ⇒∗ w . We will show w ∈Lpal Agha-Viswanathan CS373
  • 28.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊇ L(Gpal) Proof (contd). Let w ∈L(G ), i.e., S ⇒∗ w . We will show w ∈Lpal by induction on the number of derivation steps. Agha-Viswanathan CS373
  • 29.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊇ L(Gpal) Proof (contd). Let w ∈L(G ), i.e., S ⇒∗ w . We will show w ∈Lpal by induction on the number of derivation steps. Base Case:If the derivation has only one step then the derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0 or 1 and is in LPal. Agha-Viswanathan CS373
  • 30.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊇ L(Gpal) Proof (contd). Let w ∈L(G ), i.e., S ⇒∗ w . We will show w ∈Lpal by induction on the number of derivation steps. Base Case:If the derivation has only one step then the derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0 or 1 and is in LPal. Induction Step:Consider an ( n + 1)-step derivation of w . It must be of the form S ⇒ 0S0 ∗ 0x 0 = w or ⇒ S ⇒ 1S1 ∗ 1x 1 = w . ⇒ Agha-Viswanathan CS373
  • 31.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Proving Correctness of CFG Lpal ⊇ L(Gpal) Proof (contd). Let w ∈L(G ), i.e., S ⇒∗ w . We will show w ∈Lpal by induction on the number of derivation steps. Base Case:If the derivation has only one step then the derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0 or 1 and is in LPal. Induction Step:Consider an ( n + 1)-step derivation of w . It must be of the form S ⇒ 0S0 ∗ 0x 0 = w or ⇒ S ⇒ 1S1 ∗ 1x 1 = w . In either case S ⇒ ∗ ⇒ x in n-steps. Hence x ∈LPal and so w = w R. Q Agha-Viswanathan CS373
  • 32.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is a tree satisfying the following conditions: S 0 S 0 1 S 1 1 S 1 s Example Parse Tree withyield 011110 Agha-Viswanathan CS373
  • 33.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is a tree satisfying the following conditions: Each interior node is labeled by a variable in V S 0 S 0 1 S 1 1 S 1 s Example Parse Tree withyield 011110 Agha-Viswanathan CS373
  • 34.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is a tree satisfying the following conditions: Each interior node is labeled by a variable in V Each leaf is labeled by either a variable, a terminal or s;a leaf labeled by smust be the only child of its parent. S 0 S 0 1 S 1 1 S 1 s Example Parse Tree withyield 011110 Agha-Viswanathan CS373
  • 35.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is a tree satisfying the following conditions: Each interior node is labeled by a variable in V Each leaf is labeled by either a variable, a terminal or s;a leaf labeled by smust be the only child of its parent. If an interior node labeled by A with children labeled by X1, X2, ...Xk (from the left), then A → X1X2 · · · Xk mustbe a rule. S 0 S 0 1 S 1 1 S 1 s Example Parse Tree withyield 011110 Agha-Viswanathan CS373
  • 36.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is a tree satisfying the following conditions: Each interior node is labeled by a variable in V Each leaf is labeled by either a variable, a terminal or s;a leaf labeled by smust be the only child of its parent. If an interior node labeled by A with children labeled by X1, X2, ...Xk (from the left), then A → X1X2 · · · Xk mustbe a rule. S 0 S 0 1 S 1 1 S 1 s Example Parse Tree withyield 011110 Yield of a parse treeis the concatenation of leaf labels (left–right) Agha-Viswanathan CS373
  • 37.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees and Derivations Proposition A ∗ Let G = (V , Σ, R, S) be a CFG. For any A ∈V and α ∈(V ∪Σ)∗, ⇒ α iffthereis a parse treewith root labeled A and whose yield is α. Agha-Viswanathan CS373
  • 38.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees and Derivations Proposition A ∗ Let G = (V , Σ, R, S) be a CFG. For any A ∈V and α ∈(V ∪Σ)∗, ⇒ α iffthereis a parse treewith root labeled A and whose yield is α. Proof. (⇒):Proof by induction on the number of steps in the derivation. Base Case:If A ⇒ α then A → α is a rule in G . There is a tree of height 1, with root A and leaves the symbols in α. ··→ A α1 α2 · · · αn Parse Tree for Base Case Agha-Viswanathan CS373
  • 39.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees for Derivations Proof (contd). (⇒):Proof by induction on the number of steps in the derivation. Induction Step:Let A ∗ α in ⇒ k + 1steps. Agha-Viswanathan CS373
  • 40.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees for Derivations Proof (contd). (⇒):Proof by induction on the number of steps in the derivation. Induction Step:Let A ∗ α in ⇒ k + 1steps. Then A ∗ α1Xα2 ⇒ α1γα2 =α, ⇒ where X → X1 · · · Xn = γ is arule Agha-Viswanathan CS373
  • 41.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees for Derivations Proof (contd). (⇒):Proof by induction on the number of steps in the derivation. Induction Step:Let A ∗ α in ⇒ k + 1steps. Then A ∗ α1Xα2 ⇒ α1γα2 =α, ⇒ where X → X1 · · · Xn = γ is arule By ind. hyp., there is a tree with root A and yield α1Xα2. A α1 X α2 Parse Tree for Induction Step Agha-Viswanathan CS373
  • 42.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Parse Trees for Derivations Proof (contd). (⇒):Proof by induction on the number of steps in the derivation. Induction Step:Let A ∗ α in ⇒ k + 1steps. Then A ∗ α1Xα2 ⇒ α1γα2 =α, ⇒ where X → X1 · · · Xn = γ is arule By ind. hyp., there is a tree with root A and yield α1Xα2. Add leaves X1, . . . Xn and make them children of X . New tree isa parse tree with desired yield. ··→ A α1 X α2 γ Parse Tree for Induction Step Agha-Viswanathan CS373
  • 43.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐):Assume that there is a parse tree with root A and yield α. Need to show that A ∗ α. ⇒ ··→ Agha-Viswanathan CS373
  • 44.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐):Assume that there is a parse tree with root A and yield α. Need to show that A ∗ α. Proof by induction on the number of⇒ internal nodes in the tree. ··→ Agha-Viswanathan CS373
  • 45.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐):Assume that there is a parse tree with root A and yield α. Need to show that A ∗ α. Proof by induction on the number of⇒ internal nodes in the tree. Base Case:If tree has only one internal node, then it has the form as in picture A α1 α2 · · · αn Parse Tree with one internal node ··→ Agha-Viswanathan CS373
  • 46.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐):Assume that there is a parse tree with root A and yield α. Need to show that A ∗ α. Proof by induction on the number of⇒ internal nodes in the tree. Base Case:If tree has only one internal node, then it has the form as in picture Then, α = X1 ···Xn and A → α is a rule. Thus, A ∗ α. ⇒ A α1 α2 · · · αn Parse Tree with one internal node ··→ Agha-Viswanathan CS373
  • 47.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐) Induction Step:Suppose α is the yield of a tree with k + 1 interior nodes. Let X1, X2, . . . Xn be the children of the root ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn must be a rule. A X1 X2 Xn··· ··· α1 α2 αn Tree with k+1 internal nodes Agha-Viswanathan CS373
  • 48.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐) Induction Step:Suppose α is the yield of a tree with k + 1 interior nodes. Let X1, X2, . . . Xn be the children of the root ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn must be a rule. Let αi be the yield of the tree rooted at Xi ; so Xi is a leaf αi = Xi A X1 X2 Xn··· ··· α1 α2 αn Tree with k+1 internal nodes Agha-Viswanathan CS373
  • 49.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐) Induction Step:Suppose α is the yield of a tree with k + 1 interior nodes. Let X1, X2, . . . Xn be the children of the root ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn must be a rule. Let αi be the yield of the tree rooted at Xi ; so Xi is a leaf αi = Xi Now if j< i then all the descendents of Xj are to the left of the descendents of Xi . So α = α1α2 ···αn. ··→ A X1 X2 Xn··· ··· α1 α2 αn Tree with k+1 internal nodes Agha-Viswanathan CS373
  • 50.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). α is the yield of a tree with k + 1(⇐) Induction Step:Suppose interior nodes. A X1 X2 Xn··· ··· α1 α2 αn Agha-Viswanathan CS373
  • 51.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐) Induction Step:Suppose α is the yield of a tree with k + 1 interior nodes. ∗ αi and if Xi is not a leaf Each subtree rooted at Xi has at most k internal nodes. So if Xi is a leaf Xi ⇒ then Xi ⇒∗ αi (ind. hyp.). A X1 X2 Xn··· ··· α1 α2 αn Agha-Viswanathan CS373
  • 52.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Derivations for Parse Trees Proof (contd). (⇐) Induction Step:Suppose α is the yield of a tree with k + 1 interior nodes. ∗ αi and if Xi is not a leaf ∗ αi (ind. hyp.). Each subtree rooted at Xi has at most k internal nodes. So if Xi is a leaf Xi ⇒ then Xi ⇒ Thus A ⇒ X1X2 ···Xn ⇒ ∗∗ α1X2 · · · Xn ⇒ α1α2 ···Xn ⇒∗ α1 ···αn = α Q A X1 X2 Xn··· ··· α1 α2 αn Agha-Viswanathan CS373
  • 53.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Recap ... For a CFG G with variable A the following are equivalent 1 A ∗ ⇒ w 2 There is a parse tree with root A and yield w Agha-Viswanathan CS373
  • 54.
    Context Free Grammars Ambiguity Definitions ProvingProperties Parse Trees Recap ... For a CFG G with variable A the following are equivalent 1 A ∗ ⇒ w 2 There is a parse tree with root A and yield w Context-free-ness CFGs have the property that if X ⇒ ∗ γ then αXβ ∗ αγβ ⇒ Agha-Viswanathan CS373
  • 55.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Example: English Sentences English sentences can be described as (S) →(NP)(VP) (NP) → (CN)|(CN)(PP) (VP) → (CV )|(CV )(PP) (PP) →(P)(CN) (CN) →(A)(N) (CV ) → (V) |(V )(NP) (A) → a|the (N) → boy |girl |bat (V ) → hits |likes |sees (P) →with Agha-Viswanathan CS373
  • 56.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Multiple Parse Trees Example 1 The sentence “the girl hits the boy with the bat” has the following parse tree (S) (NP) (CN) (A) the girl (VP) (V ) (N) hits (NP) the (CN) (PP) (A) (N)(P) (CN) boywith (A) (N) the bat Agha-Viswanathan CS373
  • 57.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity the girl the the girl hits boywith (A) (N) (A) (N) the bat the boy Multiple Parse Trees Example 1 The sentence “the girl hits the boy with the bat” has the following parse trees (S) (S) (NP) (VP) (NP) (VP) (CN) (V ) (NP) (CN) (CV ) (PP) (A) (N) hits (CN) (PP) (A) (N) (V ) (NP) (P) (CN) (A) (N)(P) (CN) (CN) with (A) (N) the bat Agha-Viswanathan CS373
  • 58.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Example: Arithmetic Expressions Consider the language of all arithmetic expressions (E ) built out of integers (N) and identifiers (I ), using only + and ∗ Agha-Viswanathan CS373
  • 59.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Example: Arithmetic Expressions Consider the language of all arithmetic expressions (E ) built out of integers (N) and identifiers (I ), using only + and ∗ Gexp = ({E,I,N},{a,b,0,1,(,),+,∗,−},R, E ) where R is E → I |N | − N |E + E |E ∗E |(E) I → a |b |Ia |Ib N → 0 |1 |N0 |N1 Agha-Viswanathan CS373
  • 60.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Multiple Parse Trees Example 2 The parse tree for expression a + b ∗a in the grammar Gexp is E E E + I E a I b ∗ E I a Agha-Viswanathan CS373
  • 61.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Multiple Parse Trees Example 2 The parse trees for expression a + b ∗a in the grammar Gexp is E E E + I E a I b ∗ E I a E E I a E + ∗ E E I I a b Agha-Viswanathan CS373
  • 62.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Ambiguity Definition A grammar G = (V ,Σ, R, S) is said to beambiguousif there is w ∈Σ∗ for which there are two different parse trees. Agha-Viswanathan CS373
  • 63.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Ambiguity Definition A grammar G = (V ,Σ, R, S) is said to beambiguousif there is w ∈Σ∗ for which there are two different parse trees. Warning! Existence of two derivations for a string does not mean the grammar is ambiguous! Agha-Viswanathan CS373
  • 64.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Removing Ambiguity Ambiguity maybe removed either by Agha-Viswanathan CS373
  • 65.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Removing Ambiguity Ambiguity maybe removed either by Using the semantics to change the rules. Agha-Viswanathan CS373
  • 66.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Removing Ambiguity Ambiguity maybe removed either by Using the semantics to change the rules. For example, if we knew who had the bat (the girl or the boy) from the context, we would know which is the right interpretation. Agha-Viswanathan CS373
  • 67.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Removing Ambiguity Ambiguity maybe removed either by Using the semantics to change the rules. For example, if we knew who had the bat (the girl or the boy) from the context, we would know which is the right interpretation. Adding precedence to operators. Agha-Viswanathan CS373
  • 68.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Removing Ambiguity Ambiguity maybe removed either by Using the semantics to change the rules. For example, if we knew who had the bat (the girl or the boy) from the context, we would know which is the right interpretation. Adding precedence to operators. For example, ∗binds more tightly than +, or “else” binds with the innermost “if”. Agha-Viswanathan CS373
  • 69.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity An Example Recall, Gexp has the following rules E → I |N | − N |E + E |E ∗E |(E) I → a |b |Ia |Ib N → 0 |1 |N0 |N1 Agha-Viswanathan CS373
  • 70.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity An Example Recall, Gexp has the following rules E → I |N | − N |E + E |E ∗E |(E) I → a |b |Ia |Ib N → 0 |1 |N0 |N1 New CFG Gt exp has the rules I → a |b |Ia |Ib N → 0 |1 |N0 |N1 F → I |N | − N |(E) T → F |T ∗F E → T |E + T Agha-Viswanathan CS373
  • 71.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Ambiguity: Computational Problems Removing Ambiguity Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and Gt is unambiguous. Agha-Viswanathan CS373
  • 72.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Ambiguity: Computational Problems Removing Ambiguity Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and Gt is unambiguous. There is no algorithm that can solve the above problem! Agha-Viswanathan CS373
  • 73.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Ambiguity: Computational Problems Removing Ambiguity Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and Gt is unambiguous. There is no algorithm that can solve the above problem! Deciding Ambiguity Problem:Given CFG G , determine if G is ambiguous. Agha-Viswanathan CS373
  • 74.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Ambiguity: Computational Problems Removing Ambiguity Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and Gt is unambiguous. There is no algorithm that can solve the above problem! Deciding Ambiguity Problem:Given CFG G , determine if G is ambiguous. The problem is undecidable. Agha-Viswanathan CS373
  • 75.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Problem:Is it the case that for every CFG G , there is a grammar Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt cannot be constructed algorithmically? Agha-Viswanathan CS373
  • 76.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Inherently Ambiguous Languages Problem:Is it the case that for every CFG G , there is a grammar Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt cannot be constructed algorithmically? No! There are context-free languages L such that every grammar for L is ambiguous. Agha-Viswanathan CS373
  • 77.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Inherently Ambiguous Languages Problem:Is it the case that for every CFG G , there is a grammar Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt cannot be constructed algorithmically? No! There are context-free languages L such that every grammar for L is ambiguous. Definition A context-free language L is said to beinherently ambiguousif every grammar G for L is ambiguous. Agha-Viswanathan CS373
  • 78.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Inherently Ambiguous Languages An Example Consider L = {aibjck |i = j or j = k} Agha-Viswanathan CS373
  • 79.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Inherently Ambiguous Languages An Example Consider L = {aibjck |i = j or j = k} One can show that any CFG G for L will have two parse trees on anbncn, for all but finitely many values of n Agha-Viswanathan CS373
  • 80.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Inherently Ambiguous Languages An Example Consider L = {aibjck |i = j or j = k} One can show that any CFG G for L will have two parse trees on anbncn, for all but finitely many values of n One that checks that number of a’s = number of b’s Agha-Viswanathan CS373
  • 81.
    Context Free Grammars Ambiguity TheConcept Removing Ambiguity Inherently Ambiguous Languages An Example Consider L = {aibjck |i = j or j = k} One can show that any CFG G for L will have two parse trees on anbncn, for all but finitely many values of n One that checks that number of a’s = number of b’s Another that checks that number of b’s = number of c ’s Agha-Viswanathan CS373