SlideShare a Scribd company logo
1 of 34
Introduction to Compiler
Presented By:
HIRA SHAHZAD
JAVERIA KHALID
TANZEELA HUSSAIN
1
• All computers only understand machine language
• Therefore, high-level language instructions must be translated into
machine language prior to execution
2
10000010010110100100101……
This is
a program
Why Use a compiler?
Compiler
• A compiler is a large program that can read a program in one language the source
language - and translate it into an equivalent program in another language - the target
language;
• An important role of the compiler is to report any errors in the source program that it
detects during the translation process
• If the target program is an executable machine-language program, it can then be called
by the user to process inputs and produce outputs.
3
Source
Program
Compiler
Target
Program
Error messages Output
Input
Example
Source Code Target Code
4
Interpreter
An interpreter is another common kind of language processor. Instead of producing a target
program as a translation, an interpreter appears to directly execute the operations specified in the
source program or inputs supplied by the user
The machine-language target program produced by a compiler is usually much faster than an
interpreter at mapping inputs to outputs . An interpreter, however, can usually give better error
diagnostics than a compiler, because it executes the source program statement by statement.
5
Source
Program
Interpreter
Error messages
Input
Output
Working Process of Compilers Vs Interpreter
Compilation Process:
Interpretive Process:
6
Source
program
Data
Object
program
Results
Data
Compiler
Executing
Computer
Result
Source
program Interpreter
Compile time Run time
Sr.
Compiler Interpreter
1 Compiler Takes Entire program as input Interpreter Takes Single instruction as
input .
2 Intermediate Object Code is Generated No Intermediate Object Code is
Generated
3 Conditional Control Statements are
Executes faster
Conditional Control Statements are
Executes slower
4 Memory Requirement : More(Since
Object Code is Generated)
Memory Requirement is Less
5 Program need not be compiled every time Every time higher level program is
converted into lower level program
6 Errors are displayed after entire
program is checked
Errors are displayed for every
instruction interpreted (if any)
7 Programming language like C, C++ use
compilers.
Programming language like Python,
Ruby use interpreters.
7
Context of a Compiler
• The programs which assist the compiler to
convert a skeletal source code into executable
form make the context of a compiler and is as
follows:
• Preprocessor:
The preprocessor scans the source code and
includes the header files which
contain relevant information for various
functions.
• Compiler:
The compiler passes the source code through
various phases and generates the
target assembly code.
8
Cont….
• Assembler:
The assembler converts the assembly code into relocatable machine code or object
code. Although this code is in 0 and 1 form, but it cannot be executed because this
code has not been assigned the actual memory addresses.
• Loader/Link Editor:
It performs two functions. The process of loading consists of taking machine code,
altering the relocatable addresses and placing the altered instructions and data in
memory at proper location.
The link editor makes a single program from several files of relocatable machine
code. These files are library files which the program needs.
The loader/link editor produces the executable or absolute machine code.
9
Phases of Compiler Design
A compiler operates in phases. A phase is a logically interrelated operation
that takes source program in one representation and produces output in
another representation. The phases of a compiler are shown in below
There are two phases of compilation.
 Analysis (Machine Independent/Language Dependent)
 Synthesis(Machine Dependent/Language independent)
