Java Reference
In-Depth Information
that builds the AST forbids many ill-formed trees. Thus we do not need a
visit
method for IntLiteral because an assignment like 1=a is syntactically illegal.
If there is any doubt about whether an illegal construct might appear as an
L-value, a
method for the corresponding AST node can be included in
LHSSemanticsVisitor that warns that the construct may not be the target of an
assignment.
visit
/
Visitor code for Marker
59
on page 325
/
procedure
( Identifier
id
)
id
.
type
←
errorType
id
.
attributeRe f
←
visit
null
attributeRe f
←
currentSymbolTable
.
retrieveSymbol(
id
.
name
)
if
attributeRe f
=
67
null
then call
error
(
id
.
name
,
”
has not been declared
”)
else
if
is
D
ata
O
bject
(
attributeRe f
)
68
then
id
.
attributeRe f
←
attributeRe f
id
.
type
←
id
.
attributeRe f
.
variableType
else
call
error
(
id
.
name
,
”
does not name a data ob ject
”)
end
method for Identiferuses the symbol table to discover the “mean-
ing” of the identifier. It begins by setting the
type
field to
errorType
and the
attributesRe f
field to null. These are default values, in case the Identiferis incor-
rectly defined or used. A call to
The
visit
at Marker
67
then checks that
the named identifer has been properly declared. It will return
null
as a result
if the identifier is not found. Because the identifier is being used in a context
where it must name a data object (which has a value), a call to
retreive
S
ymbol
bject
(Marker
68
) verifies that the identifier meets this requirement. (For example,
an identifier declared as the name of a type would pass the first check but fail
the second one.) Presuming the identifier does indeed name a data object, the
type and attributes information of that data object are saved in the AST node.
If either test fails, then the default
errorType
is retained.
The visitor for Identifier in LHSSemanticVisitor (Marker
64
)firstusesa
SemanticVisitor instance to run the
is
D
ata
O
visit
method defined in this section. In
addition, it uses a method named
to make sure that id names a
data object that can be the target of assignment. For example,
is
A
ssignable
will
return false if id is a constant. It also includes a check for id.attributeRef
being
null
because id is not defined or does not name a data object and will
return false in that case, too.
is
A
ssignable