Java Reference
In-Depth Information
12. After checking that the type of node 4 is an array and verifying that
the type of the index expression represented by node 5 is indeed integer
(Marker 75 in Figure 8.37), this visitor sets the type field of node 2 to the
elementType field of type of node 4. Control then returns to the visitor
for node 1.
13. The code at Marker 78 in Figure 8.38 checks that the type just set for
node 2 designates a structure. Presuming that it does, the identifier f
from the name field in node 3 is looked up in the symbol table associated
with the struct type of node 2. The type retrieved for f will be set as the
value of the type field of node 1 and control will then return to the visitor
that invoked the traversal on node 1.
While this may seem like a rather complex process, it is important to
understand that it enables the code for each
method to deal with local
information only. (Reaching down for field names is a special case, where
the fieldName subtree can only be an Identifier node.) Names composed in
arbitrarily complex ways can always be handled with the simple steps defined
for each kind of AST node. In particular, the type checking done within each
node never depends on the structure of the AST belowor surrounding the node
being processed, only on local information and the type fields of subtrees.
visit
8.9 Summary
Although the interface for a symbol table is quite simple, the details underlying
a symbol table's implementation play a significant role in the performance of
the symbol table. Most modern programming languages are statically scoped.
The symbol table organization presented in this chapter e
ciently represents
scope-relative symbols in a block-structured language. Each language places
its own requirements on how symbols can be declared and used. Most lan-
guages include rules for symbol promotion to a global scope. Issues such
as inheritance, overloading, and aggregate data types should be considered
when designing a symbol table. A symbol table is used to associate names
with a variety of information, generally referred to as atttributes; types are a
common, distinguished kind of attribute. Types and other attributes are rep-
resented by complex, customized data structures designed based on language
characteristics to store the appropriate descriptive information.
This chapter made extensive use of the visitor pattern first introduced in
Chapter 7 to process an abstract syntax tree.
methods associated with
nodes of the tree representing declarations build the symbol table and the
structures associated with names declared in the program being compiled.
This same mechanism also accomplishes type checking during declaration
visit
 
 
Search WWH ::




Custom Search