Java Reference
In-Depth Information
The actions to be performed bySemanticsVisitor and TopDeclVisitor for
each node type will be specified in the sections that follow in the form of a
visit
method which takes an instance of the node type as a parameter. The
exact same actions could be implemented by methods defined directly within
the classes defining the abstract syntax tree node types. The disadvantage of
this latter approach is that the code implementing the semantics pass would
be scattered across many class definitions. These same actions can also be
used even if the implementation language for a compiler does not support
objects. A recursive traversal equivalent to that implemented by the visitor
pattern can be performed by a single routine containing a large switch or case
statement with an alternative for each kind of node in an abstract syntax tree.
Once execution of the tree traversal defined by SemanticsVisitoris finished, the
analysis phase of the compiler is complete. Subsequent chapters will describe
additional phases of the compiler that ultimately synthesize the target code it
produces.
A reflective mechanism for achieving such double dispatch is presented
in Section 7.7.3 on page 268. A review of that material may be helpful before
proceeding with this chapter.
Figure 8.11 provides an outline of the declarations processing visitors that
will be described in Section 8.6, which covers variable and type declarations,
and Section 8.7, whichhandles classes andmethods. Agiven visitor is typically
tasked with performing a relatively narrow set of activities. For the purposes
of processing declarations, it is helpful to organize the visitors as follows:
SemanticsVisitor is the top-level visitor for processing declarations and do-
ing semantic checking on an AST's nodes. There must be a
method
defined by SemanticsVisitor or one of its specializations for every kind
of AST node. In addition to the declaration processing visitor to follow,
type checking visitors are discusses in Section 8.8 and Chapter 9.
visit
TopDeclVisitor is a specialized visitor invoked by SemanticsVisitor for pro-
cessing declarations. It is responsible for building the symbol table
structures corresponding to variable, type, class and method declara-
tions. In the case of method declarations, it also initiates processing of
each method's contents.
TypeVisitor is a specialized visitor used to handle an identifier that represents
a type or a syntactic form that defines a type (such as an array).
For each AST node of interest, we present algorithm-style code in the form
of a
method that illustrates a strategy for processing the construct in the
context of a declaration-handling pass over the AST .
visit
 
Search WWH ::




Custom Search