Compilation process is partitioned into no-of-sub processes called ‘phases’.
10
11
Phase-1: Lexical Analysis
• Lexical analyzer reads the stream of characters making up the source
program and groups the characters into meaningful sequences called
lexeme
• For each lexeme, the lexical analyzer produces a token of the form that it
passes on to the subsequent phase, syntax analysis
(token-name, attribute-value)
• Token-name: an abstract symbol is used during syntax analysis.
• attribute-value: points to an entry in the symbol table for this token.
12
Example:
newval := oldval + 12 Tokens:
newval Identifier
= Assignment operator
oldval Identifier
+ Add operator
12 Number
Lexical analyzer truncates white spaces and also removes errors.
13
14
Phase-2: Syntax Analysis
• Also called Parsing or Tokenizing.
• The parser uses the first components of the tokens produced by the lexical
analyzer to create a tree-like intermediate representation that depicts the
grammatical structure of the token stream.
• A typical representation is a syntax tree in which each interior node
represents an operation and the children of the node represent the
arguments of the operation
15
Example: 16
Phase-3: Semantic Analysis
• The semantic analyzer uses the syntax tree and the information in the
symbol table to check the source program for semantic consistency with
the language definition.
• Gathers type information and saves it in either the syntax tree or the
symbol table, for subsequent use during intermediate-code generation.
• An important part of semantic analysis is type checking, where the
compiler checks that each operator has matching operands.
• For example, many programming language definitions require an array
index to be an integer; the compiler must report an error if a floating-point
number is used to index an array.
• Example: newval := oldval+12
The type of the identifier newval must match with the type of expression (oldval+12).
17
Example:
• Semantic analysis
• Syntactically correct, but semantically incorrect
example:
sum = a + b;
int a;
double sum; data type mismatch
char b;
Semantic records
a integer
sum double
b char
18
Phase-4: Intermediate Code Generation
After syntax and semantic analysis of the source program, many compilers
generate an explicit low-level or machine-like intermediate representation
(a program for an abstract machine). This intermediate representation
should have two important properties:
• it should be easy to produce and
• it should be easy to translate into the target machine.
The considered intermediate form called three-address code, which consists
of a sequence of assembly-like instructions with three operands per
instruction. Each operand can act like a register.
This phase bridges the analysis and synthesis phases of translation.
19
Example:
newval := oldval + fact * 1
Id1 := Id2 + Id3 * 1
Temp1 = into real (1)
Temp2 = Id3 * Temp1
Temp3 = Id2 + Temp2
Id1 = Temp3
20
Phase-5: Code Optimization
• The compiler looks at large segments of the program to decide how to
improve performance
• The machine-independent code-optimization phase attempts to improve the
intermediate code so that better target code will result.
• Usually better means:
• faster, shorter code, or target code that consumes less power.
• There are simple optimizations that significantly improve the running time
of the target program without slowing down compilation too much.
• Optimization cannot make an inefficient algorithm efficient - “only makes
an efficient algorithm more efficient”
21
Example:
• The above intermediate code will be optimized as:
Temp1 = Id3 * 1
Id1 = Id2 + Temp1
22
Phase-6: Code Generation
• The last phase of translation is code generation.
• Takes as input an intermediate representation of the source program and
maps it into the target language
• If the target language is machine, code, registers or memory locations are
selected for each of the variables used by the program.
• Then, the intermediate instructions are translated into sequences of
machine instructions that perform the same task.
• A crucial aspect of code generation is the judicious assignment of registers
to hold variables.
23
Example:
Id1 := Id2 + Id3 * 1
MOV R1,Id3
MUL R1,#1
MOV R2,Id2
ADD R1,R2
MOV Id1,R1
24
25
Symbol-Table Management
• The symbol table is a data structure containing a record for each variable
name, with fields for the attributes of the name.
• The data structure should be designed to allow the compiler to find the
record for each name quickly and to store or retrieve data from that record
quickly
• These attributes may provide information about the storage allocated for a
name, its type, its scope (where in the program its value may be used), and
in the case of procedure names, such things as the number and types of its
arguments, the method of passing each argument (for example, by value or
by reference), and the type returned.
26
new Val Id1 & attribute
old Val Id2 & attribute
fact Id3 &attribute
Error Handling Routine:
• One of the most important functions of a compiler is the detection and
reporting of errors in the source program. The error message should allow
the programmer to determine exactly where the errors have occurred.
Errors may occur in all or the phases of a compiler.
• Whenever a phase of the compiler discovers an error, it must report the
error to the error handler, which issues an appropriate diagnostic message.
Both of the table-management and error-Handling routines interact with all
phases of the compiler.
27
One pass compiler
• One pass compiler passes through the source code of each compilation unit
only once.
• Their efficiency is limited because they don’t produce intermediate codes
which can be refined easily.
• One pass compilers very common because of their simplicity.
• Check for semantic errors and generate code.
• They are faster then multi pass compilers.
• Also known as Narrow compiler.
• Pascal and C are both languages that allow one pass compilation.
28
Multi-pass compilers
• The input is passed through certain phases in one pass. Then the output of
previous phases is passed through other phases in second pass and so on
until the desired output is generated.
• It requires less memory because each pass takes output of previous phase
as input.
• It may create one or more intermediate code.
• Also known as wide compiler.
• Modula-2 is a language whose structure requires that a compiler has at
least two passes.
29
The phases of a compiler are collected into front end and back end.
The FRONT END consists of those phases that depend primarily on
the source program. These normally include Lexical and Syntactic
analysis, Semantic analysis ,and the generation of intermediate code.
A certain amount of code optimization can be done by front end as
well.
The BACK END includes the code optimization phase and final
code generation phase, along with the necessary error handling and
symbol table operations.
The front end Analyzes the source program and produces
intermediate code while the back end Synthesizes the target program
from the intermediate code.
Front End vs Back End of a Compilers 30
Cont….
The front end phase consists of those phases that primarily depend on
source program and are independent of the target machine.
Back end phase of compiler consists of those phases which depend on
target machine and are independent of the source program.
Intermediate representation may be considered as middle end, as it
depends upon source code and target machine.
31
32
33
34

