next up previous
Next: Exercise 7. Up: Final-13-04-2004 Previous: Exercise 5.

Exercise 6.

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$bfT
T $ \longmapsto$ $ \bf boolean$
T $ \longmapsto$ $ \bf integer$
E $ \longmapsto$ $ \bf literal$
E $ \longmapsto$ $ \bf num$
E $ \longmapsto$ $ \bf id$
E $ \longmapsto$ E1 $ \bf +$ E2
E $ \longmapsto$ E1 $ \bf =$ E2
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 slight 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 6  
\fbox{
\begin{minipage}{15 cm}
\begin{tabular}{l\vert ll}
$D \longmapsto {\bf i...
...type$\ := & \\
& & \\
& & \\
& & \\
& & \\
\end{tabular}\end{minipage}}

Note that only the type of the non-terminals E, T is required, not their value.


next up previous
Next: Exercise 7. Up: Final-13-04-2004 Previous: Exercise 5.
Marc Moreno Maza
2004-12-02