You are on page 1of 4

CONFIDENTIAL TEST2/CSC569/MARAUG2022

UNIVERSITI TEKNOLOGI MARA


TEST 2

COURSE : PRINCIPLES OF COMPILERS


COURSE CODE : CSC569
TIME : 1 HOUR 30 MINUTES
SEMESTER : MARCH – AUGUST 2022

INSTRUCTION TO CANDIDATES

1. Test will be conducted from 2 PM to 3.30PM.


2. You are required to answer ALL questions by handwriting.
3. Once completed, PDF scan your answer individually according to question number:
Q1, Q2, Q3. Use a standardized file naming convention, YOUR FULL
NAME_QUESTION NUMBER.pdf e.g MOHD ALI BIN ABU_B1.pdf.
4. Submit each of these answers separately via Google Classroom.
5. You will be given an extra 15 minutes for submission purpose (Due at 3.45 PM).
6. Points will be deducted for late submission.
7. Answers that are found to be similar as another individual’s, and/or taken directly
from any published source WILL NOT be accepted.

Reminder: This is one of the assessments for CSC569. Therefore, to


discuss/share/disseminate the questions and answers amongst your
classmates/coursemates are strictly prohibited. If you are found to be guilty or
committed one of those actions, your marks will be deducted, and you will be
penalised.

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL TEST2/CSC569/MARAUG2022

QUESTION 1
Given the following grammar, show the recursive descent parsers of non-terminals S
and A.
1. S → a S b A
2. S → c S
3. S → ε
4. A → b A S d
5. A → ε
(10 Marks)

QUESTION 2
Given the following translation grammar, answer these following questions.
1. Expr → Factor {1} Tlist {2}
2. Factor → ident Tlist {3} num
3. Factor → {4}
4. Tlist → Factor num
5. Tlist → {5}

Find the selection sets for the grammar.


(15 Marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL TEST2/CSC569/MARAUG2022

QUESTION 3
Consider the following attributed translation grammar for Decaf expression and the table
for comparison codes:

Stmt  AssignStmt|WhileStmt|
IfStmt|CompoundStmt| ;
AssignStmt  AssignExprp ;
WhileStmt  while {LBL}Lbl1 ( BoolExprLbl2 ) Lbl1newlab()
Stmt {JMP}Lbl1 {LBL}Lbl2
IfStmt  if (BoolExprLbl1) Stmt {JMP}Lbl2 Lbl2newlab()
{LBL}Lbl1 ElsePart {LBL}Lbl2
ElsePart  else Stmt | ε
BoolExprLbl1  Exprp comparec Exprq Lbl1newlab()
{TST}p,q,,7-c,Lbl1
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  - Factorq {Neg}q,,p p  alloc()
Factorp  nump
Factorp  identifierp

Comparison Code Logical Code for


(the c value in Complement Complement
the grammar)
== 1 != 6
< 2 >= 5
> 3 <= 4
<= 4 > 3
>= 5 < 2
!= 6 == 1

a) Construct an attributed derivation tree for the following Decaf control statement:

f * 12 - 5 <= 14
(14 Marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL TEST2/CSC569/MARAUG2022

b) Convert the following segment of Decaf program into a sequence of atoms:


while ( f <= 14 )
{
f = f + n;
}
(6 Marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like