Professional Documents
Culture Documents
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"
QUESTION 1 (5 marks)
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;
Using the grammar for Decaf compiler (top down), show an attributed derivation tree for the
following expression:
−𝑧
5+𝑦
−𝑥
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;
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
Lbl1newlab() Lbl2newlab()Lbl3newlab()
WhileStmt while {LBL}Lbl1 ( BoolExprLbl2 ) Stmt {JMP}Lbl1 {LBL}Lbl2
Lbl1newlab()
IfStmt if ( BoolExprLbl1 ) Stmt {JMP}Lbl2 {LBL}Lbl1 ElsePart
{LBL}Lbl2
Lbl2newlab()
ElsePart else Stmt | ε
CompoundStmt { StmtList }
StmtList StmtList Stmt | ε
OptAssignExprp AssignExprp | ε
OptBoolExprLbl1 BoolExprLbl1 | ε
BoolExprLbl1 Exprp comparec Exprq {TST}p,q,,7-c,Lbl1
Lbl1newlab()
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