Java Reference
In-Depth Information
Assigning
type
targetName
valueExpr
Name
Expr
Subtree
Subtree
Figure 8.32: Abstract Syntax Tree for an Assignment
simple identifiers and literals. The method for assignment statements is then
detailed in Section 8.8.2. Section 8.8.3 deals with expressions involving unary
and binary operators and thereafter in Section 8.8.4 the techniques needed to
compile simple record and array references are presented.
The AST form of an assignment statement is shown in Figure 8.32. A
targetName subtree may be simply an identifier or something more complex,
such as an indexed array or a field in a struct or class. Similarly, a valueExpr
subtree may be a simple identifier or literal, or a complex computation in-
volving operator evaluation and method calls. No matter how complex the
subtrees are, our approach to semantic analysis will be uniform; visitors will
traverse both subtrees checking for semantic errors and determining types.
AsimplenamecanobviouslyberepresentedbythesameIdentifier node
that we used to represent type names and in many other contexts as we
processed declarations. The simplest form of an expression is also just a
name (as seen in the assignment statement a=b) or a literal constant (as in
a=5).
However, semantic analysis of an assignment statement is not as simple
as it first seems. Look at the trivial assignment a=b. In this assignment the
two identifiers have rather di
erent meanings. a stands for the address of a,
the location that will receive the assigned value. b stands for the value at the
address associated with b. We say that the a is providing an left value (L-
value) because this is the meaning of an identifier on the left-hand side of
an assignment statement (i.e., an address). Similarly, we say that b denotes
a right value (R-value) because this is how we interpret an identifier on the
right-hand side of an assignment (i.e., a value or the contents of an address).
This di
ff
erentiation between L-values and R-values explains why a named
constant may be the source value of an assignment but not its target.
In terms of our semantic analysis, we shall use SemanticsVisitor to ana-
ff
 
 
Search WWH ::




Custom Search