SlideShare une entreprise Scribd logo
1  sur  40
Télécharger pour lire hors ligne
CHAPTER # 01
INTRODUCTIONTO COMPILER
DEPARTMENT OF SOFTWARE ENGINEERING
SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY
COURSE INSTRUCTORS:
 ENGR. FARHEEN QAZI
 ENGR. SANOBER SOOMRO
COURSE SCOPE
 Aim:
 To learn techniques of a modern compiler
 Main reference:
 Alfred V. Aho, Monica S. Lam, Ravi Sethi & Jeffrey D. Ullman,
Compilers Principles, Techniques and Tools, Pearson, 2nd
Edition (2020), ISBN-13: 978-0321486813.
 Supplementary references:
 Torben Aegidius Mogensen, Introduction to compiler Design
(Springer), 2nd Edition (2017), ISBN: 978-3-319-66966-3.
MARKS DISTRIBUTION (TENTATIVE)
• Mid-Term Exam – 30 %
• Final Exam – 50 %
• Assignment - 10 %
• Quiz - 10 %
TODAY’S AGENDA
 Overview
 Compiler definition
 CompilerVS Interpreter
 Program execution sequence
 History of Compiler
 Models of Compiler
 Phases of compiler
 Software Tools Performing Analysis
 Types of compiler
COMPILER LEARNING
 Isn’t it an old discipline?
 Yes, it is a well-established discipline
 Algorithms, methods and techniques are researched and
developed in early stages of computer science growth
 There are many compilers around and many tools to
generate them automatically
 So, why we need to learn it?
 Although you may never write a full compiler
 But the techniques we learn is useful in many tasks like
writing an interpreter for a scripting language, validation
checking for forms and so on
OVERVIEW
 Programming languages are notations for describing
computations to people and to machines.
 The world as we know it depends on programming languages,
because all the software running on all the computers was
written in some programming language.
 But, before a program can be run, it must first be translated
into a form in which it can be executed by a computer.
 The software systems that do this translation are called
Language Processing Systems (Compilers, Interpreters and
Assemblers etc.) or Translators and compiler is one of them.
CONTD….
 This course is about how to design and implement compilers.
 A few basic ideas can be used to construct translators for a
wide variety of languages and machines.
 Besides compilers, the principles and techniques for compiler
design are applicable to so many other domains that they are
likely to be reused many times in the career of a computer
scientist.
 The study of compiler writing touches upon programming
languages, machine architecture, language theory, algorithms,
and software engineering.
TERMINOLOGY
 Compiler:
A program that converts instructions into a machine-
code or lower-level form so that they can be read and
executed by a computer.
OR
A compiler is a computer program that
translates computer code written in one programming
language (the source language) into another language (the
target language).
The name compiler is primarily used for programs that
translate source code from a high-level programming
language to a lower level language (e.g., assembly
language) to create an executable program.
ABSTRACTVIEW
 Recognizes legal (and illegal) programs
 Generate correct code
 Manage storage of all variables and code
 Agreement on format for object (or assembly) code
Source
code
Machine
code
Compiler
errors
DIFFERENCE BETWEEN COMPILER AND INTERPRETER
Interpreter Compiler
• Translates program one
statement at a time.
• Scans the entire program and
translates it as a whole into
machine code.
• Interpreters usually take less
amount of time to analyze the
source code. However, the
overall execution time is
comparatively slower than
compilers.
• Compilers usually take a large
amount of time to analyze the
source code. However, the
overall execution time is
comparatively faster than
interpreters.
• Programming languages like
JavaScript, Python, Ruby use
interpreters.
• Programming languages like C,
C++, Java use compilers.
FLOW OF PROGRAM EXECUTION
SHORT HISTORY OF COMPILER
MODEL OF A COMPILER
 This model is collectively called the Analysis-Synthesis model
of compilation.
 The analysis part is often called the front end (FE) of the
compiler.
 The synthesis part is the back end (BE).
ANALYSIS OR FRONT END OF A COMPILER
 Analysis element breaks the acknowledgment program into
