TOC Lecture-10
TOC Lecture-10
Ambiguity in
Context Free Grammar
Parsing and Ambiguity
• A terminal string may be generated by a number of different
derivations.
• Question
− Is there a unique leftmost derivation of every sentence
(string) in the language of a grammar?
NO
Parsing and Ambiguity: Example
• Consider the expression grammar:
E E + E | E * E | (E) | -E | id
• Two different leftmost derivations of id + id * id
E E+E E E*E
id + E E+E*E
id + E * E id + E * E
id + id * E id + id * E
id + id * id id + id * id
E E
E + E E * E
id E * E E + E id
id id id id
Parsing and Ambiguity
• A CFG is ambiguous if there is a string w L(G) that can be
derived by two distinct leftmost derivations.
• L(G) = a+
G1: S bS | aA G2: S bS | A
A bA | A Ab |
Parsing and Ambiguity
• Consider the following grammar
E E + E | E * E | num
• Consider the sentence 1 + 2 * 3
• Leftmost Derivation-1 E
E E+E
E + E
1+E
1+E*E 1 E * E
1+2*E
1+2*3 2 3
• Leftmost Derivation-2 E
E E*E
E+E*E E * E
1+E*E E + E 3
1+2*E
1+2*3 1 2
Parsing and Ambiguity
• Different parse trees correspond to different evaluations
(meaning).
LMD-1 LMD-2
+ *
+
*
1 2 3 1 2 3
=7 =9
Eliminating Ambiguity
• Ambiguous: E E + E | E * E | num
– E E*T | T
– T T + num | num
<digit> 0|1|2|3|4|5|6|7|8|9
<letter> a|b|c|…|y|z|A|B|C|…|Y|Z
<sign> +|-
CFGs & Programming Languages
<identifier> <letter> <identifier tail>
<identifier tail> <letter> <identifier tail> | <digit> <identifier tail>
1. Recursive Grammar
2. Non-Recursive Grammar
Recursive Grammar
• A grammar is said to be recursive if it contains at least one
production that has the same variable at both its LHS and
RHS.
OR
• A grammar is said to be recursive if and only if it generates
infinite number of strings.
• Two types:
1. Left recursive grammar
2. Right recursive grammar
Left Recursive Grammar
• A recursive grammar is said to be left recursive if the leftmost
variable of RHS is same as variable of LHS.
OR
• A recursive grammar is said to be left recursive if it has Left
Recursion.
• Example:
A Aα | β
Right Recursive Grammar
• A recursive grammar is said to be right recursive if the
rightmost most variable of RHS is same as variable of LHS.
OR
• A recursive grammar is said to be left recursive if it has Right
Recursion.
• Example:
A αA | β
Problem of Left Recursion
A Aα | β A βα*
A
A α A( )
{
A α
A( );
α;
A α
}
β
The function A( ) recursively calls itself without
checking any condition Infinite loop!
Solution of Left Recursion
Left Recursion Right Recursion
A Aα | β A βα* A αA | β A α*β
A( ) A( )
{ {
A( ); α;
α; A( );
} }
Conversion of Left Recursive to Right Recursive Grammar
A Aα | β A βα*
A β A’ A A
A α A’ | λ β A’ β A’
α A’
λ
α A’
λ
Important Notes
• The grammar which is either left recursive or right recursive is
always unambiguous.
– Examples:
S aS | b (Unambiguous Grammar)
S Sa | b (Unambiguous Grammar)
P P+Q|Q
• Solution:
P QP’
P’ +QP’ | λ
Question-2
• Eliminate the left recursion:
S S 0 S 1 S | 01
• Solution:
S 01S’
S’ 0 S 1 S S’ | λ
Question-3
• Eliminate the left recursion:
A (B) | b
B B*A|A
• Solution:
A (B) | b
B AB’
B’ * AB’ | λ
Question-4
• Eliminate the left recursion:
• Solution:
A β1 A’ | β2 A’ | β3 A | . . .
A’ α1 A’ | α2 A’ | α3 A’ | . . . | λ
Question-5
• Eliminate the left recursion:
X X1 | Y1 | 0
Y Y0 | X1 | 0
• Solution:
X Y 1 X’ | 0 X’
X’ 1 X’ | λ
Y 0 X’ 1 Y’
Y’ 0 Y’ | 1 X’ 1 Y’ | λ
Non-Recursive Grammar
• A grammar is said to be non-recursive if it contains no production
that has the same variable at both its LHS and RHS.
OR
• A grammar is said to be non-recursive if and only if it generates
finite number of strings.
• Example:
S aA | bB
A a|b
B c|d