Java Reference
In-Depth Information
The second half of the chapter examines techniques for processing decla-
rations and then goes on to show how the information derived from declara-
tions is used to do type checking on assignments and expressions. Section 8.5
describes the representations used for the attributes that are associated with
names by the symbol table. Section 8.6 details howAST structures representing
simple declarations are processed to build a representation of the declarations
in the symbol table. Techniques necessary for a handling class and method
declarations are presented in Section 8.7. Finally, Section 8.8 illustrates how
type checking is done, using the information stored in the symbol table as
declarations are processed.
8.1 Constructing a Symbol Table
In this section, we consider how to construct a symbol table for a simple, block-
structured language. Assuming an AST has been constructed as described in
Chapter 7, we walk (make a pass over) the AST for two purposes:
to process symbol declarations and
to connect each symbol reference with its declaration.
Symbol references are connected with declarations through the symbol table.
An AST node that mentions a symbol by name is enriched with a reference to
the name's entry in the symbol table. If such a connection cannot be made,
then the o
ending reference is improperly declared and an error message is
issued. Otherwise, subsequent passes can use the symbol table reference to
obtain information about the symbol, such as its type, storage requirements,
and accessibility.
The block-structured program shown in Figure 8.1(a) contains two nested
scopes. Although the program uses keywords such as float and int,no
symbol table action is required for these symbols if they are recognized by
the scanner as terminal symbols. Most programming language grammars
demand such precision of the scanner to avoid ambiguity.
The program in Figure 8.1(a) begins by importing function definitions
for f and g. The compiler finds these, determines that their return types
are void, and then records the functions in the symbol table as its first two
entries. The declarations for w and x in the outer scope are entered as symbols
3 and 4, respectively, in the symbol table illustrated in Figure 8.1(c). The inner
scope's redeclaration of x and its declaration of z are entered as symbols 5
and 6, respectively. The AST in Figure 8.1(b) refers to symbols by name. In
Figure 8.1(d), the names are replaced by symbol table references. In particular,
the references to x shown only by name in Figure 8.1(b) are declaration-specific
in Figure 8.1(d). In the symbol table, the references contain the original name
as well as type information processed from the symbol declarations.
ff
 
 
Search WWH ::




Custom Search