SlideShare ist ein Scribd-Unternehmen logo
1 von 55
Jeena Thomas, Asst Professor, CSE, SJCET Palai
1
» Phases of a compiler
» Analysis and synthesis phases
Jeena Thomas, Asst Professor, CSE, SJCET Palai
2
» A compiler is a kind of translator.
TRANSLATORSoftware that accepts text in
certain language
(SOURCE LANGUAGE)
Text in another language
,preserving the meaning
of text
(TARGET/OBJECT
LANGUAGE)
Jeena Thomas, Asst Professor, CSE, SJCET Palai
3
» A translator, is a generalized form of compiler.
» When the object language is a low level language,
such a translator is called a compiler.
» This conversion process is essential for the hardware
to interpret and perform the semantics of the input
program.
» As an important part of this translation process, the
compiler reports to its user the presence of errors in
source program.
Jeena Thomas, Asst Professor, CSE, SJCET Palai
4
Jeena Thomas, Asst Professor, CSE, SJCET Palai
5
» Compiler is a program written in source language and
translates it into an equivalent target language.
Jeena Thomas, Asst Professor, CSE, SJCET Palai
6
» Source code
» a=(b+c)*(b+c)*2
Target code
MOV b,R2
ADD R2,c
MUL R2,R2
MUL R2, #2.0
MOV R2,a
7
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» FORTRAN compilers of the late 1950s
» 18 person-years to build
8
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Writing a compiler gives a student experience with large-
scale applications development. Your compiler program may
be the largest program you write as a student. Experience
working with really big data structures and complex
interactions between algorithms will help you out on your
next big programming project.
» Compiler writing is one of the shining triumphs of CS theory.
It demonstrates the value of theory over the impulse to just
"hack up" a solution.
» Compiler writing is a basic element of programming
language research. Many language researchers write
compilers for the languages they design.
» Many applications have similar properties to one or more
phases of a compiler, and compiler expertise and tools can
help an application programmer working on other projects
besides compiler
Jeena Thomas, Asst Professor, CSE, SJCET Palai
9
» Throughout the 1950’s, compilers were considered
difficult programs to write.
» The first Fortan compiler took 18 staff-years o
implement.
» Good implementation languages, programming
environments, and software tool has been
developed as the systematic techniques for
handling many of important tasks that occur
during compilation.
» With these advances, a substantial compiler can be
implemented even as a student project in a one-
semester compiler-design course.
Jeena Thomas, Asst Professor, CSE, SJCET Palai
10
» is more broadly applicable and has been
employed in rather unexpected areas.
» Text-formatting languages, preprocessor
packages
» Silicon compiler for the creation of VLSI circuits
» Command languages of OS
» Query languages of Database systems
11
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Hierarchy of operations to be maintained
-to determine the correct order of evaluation of
the expressions.
 Maintaining data type integrity
-each part of complex expression can be made of
different types.
 Compiler as prior knowledge about the nature of
user defined data types.
- struct, enum, union,
 Appropriate storage mappings for data structures
