Java Reference
In-Depth Information
Prog
Dcls
Stmts
Stmts
Stmt
Stmts
Stmt
Expr
Dcls
Expr
Dcl
Dcl Dcls
Val
Val
Val
Stmt Stmts
Expr
floatdcl
id
intdcl
id
id assign
inum
id
assign
id
plus
fnum
print
id
$
fb
i
a
a
=
5
b
=
a
+
3.2
p
b
Figure 2.4: An ac program and its parse tree.
We next consider the phases involved in compiling the ac program shown
in Figure 2.4. The derivation shown textually in Figure 2.2 can be represented
as a derivation (or parse) tree, also shown in Figure 2.4. An input stream
can be automatically transformed into a stream of tokens using the techniques
presented in Chapter 3.
In the following sections we examine each step of the compilation process
for the ac language, assuming an input that would produce the derivation
shown in Figure 2.2. While the treatment is somewhat simplified, the goal is
to show the purpose and data structures of each phase.
2.3 Phases of a Simple Compiler
The rest of this chapter presents a simple compiler for ac, structured according
to the illustration in Figure 1.4 on page 15. The phases in the translation process
are as follows:
1. The scanner reads a source ac programas a text file and produces a stream
of tokens. For example, strings such as 5 and 3.2 are recognized as inum
and fnum tokens. Reserved keywords such as f and p are distinguished
 
Search WWH ::




Custom Search