Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Compiler Design 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Compiler

Design
(Solutions for Vol‐1_Classroom Practice Questions)

08. Ans: (b)


2. Lexical Analysis
Sol: Syntax analysis can be expanded but the
01. Ans: (a) CFG describes the syntax becomes
Sol: Comments are deleted during lexical cumbersome.
analysis, by ignoring comments.
09. Ans: (a)
02. Ans: (a) Sol: The identifiers are entered into the symbol
Sol: The expansion of macro is done as the input table during lexical analysis phase.
tokens are generated during the lexical
analysis phase. 10. Ans: (a)
Sol: As I/O to an external device is involved
03. Ans: (a) most of the time is spent in lexical analysis
Sol: As soon as an identifier identifies as
lexemes the scanner checks whether it is a 11. Ans: (a)
reserved word. Sol: The lex utility creates a DFA from the
regular definition.
04. Ans: (c)
Sol: Type checking is a semantic feature. 12. Ans: (b)
Sol: The specifications of lexical analysis we
05. Ans: (a) write in lex language, when it run through
Sol: Compiler identifies only Grammatical lex compiler it generates an output called
errors, but not logical & runtime errors. lex.yy.c.

06. Ans: (d) 13. Ans: (a)


Sol: A compiler that runs on one machine and Sol: Parenthesis matching cannot be done at the
generates code for another machine is called lexical analysis phase.
cross compiler.
14. Ans: 20 15. Ans: 7
07. Ans: (b)
Sol: The object code which is obtained from 16. Ans: (b)
Assembler is in Hexadecimal, which is not Sol: if , (, x , > =, y, ), {, x, =, x, +, y, ; , },
executable, but it is relocated. else, {, x, =, x, –, y, ; , }, ; ,
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 2 : Compiler Design

17. Ans: (d) 03. Ans: (a)
Sol: All are tokens only. Sol: The grammar which is both left and right
recursive is always ambiguous grammar.
18. Ans: (c)
Sol: Syntax tree is input to semantic analyzer. 04. Ans: (d)
Character stream is input to lexical analyzer. Sol: S
Intermediate representation is input to code a b
S
generation. Token stream is input to syntax
analyzer. b S a

a S b
19. Ans: 18
b

20. Ans: (b)


Hence the option (d) is correct.

3. Parsing Techniques 05. Ans: 2


Sol:
E E
01. Ans: (b)
E + E E + E
Sol: As + is left associative the left most +
should be reduced first id E + E E + E id

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.

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 3 : CSIT‐Postal Coaching Solutions

08. Ans: (c) 15. Ans: (d)
Sol: The production of the form A → A / is Sol: Operator precedence parser is a shift reduce
left recursive, and can be eliminated by parser.
replacing with
A → βA1 16. Ans: (c)
1
A →  A / 1
Sol: first(s) = first(A)  first(a)  first (Bb)
= {d}  {f, a} {e, b}={a,b,d,e,f}
09. Ans: (d)
Sol: ↑ is least precedence and left associative 17. Ans: (d)
+ is higher precedence and right associative Sol: {$, s} both follow additional.

10. Ans: (c) 18. Ans: (c)


Sol: Precedence from low to high is ↑, +, id. Sol: first(A) = {a, c}, follow(A) = {b, c}
first(A)  follow(A) = {c}
11. Ans: (b)
Sol: – > * 19. Ans: (d)
Sol: Follow(B) = First(C)First(x)  Follow (D)
12. Ans: 144 = {y, m} {x}  Follow(A) First(B)
Sol: 3–2*4$2*3$2 = {y, m, x}  {$}  {w, x}
1*4$2*3$2 = {w, x, y, m, $}
1*16*9
16*9 20. Ans: (a)
= 144 Sol: Follow (S) = {$}
Consider S  [SX]
13. Ans: (b) Follow (S) = First (X)
Sol: Rule ‘a’ evaluates to 4096 = {+,–, b}  {]}
Rule ‘b’ evaluates to 65536 = {+, – , b, ]}
Rule ‘c’ evaluates to 32 Consider X  + SY
Follow(S) = First (Y)
14. Ans: (c) = {–}  Follow(X)
Sol: A bottom up parsing technique builds the
= {–}  {c, ]}
derivation tree in bottom up and simulates a
= {–, c, ]}
rightmost derivation in reverse

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 4 : Compiler Design

