Java Reference
In-Depth Information
procedure
build
S
ymbol
T
able
()
call
process
N
ode
(
ASTroot
)
end
procedure
process
N
ode
(
node
)
switch
(
(
node
)
)
case
Block
kind
call
symtab
.
openScope()
case
Dcl
call
symtab
.
enterSymbol(
node
.
name
,
node
.
type
)
case
Re f
sym
←
symtab
.
retrieveSymbol(
node
.
name
)
if
sym
=
1
null
then call
,
sym
)
foreach
c
∈
node
.
getChildren()
do call
processNode(
c
)
if
error
(”
Undeclared symbol
:”
kind
(
node
)
=
Block
then
call
symtab
.
closeScope()
2
end
Figure 8.2: Building the symbol table
enter
(
name
,
type
)
enters
name
in the symbol table's current scope.
The parameter
type
conveys the data type and access attributes of
name
's
declaration.
S
ymbol
retrieve
S
ymbol
(
name
)
returns the symbol table's currently validdeclaration
for
name
.
If no declaration for name is currently in e
ff
ect, then a null
pointer is returned.
declared
(
name
)
tests whether
name
is present in the symbol table's
current (innermost) scope. If it is, true is returned. If
name
is in an outer
scope, or is not in the symbol table at all, false is returned.
L
ocally
To illustrate the use of this interface, Figure 8.2 contains code to build the
symbol table for the AST shown in Figure 8.1. The code is specialized to the
type of the AST node. Actions may be performed both before and after a
given node's children are visited. Prior to visiting the children of a
Block
node,
code at Marker
1
increases opens a new scope. After the subtree of the
Block
is processed, Marker
2
abandons the scope. The code for
Re f
retrieves the
symbol's current definition in the symbol table. If none exists, then an error
message is issued.