Java Reference
In-Depth Information
/
Visitor methods
/
procedure
(SymDeclaring
n
)
if
n
.
getType()= floatdcl
then call
E
visit
(
n
.
getId(),float )
nter
S
ymbol
(
n
.
getId(),integer )
else call
E
nter
S
ymbol
end
/
Symbol table management
/
procedure
E
nter
S
ymbol
(
name
,
type
)
if
SymbolTable
[
name
]
null
then
SymbolTable
[
name
]
=
←
type
else call
error
( "duplicate declaration")
end
function
L
(
name
)
returns
type
return
SymbolTable
[
name
]
end
ookup
S
ymbol
Figure 2.10: Symbol table construction for ac.
•
Declarations and name scopes are processed to construct a
symbol table
,
so that declarations and uses of identifiers can be properly coordinated.
•
Language- and user-defined types are examined for consistency.
•
Operations and storage references are processed so that type-dependent
behavior can become explicit in the program representation.
For the ac language, we focus on two aspects of semantic analysis: symbol-
table construction and type checking.
In ac, identifiersmust be declaredprior to use, but this requirement is not easily
enforced during syntax analysis. Symbol-table construction is a semantic-
processing activity that traverses the AST to record all identifiers and their
types in a
symbol table
. In most languages the set of potential identifiers is
essentially infinite. In ac a program canmention at most 23 distinct identifiers.
As a result, an ac symbol table has 23 entries indicating each identifier's type:
integer, float,orunused(
null
). In most programming languages the type
information associated with a symbol includes other attributes, such as the
identifier's scope of visibility, storage class, and protection properties.
To c rea t e an ac symbol table, we traverse the AST, counting on the pres-
ence of a
symbol-declaring node
to trigger appropriate e
ff
ects on the symbol