Java Reference
In-Depth Information
Marker 26 inserts a node indicating an actual dereference (fetch through a
pointer). Figure 7.22 shows the ASTs for various uses of the operators
and & .
7.7 Design Patterns for ASTs
With the AST established as the primary post-parsing artifact of compilation,
we now consider the AST's role in the remaining phases. Chapter 2 presented
an overview of compilation, demonstrating the use of an AST for semantic
analysis and code generation in Sections 2.7 and 2.8. While the infrastructure
for creating andmanaging ASTs could be based on the coverage in Sections 7.4
and 7.5 alone, it is worthwhile to consider how those tasks could be simplified
through application of modern, object-oriented principles and techniques.
Design patterns [GHJV95] have emerged as a mechanism for capturing
idiomatic gestures in software and reasoning about e
ective solutions to com-
mondesignproblems. While a complete treatment of designpatterns is beyond
this text, we discuss one design pattern that can be applied with great success
in crafting a compiler. A guiding principle that greatly facilitates understand-
ing when to apply a design pattern is that patterns are generally meant to save
time and e ff ort in developing and maintaining software. Patterns can make
software easier to understand, thereby facilitating maintenance and modifica-
tion. Patterns can also simplify software construction by helping developers
recognize problems that already have reasonable solutions.
ff
7.7.1 Node Class Hierarchy
Chapter 2 presented a small language and considered construction of its AST
in Section 2.6 on page 45. Figure 2.9 on page 44 shows an example of such
an AST, which includes nodes that declare variables, represent computations,
and call for the printing of a variable's value.
The node-management issues presented in Section 7.4 are common to all
nodes, so it is reasonable to have every node of Figure 2.9 extend a base class
that can connect siblings, adopt children, and facilitate traversal of an AST.
Beyond those basic functions, what class hierarchy should we impose on the
node types of an AST? To answer that question, we must look at how the
various compiler phases treat the AST nodes.
Consider the phases described in Sections 2.7 and 2.8. There is a close
physical and logical resemblance between the plus and assign nodes: both
have two children and both are involved in computation. However, their
treatment varies substantially by phase:
 
 
 
Search WWH ::




Custom Search