SlideShare a Scribd company logo
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

More Related Content

What's hot

Finite Automata
Finite AutomataFinite Automata
Finite Automata
Mukesh Tekwani
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
Radhakrishnan Chinnusamy
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
Introduction to MPI
Introduction to MPI Introduction to MPI
Introduction to MPI
Hanif Durad
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
hafizhamza0322
 
advanced computer architesture-conditions of parallelism
advanced computer architesture-conditions of parallelismadvanced computer architesture-conditions of parallelism
advanced computer architesture-conditions of parallelism
Pankaj Kumar Jain
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
Tsegazeab Asgedom
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
Mohammad Imam Hossain
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
Richa Sharma
 
Flat unit 1
Flat unit 1Flat unit 1
Flat unit 1
VenkataRaoS1
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
Kandarp Tiwari
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
Animesh Chaturvedi
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
FInite Automata
FInite AutomataFInite Automata
FInite Automata
Mobeen Mustafa
 
2.2. language evaluation criteria
2.2. language evaluation criteria2.2. language evaluation criteria
2.2. language evaluation criteria
annahallare_
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Mohammad Ilyas Malik
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
Lecture 1 introduction to parallel and distributed computing
Lecture 1   introduction to parallel and distributed computingLecture 1   introduction to parallel and distributed computing
Lecture 1 introduction to parallel and distributed computing
Vajira Thambawita
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
Vikram Nandini
 

What's hot (20)

Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
Introduction to MPI
Introduction to MPI Introduction to MPI
Introduction to MPI
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
advanced computer architesture-conditions of parallelism
advanced computer architesture-conditions of parallelismadvanced computer architesture-conditions of parallelism
advanced computer architesture-conditions of parallelism
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Flat unit 1
Flat unit 1Flat unit 1
Flat unit 1
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
FInite Automata
FInite AutomataFInite Automata
FInite Automata
 
2.2. language evaluation criteria
2.2. language evaluation criteria2.2. language evaluation criteria
2.2. language evaluation criteria
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Lecture 1 introduction to parallel and distributed computing
Lecture 1   introduction to parallel and distributed computingLecture 1   introduction to parallel and distributed computing
Lecture 1 introduction to parallel and distributed computing
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 

Viewers also liked

On Inherent Complexity of Computation, by Attila Szegedi
On Inherent Complexity of Computation, by Attila SzegediOn Inherent Complexity of Computation, by Attila Szegedi
On Inherent Complexity of Computation, by Attila Szegedi
ZeroTurnaround
 
Stress managmentandmanagingambiguity final
Stress managmentandmanagingambiguity finalStress managmentandmanagingambiguity final
Stress managmentandmanagingambiguity final
Anne Yurasek
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
Edhole.com
 
Organisation behaviour
Organisation behaviourOrganisation behaviour
Organisation behaviour
Krishna Chaturvedi
 
7.1. procedimientos almacenados
7.1.  procedimientos almacenados7.1.  procedimientos almacenados
7.1. procedimientos almacenados
Jorge Luis Lopez M
 
Revisão OCPJP7 - Class Design (parte 02)
Revisão OCPJP7 - Class Design (parte 02) Revisão OCPJP7 - Class Design (parte 02)
Revisão OCPJP7 - Class Design (parte 02)
Julio Cesar Nunes de Souza
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)bolovv
 
computer architecture.
computer architecture.computer architecture.
computer architecture.
Shivalik college of engineering
 
Knapsack
KnapsackKnapsack
Knapsack
Karthik Chetla
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
Shiraz316
 
5. spooling and buffering
5. spooling and buffering 5. spooling and buffering
5. spooling and buffering myrajendra
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
Marina Santini
 
Ambiguity
AmbiguityAmbiguity
Ambiguity
Jose Herrera
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recoveryTech_MX
 

Viewers also liked (20)

On Inherent Complexity of Computation, by Attila Szegedi
On Inherent Complexity of Computation, by Attila SzegediOn Inherent Complexity of Computation, by Attila Szegedi
On Inherent Complexity of Computation, by Attila Szegedi
 
Stress managmentandmanagingambiguity final
Stress managmentandmanagingambiguity finalStress managmentandmanagingambiguity final
Stress managmentandmanagingambiguity final
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Organisation behaviour
Organisation behaviourOrganisation behaviour
Organisation behaviour
 
7.1. procedimientos almacenados
7.1.  procedimientos almacenados7.1.  procedimientos almacenados
7.1. procedimientos almacenados
 