an essential or characteristic part of something abstract.
pieces and imposes a grammatical ordering on them which
further uses this structure to create an intermediate version
of the source program.
 It is also call as front end of compiler.
SYNTHESIS OR BACK END OF A COMPILER
 Synthesis factor takes the intermediate explanation as input
and transforms it to the intended program.
 It is also termed as back end of compiler.
Source Code
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Intermediate Code Generator
Code Optimizer
Code Generator
Object Code
Symbol
Table
Manager
Error
Handler
Synthesis
Analysis
Synthesis
Tokens
Syntax Tree
Syntax Tree
Intermediate Representation
Intermediate
Representation
PHASES OF COMPILER
LEXICAL ANALYZER
 Lexical analysis is the first phase of compiler which is also termed
as scanning.
 Source program is scanned to read the stream of characters
and those characters are grouped to form a sequence called
lexemes which produces token as output.
 Example c=a+b*5;
 Token Template:
 <token-name, attribute-value>
<id1, c> <=> <id2, a> <+> <id3, b> < *> <num, 5> <; >
SYNTAX ANALYZER
 Syntax analysis is the second phase of compiler which is
also called as parsing.
 Parser converts the tokens gave by lexical analyzer into a
tree like version called parse tree.
 A parse tree describes the syntactic structure of the input.
 Example: int a
int int a
SEMANTIC ANALYZER
 It checks for the semantic consistency.
 Type information is gathered and stored in symbol table or
in syntax tree.
 Performs type checking.
 Example : int a;
a=6;
int a;
a=3.142;
INTERMEDIATE CODE GENERATOR
 Intermediate code nature produces intermediate
representations for the source program.
 It generates three address code in which every memory
location acts like register
 Example : t1 = inttofloat (5)
t2 = id3* t1
t3 = id2 + t2
id1 = t3
CODE OPTIMIZER
 Code optimization phase gets the intermediate code as
input and produces optimized intermediate code as output.
 It results in faster running machine code.
 It can be done by reducing the number of lines of code for
a program.
CODE OPTIMIZER
 Example
Temp1=inttoreal(60)
Temp2=id3*Temp1
Temp3=id2+Temp2
id1=Temp3
After Optimization
Temp1=id3*60.0
id1=id2+Temp1
TARGET CODE GENERATOR
 Code category is the final phase of a compiler.
 It gets input from code optimization phase and produces the
target code as result.
 Intermediate instructions are translated into a sequence of
machine instructions that perform the same task.
TARGET CODE GENERATOR
SYMBOLTABLE MANAGER
 Important Functions:
 Record Identifiers used in program
 Collect info about various attributes of each identifier
.
 Stores:
 Storage Location
 Type
 Scope
 Procedure name
 No. of type of arguments
 ReturnType
 Symbol table is data structure containing record for each
identifiers
 with fields for attributes of identifiers.
 It is prepared at lexical analysis and later phases add information.
ERROR DETECTION & REPORTING
 Each phase can encounter errors.
 Compilation can proceed only after solving errors generated
by lexical, syntax & semantic analysis.
 If code doesn’t form tokens, structure is violated which is
detected by lexical analysis.
 If syntax is violated, error is detected by syntax phase.
 During semantic analysis, compiler tries to detect constructs
that have right syntactic structure.
SOFTWARETOOLS PERFORMINGANALYSIS
 Software tools other than compilers that perform analysis
on Source Program are:
Structure Editors: Inputs a sequence of commands to build
a source program.
 The structure editor performs text creation, modification
and analyzes program text for putting an appropriate
hieratical structure on the source program.
 E.g. Matching do-while and parenthesis { }
CONTD….
Pretty Printers:
 Analyzes a program and prints it in such a way that the
structure of program becomes clearly visible.
 i.e. Indentation
Static Checkers:
 Reads a program, analyzes it and attempts to discover
potential bugs without running the program.
 E.g. Parts of source program that can never be executed
and using variables without being defining them.
 Works on dry run techniques.
