Java Reference
In-Depth Information
/
Visitor code for Marker
12
on page 302
/
procedure
visit
(VariableListDeclaring
vld
)
typeVisitor
←
new
TypeVisitor()
20
call
vld
.
typeName
.
accept(
typeVisitor
)
21
foreach
id
∈
vld
.
idList
do
22
if
currentSymbolTable
.
declaredLocally(
id
.
name
)
then
call
23
error
(”
This variable is already declared
:”
,
id
.
name
)
id
.
type
←
errorType
id
.
attributesRe f
←
null
else
id
.
type
←
vld
.
typeName
.
type
attr
.
kind
←
variableAttributes
attr
.
variableType
←
id
.
type
id
.
attributesRe f
←
attr
call
currentSymbolTable
.
enterSymbol(
id
.
name
,
attr
)
24
end
Figure 8.13:
visit
method in TopDeclVisitor for VariableListDeclaring.
The
method inFigure 8.14 defines the actions performedby the TypeVisitor
when it visits an Identifier. It begins by looking for the identifier in the symbol
table using
visit
at Marker
25
. If the attributes returned indicate
that
id
does name a type, then a reference to the type descriptor for that type
is assigned at Marker
26
to
type
, which stores the result of this retrieval pro-
cess. If the Identifier does not name a type, an error message is produced at
Marker
27
.
type
is set to refer to a special type descriptor,
errorType
, indicating
the error. Use of
errorType
to streamline error reporting will be discussed in
retrieve
S
ymbol
/
Visitor code for Marker
16
on page 302
/
procedure
visit
( Identifier
id
)
attr
←
currentSymbolTable
.
retrieveSymbol(
id
.
name
)
if
attr
null and
attr
.
kind
=
typeAttributes
then
25
id
.
type
←
attr
.
thisType
id
.
attributesRe f
←
attr
26
else
call
error
(”
This identi f ier is not a type name
:”
,
id
.
name
)
27
id
.
type
←
errorType
id
.
attributesRe f
←
null
end
Figure 8.14:
visit
method in TypeVisitor for Identifier.