Java Reference
In-Depth Information
2.6 Abstract Syntax Trees
The scanner and parser together accomplish the syntax analysis phase of a
compiler. They ensure that the compiler's input conforms to a language's
token and CFG specifications. While the process of compilation begins with
scanning and parsing, following are some aspects of compilation that can be
di
cult or even impossible to perform during syntax analysis:
Most programming language specifications include prose that describes
aspects of the language that cannot be specified in a CFG. For example,
strongly typed languages insist that symbols be used in ways consistent
with their type declaration. For languages that allow new types to be
declared, a CFG cannot presuppose the names of such types nor the
manner in which they should properly be used. Even if the set of types
is fixed by a language, enforcing proper usage usually requires some
context sensitivity that is clearly not available in a CFG.
Some languages use the same syntax to describe phrases whose meaning
cannot be made clear in a CFG. For example, the phrase x.y.z in Java
could mean a package x,aclassy, and a static field z. That same phrase
could also mean a local variable x,afieldy, and another field z.In
fact, many other meanings are possible: Java provides (6 pages of) rules
to determine which of the possible interpretations holds for a given
phrase, given the packages and classes that are present during a given
compilation.
Most languages allow operators to be overloaded to mean more than
one actual operation. For example, the + operator might mean numerical
addition or the appending of strings. Some languages allow the meaning
of an operator to be defined in the program itself.
In all of the above cases, a programming language's CFG alone provides
insu
cient information to understand the full meaning of a program.
For relatively simple languages, syntax-directed translation can perform
almost all aspects of program translation during syntax analysis. Com-
pilers written in that fashion are arguably more e
cient than compilers
that perform a separate pass over a program for each phase. However,
from a software engineering perspective, the separation of activities and
concerns into phases (such as syntax analysis, semantic analysis, opti-
mization, and code generation)makes the resulting compilermuch easier
to write and maintain.
In response to the above concerns, we might consider using the parse tree
as the structure that survives syntax analysis and is used for the remaining
phases. However, as Figure 2.4 shows, such trees can be rather large and
unnecessarily detailed, even for very simple grammars and inputs.
 
 
Search WWH ::




Custom Search