Java Reference
In-Depth Information
methods in the class. In Java and C
, a class's fields and methods bearing
the protected attribute are available to the class's subclasses. The parameters
and local variables of a method are available within the given method. Finally,
names declared within a statement-block are available in all contained blocks,
unless the name is redeclared in an inner scope.
++
8.2.2 One Symbol Table or Many?
As noted above, there are two common approaches to implementing block-
structured symbol tables. A symbol table may be associated with each scope
or all symbols may be entered in a single, global table. A single symbol table
must accommodate multiple, active declarations of the same symbol. Despite
this complication, searching for a symbol can be faster in a single symbol table.
We next consider this issue in greater detail.
An Individual Table for Each Scope
If an individual table is created for each scope, some mechanism must be in
place to ensure that a search produces the name defined by the nested-scope
rules. Because name scopes are opened and closed in a last-in, first-out ( LIFO )
manner, a stack is an appropriate data structure for organizing such a search.
Thus, a scope stack of symbol tables can be maintained, with one entry in the
stack for each open scope. The innermost scope appears at the top of the stack.
The next containing scope is second from the top, and so forth. When a new
scope is opened,
creates and pushes a new symbol table on the
stack. When a scope is closed,
open
S
cope
, the top symbol table is popped.
A disadvantage of this approach is that we may need to search for a name
in a number of symbol tables before the symbol is found. The cost of this
stack search varies from program to program, depending on the number of
nonlocal name references and the depth of nesting of open scopes. In fact, it
is widely known that most lookups of symbols in block-structured languages
return symbols in the inner- or outer-most scopes. With a table per scope,
intermediate scopes must be checked before an outermost declaration can be
returned.
An example of this symbol table organization is shown in Figure 8.3.
close
S
cope
One Symbol Table
In this organization, all names in a compilation unit's scopes are entered into
the same table. If a name is declared in di
erent scopes, then the scope name
or depth helps identify the name uniquely in the table. With a single symbol
table,
ff
retrieve
S
ymbol
need not chain through scope tables to locate a name.
 
 
Search WWH ::




Custom Search