Bangladesh University of Business & Technology (BUBT)
Department of Computer Science and Engineering
Final Examination: Summer 2021
Course Code: CSE 323 │Course Title: Compiler Design
Intake: 41st, Program: B.Sc. Engg. in CSE
Marks – 40
[Answer all the questions]
SLN Questions Marks
CO2 1. Consider the following grammar: [6+4]
1. S → A
2. A → A * A
3. A → (A)
4. A → id
a) Generate the canonical collection of LR(0) items and construct
the LR(0) parsing table for the above grammar.
b) Show the parsing of the string “id*id*id” using the above
LR(0) parsing table, which might generate an error. Analyze the
error and provide a solution for it.
CO2 2. a) Consider the following Syntax Directed Definition (SDD): [6]
Productions Rules
E→ id = A; print{A.val}
A→ BD D.d = B.val
A.val = D.a
D→ *BD1 D1.d = D.d * B.val
D.a = D1.a
D→ +BD1 D1.d = D.d + B.val
D.a = D1.a
Page | 1 of 1
D→ Ɛ D.a = D.d
B→ (A) B.val = A.val
B→ num B.val = num.lexval
i. Draw the annotated parse tree for the following expression
(Here, replace x with the last digit of your ID before building
the parse tree):
id = 5 + (9*x);
ii. Draw the dependency graph for the same expression.
b) Perform Shift-Reduce parsing for the given string “(b,(b))” [4]
considering the following grammar:
A → (B) | b
B → B, A | A
CO3 3. a) Consider the following expression (Here, replace q with the last digit of [5]
your ID before translating the statement):
p = (p–q*r)+(–(q*r)+s)
Translate the expression into:
i. Directed Acyclic Graph (DAG)
ii. Quadruples
And, then, write down the necessity of DAG in compiler design.
b) Apply next use information algorithm in the following code fragment: [5]
(Assume that all the variables are live at line zero and all temporaries are
dead at the end of the block):
1. i=1
2. j=1
3. t1 = 10*i
Page | 2 of 1
4. t2 = t1 + j
5. t3 = 8*t2
6. t4 = t3 – 88
7. a[t4] = 0.0
8. t5 = j+1
9. j= t5
10.if j<=10 goto..
And, then, write down the necessity of finding the next use information
of a variable in a basic block.
CO4 4. a) Consider the following block of code: [5]
int n=5,m=0;
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
a = n%i;
flag=a;
break;
}
else
{
b = n%i;
flag = 0;
}
}
Page | 3 of 1
i. Write down three address code for the given block of code.
ii. Then draw the flow graph. And write down the reason of any bock
being basic block.
b) Analyze the flow graph found in question 4(a) and perform the result of [5]
applying common sub-expression elimination, constant folding, copy
propagation and dead code elimination as much as possible to this code.
You should explain the changes in each step.
Page | 4 of 1