Java Reference
In-Depth Information
make
N
(
t
)
is a
factory
method that creates a node whose contents and
accessor methods depend on the type of
t
.
For example,
ode
( int
n
) instantiates a node that represents the
constant integer
n
and that o
make
N
ode
ff
ers an accessor method that returns
n
.
make
(Symbol
s
) instantiates a node for a symbol
s
.Methodsmust
be included to set and get the symbol table entry for
s
,fromwhichits
type, protection, and scope information can be retrieved.
make
N
ode
(Operator
o
) instantiates a node for an operation, such as
addition or subtraction. Details of the operation must be provided by
accessor methods.
make
N
ode
( ) instantiates a
null
node that explicitly represents the ab-
sence of structure. For consistency in processing an AST, it is better to
have a null node than to have gaps in the AST or null pointers.
x
.
make
S
iblings
(
y
)
causes
y
to become
x
's sibling, using the code shown in
Figure 7.13. In the case where
x
has no right sibling and
y
is its own
leftmost sibling,
y
becomes
x
's right sibling.
More generally,
x
is a (typically rightmost) node on a list of siblings
xsibs
;
y
is a node on a (typically singleton) list of siblings
ysibs
.Themethod
appends the lists
xsibs
and
ysibs
. All siblings point to
x
's parent as their
parent and to
x
's leftmost sibling as their leftmost sibling.
To facilitate chaining of nodes through recursive grammar rules, this
method returns a reference to the rightmost sibling resulting from the
invocation.
x
.
adopt
C
hildren
(
y
)
adopts
y
and all of its siblings under the parent
x
,using
the code shown in Figure 7.13.
N
ode
make
F
,...,
kid
n
)
is included for convenience. This method
generates a family with exactly
n
children under a parent node
op
.The
code for the most common case (
n
=
amily
(
op
,
kid
1
,
kid
2
2) is:
function
,
kid
2)
returns
Node
return
makeNode
(
op
)
.
adoptChildren(
kid
1
.
makeSiblings(
kid
2))
end
make
F
amily
(
op
,
kid
1
With our AST data structure and methods in place, we next consider issues
related to the design of a particular AST based on a given grammar and
language translation problem.
Because the AST is centrally involved in most of a compiler's activities, its
design can and should evolve with the functionality of the compiler. A prop-