Consider Y → – S X c The grammar is LL(3) as on input abc there
Follow(S) = First(X) is not choice.
= {+, –, b}  First(c)
= {+, –, b, c} 26. Ans: (c)
 Follow(S) = {+, – , b, c, ], $} Sol: To distinguish between
S → if expr then stmt
21. Ans: (c) & S → if expr then stmt else stmt
Sol: Follow (T) = {+, $} We need a look ahead of 5 symbols.

First (S) = {a, +, }


 Follow (T)  First (S) = {+} 27. Ans: (c)
Sol: * has a higher precedence than +.
Consider
22. Ans: (d)
Sol: Follow(A)=first(B)Follow(S)  Follow(B) E
={e}  {f}  {c, d}={c,d,e,$}.
E + T

23. Ans: (d) T T* F


Sol: Follow (S) = { $, a,d}
F F id
Follow (A) = {a}
Follow (B) = {a,d}
id id
Follow (C) = {$, a, d}
28. Ans: (a)
24. Ans: (c) Sol: A left recursive grammar cannot be LL(1).
Sol: The predictive parsing table.
identifier * $ 29. Ans: (c)
<expression> <expression>→
<factor><rest> Sol: A →  production is added in ‘A’ row and
<rest> <rest>→ <rest>→ Follow(A) column.
*<expression>
<factor> <factor>
→ identifier 30. Ans: (d)
Sol: S→aSbs and S→ both appear in ‘S’ row
25. Ans: (c) and ‘a’ column.
Sol: The grammar is not LL(1), as on input
symbol a there is a choice. 31. Ans: (b)
The grammar is not LL(2), as input ab there Sol: The first 2 symbols of ‘S’ production is
is a choice. distinct hence the grammar is LL(2).

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 5 : CSIT‐Postal Coaching Solutions

32. Ans: (d) 40. Ans: (d)
Sol: The rightmost derivation is Sol: Possible relations with ‘c’ are d>c and
<accumulated_sum>→<accumulated_sum> c>$ only.
*<number>
→<accumulated_sum>+<number>* number
41. Ans: (b)
→ number + number * number
Sol: The grammar E  E + E/a can have an
33. Ans: (d) operator precedence parser but not an LR
Sol: An operator grammar is -free grammar and parser.
no two non terminals are adjacent.
42. Ans: (a)
34. Ans: (c) Sol: The grammar
Sol: An operator grammar is ‘’ free grammar E → E + T | T, T → i
and no two non-terminals are adjacent. is left recursive. So it is not LL(1) but is
LR(0). So (a) is true & (b) is false.
35. Ans: (c) The grammar
S → a | aA
Sol: An operator grammar is ‘’-free grammar
A→b
and has no two adjacent non-terminals.
has the LR(0) machine

36. Ans: (d)


S → .a a S → a.
Sol: As per normal HLL rules exponentiation is
S → .aA S → a.A
right associative where as –, +, * are left A → .b
associative.

Hence not LR(1) but is SLR(1).


37. Ans: (d)
Sol: Lead (S) = {a}  {c}  Lead (B)  {d}
43. Ans: (d)
= { a,c,d,e}
Sol: The grammar
38. Ans: (b) E → E +E | E  E | i
Sol: Trail (E) = {+}  Trail(T) Can have a shift reduce parser if we use the
= {+, *}  Trail(F) precedence and associativity of operations.
= {+, *, ), id}
The operator precedence technique works
with some ambiguous grammars.
39. Ans: (b)
Sol: Lead (E) >+ and lead (E) contains {+, ↑, id}

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 6 : Compiler Design

44. Ans: (d) 49. Ans: (a)
Sol: The grammar Sol: The LR(0) machine for the grammar
S → a | A, A → a <session>→ play. <session>
<life> → . <session> <session>
is neither LL(1) nor LR(0) & is ambiguous. <session> → . play <session> <session> → .<session> <session>
<session> → . rest <session> → .play <session>
No ambiguous grammar can be LL or LR. <session> → .rest

<session> play <session> <session>


