Java Reference
In-Depth Information
Markers 6 and 7 capture the values synthesized from adjacent Value
subtrees to form their sum. The result is synthesized by the return at
Marker 8 .
The parameter thus f ar of V
represents the product of the factors
parsed so far. Marker 9 causes Values to inherit the value of an empty
partial product (i.e., 1).
The product continues at Marker 11 by incorporating the next factor,
which is synthesized at Marker 10 .
The product finishes at Marker 12 , when the production Values
alues
→λ
is applied. The partial product passed in as thus f ar is the complete
product, and the return at Marker 12 initiates synthesis of that value up
the V
alues
call-chain.
7.4 Abstract Syntax Trees
While many of a compiler's tasks could be performed in a single phase via
syntax-directed translation, modern software practices discourage implement-
ing so much functionality to a single component such as the parser. Tasks
such as semantic analysis, symbol table construction, program optimization,
and code generation are each deserving of separate treatment in a compiler.
Squeezing all of those tasks into a single compiler phase is an admirable feat
of engineering, but the resulting compiler is di
cult to understand, extend,
and maintain.
We therefore consider the design and implementation of a data structure,
known as the AST, that will serve as the central data structure for all post-
parsing activities. The goal of syntax-directed translation is then simplified to
the construction of an AST. The AST must be concise, but it must also be su
-
ciently flexible to accommodate the diversity of post-parsing phases. It is not
uncommon to revisit the design of the AST during the life cycle of a compiler's
development. As discussed in Section 7.7, object-oriented techniques such as
the visitor pattern facilitate robust compiler design, separation of concerns, and
phase interoperability.
7.4.1 Concrete and Abstract Trees
We begin by distinguishing concrete from abstract syntax trees, as first discussed
in Section 2.6 on page 45. Parse trees such those shown in Figures 7.3 and 7.4
are concrete , in the sense that they show every detail of the parse. Consider
the nonterminal Digs and the parse tree shown in Figure 7.3. The tree leans to
the left because the rules for Digs are left recursive. If these rules were right
recursive, then the parse subtree for Digs would change accordingly.
 
 
 
Search WWH ::




Custom Search