- allocation of memory for data
Jeena Thomas, Asst Professor, CSE, SJCET Palai
12
» The compiler must resolve the occurrence of
each variable name in a program to determine
the name space to which a referenced variable
belongs to.(Symbol table)
» Compiler should have facilities to handle different
control structures like ‘if-then-else’, ‘for’, ‘while’
etc. The compiler should have the facilities to
increment the loop variable and terminate the
loop.
13
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Process of compilation is highly complex, it is split
into a series of subprocesses called phases.
» A phase is a logically cohesive operation that takes
as input one representation of source program and
produces as output another representation.
» Activities of compilation split into two parts
1) Analysis part
2) Synthesis part
14
Jeena Thomas, Asst Professor, CSE, SJCET Palai
15
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Analysis of source program
» is done by the front end of compiler
» It determines meaning of source string.
» Synthesis of target program
» Is done by the back end of the compiler.
» An equivalent target string is constructed from
the output given by the front end of compiler.
16
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» In compiling, analysis has three phases:
» Linear analysis: stream of characters read from
left-to-right and grouped into tokens; known as
lexical analysis or scanning
» Hierarchical analysis: tokens grouped
hierarchically with collective meaning; known
as parsing or syntax analysis
» Semantic analysis: check if the program
components fit together meaningfully
17
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Optimization of code
» Allocation of memory
» Generation of code
18
Jeena Thomas, Asst Professor, CSE, SJCET Palai
Jeena Thomas, Asst Professor, CSE, SJCET Palai
19
20
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Performs the linear analysis on the source
program.
» It reads a stream of characters making up the
source program from left to right and groups them
into tokens.
» A token is defined as a sequence of characters that
have a collective meaning.
» For each token identified, this phase also
determines the category of the token as identifier,
constant or reserved words and its attribute that
identifies the symbol’ position in the symbol table
21
Jeena Thomas, Asst Professor, CSE, SJCET Palai
22
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Identifiers are names of variables, constants,
functions, data types, etc.
» Store information associated with identifiers
» Information associated with different types of
identifiers can be different
» Information associated with variables are
name, type, address, size (for array), etc.
» Information associated with functions are name
, type of return value, parameters, address, etc.
23
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Consider the following statement
» a=(b+c)*(b+c)*2--------------------------------(1)
24
Jeena Thomas, Asst Professor, CSE, SJCET Palai
Symbol Category Attribute
a Identifier #1
= operator Assignment(1)
b Identifier #2
+ operator Arithmetic(1)
c Identifier #3
* operator Arithmetic(2)
( operator Open parenthesis(1)
) operator Closed parenthesis(1)
2 Constant #4
25
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» This phase performs hierarchical analysis on the
source program.
» Here, the tokens are grouped into hierarchically
nested collections with collective meaning called
expressions or statements.
» It determines structure of source language.
» Represents the grammar / syntax of the language.
» These grammatical phrases are represented in the
form of parse tree.
26
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Describes the syntactic structure of input
» The terminal nodes represent the tokens and
interior nodes represent the expressions.
27
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Syntactic structures also represented using syntax
trees.
» A syntax tree is a compressed representation of
the parse tree, where the operators appear as
interior nodes and operands for this operator as
their children
28
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Syntax tree is a compressed representation of a
parse tree.
» The interior node in a syntax tree represent an
operator, whereas the interior nodes in a parse
tree represent an expression.
» The leaf node of a syntax tree represent the
operand, whereas leaf node in a parse tree
represent the tokens.
29
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Goal- is to determine the meaning of a source
string.
» It checks the source program for semantic errors
and gathers the type of information that can be
used in subsequent phases of compilation.
» Type checking for operations also performed
during this phase.
» Output- Annotated tree
30
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» It is a part of the synthesis process of the
compiler.
» The intermediate code is the representation for
an abstract machine.
» Using the intermediate code, optimization and
code generation can be performed.
31
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» It should be easily generated from semantic
representation of the source program.
» It should be easy to translate the intermediate
code to target language.
» It should be capable of holding the values
computed during translation.
» It should maintain precedence ordering of the
source language.
» It should be capable of holding the correct number
of operands of the instruction.
32
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» T1=intoreal(2)
» T2=b+c
» T3=b+c
» T4=T2*T3
» T5=T4*T1
» a=T5
33
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» The main aim of this phase is to improve on the
intermediate code to generate a code that runs
faster and/or occupies less space.
» It is used to establish trade off between
compilation speed and execution speed.
34
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» T1=inttoreal(2)
» T2=b+c
» T3=T2*T2
» T4=T3*T1
» a=T4
35
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» The main aim of this phase is to allocate storage
and generate a relocatable machine/ assembly
code.
» Memory locations and registers are allocated for
variables.
» The instructions in intermediate code format are
converted into machine instructions.
36
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» MOV R2, b
» ADD R2,c
» MUL R2,R2
» MUL R2, #2.0
» MOV R2, a
37
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» The compiler also attempts to improve the target
code generated by the code generator by choosing
proper addressing modes to improve the
performance, replacing slow instructions by fast
ones and eliminating redundant instructions.
» MUL R2, #2.0-------------------SHL(Shift Left
Instruction)
38
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» MOV b, R2
» ADD R2,c
» MUL R2,R2
» SHL R2
» MOV R2, a
39
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» 1.)Symbol Table Management
» 2.) Literal Table Management
» 3.) Error Detection and Reporting
40
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» A symbol table is a data structure that contains a
record for each identifier with fields for the
attributes of the identifier.
» This data structure has facilities to
manipulate(add/delete) the elements in it.
» The type information about the identifier is
detected during lexical analysis phase and is
entered into the symbol table.
» This information is used during the intermediate
code generation and code generation phases of
compiler to verify type information.
41
Jeena Thomas, Asst Professor, CSE, SJCET Palai
42
Jeena Thomas, Asst Professor, CSE, SJCET Palai
Address Symbol Attribute Memory Location
1 A id,real 1000
2 B id,real 1100
3 C id,real 1110
» literal table maintains the details of constants
and strings used in the program.
» It reduces the size of a program in memory by
allowing reuse of constants and strings.
» It is also needed by the code generator to
construct symbolic addresses for literals.
43
Jeena Thomas, Asst Professor, CSE, SJCET Palai
Address Literal Attribute Memory Location
4 2 const,int 1200
44
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Each phase encounters errors.
» After detecting the errors, this phase must deal
with the errors to continue with the process of
compilation.
45
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» 1. Lexical analyzer: Misspelled tokens
» 2.Syntax analyzer: syntax errors like missing
parenthesis
» 3.Intermediate code generator: Incompatible
operands for an operator
» 4. Code Optimizer: Unreachable statements
» 5. Code Generator :Memory restrictions to store a
variable. For example, when the value of an
integer variable exceeds its size.
» Symbol tables: Multiply declared identifiers
46
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Show the output of all the phases of he
compiler for the following line o code
» A[index]=4+2+index
47
Jeena Thomas, Asst Professor, CSE, SJCET Palai
48
Jeena Thomas, Asst Professor, CSE, SJCET Palai
49
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Scanner generators
» Parser generators
» Syntax-directed translation engines
» Automatic code generators
» Data-flow engines
50
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Scanner generators:
» generate lexical analyzers automatically from
the language specifications written using
regular expressions.
» It generates a finite automaton to recognize the
regular expression.
» Example-lex
51
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» parser generators
» They produce syntax analyzers from Context
Free Grammar(CFG).
» As syntax analysis phase is highly complex and
consumes manual and compilation time, these
parser generators are highly useful.
» Example-yacc
52
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Syntax-directed translation engines
» These engines have routines to traverse the
parse tree and produce intermediate code.
» The basic idea is that one or more translations
are associated with each node of parse tree.
53
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Automatic code generators
» These tools convert the intermediate language
into machine language for the target machine
using a collection of rules.
» Template matching process is used.
» An intermediate language statement is replaced
by its equivalent machine language statement
54
Jeena Thomas, Asst Professor, CSE, SJCET Palai
» Data-flow engines
» It is used in code optimization.
» These tools perform good code optimization
using “data-flow analysis” which gathers
information that flows from one part of the
program to another.
55
Jeena Thomas, Asst Professor, CSE, SJCET Palai

