SlideShare a Scribd company logo
4
Most read
5
Most read
7
Most read
Compiler
By:Adil
no compiler
Not Getting (-.-)
//By:Adil
Code
Agenda of today presentation
• What is Compiler
• Brief History of compiler
• Task of compiler
• Phases of compiler
source code
Compiler
Machine code
What is Compiler
• Is a program that translates one language
to another
• Takes as input a source program typically
written in a high-level language
• Produces an equivalent target program
typically in assembly or machine language
• Reports error messages as part of the
translation process
Brief history of Compiler
• The term “compiler” was coined in the early
1950s by Grace Murray Hopper
• The first compiler of the high-level language
FORTRAN was developed between 1954 and
1957 at IBM
• The first FORTRAN compiler took 18 person-
years to create
Compiler tasks
A compiler must perform two tasks:
 analysis of source program: The analysis part breaks up the
source program into constituent pieces and imposes a
grammatical structure on them. It then uses this structure to
create an intermediate representation of the source program.
 synthesis of its corresponding program: constructs the
desired target program from the intermediate representation
and the information in the symbol table.
The analysis part is often called the front end of the compiler;
the synthesis part is the back end.
Compiler phases
• Lexical Analyzer
• Syntax Analyzer
• Semantic Analyzer
• Intermediate Code
Generator
• Code Optimizer
• Code Generation
Lexical Analysis (scanner): The
first phase of a compiler
• 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, an
• attribute-value: points to an entry in the symbol table for this token.
• Tokensrepresent basic program entities such as:
Identifiers, Literals, Reserved Words, Operators, Delimiters, etc.
Example:
1.”position” is a lexeme mapped into a token (id,
1), where id is an abstract symbol standing
for identifier and 1 points to the symbol table
entry for position. The symbol-table entry for
an identifier holds information about the
identifier, such as its name and type.
2. = is a lexeme that is mapped into the token (=).
Since this token needs no attribute-value, we
have omitted the second component. For
notational convenience, the lexeme itself is
used as the name of the abstract symbol.
3. “initial” is a lexeme that is mapped into the
token (id, 2), where 2 points to the symbol-
table entry for initial.
4. + is a lexeme that is mapped into the token (+).
5. “rate” is a lexeme mapped into the token (id,
3), where 3 points to the symbol-table entry
for rate.
6. * is a lexeme that is mapped into the token
(*) .
7. 60 is a lexeme that is mapped into the token
(60)
Blanks separating the lexemes would be discarded
by the lexical analyzer.
position = initial + 60
*
rate
Table
id 1
id 2
id 3
token lexem
Syntax Analysis (parser) : The second phase of the
compiler
• 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
token is
id1 += *id3id2 60
Syntax Analysis Example
Pay = Base + Rate* 60
 The seven tokens are grouped into a parse tree
Assignment stmt
identifier
pay
= expression
expression expression
+
identifier
base
Rate*60
Semantic Analysis: Third phase of the compiler
The semantics of a program are its meaningas opposed to syntax or structure
The semantics consist of:
Runtime semantics
behavior of program at runtime
Static semantics–checked by the compile
Static semantics include:
Static semantics–checked by the compile
Declarations of variables and constants before use
Calling functions that exist (predefined in a library or defined by the user)
Passing parameters properly
Type checking.
Annotates the syntax tree with type information
Semantic Analysis: Third phase of the compiler
The semantics of a program are its meaningas
opposed to syntax or structure
The semantics consist of:
Runtime semantics
behavior of program at runtime
Static semantics–checked by the compile
Static semantics include:
Static semantics–checked by the compile
Declarations of variables and constants before
use
Calling functions that exist (predefined in a
library or defined by the user)
Passing parameters properly
Type checking.
Annotates the syntax tree with type information
Intermediate Code Generation: three-address code
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.
Code Optimization: to generate better target
code
• 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.
• The optimizer can deduce that the conversion of 60 from integer to floating
point can be done once and for all at compile time, so the int to float
operation can be eliminated by replacing the integer 60 by the floating-point
number 60.0. Moreover, t3 is used only once
• There are simple optimizations that significantly improve the running time
of the target program without slowing down compilation too much.
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.
Translation of an assignment
statement

More Related Content

PPTX
Basic of compiler
PPT
Compiler Construction introduction
PPTX
Phases of compiler
PPT
Compiler Design Basics
PPTX
Input buffering
PPT
Compiler Design
PPT
Coding
PDF
Lecture 01 introduction to compiler
Basic of compiler
Compiler Construction introduction
Phases of compiler
Compiler Design Basics
Input buffering
Compiler Design
Coding
Lecture 01 introduction to compiler

