next up previous
Next: L-attributed Definitions Up: Compiler Theory: Syntax-Directed Translation Previous: Syntax Trees for Expressions

Translation Schemes

In this section, we enhance the notion of a syntax-directed definitions in order to specify the order of evaluation of the semantic rules, leading to the concept of a translation scheme.


KEY FEATURES.

Translation schemes are well adapted when the semantic rules are procedure calls.


A TRANSLATION SCHEME is a context-free grammar in which semantic rules are embedded within the right sides of the productions.

Example 8   Let L be a language of arithmetic expressions over the alphabet

$\displaystyle \Sigma$  =  $\displaystyle \Sigma_{{{op}}}^{{}}$  $\displaystyle \cup$  $\displaystyle \Sigma_{{{val}}}^{{}}$  $\displaystyle \cup$  {$\displaystyle \bf ($,$\displaystyle \bf )$}. (6)

where $ \Sigma_{{{op}}}^{{}}$ is the set of operators and $ \Sigma_{{{val}}}^{{}}$ the set of values. We assume that L is binary infix. We associate to L a language L' of arithmetic expressions, binary postfix. This is given by a map p defines as follows. The translation from L to L' can be made by a syntax-directed definition of the form
Production Semantic Rule
A $ \longmapsto$ a A.str := a
A $ \longmapsto$ (B) A.str := B.str
A $ \longmapsto$ B f C A.str := concat(B.str, C.str, " f")
where A.str, B.str and C.str are synthesized attributes of type string and where concat is a concatenation function for strings. Since this syntax-directed definition has no inherited attributes, the evaluation of the attributes (and consequently the translation) will be easy. However, observe that for a large expression, a certain amount of time and space will be needed to create all the attributes.

It is possible to speed up translations from L to L' by considering the following translation scheme.

A $ \longmapsto$ a { output(a) }
A $ \longmapsto$ (B) { }
A $ \longmapsto$ B f C { output(f) }

We illustrate the notions of syntax tree and translation scheme with the expression a + (a + d )*(b - c).

Figure 11: A syntax tree for the expression a + (a + d )*(b - c).
\begin{figure}\htmlimage
\centering\includegraphics[scale=.4]{syntaxTree.eps}
\end{figure}
Figure 12: A parse tree for the expression a + (a + d )*(b - c).
\begin{figure}\htmlimage
\centering\includegraphics[scale=.4]{parseTree.eps}
\end{figure}
Figure 13: A parse tree decorated with translation rules for the expression a + (a + d )*(b - c).
\begin{figure}\htmlimage
\centering\includegraphics[scale=.4]{translationTree.eps}
\end{figure}


next up previous
Next: L-attributed Definitions Up: Compiler Theory: Syntax-Directed Translation Previous: Syntax Trees for Expressions
Marc Moreno Maza
2004-12-02