Weitere Àhnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Assembly language
Assembly languageAssembly language
Assembly language
 
Unit 3 sp assembler
Unit 3 sp assemblerUnit 3 sp assembler
Unit 3 sp assembler
 
Types of Compilers
Types of CompilersTypes of Compilers
Types of Compilers
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
Compilers
CompilersCompilers
Compilers
 
Assemblers
AssemblersAssemblers
Assemblers
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
 

Andere mochten auch

Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 
C:\Documents And Settings\Fredlin\Desktop\Ic Design\Synthesis200301
C:\Documents And Settings\Fredlin\Desktop\Ic Design\Synthesis200301C:\Documents And Settings\Fredlin\Desktop\Ic Design\Synthesis200301
C:\Documents And Settings\Fredlin\Desktop\Ic Design\Synthesis200301
guest77988fe
 
Cryptography & Network Security By, Er. Swapnil Kaware
Cryptography & Network Security By, Er. Swapnil KawareCryptography & Network Security By, Er. Swapnil Kaware
Cryptography & Network Security By, Er. Swapnil Kaware
Prof. Swapnil V. Kaware
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
bolovv
 
Software engineering lecture notes
Software engineering   lecture notesSoftware engineering   lecture notes
Software engineering lecture notes
Ammar Shafiq
 