More Related Content

What's hot

Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignAkhil Kaushik
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSARASWATHI S
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lexAnusuya123
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventionssaranyatdr
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Process management os concept
Process management os conceptProcess management os concept
Process management os conceptpriyadeosarkar91
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programmingMukesh Tekwani
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 

What's hot (20)

Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
MACRO PROCESSOR
MACRO PROCESSORMACRO PROCESSOR
MACRO PROCESSOR
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventions
 
Process state in OS
Process state in OSProcess state in OS
Process state in OS
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Compilers
CompilersCompilers
Compilers
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Three Address code
Three Address code Three Address code
Three Address code
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 

Viewers also liked

Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical AnalysisMunni28
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / TranslatorsProject Student
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFAkunj desai
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsingkunj desai
 
Programming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwaresProgramming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwaresNisarg Amin
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automatadeepinderbedi
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
Language translator
Language translatorLanguage translator
Language translatorasmakh89
 

Viewers also liked (20)

Ken Smith - Tokenization
Ken Smith - TokenizationKen Smith - Tokenization
Ken Smith - Tokenization
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
optimization of DFA
optimization of DFAoptimization of DFA
optimization of DFA
 
Nfa vs dfa
Nfa vs dfaNfa vs dfa
Nfa vs dfa
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / Translators
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
DFA Minimization
DFA MinimizationDFA Minimization
DFA Minimization
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Programming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwaresProgramming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwares
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
Dfa vs nfa
Dfa vs nfaDfa vs nfa
Dfa vs nfa
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Translators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreterTranslators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreter
 
Language translator
Language translatorLanguage translator
Language translator
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Code generation
Code generationCode generation
Code generation
 

Similar to Intro to Compiler Phases

Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler languageAshok Raj
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction Thapar Institute
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfDrIsikoIsaac
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in detailskazi_aihtesham
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overviewamudha arul
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfDrIsikoIsaac
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptxssuser3b4934
 
Chapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialChapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialgadisaAdamu
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)guest91cc85
 
Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)guest251d9a
 
Compilerdesignnew 091219090526-phpapp02
Compilerdesignnew 091219090526-phpapp02Compilerdesignnew 091219090526-phpapp02
Compilerdesignnew 091219090526-phpapp02Anil Thakral
 
C program execution and algorithm
C program execution and algorithm C program execution and algorithm
C program execution and algorithm Kunal Pandhram
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Tirumala Rao
 

Similar to Intro to Compiler Phases (20)

Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
Chapter1.pdf
Chapter1.pdfChapter1.pdf
Chapter1.pdf
 
Chapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialChapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course Material
 
Compiler Design(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
 
Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)
 
Compilerdesignnew 091219090526-phpapp02
Compilerdesignnew 091219090526-phpapp02Compilerdesignnew 091219090526-phpapp02
Compilerdesignnew 091219090526-phpapp02
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Phases of Compiler.pdf
Phases of Compiler.pdfPhases of Compiler.pdf
Phases of Compiler.pdf
 
C program execution and algorithm
C program execution and algorithm C program execution and algorithm
C program execution and algorithm
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
 

Recently uploaded

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Recently uploaded (20)

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 