45. Ans: (d)
play
Sol: No ambiguous grammar can be LR(1). <life> → <session> <session> <life> → <session> <session>.
<session> → . play <session>
<session> → . rest
46. Ans: (c)
rest
Sol: The grammar rest rest
<session>
S → Aa | Bb <session> → rest.
<life> → <session> <session>.
A→
B →  is LL(1) but not LR(0)
The LR(0) machine has a conflict. 50. Ans: (b)
S → .Aa A a Sol: The LR(0) machine
S → A.a S → Aa.
S → .Bb E → FR
A→. B b
S → B.b S → Bb. R → E/
B→. 
A → . F → id
conflict
B → . R
E → F.R E → FR.
E → .FR F R → .*E
The grammar is
F → .id R→. 
S → a | ab R → .
Is LR(2) & not LR(1). id
* F
F →id. R → *.E
47. Ans: (d) E → .FR E
id R → *E.
F → .id
Sol: Every LR(0) grammar is SLR(1)
Every SLR(1) grammar is LALR(1)
Every LALR(1) grammar is LR(1)
The grammar S → a is both LL(2) & LR(0) 51. Ans: (b)
trivially. Sol:
S1→.S
S→.SB
48. Ans:(b) S→.A
Sol: Every LL(1) is LR (1) A→.a

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 7 : CSIT‐Postal Coaching Solutions

52. Ans: 7 56. Ans: (d)
Sol: Sol: The grammar is ambiguous.