Logic synthesis with synopsys design compiler
Logic synthesis with synopsys design compilerLogic synthesis with synopsys design compiler
Logic synthesis with synopsys design compiler
naeemtayyab
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
Danial Mirza
 

Andere mochten auch (20)

Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
C:\Documents And Settings\Fredlin\Desktop\Ic Design\Synthesis200301
C:\Documents And Settings\Fredlin\Desktop\Ic Design\Synthesis200301C:\Documents And Settings\Fredlin\Desktop\Ic Design\Synthesis200301
C:\Documents And Settings\Fredlin\Desktop\Ic Design\Synthesis200301
 
X Windows
X WindowsX Windows
X Windows
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Computer Networks Foundation - Study Notes
Computer Networks Foundation - Study NotesComputer Networks Foundation - Study Notes
Computer Networks Foundation - Study Notes
 
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015
Ionuț G. Stan - Let’s write a type checker at I T.A.K.E. Unconference 2015
 
Ch6
Ch6Ch6
Ch6
 
Cryptography & Network Security By, Er. Swapnil Kaware
Cryptography & Network Security By, Er. Swapnil KawareCryptography & Network Security By, Er. Swapnil Kaware
Cryptography & Network Security By, Er. Swapnil Kaware
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
MMS2401 - Multimedia system and Communication Notes
MMS2401 - Multimedia system and Communication NotesMMS2401 - Multimedia system and Communication Notes
MMS2401 - Multimedia system and Communication Notes
 
Cryptography for software engineers
Cryptography for software engineersCryptography for software engineers
Cryptography for software engineers
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Compiler Construction Course - Introduction
Compiler Construction Course - IntroductionCompiler Construction Course - Introduction
Compiler Construction Course - Introduction
 
Software engineering lecture notes
Software engineering   lecture notesSoftware engineering   lecture notes
Software engineering lecture notes
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Logic synthesis with synopsys design compiler
Logic synthesis with synopsys design compilerLogic synthesis with synopsys design compiler
Logic synthesis with synopsys design compiler
 
Computer Networks Lecture Notes
Computer Networks Lecture NotesComputer Networks Lecture Notes
Computer Networks Lecture Notes
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
 

Ähnlich wie Compiler construction

Compiler_Project_Srikanth_Vanama
Compiler_Project_Srikanth_VanamaCompiler_Project_Srikanth_Vanama
Compiler_Project_Srikanth_Vanama
Srikanth Vanama
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
arpitaeron555
 
Compier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.pptCompier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.ppt
Apoorv Diwan
 

Ähnlich wie Compiler construction (20)

Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
mt_cat_presentations CAT TRANSLATION PPT
mt_cat_presentations CAT TRANSLATION PPTmt_cat_presentations CAT TRANSLATION PPT
mt_cat_presentations CAT TRANSLATION PPT
 
Compiler_Project_Srikanth_Vanama
Compiler_Project_Srikanth_VanamaCompiler_Project_Srikanth_Vanama
Compiler_Project_Srikanth_Vanama
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
 
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
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
Language processors
Language processorsLanguage processors
Language processors
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Parsing
ParsingParsing
Parsing
 
Ss ui lecture 2
Ss ui lecture 2Ss ui lecture 2
Ss ui lecture 2
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
 
Compier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.pptCompier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.ppt
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
Presentation1
Presentation1Presentation1
Presentation1
 
Presentation1
Presentation1Presentation1
Presentation1
 

Mehr von Muhammed Afsal Villan

Mehr von Muhammed Afsal Villan (9)