TYPES OF COMPILER
The types of compiler are
 Single Pass Compilers
 Two Pass Compilers
 Multi-pass Compilers
SINGLE PASS COMPILER
 If we combine or group all the phases of compiler design in
a single module known as single pass compiler.
 A one pass/single pass compiler is that type of compiler that
passes through the part of each compilation unit exactly
once.
 Single pass compiler is faster and smaller than the multi pass
compiler.
SINGLE PASS COMPILER
TWO PASS COMPILER
 A two pass compiler is a type of compiler that processes
the source code or abstract syntax tree of a program multiple
times.
 In two pass compiler we divide phases in two pass.
TWO PASS COMPILER
FIRST PASS OFTWO PASS COMPILER
 In first pass the included phases are work as front end and
analytic part means all phases analyze the High level
language and convert them three address code.
 First pass is platform independent because the output of
first pass is as three address code which is useful for every
system and the requirement is to change the code
optimization and code generator phase which are comes
to the second pass.
SECOND PASS OFTWO PASS COMPILER
 Second Pass the included phases work as back end and
the synthesis part refers to taking input as three address
code and convert them into Low level language/assembly
language.
 Second pass is platform dependent because final stage of a
typical compiler converts the intermediate representation
of program into an executable set of instructions which is
dependent on the system.
MULTI PASS COMPILER
 If we want to design a compiler for different programming
language for same machine. In this case for each
programming language there is requirement of making Front
end/first pass for each of them and only one Back
end/second pass.
MULTI PASS COMPILER
MULTI PASS COMPILER
 If we want to design a compiler for same programming
language for different machine/system. In this case we make
different Back end for different Machine/system and make
only one Front end for same programming language.
MULTI PASS COMPILER
THANKYOU

Contenu connexe

Tendances

Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
Tech_MX
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 

Tendances (20)

Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Compilers
CompilersCompilers
Compilers
 
Compiler design
Compiler designCompiler design
Compiler design
 
Compiler
Compiler Compiler
Compiler
 
basics of compiler design
basics of compiler designbasics of compiler design
basics of compiler design
 
Ch1
Ch1Ch1
Ch1
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis: COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis:
 
Techniques & applications of Compiler
Techniques & applications of CompilerTechniques & applications of Compiler
Techniques & applications of Compiler
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
 
Compiler
Compiler Compiler
Compiler
 
Presentation compiler design
Presentation compiler designPresentation compiler design
Presentation compiler design
 
Compiler design Introduction
Compiler design IntroductionCompiler design Introduction
Compiler design Introduction
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 

Similaire à Chapter#01 cc

2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
venkatapranaykumarGa
 
Language translators
Language translatorsLanguage translators
Language translators
Aditya Sharat
 

Similaire à Chapter#01 cc (20)

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
 
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
Phases of CompilerPhases of Compiler
Phases of Compiler
 
SSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdfSSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdf
 
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
 
Assignment1
Assignment1Assignment1
Assignment1
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
design intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdfdesign intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdf
 
3.2
3.23.2
3.2
 
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
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptx
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 
01. Introduction.ppt
01. Introduction.ppt01. Introduction.ppt
01. Introduction.ppt
 
Language translators
Language translatorsLanguage translators
Language translators
 

Dernier

Call Girls in Lahore 03068178123 Mr Jimmy
Call Girls in Lahore 03068178123 Mr JimmyCall Girls in Lahore 03068178123 Mr Jimmy
Call Girls in Lahore 03068178123 Mr Jimmy
Escorts in Lahore 03068178123
 
JUAL OBAT PENGGUGUR KANDUNGAN PALANGKARAYA 087776-558899 KLINIK ABORSI PALANG...
JUAL OBAT PENGGUGUR KANDUNGAN PALANGKARAYA 087776-558899 KLINIK ABORSI PALANG...JUAL OBAT PENGGUGUR KANDUNGAN PALANGKARAYA 087776-558899 KLINIK ABORSI PALANG...
JUAL OBAT PENGGUGUR KANDUNGAN PALANGKARAYA 087776-558899 KLINIK ABORSI PALANG...
Cara Menggugurkan Kandungan 087776558899
 
