Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 8

[ 187 ]

Compiler phases


As the compiler works through the different phases, it adds detail to the AST. In all
the examples we will work on in this chapter, we will have sufficient detail for our
transformations to work on in the semantic analysis phase of compilation. It is worth
understanding the compiler phases and what additional information is added to the
AST at each phase.


The AST viewer is a useful tool for exploring this. The preceding screenshot is the
panel where the AST tree is displayed; the AST viewer also maintains a source view.
This view is also augmented to reflect what nodes have been added to the AST. Try
switching phases in the view, and you will see this happen. Now, let's look at the
individual compiler phases:



  • Initialization: In this phase, the compiler opens up all source files and
    configures its environment. If we are using CompilerConfiguration with
    the GroovyShell class, this is where that object is opened and interrogated.
    You will notice in the AST viewer that the AST is empty at this point. AST
    generation takes place later.

  • Parsing: The Groovy grammar is used to produce a tree of tokens
    representing the source code. The Groovy grammar is implemented using
    Apache Antlr, see http://www.antlr.org. Note that the tokens generated
    at this phase are not yet an AST, so the tree view in AST viewer will still
    be empty.

Free download pdf