Software engineering : Layered Architecture
Software engineering : Layered ArchitectureSoftware engineering : Layered Architecture
Software engineering : Layered Architecture
 
Software development process models
Software development process modelsSoftware development process models
Software development process models
 
Bluetooth - Comprehensive Presentation
Bluetooth - Comprehensive PresentationBluetooth - Comprehensive Presentation
Bluetooth - Comprehensive Presentation
 
3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals
 
Properties of Fourier transform
Properties of Fourier transformProperties of Fourier transform
Properties of Fourier transform
 
Barcodes - Types, Working and Hardware
Barcodes - Types, Working and HardwareBarcodes - Types, Working and Hardware
Barcodes - Types, Working and Hardware
 
8255 Programmable parallel I/O
8255 Programmable parallel I/O 8255 Programmable parallel I/O
8255 Programmable parallel I/O
 
Direct memory access (dma) with 8257 DMA Controller
Direct memory access (dma) with 8257 DMA ControllerDirect memory access (dma) with 8257 DMA Controller
Direct memory access (dma) with 8257 DMA Controller
 
Programmable Timer 8253/8254
Programmable Timer 8253/8254Programmable Timer 8253/8254
Programmable Timer 8253/8254
 

KĂŒrzlich hochgeladen

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

KĂŒrzlich hochgeladen (20)

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] đŸ„ Women's Abortion Clinic in T...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïžcall girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïžcall girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 

