Java Reference
In-Depth Information
we are in (and details of its declaration) by using the reference that is non-null.
The details of semantic analysis for return statements appears in Fig-
ure 9.18. We assume
(). returnType gives us the type re-
turned by the method currently being translated (this type may be void). The
auxiliarymethod
get
C
urrent
M
ethod
( T 1, T 2) tests whether type T 2 is assignable to type
T 1 (using the assignability rules of the language being compiled).
C
assignable
have semantic rules very similar to those of Java. A value
may only be returned from a non-void function and the value returned must
be assignable to the function's return type. In C, a return without a value is
allowed in a non-void function (with undefined behavior).
and C
++
Goto Statements
Java contains no goto statement, but many other languages, including C, C
,
and C
, do. These languages, and almost all languages that allow gotos,
restrict them to be intraprocedural . That is, a label and all gotos that reference
it must be in the same procedure or function.
As noted above, identifiers used as labels are usually considered distinct
from identifiers used for other purposes. Thus, in C, C
++
,andC
++
,thestate-
ment:
a: a=a+1;
is legal. Labels may be kept in a separate symbol table, distinct from the main
symbol table used to store ordinary declarations.
Labels need not be defined before they are used; ”forward gotos” are
allowed. Semantic checking must guarantee that all labels used in gotos are in
fact defined somewhere in the current method.
Because of potential forward references, it is a good idea to check labels
and gotos in two steps. First, the AST that represents the entire body of a
method or subprogram is traversed, gathering all label declarations into a
declaredLabels table stored as part of the current subprogram's symbol table.
Duplicate labels are detected as declaredLabels is built.
During normal semantic processing of the body of a subprogram (af-
ter declaredLabels has been built), an AST for a goto can access declaredLabels
(through the current subprogram's symbol table). Checking for valid label
references (whether forward or not) is straightforward.
A few languages, such as Pascal, allow nonlocal gotos . A nonlocal goto
transfers control out of a subprogram (forcing a return) to a label in a scope that
contains the current procedure. Nonlocal gotos can be checked bymaintaining
a stack (or list) of declaredLabels tables, one for each nested procedure. A goto
is valid if its target appears in any of the declaredLabels tables.
 
Search WWH ::




Custom Search