What's hot (20)

PPTX
Error Detection & Recovery
PPT
Shell and its types in LINUX
PPTX
Syntax Analysis - LR(0) Parsing in Compiler
PDF
Basic Computer Organization and Design
PPT
OS Services, System call, Virtual Machine
PPTX
Fundamentals of c programming
PDF
Compiler Construction | Lecture 1 | What is a compiler?
PPTX
Compiler design
PPTX
The analysis synthesis model of compilation
PPTX
process control block
PPTX
Text Editor in System software
PPTX
Introduction to c programming language
PPTX
6. describing syntax and semantics
PPTX
CS304PC:Computer Organization and Architecture Session 11 general register or...
PDF
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
PPTX
Operating system 02 os as an extended machine
PPTX
Variables in C and C++ Language
PPT
Introduction to Compiler Construction
PPTX
Principles of programming
Error Detection & Recovery
Shell and its types in LINUX
Syntax Analysis - LR(0) Parsing in Compiler
Basic Computer Organization and Design
OS Services, System call, Virtual Machine
Fundamentals of c programming
Compiler Construction | Lecture 1 | What is a compiler?
Compiler design
The analysis synthesis model of compilation
process control block
Text Editor in System software
Introduction to c programming language
6. describing syntax and semantics
CS304PC:Computer Organization and Architecture Session 11 general register or...
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Operating system 02 os as an extended machine
Variables in C and C++ Language
Introduction to Compiler Construction
Principles of programming
Ad

Viewers also liked (20)

PDF
Phases of the Compiler - Systems Programming
PPTX
Phases of Compiler
PPTX
Compiler Chapter 1
PDF
Different phases of a compiler
PPT
Lexical analyzer
PPTX
Lexical analyzer
PPT
PPT
Compiler Design - Introduction to Compiler
PPT
What is Compiler?
PPTX
Compiler vs Interpreter-Compiler design ppt.
PPTX
Compilers
PPT
Lex (lexical analyzer)
DOC
Compiler Design(NANTHU NOTES)
PPTX
Code generation
PPTX
Role-of-lexical-analysis
PPTX
Phases of a Compiler
PPTX
compiler and their types
ODP
About Tokens and Lexemes
PPTX
Interpreter
PPTX
phases of compiler-analysis phase
Phases of the Compiler - Systems Programming
Phases of Compiler
Compiler Chapter 1
Different phases of a compiler
Lexical analyzer
Lexical analyzer
Compiler Design - Introduction to Compiler
What is Compiler?
Compiler vs Interpreter-Compiler design ppt.
Compilers
Lex (lexical analyzer)
Compiler Design(NANTHU NOTES)
Code generation
Role-of-lexical-analysis
Phases of a Compiler
compiler and their types
About Tokens and Lexemes
Interpreter
phases of compiler-analysis phase
Ad

Similar to what is compiler and five phases of compiler (20)

PDF
Lecture 2.1 - Phase of a Commmmpiler.pdf
PPTX
Chapter 1.pptx
PPT
Concept of compiler in details
PPT
Compiler Construction
PPTX
The Phases of a Compiler
PPTX
Compiler Design
PPTX
Compiler Construction-2 for bs computer science.pptx
PDF
unit1pdf__2021_12_14_12_37_34.pdf
PPTX
1._Introduction_.pptx
PPTX
Chapter-1.pptx compiler Design Course Material
PPT
A basic introduction to compiler design.ppt
PPT
A basic introduction to compiler design.ppt
PPTX
COMPILER CONSTRUCTION KU 1.pptx
PDF
Compilers Principles, Practice & Tools Compilers
PDF
Chapter1pdf__2021_11_23_10_53_20.pdf
PPT
Compier Design_Unit I.ppt
PPT
Compier Design_Unit I.ppt
PPTX
A Lecture of Compiler Design Subject.pptx
PPTX
Unit 1.pptx
PPTX
System software module 4 presentation file
Lecture 2.1 - Phase of a Commmmpiler.pdf
Chapter 1.pptx
Concept of compiler in details
Compiler Construction
The Phases of a Compiler
Compiler Design
Compiler Construction-2 for bs computer science.pptx
unit1pdf__2021_12_14_12_37_34.pdf
1._Introduction_.pptx
Chapter-1.pptx compiler Design Course Material
A basic introduction to compiler design.ppt
A basic introduction to compiler design.ppt
COMPILER CONSTRUCTION KU 1.pptx
Compilers Principles, Practice & Tools Compilers
Chapter1pdf__2021_11_23_10_53_20.pdf
Compier Design_Unit I.ppt
Compier Design_Unit I.ppt
A Lecture of Compiler Design Subject.pptx
Unit 1.pptx
System software module 4 presentation file

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Institutional Correction lecture only . . .
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharma ospi slides which help in ospi learning
Renaissance Architecture: A Journey from Faith to Humanism
Microbial diseases, their pathogenesis and prophylaxis
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
01-Introduction-to-Information-Management.pdf
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems
2.FourierTransform-ShortQuestionswithAnswers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Institutional Correction lecture only . . .
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

