Lecture11 12
Lecture11 12
(i) S S (ii) S S
+ *
S S S + S
3 * 5
4 5 3 4
3+(4*5) = 23 (3+4)*5 = 35
4 * 5 S S
Similarly
(ii) => 7 =>35
S * 5 * 5
=>
3 +4
Addressing Ambiguities in CFG
The ambiguity that has been observed in above example
can be removed with a change in the CFG as discussed
in the following example
(i) S + S (ii) S * S
3 S * S S + S 5
4 5 3 4
(i) S + S => +
(i) 3 *
3 S * S
4 5 4 5
S S +3*45
(ii) S * S => *
S + S 5 (ii) + 5
3 4
* +3 4 5
4 3
Evaluation Procedure:
+3*4 5 = +3 20 = 23
*+3 4 5 = * 7 5 = 35
Note
• 4*5+3 is an infix arithmetic expression
• While an arithmetic expression in (o-o-o) form is a
prefix arithmetic expression.
Evaluation Procedure: S
*
*+*+1 2+3 4 5 6 = *+*3+3 4 5 6 + 6
= *+*3 7 5 6 = *+21 5 6 = *26 6 = 156. * 5
+ +
1 23 4
a S
a S
aa aXX
bX
aXb
abX abb
bab bb
aabb
aabX aXab
abab abb
S → X|b, X → aX X b
Total language tree of the above CFG aX
aaX
It is to be noted that the only word in
aaa …aX
this language is b.