SlideShare a Scribd company logo
1 of 25
Download to read offline
SRI RAMAKRISHNA INSTITUTE OF TECHNOLOGY,
COIMBATORE-10
(An Autonomous Institution)
Department of Information Technology
Academic Year 2021-22 III Year 5th Semester
UITC013 - PRINCIPLES OF COMPILER
DESIGN
M. MARIMUTHU, AP/IT
What are Compilers?
• Compiler is a program which translates a
program written in one language (the source
language) to an equivalent program in other
language (the target language)
Compilers
• Translates from one representation of the
program to another
• Typically from high level source code to low
level machine code or object code.
• Source code is normally optimized for human
readability.
– Expressive: matches our notion of languages
– Redundant to help avoid programming errors
Compilers…(2)
• Machine code is optimized for hardware.
– Redundancy is reduced
– Information about the intent is lost.
Compilers vs Interpreters
Phases of a Compiler
Phases of a Compiler..
• A compiler operates in phases, each of which
transforms the source program from one
representation into another representation.
• They communicate with error handlers.
• They communicate with the symbol table.
Symbol Table Management
• An essential function of a compiler is to record
the identifiers used in the source program and
collect information about various attributes of
each identifier.
• These attributes may provide information about
the storage allocated for an identifier, its type, its
scope and in the case of procedure names, such
things as the number and types of its arguments,
the method of passing each argument and the
type returned.
Symbol Table Management…
• A symbol table is a data structure containing a
record for each identifier, with fields for the
attributes of the identifier.
• The data structure allows us to find the record
for each identifier quickly and to store or
retrieve data from that record quickly.
• When an identifier in the source program is
detected by the lexical analyzer, the identifier
is entered into the symbol table.
Symbol Table Management…
• However, the attributes of an identifier cannot
normally be determined during lexical analysis.
For example, in a Pascal declaration like.
Var a,b,c: real;
The type “real” is not known when a,b and c are
seen by the lexical analyzer.
• The remaining phases enter information about
identifiers into the symbol table and use this
information in various ways.
Error Detection and Reporting
• Each phase can encounter errors. However,
after detecting an error, a phase must deal
with that error, so that compilation can
proceed allowing further errors in the source
program to be detected.
• The lexical phase can detect errors where the
characters remaining in the input do not form
any token of the language.
Error Detection and Reporting
• Error where the token stream violates the
structure rules of the language are
determined by the syntax analysis phase.
• During semantic analysis the compiler tries to
detect constructs that have the right syntactic
structure but no meaning to the operation
involved.
Error Detection and Reporting
• All phases may encounter errors such as,
• Lexical Phase – Unable to proceed because
the next token in the source program may be
misspelled.
• Syntax Phase – Structure of the statement
violates the rules of programming language.
• Semantic – No meaning in the operation
involved.
Error Detection and Reporting
• Intermediate – Operands have incompatible
data types.
• Code Optimizer – Certain statements may
never be reached.
• Code Generation – Constant is too long.
• Symbol table – Multiple declared variables.
Lexical Analysis
• The lexical analysis phase reads the characters
in the source program and groups them into a
stream of tokens.
• Each token represents a logically cohesive
sequence of characters, such as an identifier, a
keyword, a punctuation character or a
operator, etc.
• The character sequence forming a token is
called the lexeme for the token.
Lexical Analysis
• Certain tokens will be augmented by a “lexical
value”. The lexical analyzer not only generates
a token, but also it enters the lexeme into the
symbol table.
• Eg., Consider the expression
a:= b+c*20
The representation of the above expression
after lexical analysis is,
id1 = id2 + id3 * 20
Syntax Analysis
• This phase receives the tokens generated by previous
phase (Lexical Analysis) as input and produces a
hierarchical structure called syntax or Parse tree as
output.
• It checks whether the statements are up to the syntax
of the programming language constructs or not.
• A Parse tree represents the syntactic structure of the
input. A Syntax tree is a compressed representation of
the parse tree in which the operators appear as the
interior nodes and the operands as child nodes.
Semantic Analysis
• This phase checks the source program for the
meaning of syntax tree, semantic errors and
gathers “type compatible information” for the
subsequent phases.
• Type Checking - “for each operator, the
operands are checked to find whether they
are acceptable for that operator.
• For example, character data type cannot be
used in an arithmetic expression
Intermediate Code Generation
• This phase converts the output of semantic
analyzer into an intermediate representation.
• Intermediate representation should have two
important properties:-
– It should be easy to produce,
– Easy to translate into the target program
• Some of the intermediate forms are
– Syntax Trees
– Postfix Notation
– Three address code – having at most three operands
Code Optimization
• Object code programs need to be small and
faster than the source program. Hence this
phase optimizes the intermediate
representation of the source program into an
efficient code by reducing the unnecessary
statements and operands.
Code Optimization - Examples
• Detection and removal of dead (unreachable)
code.
• Local optimization :- Elimination of common
sub expressions
• Loop optimization :- Finding out loop
invariants and avoiding them.
Code Generation
• This phase converts the optimized
intermediate code into target code which can
be either Assembly code or Machine code.
• This phase also allocates memory locations for
the variables used in the program (allocation
of registers and memory)
Principles of Compiler Design
Principles of Compiler Design
Principles of Compiler Design

