Java Reference
In-Depth Information
8.2 Block-Structured Languages and Scopes
Most programming languages allow scopes to be nested statically, based on
concepts introduced by Algol 60. Languages that allow nested name scopes
are known as block-structured languages . While the mechanisms that open
and close scopes can vary by language, we assume that the
open
S
cope
and
close
methods are the uniform mechanism for opening and closing
scopes in the symbol table. In this section, we consider various language
constructs that call for the opening and closing of scopes. We also consider
the issue of allocating a symbol table for each scope as compared with using a
single, global symbol table.
S
cope
8.2.1 Handling Scopes
Every symbol reference in an AST occurs in the context of defined scopes. The
scope defined by the innermost such context is known as the current scope .
The scopes defined by the current scope and its surrounding scopes are known
as the open scopes or currently active scopes . All other scopes are said to be
closed . Based on these definitions, current, open, and closed scopes are not
fixed attributes; instead, they are defined relative to a particular point in the
program. The following are some common visibility rules that define the
interpretation of a name in the presence of multiple scopes:
At any point in the text of a program, the accessible names are those that
are declared in the current scope and in all other open scopes.
If a name is declared in more than one open scope, then a reference to the
name is resolved to the innermost declaration—the one that most closely
surrounds the reference.
New declarations can be made only in the current scope.
Most languages o
er mechanisms to install or resolve symbol names in the
outermost, program-global scope. In C, names bearing the extern attribute
are resolved globally. In Java, a class can reference any class's public static
fields, but these fields do not populate a single, flat name space. Instead, each
such field must be fully qualified by its containing class.
Programming languages have evolved to allow various useful levels of
scoping. C and C
ff
er a compilation-unit scope where names declared out-
side of all methods are available within the compilation unit's methods. Java
o
++
o
ff
ers a package-level scope in which classes can be organized into packages
that can access all package-scoped methods and fields. In C, every function
definition is available in the global scope, unless the definition has the static
attribute. In C
ff
++
and Java, names declared within a class are available to all
 
 
 
Search WWH ::




Custom Search