Java Reference
In-Depth Information
8.1.1 Static Scoping
Modern programming languages o
er scopes to confine the activity of a name
to a prescribed region of a program. A name may be declared no more than
once in any given scope. For statically scoped, block-structured languages,
references are typically resolved to the declaration in their closest containing
scope. Additionally, most languages contain directives to promote a given
declaration or reference to the program's global scope —a name space shared
by all compilation units. Section 8.4.4 discusses these issues in greater detail.
Proper use of scopes results in programs whose behavior is easier to un-
derstand. Figure 8.1(a) shows a program with two nested scopes. Declared
in the program's outer scope, w is available in both scopes. The activity of z
is confined to the inner scope. The name x is available in both scopes, but
the meaning of x changes when the inner scope redeclares x. In general, the
static scope of an identifier includes its defining block as well as any contained
blocks that do not themselves contain a declaration for the identifier.
Languages are typicallydesigned to use punctuation or keywords todefine
static scopes. For example, in C and Java TM , scopes are opened and closed by
the appropriate braces, as shown in Figure 8.1(a). In these languages, a scope
can declare types and variables. However, methods (function definitions)
cannot appear in inner scopes. In, Ada and other languages with Algol-like
syntax, the reserved keywords begin and end open and close scopes, respec-
tively. Within each scope, types, variables, and procedures can be declared.
In some languages, references to namesinouterscopescanincurover-
head at runtime. As discussed in Chapter 11, an important consideration is
whether a language allows the nested declaration of methods. C and Java
prohibit this, while ML allows functions to be defined in any scope. For C and
Java, a method's local variables can be flattened by renaming nested symbols
and moving their declaration to the method's outermost scope. Exercise 14
considers this in greater detail.
ff
8.1.2 A Symbol Table Interface
A symbol table is responsible for tracking which declaration is in e
ect when
a reference to the symbol is encountered. In this section, we define a symbol
table interface for processing symbols in a block-structured, statically scoped
language. The methods in our interface are as follows:
ff
open
S
() opens a new scope in the symbol table. New symbols are en-
tered in the resulting scope.
cope
close
() closes the most recently opened scope in the symbol table.
Symbol references subsequently revert to outer scopes.
S
cope
 
 
 
Search WWH ::




Custom Search