More Related Content

What's hot

Chapter 11c coordination agreement
Chapter 11c coordination agreementChapter 11c coordination agreement
Chapter 11c coordination agreement
AbDul ThaYyal
 
dos mutual exclusion algos
dos mutual exclusion algosdos mutual exclusion algos
dos mutual exclusion algos
Akhil Sharma
 

What's hot (20)

Voting protocol
Voting protocolVoting protocol
Voting protocol
 
Cs556 section2
Cs556 section2Cs556 section2
Cs556 section2
 
9 fault-tolerance
9 fault-tolerance9 fault-tolerance
9 fault-tolerance
 
Fault Tolerant and Distributed System
Fault Tolerant and Distributed SystemFault Tolerant and Distributed System
Fault Tolerant and Distributed System
 
Fault tolerance
Fault toleranceFault tolerance
Fault tolerance
 
1.intro. to distributed system
1.intro. to distributed system1.intro. to distributed system
1.intro. to distributed system
 
Communications is distributed systems
Communications is distributed systemsCommunications is distributed systems
Communications is distributed systems
 
Fault tolerance in Information Centric Networks
Fault tolerance in Information Centric NetworksFault tolerance in Information Centric Networks
Fault tolerance in Information Centric Networks
 
CS6601 DISTRIBUTED SYSTEMS
CS6601 DISTRIBUTED SYSTEMSCS6601 DISTRIBUTED SYSTEMS
CS6601 DISTRIBUTED SYSTEMS
 
Distributed Systems Introduction and Importance
Distributed Systems Introduction and Importance Distributed Systems Introduction and Importance
Distributed Systems Introduction and Importance
 
Fault Tolerance 101
Fault Tolerance 101 Fault Tolerance 101
Fault Tolerance 101
 
Analysis of mutual exclusion algorithms with the significance and need of ele...
Analysis of mutual exclusion algorithms with the significance and need of ele...Analysis of mutual exclusion algorithms with the significance and need of ele...
Analysis of mutual exclusion algorithms with the significance and need of ele...
 
Chapter 11c coordination agreement
Chapter 11c coordination agreementChapter 11c coordination agreement
Chapter 11c coordination agreement
 
Message Passing, Remote Procedure Calls and Distributed Shared Memory as Com...
Message Passing, Remote Procedure Calls and  Distributed Shared Memory as Com...Message Passing, Remote Procedure Calls and  Distributed Shared Memory as Com...
Message Passing, Remote Procedure Calls and Distributed Shared Memory as Com...
 
Middleware and Middleware in distributed application
Middleware and Middleware in distributed applicationMiddleware and Middleware in distributed application
Middleware and Middleware in distributed application
 
3. challenges
3. challenges3. challenges
3. challenges
 
dos mutual exclusion algos
dos mutual exclusion algosdos mutual exclusion algos
dos mutual exclusion algos
 
Replication in Distributed Systems
Replication in Distributed SystemsReplication in Distributed Systems
Replication in Distributed Systems
 
Technical Architectures
Technical ArchitecturesTechnical Architectures
Technical Architectures
 
Basic features of distributed system
Basic features of distributed systemBasic features of distributed system
Basic features of distributed system
 

Similar to Principles of Compiler Design

Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 

Similar to Principles of Compiler Design (20)

System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation file
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
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
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
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
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Phases of Compiler.pdf
Phases of Compiler.pdfPhases of Compiler.pdf
Phases of Compiler.pdf
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
Principles of Compiler Design - Introduction
Principles of Compiler Design - IntroductionPrinciples of Compiler Design - Introduction
Principles of Compiler Design - Introduction
 
1 compiler outline
1 compiler outline1 compiler outline
1 compiler outline
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Assignment1
Assignment1Assignment1
Assignment1
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 

Recently uploaded

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Recently uploaded (20)

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 