Compiler construction

  • 1. Jeena Thomas, Asst Professor, CSE, SJCET Palai 1
  • 2. » Phases of a compiler » Analysis and synthesis phases Jeena Thomas, Asst Professor, CSE, SJCET Palai 2
  • 3. » A compiler is a kind of translator. TRANSLATORSoftware that accepts text in certain language (SOURCE LANGUAGE) Text in another language ,preserving the meaning of text (TARGET/OBJECT LANGUAGE) Jeena Thomas, Asst Professor, CSE, SJCET Palai 3
  • 4. » A translator, is a generalized form of compiler. » When the object language is a low level language, such a translator is called a compiler. » This conversion process is essential for the hardware to interpret and perform the semantics of the input program. » As an important part of this translation process, the compiler reports to its user the presence of errors in source program. Jeena Thomas, Asst Professor, CSE, SJCET Palai 4
  • 5. Jeena Thomas, Asst Professor, CSE, SJCET Palai 5
  • 6. » Compiler is a program written in source language and translates it into an equivalent target language. Jeena Thomas, Asst Professor, CSE, SJCET Palai 6
  • 7. » Source code » a=(b+c)*(b+c)*2 Target code MOV b,R2 ADD R2,c MUL R2,R2 MUL R2, #2.0 MOV R2,a 7 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 8. » FORTRAN compilers of the late 1950s » 18 person-years to build 8 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 9. » Writing a compiler gives a student experience with large- scale applications development. Your compiler program may be the largest program you write as a student. Experience working with really big data structures and complex interactions between algorithms will help you out on your next big programming project. » Compiler writing is one of the shining triumphs of CS theory. It demonstrates the value of theory over the impulse to just "hack up" a solution. » Compiler writing is a basic element of programming language research. Many language researchers write compilers for the languages they design. » Many applications have similar properties to one or more phases of a compiler, and compiler expertise and tools can help an application programmer working on other projects besides compiler Jeena Thomas, Asst Professor, CSE, SJCET Palai 9
  • 10. » Throughout the 1950’s, compilers were considered difficult programs to write. » The first Fortan compiler took 18 staff-years o implement. » Good implementation languages, programming environments, and software tool has been developed as the systematic techniques for handling many of important tasks that occur during compilation. » With these advances, a substantial compiler can be implemented even as a student project in a one- semester compiler-design course. Jeena Thomas, Asst Professor, CSE, SJCET Palai 10
  • 11. » is more broadly applicable and has been employed in rather unexpected areas. » Text-formatting languages, preprocessor packages » Silicon compiler for the creation of VLSI circuits » Command languages of OS » Query languages of Database systems 11 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 12. » Hierarchy of operations to be maintained -to determine the correct order of evaluation of the expressions.  Maintaining data type integrity -each part of complex expression can be made of different types.  Compiler as prior knowledge about the nature of user defined data types. - struct, enum, union,  Appropriate storage mappings for data structures - allocation of memory for data Jeena Thomas, Asst Professor, CSE, SJCET Palai 12
  • 13. » The compiler must resolve the occurrence of each variable name in a program to determine the name space to which a referenced variable belongs to.(Symbol table) » Compiler should have facilities to handle different control structures like ‘if-then-else’, ‘for’, ‘while’ etc. The compiler should have the facilities to increment the loop variable and terminate the loop. 13 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 14. » Process of compilation is highly complex, it is split into a series of subprocesses called phases. » A phase is a logically cohesive operation that takes as input one representation of source program and produces as output another representation. » Activities of compilation split into two parts 1) Analysis part 2) Synthesis part 14 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 15. 15 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 16. » Analysis of source program » is done by the front end of compiler » It determines meaning of source string. » Synthesis of target program » Is done by the back end of the compiler. » An equivalent target string is constructed from the output given by the front end of compiler. 16 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 17. » In compiling, analysis has three phases: » Linear analysis: stream of characters read from left-to-right and grouped into tokens; known as lexical analysis or scanning » Hierarchical analysis: tokens grouped hierarchically with collective meaning; known as parsing or syntax analysis » Semantic analysis: check if the program components fit together meaningfully 17 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 18. » Optimization of code » Allocation of memory » Generation of code 18 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 19. Jeena Thomas, Asst Professor, CSE, SJCET Palai 19
  • 20. 20 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 21. » Performs the linear analysis on the source program. » It reads a stream of characters making up the source program from left to right and groups them into tokens. » A token is defined as a sequence of characters that have a collective meaning. » For each token identified, this phase also determines the category of the token as identifier, constant or reserved words and its attribute that identifies the symbol’ position in the symbol table 21 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 22. 22 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 23. » Identifiers are names of variables, constants, functions, data types, etc. » Store information associated with identifiers » Information associated with different types of identifiers can be different » Information associated with variables are name, type, address, size (for array), etc. » Information associated with functions are name , type of return value, parameters, address, etc. 23 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 24. » Consider the following statement » a=(b+c)*(b+c)*2--------------------------------(1) 24 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 25. Symbol Category Attribute a Identifier #1 = operator Assignment(1) b Identifier #2 + operator Arithmetic(1) c Identifier #3 * operator Arithmetic(2) ( operator Open parenthesis(1) ) operator Closed parenthesis(1) 2 Constant #4 25 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 26. » This phase performs hierarchical analysis on the source program. » Here, the tokens are grouped into hierarchically nested collections with collective meaning called expressions or statements. » It determines structure of source language. » Represents the grammar / syntax of the language. » These grammatical phrases are represented in the form of parse tree. 26 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 27. » Describes the syntactic structure of input » The terminal nodes represent the tokens and interior nodes represent the expressions. 27 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 28. » Syntactic structures also represented using syntax trees. » A syntax tree is a compressed representation of the parse tree, where the operators appear as interior nodes and operands for this operator as their children 28 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 29. » Syntax tree is a compressed representation of a parse tree. » The interior node in a syntax tree represent an operator, whereas the interior nodes in a parse tree represent an expression. » The leaf node of a syntax tree represent the operand, whereas leaf node in a parse tree represent the tokens. 29 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 30. » Goal- is to determine the meaning of a source string. » It checks the source program for semantic errors and gathers the type of information that can be used in subsequent phases of compilation. » Type checking for operations also performed during this phase. » Output- Annotated tree 30 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 31. » It is a part of the synthesis process of the compiler. » The intermediate code is the representation for an abstract machine. » Using the intermediate code, optimization and code generation can be performed. 31 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 32. » It should be easily generated from semantic representation of the source program. » It should be easy to translate the intermediate code to target language. » It should be capable of holding the values computed during translation. » It should maintain precedence ordering of the source language. » It should be capable of holding the correct number of operands of the instruction. 32 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 33. » T1=intoreal(2) » T2=b+c » T3=b+c » T4=T2*T3 » T5=T4*T1 » a=T5 33 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 34. » The main aim of this phase is to improve on the intermediate code to generate a code that runs faster and/or occupies less space. » It is used to establish trade off between compilation speed and execution speed. 34 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 35. » T1=inttoreal(2) » T2=b+c » T3=T2*T2 » T4=T3*T1 » a=T4 35 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 36. » The main aim of this phase is to allocate storage and generate a relocatable machine/ assembly code. » Memory locations and registers are allocated for variables. » The instructions in intermediate code format are converted into machine instructions. 36 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 37. » MOV R2, b » ADD R2,c » MUL R2,R2 » MUL R2, #2.0 » MOV R2, a 37 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 38. » The compiler also attempts to improve the target code generated by the code generator by choosing proper addressing modes to improve the performance, replacing slow instructions by fast ones and eliminating redundant instructions. » MUL R2, #2.0-------------------SHL(Shift Left Instruction) 38 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 39. » MOV b, R2 » ADD R2,c » MUL R2,R2 » SHL R2 » MOV R2, a 39 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 40. » 1.)Symbol Table Management » 2.) Literal Table Management » 3.) Error Detection and Reporting 40 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 41. » A symbol table is a data structure that contains a record for each identifier with fields for the attributes of the identifier. » This data structure has facilities to manipulate(add/delete) the elements in it. » The type information about the identifier is detected during lexical analysis phase and is entered into the symbol table. » This information is used during the intermediate code generation and code generation phases of compiler to verify type information. 41 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 42. 42 Jeena Thomas, Asst Professor, CSE, SJCET Palai Address Symbol Attribute Memory Location 1 A id,real 1000 2 B id,real 1100 3 C id,real 1110
  • 43. » literal table maintains the details of constants and strings used in the program. » It reduces the size of a program in memory by allowing reuse of constants and strings. » It is also needed by the code generator to construct symbolic addresses for literals. 43 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 44. Address Literal Attribute Memory Location 4 2 const,int 1200 44 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 45. » Each phase encounters errors. » After detecting the errors, this phase must deal with the errors to continue with the process of compilation. 45 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 46. » 1. Lexical analyzer: Misspelled tokens » 2.Syntax analyzer: syntax errors like missing parenthesis » 3.Intermediate code generator: Incompatible operands for an operator » 4. Code Optimizer: Unreachable statements » 5. Code Generator :Memory restrictions to store a variable. For example, when the value of an integer variable exceeds its size. » Symbol tables: Multiply declared identifiers 46 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 47. » Show the output of all the phases of he compiler for the following line o code » A[index]=4+2+index 47 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 48. 48 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 49. 49 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 50. » Scanner generators » Parser generators » Syntax-directed translation engines » Automatic code generators » Data-flow engines 50 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 51. » Scanner generators: » generate lexical analyzers automatically from the language specifications written using regular expressions. » It generates a finite automaton to recognize the regular expression. » Example-lex 51 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 52. » parser generators » They produce syntax analyzers from Context Free Grammar(CFG). » As syntax analysis phase is highly complex and consumes manual and compilation time, these parser generators are highly useful. » Example-yacc 52 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 53. » Syntax-directed translation engines » These engines have routines to traverse the parse tree and produce intermediate code. » The basic idea is that one or more translations are associated with each node of parse tree. 53 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 54. » Automatic code generators » These tools convert the intermediate language into machine language for the target machine using a collection of rules. » Template matching process is used. » An intermediate language statement is replaced by its equivalent machine language statement 54 Jeena Thomas, Asst Professor, CSE, SJCET Palai
  • 55. » Data-flow engines » It is used in code optimization. » These tools perform good code optimization using “data-flow analysis” which gathers information that flows from one part of the program to another. 55 Jeena Thomas, Asst Professor, CSE, SJCET Palai