Java Reference
In-Depth Information
Instead, the behavior of the visitor is considered in terms of its common
treatment of an AST's nodes.
For example, consider the while and if statements. While the structure
of those statements di
ers greatly, each involves a predicate whose type
should be Boolean ( true or false ).
ff
A TypeChecking phase that checks predicates for proper type should
treat the predicates of the while and if statements similarly. To achieve
this e
ect, we arrange for the if and while nodes to inherit from a
common class that will be handled by the TypeCheckingvisitor.
The if and while node types implement the NeedsBooleanPredicate
interface at Markers 38 and 39 in Figure 7.24.
The TypeChecking visitor intercepts such nodes with the
ff
visit
method
shown at Marker 41 .
The interfaces (or abstract classes) associatedwith a givennode type allowclear
and proper treatment of that node type for each visitor that must perform
some action for such a node. Moreover, the visitor code itself becomes self
documenting, in the sense that the
methods capture the intent and scope
of the visitor based on the abstract classes and the properties they represent.
An implementation of the reflective visitor pattern, and the application of that
pattern to some examples, can be found in the Crafting a Compiler Supplement .
visit
Summary
Syntax-directed translation can accomplish translation directly via semantic
actions that execute in concert with top-down or bottom-up parses. More
commonly, an AST is constructed as a by-product of the parse; the AST serves
as a record of the parse as well as a repository for information created and
consumed by a compiler's phases. The design of an AST is routinely revised
to simplify or facilitate compilation.
 
Search WWH ::




Custom Search