(2) A A
(1)
S1.S
S
A + A Or A + A
(3) (6)
S1→.S A
S.AA SA.A SAA. 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
Aa.A A
b (5) AaA. ambiguous it cannot be LL or LR. So, (a),
A.aA a
Ab. (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

55. Ans: (a) Consider the partial LR(1) machine shown


Sol: The grammar is LL(1), LR(0), SLR(1), above. The states Ⓧ & Ⓨ have a common
LALR(1) & LR(1).
core. However if we merge the sates to
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 8 : Compiler Design

obtain the LALR(1) machine we will end up 61. Ans:(d)
with conflicts. So the grammar is LR(1) but Sol: I0 I1 I4
not LALR(1). S  . S, $ b S  b. Ac, $ S  bd . a, $
d
S  .Aa, $ S  b. da, $ A  d ., c
S  .bAc, $ A  . d, c a
59. Ans: (a) S  .dc, $ I5
Sol: S  .bda, $ I2
c S→bda.,$
A  .d, a d S  d. c, $
Ad.,a I3
S → .Aa, $ S  dc., $
S → . bAc, $ b S → b.Ac, $
S → .Bc, $ S → b.Ba, $
S → .bBa, $ A → .d, c
A → .d, a B → .d. a
As there is no conflicts the grammar is in
B → .d, c d Ⓨ
d LALR(1).
Ⓧ A → d., c
B → d., a
A → d., a
B → d., c 62. Ans: (c)
Sol: S→.A,$ S→. A, $
Consider the partial LR(1) machine above. A→. AB, $/ Follow (A)  A→. AB, $/b
The states Ⓧ&Ⓨ have a common core but A→. , $/ Follow (A) A→., $/b

different look ahead sets. If we merge


63. Ans: (d)
Ⓧ&Ⓨ So obtain the LALR(1) a conflict
Sol:
arise.
S
S1  S, $
S1  .S, $
S  S. a, $
60. Ans: (b) S .aS, $
S .Sa, $ 3-Reduce-Conflict S-R Conflict
Sol: LR(1) items of the grammar is S→.a, $
a
S  a.S, $ S S  a.S., $
② S  a., $ S  S. a, $
① S→.a, s, S→Sa.$, S→.a,$
A  .A, $ ③
A  a. A,$/b No. of conflicts=2
A  .aA, $/b a A  .aA, $/b A A  aA. $/b
A  .Ab, $/b A  .Ab, $/b A  A.d, $/b
A  .d, $/b b
A  .d, $/b
64. Ans: (c)
A  Ab., $/b Sol: 1
S  .S. $
S . L=R, $
Item 3 has Shift-Reduce conflict. S . R, $
L→. *R,=/ $
L→.id, = /$
R→.L, $

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 9 : CSIT‐Postal Coaching Solutions

65. Ans : (c) 02. Ans: (c)
Sol: The grammar is only LR(1) Sol: The attribute ‘val’ is synthesized and the
SDD is S-attributed and every ‘S’-attributed
66. Ans: (d) is L-attributed definition
Sol: The grammar is LL(1)
03. Ans: (c)
S  .S
S  .(S) Sol: Given SDT counting the number of a’s and
S . b’s in a given string.

Every LL(1) is LR (1) 04. Ans: (c)


Sol: For input: a + b – c
67. Ans: (b)
E = – + abc
68. Ans: (b)
Sol: SLR(1) & LALR(1) have the same number
E = + ab – T=c
of states. LR(1) may have more.

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

70. Ans: (c) a


Sol: YACC uses LALR (1) parse table as it uses
less number of states requires less space and 05. Ans: (c)
takes less time for the construction of parse Sol:
tree. S

S S c

4. Syntax Directed Translation Schema 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

 S bc The dependency graph a S


1
abc a
In S1 S2 c, S1.val = 6, S2.val = 2. So answer
10. Ans: (a)
is “8”.
Sol: The rightmost derivation is
S → aB → aa BB →aa Bb → aa bb
07. Ans: (c)
Sol: (A (AB))
11. Ans: (c)
G Neg(AND(A, OR(NOT(A),B)))
Sol: S → aA {print 1}
F Neg(AND(A, OR(NOT(A),B))) S → a {print 2}
A → Sb {print 3} S
 F1 AND(A, OR(NOT(A),B))
Input: aab a a b
a A
a s b 231
( F ) AND(A, OR(NOT(A),B))
S b a A
A=F  F OR(NOT(A),B) S
a
12. Ans: (c)
( F ) OR(NOT(A),B) Sol: a1b1a2b2b3
S  a1S S  a1 S
A=F  F=B
 a1b1S S  b1S

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

A + A (1) A + A (1) E ↑ E print (↑)

A (1) a (2) c print (c)


A + a (2) A + A (1) E * E print (*)

a print(a) b print (b)


a a (2) a (2) a (2)
(2)
The Depth First Traversal of the above parse
14. Ans: (c) tree is a b * c ↑
Sol: As the grammar is ambiguous & we do not
specify the precedence of operators either
postfix form may result depending on the 5. Intermediate Code Generation
parser implementation.

01. Ans: (c)


15. Ans: (d) Sol: The purpose of using intermediate codes in
Sol: S compilers is to reuse machine independent
code for other compilers.
A B

C * A {print *} B 02. Ans: (d)


Sol: The final result is the machine language
5 {print 5} 
C + A {print +}
code. The others are all standard
intermediate forms.
6 {print 6}
C
03. Ans: (d)
7 {print 7} Sol: TAC is a statement that contains atmost
The depth first traversal of above tree prints three memory references.
567+*
04. Ans: (d)
16. Ans: (a) Sol: TAC can be implemented as a record
structure with fields for operator, and
arguments as Quadruples, triples and
indirect triples.
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 12 : Compiler Design

05. Ans: (b) 10. Ans: (d)
Sol: The Quadruples is record structure with four Sol: Peephole optimization expression is the final
fields. code.
1. (*, b, c, T1)
2. (+, a, T1, T2) 11. Ans: (d)
3. (–, T2, d, T3) Sol: DAG for the expression a*b*b is
*
06. Ans: (c) *
Sol: (1) (and, b, c, T1) a b
(2) (or, a, T1, T2, c, T3)
(3) (or, T2, c, T3) 12. Ans: (b)
Sol: DAG is constructed based on precedence
and associativity of operators, and option (b)
07. Ans: (a) is the correct representation.
Sol: 1. (+, b, c)
2. (NEG, (1)) 13. Ans: 4
3. (*, a, (2)) Sol:
+ a + 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

09. Ans: (b) e1 e2 + n4


n6 d
Sol: All assignments in SSA are to variables with + e8
distinct names a b Nodes = 8
c
p3 = a – b a=b+c n7 Edges = 10
n8
q4 = P3 * c c=a+d
p4 = u * v d=b+c
q5 = P4 + q4 e=d–b
a=e+d
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 13 : CSIT‐Postal Coaching Solutions

Number of nodes = 8 for a variation of index i by 1, 1024 bytes
Number of edges = 10 must be skipped. So the answer must be (a)

15. Ans: (a) 16. Ans: (b)


Sol: In C the storage for array is row major order. Sol: (1) (+, c, d)
Between X[l] [32] [8] & X [l+1] [32] [8] (2) ( –, b, (1))
there must be 32  8 integer of type int i.e (3) (*, e, f)
32  8  4 = 1024 bytes. So in X[i] [j] [k] (4) (+, (2), (3))
(5) (=, a, (4))

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

You might also like