2. Content
Introduction
The Structure of a Compiler
The Phases of a Compiler
The Grouping of Phases
Interpreters & Compilers
3. Introduction
• A compiler translates (or Compiler) is a software which
converts a program written in high level language (Source
Language) to low level language (Object/Target/Machine
Language).
Compiler
Compilation Error
High Level
Language
Low Level
Language
4.
5. • Read and analyze entire program
• The compiler can spot some obvious programming
mistakes.
• There are thousands of source languages, ranging from
traditional programming languages such as FORTRAN and
Pascal to specialized languages.
Introduction
6. • The basic tasks that any compiler must perform are essentially the same.
• By understanding these tasks, we can construct compilers for a wide
variety of source languages and target machines using the same basic
techniques.
• The first real compiler
– Throughout the 1950’s, compilers were considered notoriously difficult
programs to write.
– The first FORTRAN compiler, for example, took 18 staff-years to implement.
Introduction
7. Any compiler must perform two major tasks
Analysis an intermediate representation is created from the given source program.
– Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase.
Synthesis of takes the tree structure and translates the operations therein into the
target program.
– Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase.
The Structure of a Compiler
Compile
r
Analysis
Synthesi
s
8. The Structure of a Compiler
Scanner Parser
Semantic
Routines
Code
Generator
Optimizer
Source
Program Tokens Syntactic
Structure
Symbol and
Attribute
Tables
(Used by all Phases of The Compiler)
(Character Stream)
Intermediate
Representation
Target machine code
9. The Phases of a Compiler
• Lexical Analyzer
• Syntax Analyzer
• Semantic Analyzer
• Intermediate code generator
• Code optimizer
• Code generator
10. The Grouping of Phases
• Compiler front and back ends:
– Front end: analysis (machine independent)
– Back end: synthesis (machine
• Compiler passes:
– A collection of phases is done only once (single pass) or
multiple times (multi pass)
Single pass: usually requires everything to be defined
before being used in source program
Multi pass: compiler may have to keep entire program
representation in memory
11. Interpreters & Compilers
Compilers
A program that translates a program from one language (the source) to another
(the target)
Typical Implementations
– FORTRAN, C, C++, Java, COBOL, etc. etc.
– Strong need for optimization
Compiler
Error messages
Source
Program
Target
Program
Input
Output
12. Interpreters & Compilers
Interpreters
A program that reads a source program and produces the results of executing
that program.
Typical Implementations
– PERL, Python, awk, sed, sh, csh, postscript printer, Java VM
– Effective if interpreter overhead is low relative to execution cost of language
statements
Interpreter
Source
Program
Input
Output
Error messages
13. Interpreters & Compilers
• An interpreter may need to process the same piece of the syntax tree (for
example, the body of a loop) many times interpretation is slower than
executing a compiled program.
• An interpreter executes the source program statement by statement it can
usually give better error diagnostics than a compiler.
• Compilers and interpreters both must read the input – a stream of characters –
and “understand” it; analysis
w h i l e ( k < l e n g t h ) { <nl> <tab> i f ( a [ k ] > 0
) <nl> <tab> <tab>{ n P o s + + ; } <nl> <tab> }