CS179E Language Implementation Script

Compiler engineering script
Purpose To guide a team though the implementation of a compiler.
Entry Criteria
General
  • Produce a prototype implementation to assist in refining the design and specification.
  • Produce a complete compiler when the language design has stabilized with compile and run time error reporting and recovery and optimization features.
Phases Activities Description
1
Implementation 
Language
Choose a suitable implementation language (see parsing strategy).
Syntax Error 
Reporting and 
Handling
Detect 
  • Lexical  errors
  • Phrase errors
 and select an appropriate error recovery method such as 
  • immediate exit or 
  • attempt to synchronize (by consuming tokens until a token in the follow set of the current rule is found) and then continue with code generation switched off. 
Lexical errors may be handled by generating a special error token.
4
Intermediate 
Representation
Choose an intermediate representation 
  • Abstract syntax trees, or
  • other intermediate representation such as quadruples
and modify the parser to generate the intermediate representation
5
Semantic or Contextual 
Analysis
Construct a contextual analyzer 
  • identification (symbol) table or
  • decorated abstract syntax tree.
to check for compliance with 
  • Scope rules and
  • Type rules
and include the standard environment.
Context Error 
Reporting and 
Handling
Detect 
  • scope errors
  • type errors
and recover with 
  • immediate exit or
  • attempt to synchronize (by consuming tokens until a token in the follow set of the current rule is found) and continue with code generation switched off.
6
Code 
Transformation (Optional)
Construct a machine independent code transformer (optimizer) for the AST 
  • Constant folding
  • Common subexpression elimination
  • Code movement
7
   
Code 
Generator
Choose a target machine or language 
  • Assembly code
  • Stack machine (simplest)
  • Java byte code
  • Register machine (hardest)
  • High-level language such as C/C++ 
Determine the representation of the object program

Develop code functions for each phrase class.

Develop code templates for each distinct form of phrase

Complete the code generator as a visitor pattern for the AST 

Runtime Error 
Reporting and 
Handling
Detect 
  • Arithmetic overflow
  • Division by zero
  • Out-of-range indexing
call exception handling if available or exit reporting error.
8
Peephole 
Optimizer (optional)
Fine tune the code with a machine dependent peephole optimizer.
Exit criteria
A working compiler

Take One!

Based on notes by Anthony Aaby.