Revisão OCPJP7 - Class Design (parte 02)
Revisão OCPJP7 - Class Design (parte 02) Revisão OCPJP7 - Class Design (parte 02)
Revisão OCPJP7 - Class Design (parte 02)
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
Grammar
GrammarGrammar
Grammar
 
computer architecture.
computer architecture.computer architecture.
computer architecture.
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
I/O Buffering
I/O BufferingI/O Buffering
I/O Buffering
 
Parsing
ParsingParsing
Parsing
 
Knapsack
KnapsackKnapsack
Knapsack
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
5. spooling and buffering
5. spooling and buffering 5. spooling and buffering
5. spooling and buffering
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Ambiguity
AmbiguityAmbiguity
Ambiguity
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 

Similar to Ambiguty

1.3.3 Syllogism and Proofs
1.3.3 Syllogism and Proofs1.3.3 Syllogism and Proofs
1.3.3 Syllogism and Proofs
smiller5
 
Abstract Algebra Beamer Lesson1
Abstract Algebra Beamer Lesson1Abstract Algebra Beamer Lesson1
Abstract Algebra Beamer Lesson1
Monica Waters
 
Boolean Programs and Quantified Propositional Proof System -
Boolean Programs and Quantified Propositional Proof System - Boolean Programs and Quantified Propositional Proof System -
Boolean Programs and Quantified Propositional Proof System - Michael Soltys
 
20 sequences x
20 sequences x20 sequences x
20 sequences x
math266
 
20 sequences x
20 sequences x20 sequences x
20 sequences x
math266
 
Pigeonhole Principle,Cardinality,Countability
Pigeonhole Principle,Cardinality,CountabilityPigeonhole Principle,Cardinality,Countability
Pigeonhole Principle,Cardinality,Countability
Kiran Munir
 
Linear algebra-Basis & Dimension
Linear algebra-Basis & DimensionLinear algebra-Basis & Dimension
Linear algebra-Basis & Dimension
Manikanta satyala
 
L03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicL03 ai - knowledge representation using logic
L03 ai - knowledge representation using logic
Manjula V
 
The Chase in Database Theory
The Chase in Database TheoryThe Chase in Database Theory
The Chase in Database Theory
Jan Hidders
 
Adv math[unit 4]
Adv math[unit 4]Adv math[unit 4]
Adv math[unit 4]
Nald Torres
 
Discrete mathematic question answers
Discrete mathematic question answersDiscrete mathematic question answers
Discrete mathematic question answers
Samet öztoprak
 
1 Functions and Permutation.pptx
1 Functions and Permutation.pptx1 Functions and Permutation.pptx
1 Functions and Permutation.pptx
AlrondAbad
 
Lemh1a1
Lemh1a1Lemh1a1
Approximate Bayesian Computation with Quasi-Likelihoods
Approximate Bayesian Computation with Quasi-LikelihoodsApproximate Bayesian Computation with Quasi-Likelihoods
Approximate Bayesian Computation with Quasi-Likelihoods
Stefano Cabras
 
Chapter 3 REGULAR EXPRESSION.pdf
Chapter 3 REGULAR EXPRESSION.pdfChapter 3 REGULAR EXPRESSION.pdf
Chapter 3 REGULAR EXPRESSION.pdf
dawod yimer
 
Computational logic First Order Logic_part2
Computational logic First Order Logic_part2Computational logic First Order Logic_part2
Computational logic First Order Logic_part2
banujahir1
 

Similar to Ambiguty (20)

1.3.3 Syllogism and Proofs
1.3.3 Syllogism and Proofs1.3.3 Syllogism and Proofs
1.3.3 Syllogism and Proofs
 
Abstract Algebra Beamer Lesson1
Abstract Algebra Beamer Lesson1Abstract Algebra Beamer Lesson1
Abstract Algebra Beamer Lesson1
 
Boolean Programs and Quantified Propositional Proof System -
Boolean Programs and Quantified Propositional Proof System - Boolean Programs and Quantified Propositional Proof System -
Boolean Programs and Quantified Propositional Proof System -
 
20 sequences x
20 sequences x20 sequences x
20 sequences x
 
20 sequences x
20 sequences x20 sequences x
20 sequences x
 
Pigeonhole Principle,Cardinality,Countability
Pigeonhole Principle,Cardinality,CountabilityPigeonhole Principle,Cardinality,Countability
Pigeonhole Principle,Cardinality,Countability
 
Linear algebra-Basis & Dimension
Linear algebra-Basis & DimensionLinear algebra-Basis & Dimension
Linear algebra-Basis & Dimension
 
