Java Reference
In-Depth Information
We learn about compilers.
We learn about Java. j-- is a non-trivial subset of Java. The j-- compiler is written in
Java.
We work with a non-trivial object-oriented program.
1.4.1 j--Compiler Organization
Our compiler's structure is illustrated in Figure 1.8.
FIGURE 1.8 The j-- compiler.
The entry point to the j-- compiler is Main 7 . It reads in a sequence of arguments, and
then goes about creating a Scanner object, for scanning tokens, and a Parser object for
parsing the input source language program and constructing an abstract syntax tree (AST).
Each node in the abstract syntax tree is an object of a specific type, reflecting the under-
lying linguistic component or operation. For example, an object of type JCompilationUnit
sits at the root (the top) of the tree for representing the program being compiled. It has
sub-trees representing the package name, list of imported types, and list of type (that is,
class) declarations. An object of type JMultiplyOp in the AST, for example, represents a
multiplication operation. Its two sub-trees represent the two operands. At the leaves of the
tree, one nds JVariable objects and objects representing constant literals.
Each type of node in the AST defines three methods, each of which performs a specific
task on the node, and recursively on its sub-trees:
1. preAnalyze(Contextcontext) is defined only for the types of nodes that appear
near the top of the AST because j-- does not implement nested classes. Pre-analysis
deals with declaring imported types, defined class names, and class member head-
ers (method headers and fields). This is required because method bodies may make
forward references to names declared later on in the input. The context argument is
7 This and other classes related to the compiler are part of the jminusminus package under $j/j--/
src , where $j is the directory that contains thej--root directory.
 
Search WWH ::




Custom Search