what is compiler and five phases of compiler

  • 2. no compiler Not Getting (-.-) //By:Adil Code
  • 3. Agenda of today presentation • What is Compiler • Brief History of compiler • Task of compiler • Phases of compiler source code Compiler Machine code
  • 4. What is Compiler • Is a program that translates one language to another • Takes as input a source program typically written in a high-level language • Produces an equivalent target program typically in assembly or machine language • Reports error messages as part of the translation process
  • 5. Brief history of Compiler • The term “compiler” was coined in the early 1950s by Grace Murray Hopper • The first compiler of the high-level language FORTRAN was developed between 1954 and 1957 at IBM • The first FORTRAN compiler took 18 person- years to create
  • 6. Compiler tasks A compiler must perform two tasks:  analysis of source program: The analysis part breaks up the source program into constituent pieces and imposes a grammatical structure on them. It then uses this structure to create an intermediate representation of the source program.  synthesis of its corresponding program: constructs the desired target program from the intermediate representation and the information in the symbol table. The analysis part is often called the front end of the compiler; the synthesis part is the back end.
  • 7. Compiler phases • Lexical Analyzer • Syntax Analyzer • Semantic Analyzer • Intermediate Code Generator • Code Optimizer • Code Generation
  • 8. Lexical Analysis (scanner): The first phase of a compiler • 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, an • attribute-value: points to an entry in the symbol table for this token. • Tokensrepresent basic program entities such as: Identifiers, Literals, Reserved Words, Operators, Delimiters, etc.
  • 9. Example: 1.”position” is a lexeme mapped into a token (id, 1), where id is an abstract symbol standing for identifier and 1 points to the symbol table entry for position. The symbol-table entry for an identifier holds information about the identifier, such as its name and type. 2. = is a lexeme that is mapped into the token (=). Since this token needs no attribute-value, we have omitted the second component. For notational convenience, the lexeme itself is used as the name of the abstract symbol. 3. “initial” is a lexeme that is mapped into the token (id, 2), where 2 points to the symbol- table entry for initial. 4. + is a lexeme that is mapped into the token (+). 5. “rate” is a lexeme mapped into the token (id, 3), where 3 points to the symbol-table entry for rate. 6. * is a lexeme that is mapped into the token (*) . 7. 60 is a lexeme that is mapped into the token (60) Blanks separating the lexemes would be discarded by the lexical analyzer. position = initial + 60 * rate Table id 1 id 2 id 3 token lexem
  • 10. Syntax Analysis (parser) : The second phase of the compiler • 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 token is id1 += *id3id2 60
  • 11. Syntax Analysis Example Pay = Base + Rate* 60  The seven tokens are grouped into a parse tree Assignment stmt identifier pay = expression expression expression + identifier base Rate*60
  • 12. Semantic Analysis: Third phase of the compiler The semantics of a program are its meaningas opposed to syntax or structure The semantics consist of: Runtime semantics behavior of program at runtime Static semantics–checked by the compile Static semantics include: Static semantics–checked by the compile Declarations of variables and constants before use Calling functions that exist (predefined in a library or defined by the user) Passing parameters properly Type checking. Annotates the syntax tree with type information
  • 13. Semantic Analysis: Third phase of the compiler The semantics of a program are its meaningas opposed to syntax or structure The semantics consist of: Runtime semantics behavior of program at runtime Static semantics–checked by the compile Static semantics include: Static semantics–checked by the compile Declarations of variables and constants before use Calling functions that exist (predefined in a library or defined by the user) Passing parameters properly Type checking. Annotates the syntax tree with type information
  • 14. Intermediate Code Generation: three-address code 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.
  • 15. Code Optimization: to generate better target code • 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. • The optimizer can deduce that the conversion of 60 from integer to floating point can be done once and for all at compile time, so the int to float operation can be eliminated by replacing the integer 60 by the floating-point number 60.0. Moreover, t3 is used only once • There are simple optimizations that significantly improve the running time of the target program without slowing down compilation too much.
  • 16. 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.
  • 17. Translation of an assignment statement