SlideShare a Scribd company logo
Name- Suchanda Banerjee
Roll No.- 10800120085
Batch- B
Sem- 5th
Asansol Engineering College
PHASES OF COMPILER
PCC – CS 501
Compiler Design
Compiler
A compiler is a large program that can read in one language
the source language and translate it into an equivalent
program in another language the target language. It
converts the program written in a high level
language(Source Language) to a low level language(Target
language).
Phases of Compiler Design
Compiler operates in various phases each phase transforms the source
program from one representation to another. Every phase takes inputs
from its previous stage and feeds its output to the next phase of the
compiler.
There are 6 phases in a compiler. Each of this phase help in converting
the high-level langue the machine code. The phases of a compiler are:
• Lexical analysis
• Syntax analysis
• Semantic analysis
• Intermediate code generator
• Code optimizer
• Code generator
Lexical
Analyzer
Syntax
Analyzer
Semantic
Analyzer
Intermediate
Code
Generator
Code
Optimizer
Code
Generator
High
Level
Language
Assembly
Code
Symbol
Table
Error
Handling
Phase – 1: Lexical Analyzer
It is also called a scanner. It takes the output of the preprocessor (which
performs file inclusion and macro expansion) as the input which is in a pure
high-level language. It reads the characters from the source program and groups
them into lexemes (sequence of characters that “go together”). Each lexeme
corresponds to a token. Tokens are defined by regular expressions which are
understood by the lexical analyzer. It also removes lexical errors (e.g., erroneous
characters), comments, and white space.
The primary functions of this phase are:
• Identify the lexical units in a source code.
• Classify lexical units into classes like constants, reserved words, and enter
them in different tables. It will Ignore comments in the source program.
• Identify token which is not a part of the language.
Phase – 2: Syntax Analyzer
Syntax analysis is all about discovering structure in code. It determines whether
or not a text follows the expected format. The main aim of this phase is to
make sure that the source code was written by the programmer is correct or
not.
Syntax analysis is based on the rules based on the specific programing language
by constructing the parse tree with the help of tokens. It also determines the
structure of source language and grammar or syntax of the language.
The primary functions of this phase are:
• Obtain tokens from the lexical analyzer.
• Checks if the expression is syntactically correct or not.
• Report all syntax errors.
• Construct a hierarchical structure which is known as a parse tree.
Phase – 3: Semantic Analyzer
Semantic analysis checks the semantic consistency of the code. It uses the
syntax tree of the previous phase along with the symbol table to verify that
the given source code is semantically consistent. It also checks whether the code
is conveying an appropriate meaning.
Semantic Analyzer will check for Type mismatches, incompatible operands, a
function called with improper arguments, an undeclared variable, etc.
Functions of Semantic analyses phase are:
• Helps you to store type information gathered and save it in symbol table or
syntax tree.
• Allows you to perform type checking.
• In the case of type mismatch, where there are no exact type correction rules
which satisfy the desired operation a semantic error is shown.
• Collects type information and checks for type compatibility.
• Checks if the source language permits the operands or not.
Phase – 4: Intermediate Code Generator
It generates intermediate code, which is a form that can be readily executed by
a machine We have many popular intermediate codes. Example – Three address
codes etc. Intermediate code is converted to machine language using the last
two phases which are platform dependent.
Till intermediate code, it is the same for every compiler out there, but after
that, it depends on the platform. To build a new compiler we don’t need to
build it from scratch. We can take the intermediate code from the already
existing compiler and build the last two parts.
Functions on Intermediate Code generation:
• It should be generated from the semantic representation of the source
program.
• Holds the values computed during the process of translation.
• Helps you to translate the intermediate code into target language.
• Allows you to maintain precedence ordering of the source language.
• It holds the correct number of operands of the instruction.
Phase – 5: Code Optimizer
It transforms the code so that it consumes fewer resources and produces more
speed. The meaning of the code being transformed is not altered. Optimization
can be categorized into two types: machine-dependent and machine-independent.
The primary functions of this phase are:
• It helps you to establish a trade-off between execution and compilation speed.
• Improves the running time of the target program.
• Generates streamlined code still in intermediate representation.
• Removing unreachable code and getting rid of unused variables.
• Removing statements which are not altered from the loop.
Phase – 6: Code Generator
The main purpose of the Target Code generator is to write a code that the
machine can understand and also register allocation, instruction selection, etc. The
output is dependent on the type of assembler. This is the final stage of
compilation. The optimized code is converted into relocatable machine code which
then forms the input to the linker and loader.

More Related Content

Similar to Phases of Compiler.pptx

Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 
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
 

Similar to Phases of Compiler.pptx (20)

Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Principles of Compiler Design
Principles of Compiler DesignPrinciples of Compiler Design
Principles of Compiler Design
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
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
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
 
