You are on page 1of 4

CONFIDENTIAL 1 CS/FEB 2022/CSC569

UNIVERSITI TEKNOLOGI MARA


FINAL ASSESSMENT B

COURSE : PRINCIPLE OF COMPILERS


COURSE CODE : CSC569
EXAMINATION : FEBRUARY 2022
TIME : 1 HOUR 45 MINUTES (3:30 pm – 5:15 pm)

INSTRUCTIONS TO CANDIDATES

Please use your handwriting. Submit the softcopy of your answer script to me via
normalykamal@yahoo.com before 5:30 pm (19/2/2022).

Please write down the following sentence at the end of your LAST answer script and your
signature. "I hereby declare that the above answers are my own effort"

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 3 printed pages

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 2 CS/FEB 2022/CSC569

QUESTION 1 (5 marks)

Consider the following grammar:


1. S  a B c
2. A  B A
3. A  a
4. B  A B
5. B  
Show a left-most derivation that produces at least 5 terminals’ string.

QUESTION 2 (5 marks)

Show the output of a Java native code compiler, in any typical assembly language, for the following
Java input string:

if (x < y)
{
if (x < z/2)
m=x;
}
else
m=z;

QUESTION 3 (20 marks)

Consider the following grammar:


1. S  {a}aAa
2. S  b
3. A  bAS
4. A  c{a}b

a) Find the selection sets.


b) Show an extended pushdown translator.
c) Show the sequence of stacks for the extended pushdown translator for input string: acba

QUESTION 4 (10 marks)

Using the grammar for Decaf compiler (top down), show an attributed derivation tree for the
following expression:
−𝑧
5+𝑦
−𝑥

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/FEB 2022/CSC569

QUESTION 5 (10 marks)

Using the grammar for Decaf compiler (top down), show a sequence of atoms generated by the
following Decaf statement:

if (x < y)
if (x < z)
m=x;
else
m=z;
else
if (y<z)
m=y;

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/FEB 2022/CSC569

Attributed translation grammar for Decaf language and the table for comparison codes:
Stmt  AssignStmt | ForStmt | WhileStmt | IfStmt | CompoundStmt
| ;
AssignStmt  AssignExprp ;
ForStmt  for (OptAssignExprr; {LBL}Lbl1 OptBoolExprLbl4; {JMP}Lbl2
{LBL}Lbl3 OptAssignExprr) {JMP}Lbl1 {LBL}Lbl2 Stmt
{JMP}Lbl3 {LBL}Lbl4
Lbl1newlab() Lbl2newlab()Lbl3newlab()
WhileStmt  while {LBL}Lbl1 ( BoolExprLbl2 ) Stmt {JMP}Lbl1 {LBL}Lbl2
Lbl1newlab()
IfStmt  if ( BoolExprLbl1 ) Stmt {JMP}Lbl2 {LBL}Lbl1 ElsePart
{LBL}Lbl2
Lbl2newlab()
ElsePart  else Stmt | ε
CompoundStmt  { StmtList }
StmtList  StmtList Stmt | ε
OptAssignExprp  AssignExprp | ε
OptBoolExprLbl1  BoolExprLbl1 | ε
BoolExprLbl1  Exprp comparec Exprq {TST}p,q,,7-c,Lbl1
Lbl1newlab()
Exprp  AssignExprp | Rvaluep
AssignExprp  identifierp = Exprq {MOV}q,,p
Rvaluep  Termq Elistq,p
Elistp,q  + Termr {ADD}p,r,s Elists,q s  alloc()
| - Termr {SUB}p,r,s Elists,q s  alloc()
| ε q  p
Termp  Factorq Tlistq,p
Tlistp,q  * Factorr {MUL}p,r,s Tlists,q s  alloc()
| / Factorr {DIV}p,r,s Tlists,q s  alloc()
|ε q  p
Factorp  ( Exprp )
Factorp  + Factorp
Factorp  - Factorq {Neg}q,,p p  alloc()
Factorp  nump
Factorp  identifierp

Comparison Code(the c value in the


grammar)
== 1
< 2
> 3
<= 4
>= 5
!= 6

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like