Professional Documents
Culture Documents
Compilation
A compiler is a program that reads a
program written in one language (the
source language) and translates it into an
equivalent program in another language
(the target language).
Input
Oversimplified view:
Source
Program
Compiler
Target
Program
Error messages
Output
Interpreter
Instead of producing a target program
as a translation, an interpreter
performs the operations implied by the
source program.
An interpreter might build a tree and
carry out the operations at the nodes
as it walks the tree.
At the root it would discover it had an
assignment to perform.
Interpretation
Performing the operations implied by the
source program
Oversimplified view:
Source
Program
Interpreter
Output
Input
Error messages
A language-processing
system
Skeletal Source Program
Preprocessor
Source Program
Compiler
Target Assembly Program
Assembler
Relocatable Object Code
Linker
Absolute Machine Code
Libraries and
Relocatable Object Files
8
Analysis
Phases of a compiler
11
Lexical Analysis
First phase of a compiler is called
lexical analysis or scanning, it reads
stream of characters & groups the
characters into meaningful sequence
called lexemes. For each lexeme the
lexical analyzer produces as output a
token of the form
<token name, attribute value>
So, this phase perform linear analysis
on the source program.
12
Lexical analysis(contd)
13
14
15
16
Semantic analysis
Checks source program for semantic
errors
Gathers
type
information
for
subsequent code generation (type
checking)
Identifies operator and operands of
expressions and statements
17
Easy to produce
Easy to translate into target program
Code Optimization
Improve intermediate code by
producing code that runs faster
Code Generation
Generate target code, which is machine
code or assembly code
19
Symbol-Table Management
Symbol table data structure with a
record for each identifier and its
attributes
Attributes include storage allocation,
type, scope, etc
All the compiler phases insert and
modify the symbol table
20
Scanner
[Lexical Analyzer]
Tokens
Code Optimizer
Parser
[Syntax Analyzer]
Parse tree
Code Generator
Semantic Process
[Semantic analyzer]
21
Back end:
Code optimization and generation
Independent of source language
Machine dependent
22
Compiler passes:
A collection of phases is done only once (single
pass) or multiple times (multi pass)
Single pass: reading input, processing, and producing
output by one large compiler program; usually runs faster
Multi pass: compiler split into smaller programs, each
making a pass over the source; performs better code
optimization
23