JUAL OBAT ABORSI SURABAYA 081466799220 PIL CYTOTEC PENGGUGUR KANDUNGAN SURABAYA
JUAL OBAT ABORSI SURABAYA 081466799220 PIL CYTOTEC PENGGUGUR KANDUNGAN SURABAYAJUAL OBAT ABORSI SURABAYA 081466799220 PIL CYTOTEC PENGGUGUR KANDUNGAN SURABAYA
JUAL OBAT ABORSI SURABAYA 081466799220 PIL CYTOTEC PENGGUGUR KANDUNGAN SURABAYA
JUAL OBAT GASTRUL MISOPROSTOL 081466799220 PIL ABORSI CYTOTEC 1 2 3 4 5 6 7 BULAN TERPERCAYA
 
Jual Obat Aborsi Palembang ( Asli No.1 ) 085657271886 Obat Penggugur Kandunga...
Jual Obat Aborsi Palembang ( Asli No.1 ) 085657271886 Obat Penggugur Kandunga...Jual Obat Aborsi Palembang ( Asli No.1 ) 085657271886 Obat Penggugur Kandunga...
Jual Obat Aborsi Palembang ( Asli No.1 ) 085657271886 Obat Penggugur Kandunga...
ZurliaSoop
 

Dernier (16)

Call Girls in Lahore 03068178123 Mr Jimmy
Call Girls in Lahore 03068178123 Mr JimmyCall Girls in Lahore 03068178123 Mr Jimmy
Call Girls in Lahore 03068178123 Mr Jimmy
 
Vivek @ Cheap Call Girls In Mayur Vihar | Book 8448380779 Extreme Call Girls ...
Vivek @ Cheap Call Girls In Mayur Vihar | Book 8448380779 Extreme Call Girls ...Vivek @ Cheap Call Girls In Mayur Vihar | Book 8448380779 Extreme Call Girls ...
Vivek @ Cheap Call Girls In Mayur Vihar | Book 8448380779 Extreme Call Girls ...
 
Vivek @ Cheap Call Girls In Jasola | Book 8448380779 Extreme Call Girls Servi...
Vivek @ Cheap Call Girls In Jasola | Book 8448380779 Extreme Call Girls Servi...Vivek @ Cheap Call Girls In Jasola | Book 8448380779 Extreme Call Girls Servi...
Vivek @ Cheap Call Girls In Jasola | Book 8448380779 Extreme Call Girls Servi...
 
JUAL OBAT PENGGUGUR KANDUNGAN PALANGKARAYA 087776-558899 KLINIK ABORSI PALANG...
JUAL OBAT PENGGUGUR KANDUNGAN PALANGKARAYA 087776-558899 KLINIK ABORSI PALANG...JUAL OBAT PENGGUGUR KANDUNGAN PALANGKARAYA 087776-558899 KLINIK ABORSI PALANG...
JUAL OBAT PENGGUGUR KANDUNGAN PALANGKARAYA 087776-558899 KLINIK ABORSI PALANG...
 
Vivek @ Cheap Call Girls In Mandi House Puri | Book 8448380779 Extreme Call G...
Vivek @ Cheap Call Girls In Mandi House Puri | Book 8448380779 Extreme Call G...Vivek @ Cheap Call Girls In Mandi House Puri | Book 8448380779 Extreme Call G...
Vivek @ Cheap Call Girls In Mandi House Puri | Book 8448380779 Extreme Call G...
 
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
 
Vivek @ Cheap Call Girls In Jangpura | Book 8448380779 Extreme Call Girls Ser...
Vivek @ Cheap Call Girls In Jangpura | Book 8448380779 Extreme Call Girls Ser...Vivek @ Cheap Call Girls In Jangpura | Book 8448380779 Extreme Call Girls Ser...
Vivek @ Cheap Call Girls In Jangpura | Book 8448380779 Extreme Call Girls Ser...
 
