Java Reference
In-Depth Information
Start
431
E
431
T
400
E
31
T
T
31
8
num
plus
num
times
num
$
num
plus
num
times
num
$
31
+
8
*
50
$
31
+
8
*
50
$
Syntax
Semantic Values
(a)
(b)
Figure 7.1: (a) Parse tree for the displayed expression;
(b) Synthesized attributes transmit values up the parse
tree toward the root.
Formulating an appropriate set of semantic actions requires a firm under-
standing of how derivations are traced in a given parsing technique (bottom-
up or top-down). Writing clear and elegant semantic actions often requires
grammar restructuring to aid computation of semantic values at appropriate
places. After the initial step of obtaining a grammar suitable for top-down
or bottom-up parsing, it is not unusual to revise the grammar to facilitate
semantic actions during this phase of compiler construction.
In Section 7.2, we examine how to specify semantic actions for bottom-up
parsing, which essentially create parse trees in a
postorder
fashion. For syntax-
directed translation, this style nicely accommodates situations in which at-
tributes primarily flow from the leaves of a derivation tree toward its root.
If we imagine that each node of a parse tree can consume and produce in-
formation, then nodes consume information from their children and produce
information for their parent in a bottom-up parse. An example using such
synthesized attributes
flow is shown in Figure 7.1. The parse tree in Fig-