Java Reference
In-Depth Information
ArrayReferencing
StructReferencing
type
type
fieldName
arrayName
objectName
indexExpr
Identifier
Identifier
Identifier
Expr
type
type
type
Subtree
attributeRef
attributeRef
attributeRef
name (a)
name (a)
name (b)
Figure 8.36: Abstract Syntax Trees for Array and Struct References
More generally, they reference subtrees that correspond to a data object, with
an Identifiernode a simple, but common, case.
The
method for anArrayReferencingAST node is found in Figure 8.37.
The method begins with a call to
visit
. As noted above,
arrayName
can reference an arbitrarily complex subtree, just as the
indexExpr
subtree can
be any expression. For our type checking purposes, we are only interested in
the
type
field in the root node of each of these subtrees, which is set by the
visits initiated by
visit
C
hildren
.
The test at Marker
73
checks whether the
arrayName
subtree does des-
ignate an array. The check at Marker
74
checks the type of the
indexExpr
subtree. This check is designed for languages that require arrays to be indexed
by integers (most do). A more complex check must be added if the language
being implemented allows enumerated types as array indices.
The LHSSemanticsVisitor
visit
C
hildren
method for arrays is shown at Marker
65
.
It visits its
arrayName
subtree to verify that the array is assignable. (Many
languages allow arrays to be declared const or final.) It then calls the cor-
responding
visit
visit
method in SemanticsVisitor to do the rest of the semantic
analysis.
The pseudocode for the
method for a StructReferencingAST node
is found in Figure 8.38. The method does not begin with the usual call to
visit
visit
. Rather, we only traverse the AST referenced by the
objectName
field. The reason for this departure fromthe usual practice is seenatMarker
79
,
where the
fieldName
is interpreted in the symbol table context provided by
the
objectName
subtree. Prior to doing so, the method checks whether this
subtree evaluation has resulted in an error (Marker
77
) and then ensures that
it does indeed name a structure (Marker
78
). Following our convention for
error reporting, no error message is necessary in the first instance, while failure
of the local check at Marker
78
does produce a message appropriate for the
C
hildren