Compiler Construction

Sarmad Ali
Sarmad AliNetwork Engineer em SLIC
Lecture 5
Compiler Construction
The Context of a Compiler
• In addition to a compiler; several other
programs may be required to create an
executable target program.
• A source program may be divided into
modules stored in separate files.
• The task of collecting the source program is
sometimes entrusted to a distinct program
called a preprocessor.
The Context of a Compiler
• The target program created by compiler
may require further processing before it can
be run.
• The compiler creates assembly code that is
translated by an assembler into machine
code and then linked together with some
library routines into the code that actually
runs on the machine.
Preprocessors, Compilers,
Assemblers, and Linkers
Preprocessor
Compiler
Assembler
Linker
Skeletal Source Program
Source Program
Target Assembly Program
Relocatable Object Code
Absolute Machine Code
Libraries and
Relocatable Object Files
5
Analysis of the source program
Linear analysis:
• In which the stream of characters making
up the source program is read from left to
right and grouped into tokens that are
sequences of characters having a collective
meaning.
• In compiler, linear analysis is also called
LEXICAL ANALYSIS or SCANNING
Analysis of the source program
Hierarchical Analysis:
• In which characters or tokens are grouped
hierarchically into nested collections with
collective meaning.
• In compiler, hierarchical analysis is called
parsing or syntax analysis.
Analysis of the source program
Semantic analysis:
• In which certain checks are performed to
ensure that the components of a program fit
together meaningfully.
Semantic Analysis – Complications
• Handling ambiguity
– Semantic ambiguity: “I saw the Habib Bank Plaza
flying into Karachi”
Lexical Analysis
• For example in lexical analysis the characters in the
assignment statement
position := initial + rate * 60
would be grouped into the following tokens.
1. The identifier position
2. The assignment symbol :=
3. The identifier initial
4. The plus + sign
5. The identifier rate
6. The multiplication sign *
7. The number 60
Syntax Analysis
• It involves grouping the tokens of the
source program into grammatical phrases
that are tied by the compiler to synthesize
output.
• Usually the grammatical phrases of the
source program are represented by a parse
tree.
Parse tree
:=
identifier
identifier
identifier
+
*
60
Assignment
statement
position : = initial + rate * 60
position
expression
expression
expression
expression
initial
expression
rate
number
Parse tree tokens
• In expression position: = initial + rate * 60
the phrase rate * 60 is a logical unit.
• As multiplication is performed before addition.
• The expression initial + rate is followed by a *, it
is not grouped into a single phrase by itself.
Rules to identify an expression
Rule 1. Any identifier is an expression
Rule 2. Any number is an expression
Rule 3. If expression1 and expression2 are
expressions, then so are
expression1 + expression2
expression1 * expression2
Explanation of rules
• Rule (1) and (2) are (non-recursive) basic rules,
while (3) defines expression in terms of operators
applied to other expressions.
• By Rule (1), initial and rate are expressions
• By Rule (2), 60 is an expression
• By Rule (3), we can first infer that rate*60 is an
expression & finally that initial + rate * 60 is an
expression
Rule to identify a statement
• If identifier1 is an identifier, and expression2
is an expression then
identifier1 : = expression2
is a statement
Lecture 6
Context free grammar
• Lexical constructs do not require recursion,
while syntactic constructs often do.
• Context free grammars are a
formalization of recursive rules that can be
used to guide syntactic analysis.
Context free grammar
• For example recursion is not required to recognize
identifiers, which are typically strings of letters and digits
beginning with a letter.
• We would normally recognize identifiers by a simple scan
of the input stream, waiting until a character that was
neither a letter nor a digit was found
• And then grouping all the letters and digits found up to
that point into an identifier token.
• The characters so grouped are recorded in a table called a
symbol table, and remove from the input so that
processing of the next token can begin.
Phases of Compiler
Syntax Analyzer
Lexical Analyzer
Semantic Analyzer
Intermediate code generator
Code optimizer
Code generator
Target Program
Source Program
Symbol table
Manger
Error Handler
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 (where in the program it is
valid) etc
Symbol table
• A symbol table is a data structure containing a
record for each identifier, with fields for the
attributes of the identifier.
• The data structure allow 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.
Error detection and Reporting
• Each phase can encounter errors.
• After detecting an error, a phase must
somehow deal with that error, so that
compilation can proceed, allowing further
errors in the source program to be detected.
• A compiler that stops when it finds the first
error is not as helpful as it could be.
Error detection and Reporting
• The syntax and semantic analysis phases usually
handle a large fraction of the errors detectable by
the compiler.
• The lexical phase can detect errors where the
characters remaining in the input do not form any
token of the language.
• Errors where the token stream violates the
structure rules (Syntax) of the language are
determined by the syntax analysis phase.
Error detection and Reporting
• During semantic analysis the compiler tries
to detect constructs that have the right
syntactic structure but no meaning to the
operation involved.
• For example if we try to add two identifiers,
one of which is the name of an array and
the other the name of a procedure.
The Analysis Phase
position : = initial + rate * 60
• The lexical analysis phase reads the characters in
the source program and groups them into a stream
of tokens in which each token represents a
logically cohesive sequence of characters, such as
an identifier, a keyword (if, while etc), a
punctuation character or a multi-character operator
like :=
• The character sequence forming a token called the
lexeme for the token.
The Analysis Phase (Cont..)
• Certain tokens will be augmented by a
“lexical value”.
• For example when an identifier like rate is
found, the lexical analyzer not only
generates token, say id, but also enters the
lexeme rate into the symbol table, if it is not
already there.
Compiler Construction
Intermediate code generation
• After syntax and semantic analysis, some
compilers generate an explicit intermediate
representation of the source program.
• This intermediate representation has two
important properties;
– It should be easy to produce
– Easy to translate into the target machine
Intermediate code generation
• The intermediate representation can have a
variety of forms.
• It may called as “three address code”,
which is like the assembly language for a
machine in which every memory location
can act like a register.
Intermediate code generation
• Three address code consists of a sequence of
instructions, each of which has at most three
operands.
• The source program might appear in three address
code as
temp1 := inttoreal (60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3
Intermediate code generation
:=
+
*
id 1
id 2
id 3
nu
Intermediate code generation
• The intermediate form has several
properties.
• First each three address instruction has at
most one operator in addition to the
assignment.
• Thus when generating these instructions,
the compiler has to decide on the order in
which operations are to be done.
Intermediate code generation
• In our example the multiplication precedes the
addition in the source program.
• Second the compiler must generate a temporary
name to hold the value computed by each
instruction.
• Third, some “three address” instructions have
fewer than three operands for example the first
and last instruction in our example.
Code Optimization
• The code optimization
phase attempts to
improve the
intermediate code, so
the faster running
machine code will
result.
Code Optimization
temp1 := id3 * 60.0
Id1 := id2 + temp1
• There is nothing wrong with this simple algorithm; since the problem
can be fixed during the code optimization phase.
• The compiler can deduced that the conversion of 60 from integer to
real representation can be done once and for all at compile time; so the
inttoreal operation can be eliminated.
• Besides temp3 is used only once, to transmit its value to id1.
• It then becomes safe to substitute id1 for temp3, whereupon the last
statement of intermediate code is not needed and the optimized code
results.
Code Optimization
• There is a great variation in the amount of
code optimization different compiler
performs.
• In those that do the most, called “optimizing
compilers”
• A significant fraction of the time of the
compiler is spent on this phase
Code generation
• The final phase of the compiler
is the generation of target code,
consisting normally of
relocatable machine code or
assembly code.
• Memory locations are selected
for each of the variables used
by the program.
• Intermediate instructions are
each translated into a sequence
of machine instructions that
perform the same task.
• A crucial aspect is the
assignment of variable to
register.
Code generation
• The first and second
operands of each
instruction specify a
source and destination
respectively.
• The F in each
instruction tells us that
instructions deal with
floating point numbers
Code optimization & Code
generation
1 de 40

Recomendados

Compiler1 por
Compiler1Compiler1
Compiler1Natish Kumar
7.5K visualizações14 slides
Spr ch-05-compilers por
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilersVasim Pathan
6.7K visualizações41 slides
Compiler Design por
Compiler DesignCompiler Design
Compiler DesignDr. Jaydeep Patil
809 visualizações102 slides
Cpcs302 1 por
Cpcs302  1Cpcs302  1
Cpcs302 1guest5de1a5
1K visualizações44 slides
Compiler Design por
Compiler DesignCompiler Design
Compiler DesignMir Majid
46.2K visualizações18 slides
Lecture1 introduction compilers por
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilersMahesh Kumar Chelimilla
1.3K visualizações16 slides

Mais conteúdo relacionado

Mais procurados

Chapter 1 1 por
Chapter 1 1Chapter 1 1
Chapter 1 1bolovv
5.6K visualizações16 slides
phases of a compiler por
 phases of a compiler phases of a compiler
phases of a compilerMs.SHANTHI.S CSE
1.2K visualizações20 slides
Error detection recovery por
Error detection recoveryError detection recovery
Error detection recoveryTech_MX
32.1K visualizações61 slides
Different phases of a compiler por
Different phases of a compilerDifferent phases of a compiler
Different phases of a compilerSumit Sinha
18.2K visualizações4 slides
Compiler unit 1 por
Compiler unit 1Compiler unit 1
Compiler unit 1BBDITM LUCKNOW
3.5K visualizações106 slides
Compiler Construction por
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
1K visualizações39 slides

Mais procurados(20)

Chapter 1 1 por bolovv
Chapter 1 1Chapter 1 1
Chapter 1 1
bolovv5.6K visualizações
phases of a compiler por Ms.SHANTHI.S CSE
 phases of a compiler phases of a compiler
phases of a compiler
Ms.SHANTHI.S CSE1.2K visualizações
Error detection recovery por Tech_MX
Error detection recoveryError detection recovery
Error detection recovery
Tech_MX32.1K visualizações
Different phases of a compiler por Sumit Sinha
Different phases of a compilerDifferent phases of a compiler
Different phases of a compiler
Sumit Sinha18.2K visualizações
Compiler unit 1 por BBDITM LUCKNOW
Compiler unit 1Compiler unit 1
Compiler unit 1
BBDITM LUCKNOW3.5K visualizações
Compiler Construction por Ahmed Raza
Compiler ConstructionCompiler Construction
Compiler Construction
Ahmed Raza1K visualizações
Phases of compiler por Karan Deopura
Phases of compilerPhases of compiler
Phases of compiler
Karan Deopura7.6K visualizações
Compiler Design Introduction por Richa Sharma
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
Richa Sharma4.9K visualizações
Techniques & applications of Compiler por Preethi AKNR
Techniques & applications of CompilerTechniques & applications of Compiler
Techniques & applications of Compiler
Preethi AKNR13.3K visualizações
what is compiler and five phases of compiler por adilmehmood93
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
adilmehmood939.2K visualizações
Principles of compiler design por DHARANI BABU
Principles of compiler designPrinciples of compiler design
Principles of compiler design
DHARANI BABU6.5K visualizações
Cs6660 compiler design por hari2010
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
hari20102.4K visualizações
Compiler Construction introduction por Rana Ehtisham Ul Haq
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq3.5K visualizações
Lecture2 general structure of a compiler por Mahesh Kumar Chelimilla
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla2.2K visualizações
Compiler por Achyuth Dorasala
CompilerCompiler
Compiler
Achyuth Dorasala2.5K visualizações
Compiler Design(Nanthu) por guest91cc85
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
guest91cc8512.1K visualizações
Lexical analyzer por Ashwini Sonawane
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane48.3K visualizações
Phases of Compiler por A. S. M. Shafi
Phases of CompilerPhases of Compiler
Phases of Compiler
A. S. M. Shafi250 visualizações
Compiler design por sanchi29
Compiler designCompiler design
Compiler design
sanchi29119 visualizações
Compiler Chapter 1 por Huawei Technologies
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
Huawei Technologies53.3K visualizações

Destaque

Introduction to Compiler Construction por
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction Sarmad Ali
10.8K visualizações40 slides
Compiler Construction por
Compiler ConstructionCompiler Construction
Compiler ConstructionArmy Public School and College -Faisal
10.8K visualizações10 slides
Code Optimization por
Code OptimizationCode Optimization
Code Optimizationguest9f8315
48.8K visualizações46 slides
Role-of-lexical-analysis por
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysisDattatray Gandhmal
16.4K visualizações33 slides
Lecture 1 introduction to language processors por
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processorsRebaz Najeeb
1.3K visualizações33 slides
Lecture3 lexical analysis por
Lecture3 lexical analysisLecture3 lexical analysis
Lecture3 lexical analysisMahesh Kumar Chelimilla
331 visualizações12 slides

Destaque(20)

Introduction to Compiler Construction por Sarmad Ali
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
Sarmad Ali10.8K visualizações
Code Optimization por guest9f8315
Code OptimizationCode Optimization
Code Optimization
guest9f831548.8K visualizações
Role-of-lexical-analysis por Dattatray Gandhmal
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
Dattatray Gandhmal16.4K visualizações
Lecture 1 introduction to language processors por Rebaz Najeeb
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
Rebaz Najeeb1.3K visualizações
Type checking in compiler design por Sudip Singh
Type checking in compiler designType checking in compiler design
Type checking in compiler design
Sudip Singh23.4K visualizações
Lecture 02 lexical analysis por Iffat Anjum
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
Iffat Anjum5.6K visualizações
Syntax directed-translation por Junaid Lodhi
Syntax directed-translationSyntax directed-translation
Syntax directed-translation
Junaid Lodhi1.1K visualizações
Lecture 11 semantic analysis 2 por Iffat Anjum
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
Iffat Anjum6.2K visualizações
Compiler Construction Course - Introduction por Muhammad Sanaullah
Compiler Construction Course - IntroductionCompiler Construction Course - Introduction
Compiler Construction Course - Introduction
Muhammad Sanaullah2.6K visualizações
Syntax directed translation por Akshaya Arunan
Syntax directed translationSyntax directed translation
Syntax directed translation
Akshaya Arunan40.3K visualizações
Ch5a por kinnarshah8888
Ch5aCh5a
Ch5a
kinnarshah888811.8K visualizações
Phases of Compiler por Tanzeela_Hussain
Phases of CompilerPhases of Compiler
Phases of Compiler
Tanzeela_Hussain43.6K visualizações
Compilers por Bense Tony
CompilersCompilers
Compilers
Bense Tony20.3K visualizações
Phases of the Compiler - Systems Programming por Mukesh Tekwani
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems Programming
Mukesh Tekwani62K visualizações
Analysis of the source program por Huawei Technologies
Analysis of the source programAnalysis of the source program
Analysis of the source program
Huawei Technologies14.4K visualizações

Similar a Compiler Construction

The Phases of a Compiler por
The Phases of a CompilerThe Phases of a Compiler
The Phases of a CompilerRadhika Talaviya
388 visualizações23 slides
Unit 1.pptx por
Unit 1.pptxUnit 1.pptx
Unit 1.pptxNISHASOMSCS113
3 visualizações87 slides
Chapter 1.pptx por
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptxNesredinTeshome1
10 visualizações24 slides
Introduction to compiler por
Introduction to compilerIntroduction to compiler
Introduction to compilerAbha Damani
8.8K visualizações65 slides
Principles of Compiler Design por
Principles of Compiler DesignPrinciples of Compiler Design
Principles of Compiler DesignMarimuthu M
111 visualizações25 slides
1._Introduction_.pptx por
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptxAnbarasan Radhakrishnan R
5 visualizações46 slides

Similar a Compiler Construction(20)

The Phases of a Compiler por Radhika Talaviya
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
Radhika Talaviya388 visualizações
Unit 1.pptx por NISHASOMSCS113
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
NISHASOMSCS1133 visualizações
Chapter 1.pptx por NesredinTeshome1
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
NesredinTeshome110 visualizações
Introduction to compiler por Abha Damani
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani8.8K visualizações
Principles of Compiler Design por Marimuthu M
Principles of Compiler DesignPrinciples of Compiler Design
Principles of Compiler Design
Marimuthu M111 visualizações
COMPILER CONSTRUCTION KU 1.pptx por Rossy719186
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
Rossy7191864 visualizações
Concept of compiler in details por kazi_aihtesham
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
kazi_aihtesham67 visualizações
Compiler an overview por amudha arul
Compiler  an overviewCompiler  an overview
Compiler an overview
amudha arul240 visualizações
Chapter one por kiran acharya
Chapter oneChapter one
Chapter one
kiran acharya691 visualizações
Dineshmaterial1 091225091539-phpapp02 por Tirumala Rao
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
Tirumala Rao263 visualizações
Phases of Compiler.pptx por ssuser3b4934
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
ssuser3b493416 visualizações
unit1pdf__2021_12_14_12_37_34.pdf por DrIsikoIsaac
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac49 visualizações
Plc part 2 por Taymoor Nazmy
Plc  part 2Plc  part 2
Plc part 2
Taymoor Nazmy15 visualizações
Pros and cons of c as a compiler language por Ashok Raj
  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
Ashok Raj606 visualizações
Presentation1 por Zarin Tasnim
Presentation1Presentation1
Presentation1
Zarin Tasnim30 visualizações
Presentation1 por Zarin Tasnim
Presentation1Presentation1
Presentation1
Zarin Tasnim12 visualizações
Chapter1pdf__2021_11_23_10_53_20.pdf por DrIsikoIsaac
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
DrIsikoIsaac54 visualizações
Compiler Design Introduction por Thapar Institute
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
Thapar Institute 39 visualizações

Mais de Sarmad Ali

Network Engineer Interview Questions with Answers por
Network Engineer Interview Questions with Answers Network Engineer Interview Questions with Answers
Network Engineer Interview Questions with Answers Sarmad Ali
106.3K visualizações8 slides
RDBMS Model por
RDBMS ModelRDBMS Model
RDBMS ModelSarmad Ali
1.9K visualizações11 slides
RDBMS Algebra por
RDBMS AlgebraRDBMS Algebra
RDBMS AlgebraSarmad Ali
1K visualizações49 slides
RDBMS ERD por
RDBMS ERDRDBMS ERD
RDBMS ERDSarmad Ali
4.8K visualizações31 slides
RDBMS ER2 Relational por
RDBMS ER2 RelationalRDBMS ER2 Relational
RDBMS ER2 RelationalSarmad Ali
1.2K visualizações30 slides
RDBMS Arch & Models por
RDBMS Arch & ModelsRDBMS Arch & Models
RDBMS Arch & ModelsSarmad Ali
773 visualizações16 slides

Mais de Sarmad Ali(12)

Network Engineer Interview Questions with Answers por Sarmad Ali
Network Engineer Interview Questions with Answers Network Engineer Interview Questions with Answers
Network Engineer Interview Questions with Answers
Sarmad Ali106.3K visualizações
RDBMS Model por Sarmad Ali
RDBMS ModelRDBMS Model
RDBMS Model
Sarmad Ali1.9K visualizações
RDBMS Algebra por Sarmad Ali
RDBMS AlgebraRDBMS Algebra
RDBMS Algebra
Sarmad Ali1K visualizações
RDBMS ERD por Sarmad Ali
RDBMS ERDRDBMS ERD
RDBMS ERD
Sarmad Ali4.8K visualizações
RDBMS ER2 Relational por Sarmad Ali
RDBMS ER2 RelationalRDBMS ER2 Relational
RDBMS ER2 Relational
Sarmad Ali1.2K visualizações
RDBMS Arch & Models por Sarmad Ali
RDBMS Arch & ModelsRDBMS Arch & Models
RDBMS Arch & Models
Sarmad Ali773 visualizações
RDBMS ERD Examples por Sarmad Ali
RDBMS ERD ExamplesRDBMS ERD Examples
RDBMS ERD Examples
Sarmad Ali7.9K visualizações
Management Information System-MIS por Sarmad Ali
Management Information System-MISManagement Information System-MIS
Management Information System-MIS
Sarmad Ali12K visualizações
Classification of Compilers por Sarmad Ali
Classification of CompilersClassification of Compilers
Classification of Compilers
Sarmad Ali10.1K visualizações
Introduction to RDBMS por Sarmad Ali
Introduction to RDBMSIntroduction to RDBMS
Introduction to RDBMS
Sarmad Ali5.5K visualizações
About Data Base por Sarmad Ali
About Data BaseAbout Data Base
About Data Base
Sarmad Ali635 visualizações
Management information system-MIS por Sarmad Ali
Management information system-MISManagement information system-MIS
Management information system-MIS
Sarmad Ali8.8K visualizações

Último

A Guide to Applying for the Wells Mountain Initiative Scholarship 2023 por
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023A Guide to Applying for the Wells Mountain Initiative Scholarship 2023
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023Excellence Foundation for South Sudan
79 visualizações26 slides
Nelson_RecordStore.pdf por
Nelson_RecordStore.pdfNelson_RecordStore.pdf
Nelson_RecordStore.pdfBrynNelson5
46 visualizações10 slides
EILO EXCURSION PROGRAMME 2023 por
EILO EXCURSION PROGRAMME 2023EILO EXCURSION PROGRAMME 2023
EILO EXCURSION PROGRAMME 2023info33492
181 visualizações40 slides
Career Building in AI - Technologies, Trends and Opportunities por
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesWebStackAcademy
41 visualizações44 slides
MIXING OF PHARMACEUTICALS.pptx por
MIXING OF PHARMACEUTICALS.pptxMIXING OF PHARMACEUTICALS.pptx
MIXING OF PHARMACEUTICALS.pptxAnupkumar Sharma
117 visualizações35 slides
ICS3211_lecture 09_2023.pdf por
ICS3211_lecture 09_2023.pdfICS3211_lecture 09_2023.pdf
ICS3211_lecture 09_2023.pdfVanessa Camilleri
134 visualizações10 slides

Último(20)

Nelson_RecordStore.pdf por BrynNelson5
Nelson_RecordStore.pdfNelson_RecordStore.pdf
Nelson_RecordStore.pdf
BrynNelson546 visualizações
EILO EXCURSION PROGRAMME 2023 por info33492
EILO EXCURSION PROGRAMME 2023EILO EXCURSION PROGRAMME 2023
EILO EXCURSION PROGRAMME 2023
info33492181 visualizações
Career Building in AI - Technologies, Trends and Opportunities por WebStackAcademy
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy41 visualizações
MIXING OF PHARMACEUTICALS.pptx por Anupkumar Sharma
MIXING OF PHARMACEUTICALS.pptxMIXING OF PHARMACEUTICALS.pptx
MIXING OF PHARMACEUTICALS.pptx
Anupkumar Sharma117 visualizações
ICS3211_lecture 09_2023.pdf por Vanessa Camilleri
ICS3211_lecture 09_2023.pdfICS3211_lecture 09_2023.pdf
ICS3211_lecture 09_2023.pdf
Vanessa Camilleri134 visualizações
Jibachha publishing Textbook.docx por DrJibachhaSahVetphys
Jibachha publishing Textbook.docxJibachha publishing Textbook.docx
Jibachha publishing Textbook.docx
DrJibachhaSahVetphys54 visualizações
BUSINESS ETHICS MODULE 1 UNIT I_A.pdf por Dr Vijay Vishwakarma
BUSINESS ETHICS MODULE 1 UNIT I_A.pdfBUSINESS ETHICS MODULE 1 UNIT I_A.pdf
BUSINESS ETHICS MODULE 1 UNIT I_A.pdf
Dr Vijay Vishwakarma40 visualizações
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE... por Nguyen Thanh Tu Collection
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
Nguyen Thanh Tu Collection71 visualizações
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37 por MysoreMuleSoftMeetup
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
MysoreMuleSoftMeetup44 visualizações
Education of marginalized and socially disadvantages segments.pptx por GarimaBhati5
Education of marginalized and socially disadvantages segments.pptxEducation of marginalized and socially disadvantages segments.pptx
Education of marginalized and socially disadvantages segments.pptx
GarimaBhati540 visualizações
Narration lesson plan por TARIQ KHAN
Narration lesson planNarration lesson plan
Narration lesson plan
TARIQ KHAN69 visualizações
11.30.23A Poverty and Inequality in America.pptx por mary850239
11.30.23A Poverty and Inequality in America.pptx11.30.23A Poverty and Inequality in America.pptx
11.30.23A Poverty and Inequality in America.pptx
mary85023986 visualizações
Class 9 lesson plans por TARIQ KHAN
Class 9 lesson plansClass 9 lesson plans
Class 9 lesson plans
TARIQ KHAN68 visualizações
Create a Structure in VBNet.pptx por Breach_P
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptx
Breach_P82 visualizações
ANGULARJS.pdf por ArthyR3
ANGULARJS.pdfANGULARJS.pdf
ANGULARJS.pdf
ArthyR349 visualizações
MercerJesse2.1Doc.pdf por jessemercerail
MercerJesse2.1Doc.pdfMercerJesse2.1Doc.pdf
MercerJesse2.1Doc.pdf
jessemercerail301 visualizações
NodeJS and ExpressJS.pdf por ArthyR3
NodeJS and ExpressJS.pdfNodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdf
ArthyR347 visualizações

Compiler Construction

  • 3. The Context of a Compiler • In addition to a compiler; several other programs may be required to create an executable target program. • A source program may be divided into modules stored in separate files. • The task of collecting the source program is sometimes entrusted to a distinct program called a preprocessor.
  • 4. The Context of a Compiler • The target program created by compiler may require further processing before it can be run. • The compiler creates assembly code that is translated by an assembler into machine code and then linked together with some library routines into the code that actually runs on the machine.
  • 5. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source Program Target Assembly Program Relocatable Object Code Absolute Machine Code Libraries and Relocatable Object Files 5
  • 6. Analysis of the source program Linear analysis: • In which the stream of characters making up the source program is read from left to right and grouped into tokens that are sequences of characters having a collective meaning. • In compiler, linear analysis is also called LEXICAL ANALYSIS or SCANNING
  • 7. Analysis of the source program Hierarchical Analysis: • In which characters or tokens are grouped hierarchically into nested collections with collective meaning. • In compiler, hierarchical analysis is called parsing or syntax analysis.
  • 8. Analysis of the source program Semantic analysis: • In which certain checks are performed to ensure that the components of a program fit together meaningfully.
  • 9. Semantic Analysis – Complications • Handling ambiguity – Semantic ambiguity: “I saw the Habib Bank Plaza flying into Karachi”
  • 10. Lexical Analysis • For example in lexical analysis the characters in the assignment statement position := initial + rate * 60 would be grouped into the following tokens. 1. The identifier position 2. The assignment symbol := 3. The identifier initial 4. The plus + sign 5. The identifier rate 6. The multiplication sign * 7. The number 60
  • 11. Syntax Analysis • It involves grouping the tokens of the source program into grammatical phrases that are tied by the compiler to synthesize output. • Usually the grammatical phrases of the source program are represented by a parse tree.
  • 12. Parse tree := identifier identifier identifier + * 60 Assignment statement position : = initial + rate * 60 position expression expression expression expression initial expression rate number
  • 13. Parse tree tokens • In expression position: = initial + rate * 60 the phrase rate * 60 is a logical unit. • As multiplication is performed before addition. • The expression initial + rate is followed by a *, it is not grouped into a single phrase by itself.
  • 14. Rules to identify an expression Rule 1. Any identifier is an expression Rule 2. Any number is an expression Rule 3. If expression1 and expression2 are expressions, then so are expression1 + expression2 expression1 * expression2
  • 15. Explanation of rules • Rule (1) and (2) are (non-recursive) basic rules, while (3) defines expression in terms of operators applied to other expressions. • By Rule (1), initial and rate are expressions • By Rule (2), 60 is an expression • By Rule (3), we can first infer that rate*60 is an expression & finally that initial + rate * 60 is an expression
  • 16. Rule to identify a statement • If identifier1 is an identifier, and expression2 is an expression then identifier1 : = expression2 is a statement
  • 18. Context free grammar • Lexical constructs do not require recursion, while syntactic constructs often do. • Context free grammars are a formalization of recursive rules that can be used to guide syntactic analysis.
  • 19. Context free grammar • For example recursion is not required to recognize identifiers, which are typically strings of letters and digits beginning with a letter. • We would normally recognize identifiers by a simple scan of the input stream, waiting until a character that was neither a letter nor a digit was found • And then grouping all the letters and digits found up to that point into an identifier token. • The characters so grouped are recorded in a table called a symbol table, and remove from the input so that processing of the next token can begin.
  • 20. Phases of Compiler Syntax Analyzer Lexical Analyzer Semantic Analyzer Intermediate code generator Code optimizer Code generator Target Program Source Program Symbol table Manger Error Handler
  • 21. 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 (where in the program it is valid) etc
  • 22. Symbol table • A symbol table is a data structure containing a record for each identifier, with fields for the attributes of the identifier. • The data structure allow 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.
  • 23. Error detection and Reporting • Each phase can encounter errors. • After detecting an error, a phase must somehow deal with that error, so that compilation can proceed, allowing further errors in the source program to be detected. • A compiler that stops when it finds the first error is not as helpful as it could be.
  • 24. Error detection and Reporting • The syntax and semantic analysis phases usually handle a large fraction of the errors detectable by the compiler. • The lexical phase can detect errors where the characters remaining in the input do not form any token of the language. • Errors where the token stream violates the structure rules (Syntax) of the language are determined by the syntax analysis phase.
  • 25. Error detection and Reporting • During semantic analysis the compiler tries to detect constructs that have the right syntactic structure but no meaning to the operation involved. • For example if we try to add two identifiers, one of which is the name of an array and the other the name of a procedure.
  • 26. The Analysis Phase position : = initial + rate * 60 • The lexical analysis phase reads the characters in the source program and groups them into a stream of tokens in which each token represents a logically cohesive sequence of characters, such as an identifier, a keyword (if, while etc), a punctuation character or a multi-character operator like := • The character sequence forming a token called the lexeme for the token.
  • 27. The Analysis Phase (Cont..) • Certain tokens will be augmented by a “lexical value”. • For example when an identifier like rate is found, the lexical analyzer not only generates token, say id, but also enters the lexeme rate into the symbol table, if it is not already there.
  • 29. Intermediate code generation • After syntax and semantic analysis, some compilers generate an explicit intermediate representation of the source program. • This intermediate representation has two important properties; – It should be easy to produce – Easy to translate into the target machine
  • 30. Intermediate code generation • The intermediate representation can have a variety of forms. • It may called as “three address code”, which is like the assembly language for a machine in which every memory location can act like a register.
  • 31. Intermediate code generation • Three address code consists of a sequence of instructions, each of which has at most three operands. • The source program might appear in three address code as temp1 := inttoreal (60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3
  • 33. Intermediate code generation • The intermediate form has several properties. • First each three address instruction has at most one operator in addition to the assignment. • Thus when generating these instructions, the compiler has to decide on the order in which operations are to be done.
  • 34. Intermediate code generation • In our example the multiplication precedes the addition in the source program. • Second the compiler must generate a temporary name to hold the value computed by each instruction. • Third, some “three address” instructions have fewer than three operands for example the first and last instruction in our example.
  • 35. Code Optimization • The code optimization phase attempts to improve the intermediate code, so the faster running machine code will result.
  • 36. Code Optimization temp1 := id3 * 60.0 Id1 := id2 + temp1 • There is nothing wrong with this simple algorithm; since the problem can be fixed during the code optimization phase. • The compiler can deduced that the conversion of 60 from integer to real representation can be done once and for all at compile time; so the inttoreal operation can be eliminated. • Besides temp3 is used only once, to transmit its value to id1. • It then becomes safe to substitute id1 for temp3, whereupon the last statement of intermediate code is not needed and the optimized code results.
  • 37. Code Optimization • There is a great variation in the amount of code optimization different compiler performs. • In those that do the most, called “optimizing compilers” • A significant fraction of the time of the compiler is spent on this phase
  • 38. Code generation • The final phase of the compiler is the generation of target code, consisting normally of relocatable machine code or assembly code. • Memory locations are selected for each of the variables used by the program. • Intermediate instructions are each translated into a sequence of machine instructions that perform the same task. • A crucial aspect is the assignment of variable to register.
  • 39. Code generation • The first and second operands of each instruction specify a source and destination respectively. • The F in each instruction tells us that instructions deal with floating point numbers
  • 40. Code optimization & Code generation