2. OUTLINE
What is compiler
Why do we need a compiler
Different type of translator
Difference between compiler and interpreter
Compiler architecture
Phases of compiler
Exercise Questions
2
3. COMPILER
A compiler acts as a translator, transforming human-
oriented programming languages (high level language)
into computer-oriented machine languages (low level
language)
3
High level
Language
(Source
language )
Low level
language
(Target
language)
Error messages
4. We need compiler' because computers cannot understand
human language, and humans cannot understand computer
language.
Compilers are like translators that convert human-
readable/writable code into assembly code, which is very
difficult to understand and use. The compiler then converts the
assembly code into binary code
4
WHY DO WE NEED A COMPILER
5. TRANSLATOR
A translator is a computer program which
converts source language to target language.
Types of translator
Preprocessor
Compiler
Interpreter
Assembler
5
6. DIFFERENCE BETWEEN COMPILER AND
INTERPRETER
COMPILER INTERPRETER
Scans the entire program and
translates it as a whole into
machine code.
Translates program one statement
at a time.
Fast execution Slow execution
Memory Requirement is more Memory Requirement is Less
Intermediate Object Code
is Generated
No Intermediate Object Code
is Generated
Errors are displayed after entire
program is checked
Errors are displayed for every
instruction interpreted (if any)
6
8. PROGRAM EXECUTION STEPS
User writes a program in C language high − level
language.
The C compiler, compiles the program and translates it
to assembly program low − level language.
An assembler then translates the assembly program into
machine code object.
A linker tool is used to link all the parts of the program
together for execution
Executable machine code.
A loader loads all of them into memory and then the
program is executed. 8
9. COMPILER ARCHITECTURE
Any compiler must perform two major tasks
Analysis of the source program (machine independent)
Synthesis of a machine-language program (machine
dependent)
9
Compiler
11. THE STRUCTURE OF A
COMPILER
11
Scanner
[Lexical Analyzer]
Parser
[Syntax Analyzer]
Semantic Process
[Semantic analyzer]
Code Generator
[Intermediate Code Generator]
Code Optimizer
Tokens
Parse
tree
Abstract Syntax Tree w/
Attributes
Non-optimized Intermediate
Code
Optimized Intermediate Code
Code Optimizer
Target machine code
12. THE PHASES OF A COMPILER
Phase Output Sample
Programmer (source code producer) Source string A=B+C;
Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
And symbol table with names
Parser (performs syntax analysis
based on the grammar of the
programming language)
Parse tree or abstract syntax tree ;
|
=
/
A +
/
B C
Semantic analyzer (type checking,
etc)
Annotated parse tree or abstract
syntax tree
Intermediate code generator Three-address code, quads, or
RTL
int2fp B t1
+ t1 C t2
:= t2 A
Optimizer Three-address code, quads, or
RTL
int2fp B t1
+ t1 #2.3 A
Code generator Assembly code MOVF #2.3,r1
ADDF2 r1,r2
MOVF r2,A
Peephole optimizer Assembly code ADDF2 #2.3,r2
12
COP
5621
Fall
2009
13. EXERCISE QUESTIONS
What is the difference between compiler and
interpreter?
Which phase performs type checking?
In which phase scanning is used?
Which phase generate parse tree?
Why we design compiler ? can we write a
program in low level language?
13