Java Reference
In-Depth Information
LabeledStmt
stmtLabel
stmt
Stmt
Figure 9.10: Abstract Syntax Tree for a Labeled Statement
procedure
visit
( LabeledStmt
ls
)
newNode
←
new
LabelList(
ls
.
stmtLabel
,get
K
ind
(
ls
.
stmt
)
,
ls
.
stmt
)
newList
← cons
(
newNode
,get
L
abel
L
ist
())
call
set
L
abel
L
ist
(
newList
)
call
visit
C
hildren
(
ls
)
call
set
L
abel
L
ist
(
tail
(
get
L
abel
L
ist
()))
end
Figure 9.11: Semantic Analysis for a Labeled Statement
If the statement is labeled,
stmtLabel
contains the label in string form. If the
statement is unlabeled,
stmtLabel
is null. LabeledStmt also contains a field
stmt
which is the AST node representing the labeled statement. Unlabeled
statements need not have LabeledStmt as a parent, particularly in contexts
where a label is disallowed.
In Java, C, and C
++
(and most other programming languages), labels are
placed in a di
erent name space than other identifiers. This means that an
identifier used as a label may also be used for other purposes (a variable name,
a type name, a method name, and so on) without confusion. This is because
labels are used in very limited contexts (in continues, breaks, and perhaps
gotos). Labels cannot be assigned to variables, returned by functions, read
from files, etc.
We will maintain a list of labels that are currently visible to the AST
being analyzed. The function
ff
get
L
abel
L
ist
will return the current label list
(possibly null). The procedure
will set the current label list via its
parameter. The label list is set to null when we begin the analysis of a method,
constructor body, or a static initializer.
Each LabelList node contains three fields. The first is
label
(a string that
contains the name of the label). Next is
kind
(one of
iterative
,
switch
,or
other
that indicates the kind of statement that is labeled). The last is
AST
(a link to
set
L
abel
L
ist