Set A Answer Key
Set A Answer Key
Course Code & Title: 18CSC304J & COMPILER DESIGN Duration: 2 periods
Year & Sem: III Year /VI Sem Max. Marks: 50
Part A PI
Marks BL CO PO
Code
1 How many temporary variables are required to express the following
statement in three address code?
if( a+b*h>a*b +h)
A=68
a) 3 1 4 4 3 3.6.1
b) 5
c) 6
d) 4
And: d
2 Consider the following translation scheme.
S -> ER
R -> * E{print{’ * ’);
R|f
E -> F + E{print(’ + ’); | F
F -> (S) | id{print(id.value);}
Here id is a taken that represents an integer and id. value represents the 1 3 4 3 3.6.1
corresponding integer value. For an input ‘2 * 3 + 4’, this translation scheme
prints?
a) 2 * 3 + 4 b) b) 2 * + 3 4 c) 2 3 * 4 + d) 2 3 4 + *
And: d
3 How many basic blocks are there in the following code snippet?
x=8;
y=9;
z=0;
w=1;
L1:
if (x>6) {
if(y>5) {
z=x+y;
z=37;
w=x+y;
x=0; 1 3 4 3 3.6.1
Goto L1;
}}
a) 3
b) 4
c) 5
d) 6
Ans: B
4 What does the following code print? Assume that the execution begins at
Procedure Z.
procedure A
print x
1 3 4 3 3.6.1
procedure B
int x = 6
call A
procedure C
int x = 2
print x
call B
procedure Z
int x = 9;
call A
call B
call C
call A
a) 9 6 2 6 9
b) 9 2 6 9 9
c) 9 6 6 2 9
d) 9 6 6 6 9
Ans: a
5 Which of the following has lower instruction costs?
a) ADD R1, #5
SUB 4(R1), *1(R0)
b) MOV R0, #78
ADD *R0, *R1
c) MOV b, a 1 2 4 1 1.7.1
ADD c, a
d) All have equal costs
Ans: b
Ans: c
7 Which optimization techniques is used to reduce multiple jumps?
A.Latter optimization technique
B.Peephole optimization technique
1 1 5 2 2.7.1
C. Local optimization technique
D. Code optimization technique
Ans B
8 Contiguous memory allocation is possible only in
a) Heap
b) Heap and stack 1 3 5 1 1.7.1
c) Static and stack
d) Static and heap
And: c
9 Consider the following three address code. Identify the CORRECT collection
of different optimization can be performed?
m=3
j=n
v=2*n
limit = integer n / 2
L1: j = j – 1
t4 = 4 * j
1 2 5 1 1.6.1
t5 = a[t4]
if t5 > limit – v goto L1
A. Code Motion, Constant Folding, Induction Variable Elimination,
Reduction in Strength
B. Copy Propagation , Code Motion, Deadcode Elimination, Reduction in
Strength
C. Constant Folding, Copy Propagation, Deadcode Elimination, Reduction in
Strength
D. Code Motion, Constant Folding, Copy Propagation, Induction Variable
Elimination
Ans: A
Part B
11 State various methods of implementing three address statements?
Three Address Code (2)
Three address code is a sort of intermediate code that is simple to create
and convert to machine code. It can only define an expression with three
addresses and one operator. Basically, the three address codes help in
determining the sequence in which operations are action by the compiler.
Pointers for Three Address Code
● Three-address code is considered as an intermediate code and
utilised by optimising compilers.
● In the three-address code, the given expression is broken down
into multiple guidelines. These instructions translate to assembly
language with ease. 4 2 4 1 1.6.1
● Three operands are required for each of the three address code
instructions. It’s a binary operator and an assignment combined.
There are representations of three address codes, namely
1. Quadruple
2. Triples
3. Indirect Triples
Explanation with Example (2)
12 Translate the conditional statement if a< b then 1 else 0 into three address
code
4 3 5 1 1.6.1
(2)
(2)
Part B
( 2*12=20)
16 Write quadruples, triples and indirect triples for the expression:
-(a*b)+(c+d)-(a+b+c+d) and explain the sequences of code generation
algorithm.
a. Write quadruples, triples and indirect triples for the expression:
-(a*b)+(c+d)-(a+b+c+d) (9)
Sol:
First of all this statement will be converted into Three Address Code as−
t1 = a + b
t2 = −t1
t3 = c + d
t4 = t2 ∗ t3
t5 = t1 + c
t6 = t4 − t5 12 2 4 2 1.5.1
Quadruple
(0) + a b t1
(1) − t1 t2
(2) + c d t3
(3) ∗ t2 t3 t4
(4) + t1 c t5
(5) − t4 t5 t6
Triple
(0) + a b
(1) − (0)
(2) + c d
(4) + (0) c
1. Invoke a function getreg to find out the location L where the result
of computation b op c should be stored.
2. Consult the address description for y to determine y'. If the value
of y currently in memory and register both then prefer the
register y' . If the value of y is not already in L then generate the
instruction MOV y' , L to place a copy of y in L.
3. Generate the instruction OP z' , L where z' is used to show the
current location of z. if z is in both then prefer a register to a
memory location. Update the address descriptor of x to indicate
that x is in location L. If x is in L then update its descriptor and
remove x from all other descriptor.
4. If the current value of y or z have no next uses or not live on exit
from the block or in register then alter the register descriptor to
indicate that after execution of x : = y op z those register will no
longer contain y or z.
OR
17 State the syntax directed translation? How it is different from translation
(a)
schemes? Explain with an example.
syntax directed translation:
A technique of compiler execution, where the source code translation is
totally conducted by the parser, is known as syntax-directed translation.
The parser primarily uses a Context-free-Grammar to check the input
sequence and deliver output for the compiler's next stage.
It is a kind of notation in which each production of Context-Free Grammar
is related with a set of semantic rules or actions, and each grammar
symbol is related to a set of Attributes. Thus, the grammar and the group of
semantic Actions combine to make syntax-directed definitions. The
translation may be the generation of intermediate code, object code, or
adding the information in symbol table about constructs type.
Attributes are related to the grammar symbol that are the labels of the
parse tree node. In other terms, attributes are associated information with
language construct by attaching them to grammar symbols representing
that construct. An attribute can describe anything (reasonable) that it can
select a string, a number, a type, a memory location, a code fragment, etc.
For example, an attribute for an identifier can include name, scope, type,
actual arguments (number of parameters), and type of parameters, return
type, etc. The value of an attribute at the parse tree node is represented by
a semantic rule related with the production applied at that node.
(3)
OR
18 Explain the sequence of stack allocation process for a function call.
(a)
Stack Allocation
• Stack allocation is based on the idea of a control stack
• Storage is organized as a stack, and activation records are pushed
and popped as activations begin and end respectively
• Storage for the locals in each call of a procedure is contained in
the activation record for that call
• Thus locals are bound to fresh storage in each activation, because 6 2 5 1 1.6.1
a new activation record is pushed onto the stack when a call is
made
• The values of locals are deleted when the activation ends, because
the storage for locals disappears when the activation is popped
• Suppose that register top marks the top of the stack
• At runtime an activation record can be pushed and popped by
incrementing and decrementing top by the size of the record
Calling Sequences
• Procedure calls are implemented by generating calling sequences
in the target code
• A call sequence allocates an activation record and enters
information into its fields
• A return sequence restores the state of the machine so that the
calling procedure can continue exec
OR
19 Perform all possible optimization on the given code and explain the same.
t0=2
t1=a
t2=12
t3=t1+t2
t4=m[t3]
t5=t0*t4
t6=-16
t7=r+t6
t8=m[t7]
t9=m[t8] 14 3 5 5 1.6.1
t10=t9-t5
t11=4
t12=t10+t11
m[t12]=t10
Ans: 3 marks for each.
Copy propagation
t3=2+12
t4=m[t3]
t5=2*t4
t7=r+(-16)
t8=m[t7]
t9=m[t8]
t10=t9-t5
t12=t10+4
m[t12]=t10
Constant folding
t3=14
t4=m[t3]
t5=2*t4
t7=r+(-16)
t8=m[t7]
t9=m[t8]
t10=t9-t5
t12=t10+4
m[t12]=t10
Copy propagation
t4=m[14]
t5=2*t4
t7=r+(-16)
t8=m[t7]
t9=m[t8]
t10=t9-t5
t12=t10+4
m[t12]=t10
Reduction in strength
t4=m[14]
t5=t4+t4
t7=r+(-16)
t8=m[t7]
t9=m[t8]
t10=t9-t5
t12=t10+4
m[t12]=t10