phase of compiler
phase of compilerphase of compiler
phase of compiler
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
PCSG_Computer_Science_Unit_1_Lecture_2.pptx
PCSG_Computer_Science_Unit_1_Lecture_2.pptxPCSG_Computer_Science_Unit_1_Lecture_2.pptx
PCSG_Computer_Science_Unit_1_Lecture_2.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
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
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 construction tool in computer science .
compiler construction tool in computer science .compiler construction tool in computer science .
compiler construction tool in computer science .
 

Recently uploaded

Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 

Recently uploaded (20)

Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 

Phases of Compiler.pptx

  • 1. Name- Suchanda Banerjee Roll No.- 10800120085 Batch- B Sem- 5th Asansol Engineering College
  • 2. PHASES OF COMPILER PCC – CS 501 Compiler Design
  • 3. Compiler A compiler is a large program that can read in one language the source language and translate it into an equivalent program in another language the target language. It converts the program written in a high level language(Source Language) to a low level language(Target language).
  • 4. Phases of Compiler Design Compiler operates in various phases each phase transforms the source program from one representation to another. Every phase takes inputs from its previous stage and feeds its output to the next phase of the compiler. There are 6 phases in a compiler. Each of this phase help in converting the high-level langue the machine code. The phases of a compiler are: • Lexical analysis • Syntax analysis • Semantic analysis • Intermediate code generator • Code optimizer • Code generator
  • 6. Phase – 1: Lexical Analyzer It is also called a scanner. It takes the output of the preprocessor (which performs file inclusion and macro expansion) as the input which is in a pure high-level language. It reads the characters from the source program and groups them into lexemes (sequence of characters that “go together”). Each lexeme corresponds to a token. Tokens are defined by regular expressions which are understood by the lexical analyzer. It also removes lexical errors (e.g., erroneous characters), comments, and white space. The primary functions of this phase are: • Identify the lexical units in a source code. • Classify lexical units into classes like constants, reserved words, and enter them in different tables. It will Ignore comments in the source program. • Identify token which is not a part of the language.
  • 7. Phase – 2: Syntax Analyzer Syntax analysis is all about discovering structure in code. It determines whether or not a text follows the expected format. The main aim of this phase is to make sure that the source code was written by the programmer is correct or not. Syntax analysis is based on the rules based on the specific programing language by constructing the parse tree with the help of tokens. It also determines the structure of source language and grammar or syntax of the language. The primary functions of this phase are: • Obtain tokens from the lexical analyzer. • Checks if the expression is syntactically correct or not. • Report all syntax errors. • Construct a hierarchical structure which is known as a parse tree.
  • 8. Phase – 3: Semantic Analyzer Semantic analysis checks the semantic consistency of the code. It uses the syntax tree of the previous phase along with the symbol table to verify that the given source code is semantically consistent. It also checks whether the code is conveying an appropriate meaning. Semantic Analyzer will check for Type mismatches, incompatible operands, a function called with improper arguments, an undeclared variable, etc. Functions of Semantic analyses phase are: • Helps you to store type information gathered and save it in symbol table or syntax tree. • Allows you to perform type checking. • In the case of type mismatch, where there are no exact type correction rules which satisfy the desired operation a semantic error is shown. • Collects type information and checks for type compatibility. • Checks if the source language permits the operands or not.
  • 9. Phase – 4: Intermediate Code Generator It generates intermediate code, which is a form that can be readily executed by a machine We have many popular intermediate codes. Example – Three address codes etc. Intermediate code is converted to machine language using the last two phases which are platform dependent. Till intermediate code, it is the same for every compiler out there, but after that, it depends on the platform. To build a new compiler we don’t need to build it from scratch. We can take the intermediate code from the already existing compiler and build the last two parts. Functions on Intermediate Code generation: • It should be generated from the semantic representation of the source program. • Holds the values computed during the process of translation. • Helps you to translate the intermediate code into target language. • Allows you to maintain precedence ordering of the source language. • It holds the correct number of operands of the instruction.
  • 10. Phase – 5: Code Optimizer It transforms the code so that it consumes fewer resources and produces more speed. The meaning of the code being transformed is not altered. Optimization can be categorized into two types: machine-dependent and machine-independent. The primary functions of this phase are: • It helps you to establish a trade-off between execution and compilation speed. • Improves the running time of the target program. • Generates streamlined code still in intermediate representation. • Removing unreachable code and getting rid of unused variables. • Removing statements which are not altered from the loop.
  • 11. Phase – 6: Code Generator The main purpose of the Target Code generator is to write a code that the machine can understand and also register allocation, instruction selection, etc. The output is dependent on the type of assembler. This is the final stage of compilation. The optimized code is converted into relocatable machine code which then forms the input to the linker and loader.