Intro to Compiler Phases

  • 1. Introduction to Compiler Presented By: HIRA SHAHZAD JAVERIA KHALID TANZEELA HUSSAIN 1
  • 2. • All computers only understand machine language • Therefore, high-level language instructions must be translated into machine language prior to execution 2 10000010010110100100101…… This is a program Why Use a compiler?
  • 3. Compiler • A compiler is a large program that can read a program in one language the source language - and translate it into an equivalent program in another language - the target language; • An important role of the compiler is to report any errors in the source program that it detects during the translation process • If the target program is an executable machine-language program, it can then be called by the user to process inputs and produce outputs. 3 Source Program Compiler Target Program Error messages Output Input
  • 5. Interpreter An interpreter is another common kind of language processor. Instead of producing a target program as a translation, an interpreter appears to directly execute the operations specified in the source program or inputs supplied by the user The machine-language target program produced by a compiler is usually much faster than an interpreter at mapping inputs to outputs . An interpreter, however, can usually give better error diagnostics than a compiler, because it executes the source program statement by statement. 5 Source Program Interpreter Error messages Input Output
  • 6. Working Process of Compilers Vs Interpreter Compilation Process: Interpretive Process: 6 Source program Data Object program Results Data Compiler Executing Computer Result Source program Interpreter Compile time Run time
  • 7. Sr. Compiler Interpreter 1 Compiler Takes Entire program as input Interpreter Takes Single instruction as input . 2 Intermediate Object Code is Generated No Intermediate Object Code is Generated 3 Conditional Control Statements are Executes faster Conditional Control Statements are Executes slower 4 Memory Requirement : More(Since Object Code is Generated) Memory Requirement is Less 5 Program need not be compiled every time Every time higher level program is converted into lower level program 6 Errors are displayed after entire program is checked Errors are displayed for every instruction interpreted (if any) 7 Programming language like C, C++ use compilers. Programming language like Python, Ruby use interpreters. 7
  • 8. Context of a Compiler • The programs which assist the compiler to convert a skeletal source code into executable form make the context of a compiler and is as follows: • Preprocessor: The preprocessor scans the source code and includes the header files which contain relevant information for various functions. • Compiler: The compiler passes the source code through various phases and generates the target assembly code. 8
  • 9. Cont…. • Assembler: The assembler converts the assembly code into relocatable machine code or object code. Although this code is in 0 and 1 form, but it cannot be executed because this code has not been assigned the actual memory addresses. • Loader/Link Editor: It performs two functions. The process of loading consists of taking machine code, altering the relocatable addresses and placing the altered instructions and data in memory at proper location. The link editor makes a single program from several files of relocatable machine code. These files are library files which the program needs. The loader/link editor produces the executable or absolute machine code. 9
  • 10. Phases of Compiler Design A compiler operates in phases. A phase is a logically interrelated operation that takes source program in one representation and produces output in another representation. The phases of a compiler are shown in below There are two phases of compilation.  Analysis (Machine Independent/Language Dependent)  Synthesis(Machine Dependent/Language independent) Compilation process is partitioned into no-of-sub processes called ‘phases’. 10
  • 11. 11
  • 12. Phase-1: Lexical Analysis • Lexical analyzer reads the stream of characters making up the source program and groups the characters into meaningful sequences called lexeme • For each lexeme, the lexical analyzer produces a token of the form that it passes on to the subsequent phase, syntax analysis (token-name, attribute-value) • Token-name: an abstract symbol is used during syntax analysis. • attribute-value: points to an entry in the symbol table for this token. 12
  • 13. Example: newval := oldval + 12 Tokens: newval Identifier = Assignment operator oldval Identifier + Add operator 12 Number Lexical analyzer truncates white spaces and also removes errors. 13
  • 14. 14
  • 15. Phase-2: Syntax Analysis • Also called Parsing or Tokenizing. • The parser uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream. • A typical representation is a syntax tree in which each interior node represents an operation and the children of the node represent the arguments of the operation 15
  • 17. Phase-3: Semantic Analysis • The semantic analyzer uses the syntax tree and the information in the symbol table to check the source program for semantic consistency with the language definition. • Gathers type information and saves it in either the syntax tree or the symbol table, for subsequent use during intermediate-code generation. • An important part of semantic analysis is type checking, where the compiler checks that each operator has matching operands. • For example, many programming language definitions require an array index to be an integer; the compiler must report an error if a floating-point number is used to index an array. • Example: newval := oldval+12 The type of the identifier newval must match with the type of expression (oldval+12). 17
  • 18. Example: • Semantic analysis • Syntactically correct, but semantically incorrect example: sum = a + b; int a; double sum; data type mismatch char b; Semantic records a integer sum double b char 18
  • 19. Phase-4: Intermediate Code Generation After syntax and semantic analysis of the source program, many compilers generate an explicit low-level or machine-like intermediate representation (a program for an abstract machine). This intermediate representation should have two important properties: • it should be easy to produce and • it should be easy to translate into the target machine. The considered intermediate form called three-address code, which consists of a sequence of assembly-like instructions with three operands per instruction. Each operand can act like a register. This phase bridges the analysis and synthesis phases of translation. 19
  • 20. Example: newval := oldval + fact * 1 Id1 := Id2 + Id3 * 1 Temp1 = into real (1) Temp2 = Id3 * Temp1 Temp3 = Id2 + Temp2 Id1 = Temp3 20
  • 21. Phase-5: Code Optimization • The compiler looks at large segments of the program to decide how to improve performance • The machine-independent code-optimization phase attempts to improve the intermediate code so that better target code will result. • Usually better means: • faster, shorter code, or target code that consumes less power. • There are simple optimizations that significantly improve the running time of the target program without slowing down compilation too much. • Optimization cannot make an inefficient algorithm efficient - “only makes an efficient algorithm more efficient” 21
  • 22. Example: • The above intermediate code will be optimized as: Temp1 = Id3 * 1 Id1 = Id2 + Temp1 22
  • 23. Phase-6: Code Generation • The last phase of translation is code generation. • Takes as input an intermediate representation of the source program and maps it into the target language • If the target language is machine, code, registers or memory locations are selected for each of the variables used by the program. • Then, the intermediate instructions are translated into sequences of machine instructions that perform the same task. • A crucial aspect of code generation is the judicious assignment of registers to hold variables. 23
  • 24. Example: Id1 := Id2 + Id3 * 1 MOV R1,Id3 MUL R1,#1 MOV R2,Id2 ADD R1,R2 MOV Id1,R1 24
  • 25. 25
  • 26. Symbol-Table Management • The symbol table is a data structure containing a record for each variable name, with fields for the attributes of the name. • The data structure should be designed to allow the compiler to find the record for each name quickly and to store or retrieve data from that record quickly • These attributes may provide information about the storage allocated for a name, its type, its scope (where in the program its value may be used), and in the case of procedure names, such things as the number and types of its arguments, the method of passing each argument (for example, by value or by reference), and the type returned. 26 new Val Id1 & attribute old Val Id2 & attribute fact Id3 &attribute
  • 27. Error Handling Routine: • One of the most important functions of a compiler is the detection and reporting of errors in the source program. The error message should allow the programmer to determine exactly where the errors have occurred. Errors may occur in all or the phases of a compiler. • Whenever a phase of the compiler discovers an error, it must report the error to the error handler, which issues an appropriate diagnostic message. Both of the table-management and error-Handling routines interact with all phases of the compiler. 27
  • 28. One pass compiler • One pass compiler passes through the source code of each compilation unit only once. • Their efficiency is limited because they don’t produce intermediate codes which can be refined easily. • One pass compilers very common because of their simplicity. • Check for semantic errors and generate code. • They are faster then multi pass compilers. • Also known as Narrow compiler. • Pascal and C are both languages that allow one pass compilation. 28
  • 29. Multi-pass compilers • The input is passed through certain phases in one pass. Then the output of previous phases is passed through other phases in second pass and so on until the desired output is generated. • It requires less memory because each pass takes output of previous phase as input. • It may create one or more intermediate code. • Also known as wide compiler. • Modula-2 is a language whose structure requires that a compiler has at least two passes. 29
  • 30. The phases of a compiler are collected into front end and back end. The FRONT END consists of those phases that depend primarily on the source program. These normally include Lexical and Syntactic analysis, Semantic analysis ,and the generation of intermediate code. A certain amount of code optimization can be done by front end as well. The BACK END includes the code optimization phase and final code generation phase, along with the necessary error handling and symbol table operations. The front end Analyzes the source program and produces intermediate code while the back end Synthesizes the target program from the intermediate code. Front End vs Back End of a Compilers 30
  • 31. Cont…. The front end phase consists of those phases that primarily depend on source program and are independent of the target machine. Back end phase of compiler consists of those phases which depend on target machine and are independent of the source program. Intermediate representation may be considered as middle end, as it depends upon source code and target machine. 31
  • 32. 32
  • 33. 33
  • 34. 34