Overview of Compilation: Programming Language Principles
Overview of Compilation: Programming Language Principles
Prepared by
Overview of Translation
Definition: A translator is an algorithm that converts source programs into equivalent target programs.
Source
Translator
Target
Definition: A compiler is a translator whose target language is at a lower level than its source language.
Source
Interpreter
output
Translation
A source program may be translated an arbitrary number of times before the target program is generated. Source Translator1
Translator2 . . .
TranslatorN Target
Translation (contd)
Each of these translations is called a phase, not to be confused with a pass, i.e., a disk dump. Q: How should a compiler be divided into phases? A: So that each phase can be easily described by some formal model of computation, and so the phase can be carried out efficiently.
Translation (contd)
Q: How is a compiler usually divided? A: Two major phases, with many possibilities for subdivision. Phase 1: Analysis (determine correctness) Phase 2: Synthesis (produce target code) Another criterion: Phase 1: Syntax (form). Phase 2: Semantics (meaning).
Source
Lexical Analysis
Must deal with end-of-line and end-offile characters. A preliminary classification of tokens is made. For example, both program and Ex are classified as Identifier. Someone must give unambiguous rules for forming tokens.
Screening
Goals: Remove unwanted tokens. Classify keywords. Merge/simplify tokens. Sequence of Tokens
Screening
Keywords recognized. White spaces (and comments) discarded. The screener acts as an interface between the scanner and the next phase, the parser.
String-To-Tree Transduction
Goal: To build a syntax tree from the sequence of rewrite rules. The tree will be the functional representation of the source. Method: Build tree bottom-up, as the rewrite rules are emitted. Use a stack of trees.
Example
Chronologically, 1. Enter x into the DCLN table, with its type. 2. Check type compatibility for x=5. 3. X2 not declared! 4. Verify type of > is boolean. 5. Check type compatibility for +. 6. Check type compatibility between x and int, for assignment.
Code Generation
Goal: Convert syntax tree to target code. Target code could be: Machine language. Assembly language. Quadruples for a fictional machine: label opcode operands (1 or 2)
Code Generation
Example: pc on UNIX generates assembly code pi on UNIX generates code for the p machine, which is interpreted by an interpreter. pc: slow compilation, fast running code. pi: fast compilation, slow running code.
L1
L2
Code Optimization
Goals: Reduce the size of the target program. Decrease the running time of the target.
Store non-destructively, i.e., store in X, but do not destroy value on top of stack.
Summary
Source
Scanner Tokens Screener
Table Routines
Tokens Parser
Error Routines
Tree Constrainer
Tree Code Generator Code (for an abstract machine) Input Interpreter Output
Overview of Compilation
Programming Language Principles Lecture 2
Prepared by