L03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicL03 ai - knowledge representation using logic
L03 ai - knowledge representation using logic
 
FUZZY LOGIC
FUZZY LOGICFUZZY LOGIC
FUZZY LOGIC
 
The Chase in Database Theory
The Chase in Database TheoryThe Chase in Database Theory
The Chase in Database Theory
 
Adv math[unit 4]
Adv math[unit 4]Adv math[unit 4]
Adv math[unit 4]
 
Discrete mathematic question answers
Discrete mathematic question answersDiscrete mathematic question answers
Discrete mathematic question answers
 
1 Functions and Permutation.pptx
1 Functions and Permutation.pptx1 Functions and Permutation.pptx
1 Functions and Permutation.pptx
 
04_AJMS_330_21.pdf
04_AJMS_330_21.pdf04_AJMS_330_21.pdf
04_AJMS_330_21.pdf
 
Analysis Solutions CIII
Analysis Solutions CIIIAnalysis Solutions CIII
Analysis Solutions CIII
 
Lemh1a1
Lemh1a1Lemh1a1
Lemh1a1
 
Lemh1a1
Lemh1a1Lemh1a1
Lemh1a1
 
Approximate Bayesian Computation with Quasi-Likelihoods
Approximate Bayesian Computation with Quasi-LikelihoodsApproximate Bayesian Computation with Quasi-Likelihoods
Approximate Bayesian Computation with Quasi-Likelihoods
 
Chapter 3 REGULAR EXPRESSION.pdf
Chapter 3 REGULAR EXPRESSION.pdfChapter 3 REGULAR EXPRESSION.pdf
Chapter 3 REGULAR EXPRESSION.pdf
 
Computational logic First Order Logic_part2
Computational logic First Order Logic_part2Computational logic First Order Logic_part2
Computational logic First Order Logic_part2
 

More from Rajendran

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower bounds
Rajendran
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding Costs
Rajendran
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower bounds
Rajendran
 
Red black tree
Red black treeRed black tree
Red black tree
Rajendran
 
Hash table
Hash tableHash table
Hash table
Rajendran
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
Rajendran
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
Rajendran
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
Rajendran
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
Rajendran
 
Master method
Master method Master method
Master method
Rajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
Rajendran
 
Hash tables
Hash tablesHash tables
Hash tables
Rajendran
 
Lower bound
Lower boundLower bound
Lower bound
Rajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
Rajendran
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
Rajendran
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
Rajendran
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
Rajendran
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
Rajendran
 
Np completeness
Np completenessNp completeness
Np completeness
Rajendran
 
computer languages
computer languagescomputer languages
computer languages
Rajendran
 

More from Rajendran (20)

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower bounds
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding Costs
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower bounds
 
Red black tree
Red black treeRed black tree
Red black tree
 
Hash table
Hash tableHash table
Hash table
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
 
Master method
Master method Master method
Master method
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Hash tables
Hash tablesHash tables
Hash tables
 
Lower bound
Lower boundLower bound
Lower bound
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
 
Np completeness
Np completenessNp completeness
Np completeness
 
computer languages
computer languagescomputer languages
computer languages
 

Recently uploaded

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 

Ambiguty

  • 1. Context Free Grammars Ambiguity CS 373: Theory of Computation Gul Agha Mahesh Viswanathan University of Illinois, Urbana-Champaign Fall 2010 Agha-Viswanathan CS373
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. Context Free Grammars Ambiguity Definitions Proving Properties Parse Trees Example: Palindromes Example A string w is apalindromeif w = wR . Agha-Viswanathan CS373
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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
  • 40. 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
  • 41. 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
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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
  • 52. 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
  • 53. 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
  • 54. 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
  • 55. 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
  • 56. 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
  • 57. 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
  • 58. 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
  • 59. 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
  • 60. 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
  • 61. 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
  • 62. 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
  • 63. 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
  • 64. Context Free Grammars Ambiguity The Concept Removing Ambiguity Removing Ambiguity Ambiguity maybe removed either by Agha-Viswanathan CS373
  • 65. 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
  • 66. 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
  • 67. 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
  • 68. 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
  • 69. 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
  • 70. 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
  • 71. 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
  • 72. 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
  • 73. 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
  • 74. 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
  • 75. 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
  • 76. 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
  • 77. 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
  • 78. Context Free Grammars Ambiguity The Concept Removing Ambiguity Inherently Ambiguous Languages An Example Consider L = {aibjck |i = j or j = k} Agha-Viswanathan CS373
  • 79. 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
  • 80. 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
  • 81. 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