Java Reference
In-Depth Information
a string of Context (or subtypes of Context ) objects representing the compile-time
symbol table of declared names and their definitions.
2. analyze(Contextcontext) is defined over all types of AST nodes. When invoked
on a node, this method declares names in the symbol table (context), checks types
(looking up the types of names in the symbol table), and converts local variables to
osets in a method's run-time local stack frame where the local variables reside.
3. codegen(CLEmitteroutput) is invoked for generating the Java Virtual Machine
(JVM) code for that node, and is applied recursively for generating code for any
sub-trees. The output argument is a CLEmitter object, an abstraction of the output
.class file.
Once Main has created the scanner and parser,
1. Main sends a compilationUnit() message to the parser, causing it to parse the pro-
gram by a technique known as recursive descent, and to produce an AST.
2. Main then sends the preAnalyze() message to the root node (an object of type
JCompilationUnit ) of the AST. preAnalyze() recursively descends the tree down to
the class member headers for declaring the types and the class members in the symbol
table context.
3. Main then sends the analyze() message to the root JCompilationUnit node, and
analyze() recursively descends the tree all the way down to its leaves, declaring
names and checking types.
4. Main then sends the codegen() message to the root JCompilationUnit node, and
codegen() recursively descends the tree all the way down to its leaves, generating JVM
code. At the start of each class declaration, codegen() creates a new CLEmitter object
for representing a target .class file for that class; at the end of each class declaration,
codegen() writes out the code to a .class file on the file system.
5. The compiler is then done with its work. If errors occur in any phase of the compilation
process, the phase attempts to run to completion (finding any additional errors) and
then the compilation process halts.
In the next sections, we briefly discuss how each phase does its work. As this is just an
overview and a preview of what is to come in subsequent chapters, it is not important that
one understand everything at this point. Indeed, if you understand just 15%, that is fine.
The point to this overview is to let you know where stuff is. We have the rest of the text to
understand how it all works!
1.4.2 Scanner
The scanner supports the parser. Its purpose is to scan tokens from the input stream of
characters comprising the source language program. For example, consider the following
source language HelloWorld program.
importjava.lang.System;
publicclassHelloWorld{
//Theonlymethod.
publicstaticvoidmain(String[]args){
 
Search WWH ::




Custom Search