Professional Documents
Culture Documents
INSTRUCTION TO CANDIDATES
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}
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 ) Lbl1newlab()
Stmt {JMP}Lbl1 {LBL}Lbl2
IfStmt if (BoolExprLbl1) Stmt {JMP}Lbl2 Lbl2newlab()
{LBL}Lbl1 ElsePart {LBL}Lbl2
ElsePart else Stmt | ε
BoolExprLbl1 Exprp comparec Exprq Lbl1newlab()
{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
a) Construct an attributed derivation tree for the following Decaf control statement:
f * 12 - 5 <= 14
(14 Marks)