JUAL OBAT ABORSI SURABAYA 081466799220 PIL CYTOTEC PENGGUGUR KANDUNGAN SURABAYA
JUAL OBAT ABORSI SURABAYA 081466799220 PIL CYTOTEC PENGGUGUR KANDUNGAN SURABAYAJUAL OBAT ABORSI SURABAYA 081466799220 PIL CYTOTEC PENGGUGUR KANDUNGAN SURABAYA
JUAL OBAT ABORSI SURABAYA 081466799220 PIL CYTOTEC PENGGUGUR KANDUNGAN SURABAYA
 
Jual Obat Aborsi Palembang ( Asli No.1 ) 085657271886 Obat Penggugur Kandunga...
Jual Obat Aborsi Palembang ( Asli No.1 ) 085657271886 Obat Penggugur Kandunga...Jual Obat Aborsi Palembang ( Asli No.1 ) 085657271886 Obat Penggugur Kandunga...
Jual Obat Aborsi Palembang ( Asli No.1 ) 085657271886 Obat Penggugur Kandunga...
 
Vivek @ Cheap Call Girls In Dashrath Puri | Book 8448380779 Extreme Call Girl...
Vivek @ Cheap Call Girls In Dashrath Puri | Book 8448380779 Extreme Call Girl...Vivek @ Cheap Call Girls In Dashrath Puri | Book 8448380779 Extreme Call Girl...
Vivek @ Cheap Call Girls In Dashrath Puri | Book 8448380779 Extreme Call Girl...
 
Professional Basic Selling Skills - 3.5.pdf
Professional Basic Selling Skills - 3.5.pdfProfessional Basic Selling Skills - 3.5.pdf
Professional Basic Selling Skills - 3.5.pdf
 
Vivek @ Cheap Call Girls In Dilshad Garden | Book 8448380779 Extreme Call Gir...
Vivek @ Cheap Call Girls In Dilshad Garden | Book 8448380779 Extreme Call Gir...Vivek @ Cheap Call Girls In Dilshad Garden | Book 8448380779 Extreme Call Gir...
Vivek @ Cheap Call Girls In Dilshad Garden | Book 8448380779 Extreme Call Gir...
 
Role of listening in selling or negotiation..pptx
Role of listening in selling or negotiation..pptxRole of listening in selling or negotiation..pptx
Role of listening in selling or negotiation..pptx
 
Vivek @ Cheap Call Girls In Mukherjee Nagar | Book 8448380779 Extreme Call Gi...
Vivek @ Cheap Call Girls In Mukherjee Nagar | Book 8448380779 Extreme Call Gi...Vivek @ Cheap Call Girls In Mukherjee Nagar | Book 8448380779 Extreme Call Gi...
Vivek @ Cheap Call Girls In Mukherjee Nagar | Book 8448380779 Extreme Call Gi...
 
Non-verbal communication in selling and negotiation.pptx
Non-verbal communication in selling and negotiation.pptxNon-verbal communication in selling and negotiation.pptx
Non-verbal communication in selling and negotiation.pptx
 
The complete process of Lead Generation.pptx
The complete process of Lead Generation.pptxThe complete process of Lead Generation.pptx
The complete process of Lead Generation.pptx
 

