SlideShare uma empresa Scribd logo
1 de 25
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados

Cs556 section2
Cs556 section2Cs556 section2
Cs556 section2farshad33
 
9 fault-tolerance
9 fault-tolerance9 fault-tolerance
9 fault-tolerance4020132038
 
Fault Tolerant and Distributed System
Fault Tolerant and Distributed SystemFault Tolerant and Distributed System
Fault Tolerant and Distributed Systemsreenivas1591
 
Communications is distributed systems
Communications is distributed systemsCommunications is distributed systems
Communications is distributed systemsSHATHAN
 
Fault tolerance in Information Centric Networks
Fault tolerance in Information Centric NetworksFault tolerance in Information Centric Networks
Fault tolerance in Information Centric NetworksNitinder Mohan
 
Distributed Systems Introduction and Importance
Distributed Systems Introduction and Importance Distributed Systems Introduction and Importance
Distributed Systems Introduction and Importance SHIKHA GAUTAM
 
Fault Tolerance 101
Fault Tolerance 101 Fault Tolerance 101
Fault Tolerance 101 C4Media
 
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...Govt. P.G. College Dharamshala
 
Chapter 11c coordination agreement
Chapter 11c coordination agreementChapter 11c coordination agreement
Chapter 11c coordination agreementAbDul ThaYyal
 
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...Sehrish Asif
 
Middleware and Middleware in distributed application
Middleware and Middleware in distributed applicationMiddleware and Middleware in distributed application
Middleware and Middleware in distributed applicationRishikese MR
 
dos mutual exclusion algos
dos mutual exclusion algosdos mutual exclusion algos
dos mutual exclusion algosAkhil Sharma
 
Basic features of distributed system
Basic features of distributed systemBasic features of distributed system
Basic features of distributed systemsatish raj
 

Mais procurados (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
 

Semelhante a Principles of Compiler Design

System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation filejithujithin657
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.pptsivaganesh293
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.pptsivaganesh293
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxRossy719186
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionSarmad Ali
 
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 compileradilmehmood93
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compilerAbha Damani
 
Principles of Compiler Design - Introduction
Principles of Compiler Design - IntroductionPrinciples of Compiler Design - Introduction
Principles of Compiler Design - Introductionsheelarajasekar205
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction Sarmad Ali
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 

Semelhante a 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
 

Último

Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesRAJNEESHKUMAR341697
 
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.pptNANDHAKUMARA10
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEselvakumar948
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
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 ptxJIT KUMAR GUPTA
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 

Último (20)

Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
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
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
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
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 

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)