Java Reference
In-Depth Information
Tokens
AST
Source
Program
Scanner
Parser
Type Checker
Decorated
AST
Translator
Interemediate
Representation
Symbol Tables
Optimizer
Interemediate
Representation
Code Generator
Target Code
Figure 1.4: A syntax-directed compiler. AST denotes the Abstract
Syntax Tree.
•
Analysis
of the source program being compiled
•
Synthesis
of a target program that, when executed, will correctly perform
the computations described by the source program
Almost all modern compilers are
syntax-directed
. That is, the compilation
process is driven by the syntactic structure of the source program, as recog-
nized by the parser. Most compilers distill the source program's structure into
an
abstract syntax tree
(AST) that omits unnecessary syntactic detail. The
parser builds the AST out of
tokens
, the elementary symbols used to define a
programming language syntax. Recognition of syntactic structure is a major
part of the
syntax analysis
task.
Semantic analysis
examines the meaning (semantics) of the program on
the basis of its syntactic structure. It plays a dual role. It finishes the analysis
task by performing a variety of correctness checks (for example, enforcing type
and scope rules). It also begins the
synthesis phase
.
In the synthesis phase, source language constructs are translated into an
intermediate representation
(IR) of the program. Some compilers generate
target code directly. If an IR is generated, it then serves as input to a
code genera-
tor
component that actually produces the desired machine-language program.
The IR may optionally be transformed by an
optimizer
so that a more e
cient
program may be generated. A common organization of all of these compiler
components is depicted schematically in Figure 1.4. Each of these components