Chapter#01 cc

  • 1. CHAPTER # 01 INTRODUCTIONTO COMPILER DEPARTMENT OF SOFTWARE ENGINEERING SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY COURSE INSTRUCTORS:  ENGR. FARHEEN QAZI  ENGR. SANOBER SOOMRO
  • 2. COURSE SCOPE  Aim:  To learn techniques of a modern compiler  Main reference:  Alfred V. Aho, Monica S. Lam, Ravi Sethi & Jeffrey D. Ullman, Compilers Principles, Techniques and Tools, Pearson, 2nd Edition (2020), ISBN-13: 978-0321486813.  Supplementary references:  Torben Aegidius Mogensen, Introduction to compiler Design (Springer), 2nd Edition (2017), ISBN: 978-3-319-66966-3.
  • 3. MARKS DISTRIBUTION (TENTATIVE) • Mid-Term Exam – 30 % • Final Exam – 50 % • Assignment - 10 % • Quiz - 10 %
  • 4. TODAY’S AGENDA  Overview  Compiler definition  CompilerVS Interpreter  Program execution sequence  History of Compiler  Models of Compiler  Phases of compiler  Software Tools Performing Analysis  Types of compiler
  • 5. COMPILER LEARNING  Isn’t it an old discipline?  Yes, it is a well-established discipline  Algorithms, methods and techniques are researched and developed in early stages of computer science growth  There are many compilers around and many tools to generate them automatically  So, why we need to learn it?  Although you may never write a full compiler  But the techniques we learn is useful in many tasks like writing an interpreter for a scripting language, validation checking for forms and so on
  • 6. OVERVIEW  Programming languages are notations for describing computations to people and to machines.  The world as we know it depends on programming languages, because all the software running on all the computers was written in some programming language.  But, before a program can be run, it must first be translated into a form in which it can be executed by a computer.  The software systems that do this translation are called Language Processing Systems (Compilers, Interpreters and Assemblers etc.) or Translators and compiler is one of them.
  • 7. CONTD….  This course is about how to design and implement compilers.  A few basic ideas can be used to construct translators for a wide variety of languages and machines.  Besides compilers, the principles and techniques for compiler design are applicable to so many other domains that they are likely to be reused many times in the career of a computer scientist.  The study of compiler writing touches upon programming languages, machine architecture, language theory, algorithms, and software engineering.
  • 8. TERMINOLOGY  Compiler: A program that converts instructions into a machine- code or lower-level form so that they can be read and executed by a computer. OR A compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name compiler is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language) to create an executable program.
  • 9. ABSTRACTVIEW  Recognizes legal (and illegal) programs  Generate correct code  Manage storage of all variables and code  Agreement on format for object (or assembly) code Source code Machine code Compiler errors
  • 10. DIFFERENCE BETWEEN COMPILER AND INTERPRETER Interpreter Compiler • Translates program one statement at a time. • Scans the entire program and translates it as a whole into machine code. • Interpreters usually take less amount of time to analyze the source code. However, the overall execution time is comparatively slower than compilers. • Compilers usually take a large amount of time to analyze the source code. However, the overall execution time is comparatively faster than interpreters. • Programming languages like JavaScript, Python, Ruby use interpreters. • Programming languages like C, C++, Java use compilers.
  • 11. FLOW OF PROGRAM EXECUTION
  • 12. SHORT HISTORY OF COMPILER
  • 13. MODEL OF A COMPILER  This model is collectively called the Analysis-Synthesis model of compilation.  The analysis part is often called the front end (FE) of the compiler.  The synthesis part is the back end (BE).
  • 14. ANALYSIS OR FRONT END OF A COMPILER  Analysis element breaks the acknowledgment program into an essential or characteristic part of something abstract. pieces and imposes a grammatical ordering on them which further uses this structure to create an intermediate version of the source program.  It is also call as front end of compiler.
  • 15. SYNTHESIS OR BACK END OF A COMPILER  Synthesis factor takes the intermediate explanation as input and transforms it to the intended program.  It is also termed as back end of compiler.
  • 16. Source Code Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator Object Code Symbol Table Manager Error Handler Synthesis Analysis Synthesis Tokens Syntax Tree Syntax Tree Intermediate Representation Intermediate Representation PHASES OF COMPILER
  • 17. LEXICAL ANALYZER  Lexical analysis is the first phase of compiler which is also termed as scanning.  Source program is scanned to read the stream of characters and those characters are grouped to form a sequence called lexemes which produces token as output.  Example c=a+b*5;  Token Template:  <token-name, attribute-value> <id1, c> <=> <id2, a> <+> <id3, b> < *> <num, 5> <; >
  • 18. SYNTAX ANALYZER  Syntax analysis is the second phase of compiler which is also called as parsing.  Parser converts the tokens gave by lexical analyzer into a tree like version called parse tree.  A parse tree describes the syntactic structure of the input.  Example: int a int int a
  • 19. SEMANTIC ANALYZER  It checks for the semantic consistency.  Type information is gathered and stored in symbol table or in syntax tree.  Performs type checking.  Example : int a; a=6; int a; a=3.142;
  • 20. INTERMEDIATE CODE GENERATOR  Intermediate code nature produces intermediate representations for the source program.  It generates three address code in which every memory location acts like register  Example : t1 = inttofloat (5) t2 = id3* t1 t3 = id2 + t2 id1 = t3
  • 21. CODE OPTIMIZER  Code optimization phase gets the intermediate code as input and produces optimized intermediate code as output.  It results in faster running machine code.  It can be done by reducing the number of lines of code for a program.
  • 23. TARGET CODE GENERATOR  Code category is the final phase of a compiler.  It gets input from code optimization phase and produces the target code as result.  Intermediate instructions are translated into a sequence of machine instructions that perform the same task.
  • 25. SYMBOLTABLE MANAGER  Important Functions:  Record Identifiers used in program  Collect info about various attributes of each identifier .  Stores:  Storage Location  Type  Scope  Procedure name  No. of type of arguments  ReturnType  Symbol table is data structure containing record for each identifiers  with fields for attributes of identifiers.  It is prepared at lexical analysis and later phases add information.
  • 26. ERROR DETECTION & REPORTING  Each phase can encounter errors.  Compilation can proceed only after solving errors generated by lexical, syntax & semantic analysis.  If code doesn’t form tokens, structure is violated which is detected by lexical analysis.  If syntax is violated, error is detected by syntax phase.  During semantic analysis, compiler tries to detect constructs that have right syntactic structure.
  • 27. SOFTWARETOOLS PERFORMINGANALYSIS  Software tools other than compilers that perform analysis on Source Program are: Structure Editors: Inputs a sequence of commands to build a source program.  The structure editor performs text creation, modification and analyzes program text for putting an appropriate hieratical structure on the source program.  E.g. Matching do-while and parenthesis { }
  • 28. CONTD…. Pretty Printers:  Analyzes a program and prints it in such a way that the structure of program becomes clearly visible.  i.e. Indentation Static Checkers:  Reads a program, analyzes it and attempts to discover potential bugs without running the program.  E.g. Parts of source program that can never be executed and using variables without being defining them.  Works on dry run techniques.
  • 29. TYPES OF COMPILER The types of compiler are  Single Pass Compilers  Two Pass Compilers  Multi-pass Compilers
  • 30. SINGLE PASS COMPILER  If we combine or group all the phases of compiler design in a single module known as single pass compiler.  A one pass/single pass compiler is that type of compiler that passes through the part of each compilation unit exactly once.  Single pass compiler is faster and smaller than the multi pass compiler.
  • 32. TWO PASS COMPILER  A two pass compiler is a type of compiler that processes the source code or abstract syntax tree of a program multiple times.  In two pass compiler we divide phases in two pass.
  • 34. FIRST PASS OFTWO PASS COMPILER  In first pass the included phases are work as front end and analytic part means all phases analyze the High level language and convert them three address code.  First pass is platform independent because the output of first pass is as three address code which is useful for every system and the requirement is to change the code optimization and code generator phase which are comes to the second pass.
  • 35. SECOND PASS OFTWO PASS COMPILER  Second Pass the included phases work as back end and the synthesis part refers to taking input as three address code and convert them into Low level language/assembly language.  Second pass is platform dependent because final stage of a typical compiler converts the intermediate representation of program into an executable set of instructions which is dependent on the system.
  • 36. MULTI PASS COMPILER  If we want to design a compiler for different programming language for same machine. In this case for each programming language there is requirement of making Front end/first pass for each of them and only one Back end/second pass.
  • 38. MULTI PASS COMPILER  If we want to design a compiler for same programming language for different machine/system. In this case we make different Back end for different Machine/system and make only one Front end for same programming language.