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

2016

WORKBOOK
Detailed Explanations of
Try Yourself Questions

Computer Science & IT


Compiler Design
1 Introduction and Lexical Analysis

T1 : Solution

(d)
Lexical analyzer produces an error when an illegal character appears in the string pattern that makes
invalid token.

T2 : Solution

(b)
G3 and G4 are set of left recursive and set of right recursive grammars respectively. L(G3) L(G4). Given
any left recursive grammar can be converted to right recursive and vice-versa.

www.madeeasypublications.org Copyright
2 Parsing and Syntex
Directed Translation

T1 : Solution

(b)

For any particular string:


# parse trees = # LMDs = # RMDs
l=P=r

T2 : Solution

(d)

Var id.[E]
Both will be in the same state of LR(0) parser and produces SR conflict in LR(0) .
Var id.

T3 : Solution

(d)
Recursive descent parser can not use left recursive grammar but it can use right recursive grammar.

Copyright www.madeeasypublications.org
4 Computer Science & IT Compiler Design

T4 : Solution

(a)
For string : IF TRUE THEN IF TRUE THEN ELSE END has two parse trees.

stmt

ifstmt

IF bexpr THEN stmt

TRUE ifstmt
END

IF bexpr THEN stmt ELSE stmt

TRUE

stmt

ifstmt

IF bexpr THEN stmt ELSE stmt END

TRUE
ifstmt

IF bexpr THEN stmt

TRUE

The given grammar is ambiguous, hence it is not LL(1) and also not LR(1).

T5 : Solution

(d)

Set 1 has RR conflict.


A a., {b}
B ba., {b, c}
{b} {b, c}
Grammar produces RR conflict for CLR (1).

www.madeeasypublications.org Copyright
Workbook 5

T6 : Solution

(d)
Set 3 contain S as reduced item.
Follow (S) = { ), $}
In row 3, entry for column ) and $ will be rb.
E1 = rb, E2 = rb [ b:S ]
In set 5, on S it goes to set 6.
In row 5, entry for non-terminals S is state 6.
S
E3 = 6 [ (5) (6) ]
E1 = rb, E2 = rb and E3 = 6

T7 : Solution

(c)

FOLLOW (A) = LFOLLOW(A) = RFOLLOW(A)


The set of terminals followed by A are same in all sentential forms.

T8 : Solution

(a)
(a) An unambiguous grammar can have different leftmost and rightmost derivation. However, an
unambiguous grammar has only one derivation tree. So option (a) is false.
(b) LL(1) is a top-down parser.
(c) LALR is more powerful than SLR.
(d) For any parser, grammar should be unambiguous.

T9 : Solution

(a)

For Input string: aab


LR parser reduces aS to S to parse the string aab when stack has aaS.

Copyright www.madeeasypublications.org
3 Runtime Environment

T1 : Solution

(b)
Symbol table maintains: Frame pointer, Access link, Local variables, Return address, Return value, etc.

T2 : Solution

(d)

Main

A1

A2

A21

A1
FRAME ACCESS
POINTER LINKS

Given calling sequence from the program is: Main A1 A2 A21 A1


A1 and A2 are defined in Main, so A1 and A2 access links are pointed to Main.
A21 definition is available in A2, hence A21 access link points to A2.

www.madeeasypublications.org Copyright
4 Intermediate Code Generation
and Code Optimization

T1 : Solution
(d)
Constant folding,
Strength reduction,
Copy propagation,
Loop unrolling,
Code motion can be applied on the given code.

T2 : Solution

(d)
x = x 0;
for (i = 1; i < 5; i + +)
{
x = x + 1;
y = 10;
}
for (i = 1; i < 5; i + +)
{
y = y 2;
}
Apply code optimizations: [Constant folding, Strength reduction, Copy propagation, Loop unrolling, and
Code motion.]
x = 4;
y = 160;
Option (d) is correct.

Copyright www.madeeasypublications.org
8 Computer Science & IT Compiler Design

T3 : Solution

(c)
Loop unrolling makes more number of statements.
Example:
for (i = 0; i < 10; i++)
{
x = x + 1;
}

for (i = 0; i < 5; i++)
{
x = x+1;
x = x+1;
}

T4 : Solution

(b)
x = 2 can be propagated [copy propagation]

x = 2;
y = 2; y=2 propagated
z = 2P+32
z = 2 P+3y;
constant folding
z = 2P+6
strength reduction
z = P<< 1 + 6

for (i = 1; i < 10; i++)


Loop unrolling can be applied
z = z + i;

No deadcode is present in the given code, hence there is no possibility of deadcode elimination.

T5 : Solution

(b)

x = 2;
y = 2; x = 2 is propagated first
z = 2 P+3 2;
for(i=1; i<10; i++) y = 2 is propagated next
{z=z+1;}

www.madeeasypublications.org Copyright
Workbook 9

T6 : Solution

(c)
S1 : s = q + r
1 S2 : s = p + q
S3 : u = s * v

2 3
S4 : v = r + u S5 : q = s * u

4 S6 : q = v + r

(i) p is not live both at Block 2 and Block 3. There is a path from (S4 & S5) to S2, but S1 has an assignment
to p.
(ii) q is not live both at Block 2 and Block 3. There is a path from (S4 & S5) to S1, but S6 has an assignment
to q in between the path.
(iii) r is live both at Block 2 and Block 3. There is a path from (S4 & S5) to S6, S6 uses r and no assignment
to r in this path.
(iv) u is not live both at Block 2 and Block 3. There is a path from S4 to S5 and S5 to S4 where both S4 and
S5 uses u but S3 has an assignment to u in this path.
(v) Similarly s and v are not live both at Block 2 and Block 3
Only r is live both at basic Block 2 and basic Block 3.

Copyright www.madeeasypublications.org

You might also like