Java Reference
In-Depth Information
WhileLooping
loopBody
condition
. . .
StmtList
Expr
Figure 9.5: Abstract Syntax Tree for a While Statement
procedure
visit
(WhileLooping
wn
)
wn
.
terminatesNormally
←
true
wn
.
loopBody
.
isReachable
←
true
constExprVisitor
←
21
new
ConstExprVisitor()
call
wn
.
condition
.
accept(
constExprVisitor
)
conditionValue
←
wn
.
condition
.
exprValue
if
conditionValue
=
true
then
wn
.
terminatesNormally
←
false
else
if
conditionValue
=
false
then
22
wn
.
loopBody
.
isReachable
←
false
23
call
wn
.
loopBody
.
accept(
this
)
end
24
Figure 9.6: Reachability Analysis for a While Statement
semantic processing of the breakwill reset the loop's
terminatesNormally
field to
true (Marker
24
). If the control expression is non-constant, the loop is marked
as terminating normally (Marker
21
). We will assume that we have available
a visitor class ConstExprVisitorwhose methods traverse an expression AST to
determine whether it represents a constant-valued expression. If the AST is
recognized as a constant expression, then the visitors set the field
exprValue
to
the expression value; otherwise
exprValue
is set to null. ConstExprVisitor can
be very simple, perhaps only evaluating expressions whose operands are AST
nodes for literals. With a bit more e
ort, the symbol table entry for identifiers
can be checked, looking for declared constants. As discussed in Section 14.6 on
page 623, constant propagation can recognize variables that, because of flow
of control, must contain a known constant value.
The exceptions potentially thrown in a while loop are those generated
ff