Java Reference
In-Depth Information
Program
floatdcl
b
intdcl
a
assign
integer
assign
float
print
b
plus
float
id
a
inum
id
b
5
int2float
float
fnum
3.2
id
a
Figure 2.13: AST after semantic analysis.
For constants and symbol references, the visitor methods simply set the
supplied node's type based on the node's contents.
For nodes that compute values, such as plus and minus, the appropriate
type is computed by calling the utility methods in Figure 2.12. If both
types are integer, the resulting computation is integer;otherwise,the
resulting type is float.
For an assignment operation, the visitor makes certain that the value
computed by the second child is of the same type as the assigned identi-
fier (the first child).
The C
method, shown in Figure 2.12, is responsible for reconciling
the type of a pair of AST nodes using the following steps:
onsistent
1. The G
function determines the least general (i.e., simplest) type
that encompasses its supplied pair of types. For ac, if either type is float,
then float is the appropriate type; otherwise, integer will do.
eneralize
2. The C
procedure checks whether conversion is necessary, possi-
ble, or impossible. An important consequence occurs at Marker 13 in
Figure 2.12.
onvert
If conversion is attempted from integer to float, then the
 
Search WWH ::




Custom Search