Introduction To Compilation
Introduction To Compilation
Lecture 01
What is a compiler?
• Two parts
– Analysis
• Breaks up the source program into constituents
– Synthesis
• Constructs the target program
Intermediate
Language
• Structure Editors
– Gives hierarchical structure
– Suggests keywords/structures automatically
• Pretty Printer
– Provides an organized and structured look to program
– Different color, font, indentation are used
• Static Checkers
– Tries to discover potential bugs without running
– Identify logical errors, type-checking, dead codes
identification etc
• Interpreters
– Does not produce a target program
– Executes the operations implied by the program
Compilation Steps/Phases
Intermediate
Intermediate
Language
Language
Symbol
Table
Compilation Steps/Phases
• Input: result = a + b * 10
• Tokens:
identifiers
operators
Syntax Analysis (Parsing)
• Expression grammar
Exp ::= Exp ‘+’ Exp
| Exp ‘*’ Exp
| ID
| NUMBER
Input: result = a + b * 10
Assign
result +
a *
b 10
Semantic Analysis
Error
Handler
Symbol
Table
Translation of a statement
result = a + b * 10
Lexical Analyzer
Symbol Table
id1 = id2 + id3 * 10
result …….
a …….
Syntax Analyzer
b …….
Assign
id1 +
id2 *
id3 10
Translation of a statement
Assign
id1 +
id2 *
Assign
id1 +
id2 *
id3 INTTOREAL
10
Translation of a statement
Code Generator
temp1 := INTTOREAL (10)
temp2 := id3 * temp1
temp3 := id2 + temp2 MOVF id3, R2
Id1 := temp3 MULF #10.0, R2
MOVF id2, R1
ADDF R2, R1
Code Optimizer MOVF R1, id1
• Preprocessor
– Macro preprocessing
• Define and use shorthand for longer constructs
– File inclusion
• Include header files
– “Rational” Preprocessors
• Augment older languages with modern flow-of-control or
data-structures
– Language Extension
• Add capabilities to a language
• Equel: query language embedded in C
Assemblers
Source program
Compiler
Assembly program
Assembler
Loader/link-editor
• Passes
– Several phases of compilers are grouped in to passes
– Often passes generate an explicit output file
– In each pass the whole input file/source is processed
Syntax Analyzer
• Correctness
• Speed (runtime and compile time)
– Degrees of optimization
– Multiple passes
• Space
• Feedback to user
• Debugging
Other Applications