next up previous
Next: About this document ... Up: Quiz2 Previous: Exercise 2.

Exercise 3.

We consider the following grammar. As in the course, the nonterminals P, D, S, T, E stand for Program, Declaration, Statement, Type, Expression. The terminals id, boolean, integer, literal, num stand for identifier, boolean (as a type), integer (as a type), boolean literal (as a value), integer number (as a value).
P $ \longmapsto$ DS
D $ \longmapsto$ DD
D $ \longmapsto$ $ \bf id$ :  T
T $ \longmapsto$ $ \bf boolean$
T $ \longmapsto$ $ \bf integer$
E $ \longmapsto$ $ \bf literal$
E $ \longmapsto$ $ \bf num$
E $ \longmapsto$ $ \bf id$
E $ \longmapsto$ E $ \bf +$ E
E $ \longmapsto$ E $ \bf =$ E
S $ \longmapsto$ S1S2
S $ \longmapsto$ E
S $ \longmapsto$ $ \bf id$ := S1
S $ \longmapsto$ $ \bf if$ E $ \bf then$ $ \bf\{$S1$ \bf\}$
S $ \longmapsto$ $ \bf while$ E $ \bf repeat$ $ \bf\{$S1$ \bf\}$
There's one sligth difference w.r.t. the grammar of the course in the Type checking chapter: each valid statement has a value and thus a type (rather than no value and type void). The following rules compute the value of a valid statement. We associate attributes T.type, E.type, S.type, to the grammar symbols T, E, S. Each of these attributes may be boolean, integer or type_error. Then, for instance, the statement \fbox{${\bf while} \ E \ {\bf repeat} \ {\bf \{ } S_1 {\bf \} } $} is valid as soon as E.type = $ \bf boolean$ and S1.type $ \neq$ $ \bf type\_error$. The attribute $ \bf id$.entry refers to the entry of $ \bf id$ in the symbol table.

Question. Complete the following type checker for the above grammar.

Answer 3  


next up previous
Next: About this document ... Up: Quiz2 Previous: Exercise 2.
Marc Moreno Maza
2004-12-02