Java Reference
In-Depth Information
assign
assign
assign
id
deref
deref
id
deref
deref
x
y
id
id
x
id
x
id
y
y
(a)
(b)
(c)
Figure 7.22: ASTs illustrating left and right values for the assignments:
(a) x= y
(b) x=& y
(c)
x =y
The grammar in Figure 7.20 models the syntax used in C for left and right
values:
Rules 3 and 4 define the syntax for L-values. Rule 3 allows an identi-
fier to appear, in which case its interpretation is its location, as stated
above. Rule 4 allows an R-value to appear left of the assignment opera-
tor, provided it is preceded by a
is purely
syntactic, as no dereferencing takes place on its account. For example,
the statement
symbol. The presence of
431
=
0 sets the value at location 431 to 0.
Rules 5, 6, and 7 define the syntax for R-values. Rule 6 allows numeric
constants, whose interpretation is simply their value. Rule 7 allows the
location of an L-value to be an R-value. The phrase x +
1isonegreater
than the value of x ;thephrase& x +
1 is the location just past where x is
stored. If the language did not o
er the & operator, there would be no
syntax for expressing arithmetic on addresses (as is the case in Java).
Although it seems simple, the rule deserving the most attention is Rule 5.
Any references of an id right of the assignment operator must first be
derived from L which can then be reduced to R by the production R
ff
L.
An L-value is transformed into an R-value by an actual dereference—
by obtaining the value located at the L-value. The
and & operators
requirenoworktoimplement.Theyserve to provide syntax that alters
the usualmeaning of identifiers on either side of the assignment operator.
The above observations provide a basis for synthesizing an AST for the gram-
mar in Figure 7.20. Semantic actions are incorporated into the grammar as
shown in Figure 7.21. Note that Markers 25 and 27 simply return the
AST structure created below, with no extra operations inserted. However,
 
Search WWH ::




Custom Search