Principles of Compiler Design

  • 1. SRI RAMAKRISHNA INSTITUTE OF TECHNOLOGY, COIMBATORE-10 (An Autonomous Institution) Department of Information Technology Academic Year 2021-22 III Year 5th Semester UITC013 - PRINCIPLES OF COMPILER DESIGN M. MARIMUTHU, AP/IT
  • 2. What are Compilers? • Compiler is a program which translates a program written in one language (the source language) to an equivalent program in other language (the target language)
  • 3. Compilers • Translates from one representation of the program to another • Typically from high level source code to low level machine code or object code. • Source code is normally optimized for human readability. – Expressive: matches our notion of languages – Redundant to help avoid programming errors
  • 4. Compilers…(2) • Machine code is optimized for hardware. – Redundancy is reduced – Information about the intent is lost.
  • 6. Phases of a Compiler
  • 7. Phases of a Compiler.. • A compiler operates in phases, each of which transforms the source program from one representation into another representation. • They communicate with error handlers. • They communicate with the symbol table.
  • 8. Symbol Table Management • An essential function of a compiler is to record the identifiers used in the source program and collect information about various attributes of each identifier. • These attributes may provide information about the storage allocated for an identifier, its type, its scope and in the case of procedure names, such things as the number and types of its arguments, the method of passing each argument and the type returned.
  • 9. Symbol Table Management… • A symbol table is a data structure containing a record for each identifier, with fields for the attributes of the identifier. • The data structure allows us to find the record for each identifier quickly and to store or retrieve data from that record quickly. • When an identifier in the source program is detected by the lexical analyzer, the identifier is entered into the symbol table.
  • 10. Symbol Table Management… • However, the attributes of an identifier cannot normally be determined during lexical analysis. For example, in a Pascal declaration like. Var a,b,c: real; The type “real” is not known when a,b and c are seen by the lexical analyzer. • The remaining phases enter information about identifiers into the symbol table and use this information in various ways.
  • 11. Error Detection and Reporting • Each phase can encounter errors. However, after detecting an error, a phase must deal with that error, so that compilation can proceed allowing further errors in the source program to be detected. • The lexical phase can detect errors where the characters remaining in the input do not form any token of the language.
  • 12. Error Detection and Reporting • Error where the token stream violates the structure rules of the language are determined by the syntax analysis phase. • During semantic analysis the compiler tries to detect constructs that have the right syntactic structure but no meaning to the operation involved.
  • 13. Error Detection and Reporting • All phases may encounter errors such as, • Lexical Phase – Unable to proceed because the next token in the source program may be misspelled. • Syntax Phase – Structure of the statement violates the rules of programming language. • Semantic – No meaning in the operation involved.
  • 14. Error Detection and Reporting • Intermediate – Operands have incompatible data types. • Code Optimizer – Certain statements may never be reached. • Code Generation – Constant is too long. • Symbol table – Multiple declared variables.
  • 15. Lexical Analysis • The lexical analysis phase reads the characters in the source program and groups them into a stream of tokens. • Each token represents a logically cohesive sequence of characters, such as an identifier, a keyword, a punctuation character or a operator, etc. • The character sequence forming a token is called the lexeme for the token.
  • 16. Lexical Analysis • Certain tokens will be augmented by a “lexical value”. The lexical analyzer not only generates a token, but also it enters the lexeme into the symbol table. • Eg., Consider the expression a:= b+c*20 The representation of the above expression after lexical analysis is, id1 = id2 + id3 * 20
  • 17. Syntax Analysis • This phase receives the tokens generated by previous phase (Lexical Analysis) as input and produces a hierarchical structure called syntax or Parse tree as output. • It checks whether the statements are up to the syntax of the programming language constructs or not. • A Parse tree represents the syntactic structure of the input. A Syntax tree is a compressed representation of the parse tree in which the operators appear as the interior nodes and the operands as child nodes.
  • 18. Semantic Analysis • This phase checks the source program for the meaning of syntax tree, semantic errors and gathers “type compatible information” for the subsequent phases. • Type Checking - “for each operator, the operands are checked to find whether they are acceptable for that operator. • For example, character data type cannot be used in an arithmetic expression
  • 19. Intermediate Code Generation • This phase converts the output of semantic analyzer into an intermediate representation. • Intermediate representation should have two important properties:- – It should be easy to produce, – Easy to translate into the target program • Some of the intermediate forms are – Syntax Trees – Postfix Notation – Three address code – having at most three operands
  • 20. Code Optimization • Object code programs need to be small and faster than the source program. Hence this phase optimizes the intermediate representation of the source program into an efficient code by reducing the unnecessary statements and operands.
  • 21. Code Optimization - Examples • Detection and removal of dead (unreachable) code. • Local optimization :- Elimination of common sub expressions • Loop optimization :- Finding out loop invariants and avoiding them.
  • 22. Code Generation • This phase converts the optimized intermediate code into target code which can be either Assembly code or Machine code. • This phase also allocates memory locations for the variables used in the program (allocation of registers and memory)