Compiler Design 1
Compiler Design 1
Compiler Design 1
Design
(Solutions for Vol‐1_Classroom Practice Questions)
a S b
19. Ans: 18
b
id id id id
02. Ans: (d)
Sol:
S S 06. Ans: (c)
Sol: S
S S or S S S
S S a S S a S b S a S b S
a a
S S b S a S
a S b S
S S SS SS ....SS
k1 k2 k3 kl
k1 a k 2 a k 3 a
So the sentence has an infinite number of
derivations. 07. Ans: (a)
Sol: S Ad Sad is indirect left recursion.
(2) A A
(1)
S1.S
S
A + A Or A + A
(3) (6)
S1→.S A
S.AA SA.A SAA. A+A i i A+A
A.aA A A.aA a
(4)
A.b A.b i i i i
b There are two derivation trees for the
(5)
a (7)
(4) sentence i + i + i. As the grammar is
Aa.A A
b (5) AaA. ambiguous it cannot be LL or LR. So, (a),
A.aA a
Ab. (4)
A.b (b), (c), are ruled out. The answer is (d).
b
(5)
57. Ans: 2
53. Ans: (c) Sol: The LR(0) items of the grammar is
Sol: The given grammar is LR(0) and every S1 → . S d A → d.I
LR(0) is LR(1). S → . AaAb B → d.
S → . BbBa
A→.d
B → .d
54. Ans: 2
Sol: Reduce – Reduce conflict.
T
58. Ans: (a)
1 E → T.
E →.E S–R
T → T.* Sol: 1
E→.E+ conflict S → .S, $
T F S → . aAd, $ S → a.Ad, $
b
E→.T S → .bBd, $ S → a.Be, $
T→.T* S → .aBe, $ A → .c, d
F S → .bAe, B → .c, e
T → .F E E → E. + E→E+.T
1
c Ⓧ
F → . id E→E.+T b
T→.T*F A → c., d
T→.F T S → b.Bd, $ B → c., e
E→E+T.
F→.id S–R S → b.Ae,$
T→T.*F
conflict B → .c, d c A → c., e Ⓨ
A → .c, e B → c., d
c
69. Ans: 10 E=a + T=b
Sol: The number of states in both SLR(1) and
LALR(1) are same. T=a b
S S c
a b
01. Ans: (c)
Sol: SDT is part of semantic Analysis
Bottom up traversal of the parse tree results
the output: 10.
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata
: 10 : Compiler Design
06. Ans: (b) 09. Ans: (a)
Sol: S S1 S2 c{ S.val = S1.val * S2.val – 4} Sol: The leftmost derivation for aaaa is S
4 4
S a {S.val = 6} S → aS
a S
S b {S.val = 2} → aaS
3 3
The rightmost derivation of ‘abc’ is → aaaS
a S
S SSc → aaaa 2 2
A B a1b1a2S S a2 S
a1b1a2b2S S b2S
08. Ans: (c) a1b1a2b2b3 S b3
Sol: The rightmost derivation is Above is rightmost derivation
E → E +E → E + E + E ② ③
①
→E+E+E+E S b3 S b2S S a2 S
z zy zyx
→E+E+E+E+E
⑤ ④
a+b+c+d+e S b1S S a1 S
zyxy zyxyx
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata
: 11 : CSIT‐Postal Coaching Solutions
13. Ans: (a) Sol: According to the action of shift reduce
Sol: A A parser, the parse tree constructed is
E
08. Ans: 10 c d
Sol: Rewriting the given assignments
x1 = u1 – t1; → needs two new variables Number of nodes = 4
y2 = x1 v1; → needs three new variables
x3 = y2 + w1; → needs four new variables 14. Ans: (b)
y4 = t2 – z1; → needs five new variables Sol: d n2
+ e5 n3
y5 = y2 + w1 + y4; → needs 10 new
n1 e3 +
variables atmost e – e6
e4 a e7