SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Chapter one
1. Introduction Compiler
Computer Language Representation
• A computer is a logical assembly of Software and Hardware.
• The hardware understands a language, which humans cannot
understand.
• So we write programs in high-level language, which is easier
for us to understand and remember. These programs are then
fed into a series of tools and OS components to get the desired
code that can be used by the machine. This is known as
Language Processing System.
• The program written in a high-level language is known as a source
program, and the program converted into low-level language is
known as an object (or target) program.
• High-Level Language: If a program contains #define or #include
directives such as #include or #define it is called HLL. These (#)
tags are called preprocessor directives. They direct the pre-processor
about what to do.
• Pre-Processor: The pre-processor removes all the #include
directives by including the files called file inclusion and all the
#define directives using macro(A macro is a piece of code that is
given a name) expansion.
• Assembly Language: It is an intermediate state that is a
combination of machine instructions and some other useful data
needed for execution.
• Relocatable Machine Code: It can be loaded at any point and can
be run. All these files might have been compiled by separate
assemblers.
• Loader/Linker: It converts the relocatable code into absolute code
.Linker loads a variety of object files into a single file to make it
executable. Then loader loads it in memory and executes it.
Computer Language Representation
Translator:
It is a program that translates one language to another.
source code Translator target code
Types of Translator:
1.Interpreter
2.Compiler
3.Assembler
1.Interpreter:
It is one of the translators that translate high level language to low level language.
high level language Interpreter low level language
During execution, it checks line by line for errors.
Example: Basic, Lower version of Pascal.
2.Assembler:
It translates assembly level language to machine code.
assembly language Assembler machine code
Example: Microprocessor 8085, 8086.
3.Compiler:
It is a program that translates one language(source code) to another language (target
code).
source code Compiler target code
It executes the whole program and then displays the errors.
Example: C, C++, COBOL, higher version of Pascal.
PARTS OF COMPILATION
There are 2 parts to compilation:
1. Analysis
2. Synthesis
• Analysis part breaks down the source program into constituent pieces and creates
an intermediate representation of the source program.
• Synthesis part constructs the desired target program from the intermediate
representation.
PHASES OF COMPILER
A Compiler operates in phases, each of which transforms the
source program from one representation into another. The
following are the phases of the compiler:
Main phases:
1) Lexical analysis
2) Syntax analysis
3) Semantic analysis
4) Intermediate code generation
5)Code optimization
6)Code generation
Sub-Phases:
1)Symbol table management 2)Error handling
LEXICAL ANALYSIS:
• It is the first phase of the compiler. It gets input from the source program and
produces tokens as output.
• It reads the characters one by one, starting from left to right and forms the
tokens.
• Token : It represents a logically cohesive sequence of characters such as
keywords, operators, identifiers, special symbols etc.
Example: a +b =20
Here, a,b,+,=,20 are all separate tokens.
• Group of characters forming a token is called the Lexeme.
• The lexical analyzer not only generates a token but also enters the lexeme into
the symbol table if it is not already there.
SYNTAX ANALYSIS:
• It is the second phase of the compiler. It is also known as parser.
• It gets the token stream as input from the lexical analyzer of the compiler and
generates syntax tree as the output.
• Syntax tree:
• It is a tree in which interior nodes are operators and exterior nodes are operands.
Example: For a=b+c*2, syntax tree is
• It is the third phase of the compiler.
• It gets input from the syntax analysis as parse tree and checks whether the
given syntax is correct or not.
• It performs type conversion of all the data types into real data types.
INTERMEDIATE CODE GENERATION:
• It is the fourth phase of the compiler.
• It gets input from the semantic analysis and converts the input into output as
intermediate code such as three-address code.
• The three-address code consists of a sequence of instructions, each of which
has at most three operands.
• Example: t1=t2+t3
SEMANTIC ANALYSIS
CODE OPTIMIZATION
• It is the fifth phase of the compiler.
• It gets the intermediate code as input and produces optimized intermediate code as
output.
• This phase reduces the redundant code and attempts to improve the intermediate
code so that faster-running machine code will result.
• During the code optimization, the result of the program is not affected.
• To improve the code generation, the optimization involves :
- deduction and removal of dead code (unreachable code).
- calculation of constants in expressions and terms.
- collapsing of repeated expression into temporary string.
- loop unrolling.
- moving code outside the loop.
- removal of unwanted temporary variables.
CODE GENERATION
• It is the final phase of the compiler. It gets input from code optimization phase
and produces the target code or object code as result.
• Intermediate instructions are translated into a sequence of machine instructions
that perform the same task.
The code generation involves
- allocation of register and memory
- generation of correct references
- generation of correct data types
- generation of missing code
SYMBOL TABLE MANAGEMENT:
• Symbol table is used to store all the information about
identifiers used in the program.
• It is a data structure containing a record for each
identifier, with fields for the attributes of the identifier.
• It allows to find the record for each identifier quickly and
to store or retrieve data from that record.
• Whenever an identifier is detected in any of the phases, it
is stored in the symbol table.
ERROR HANDLING
• Each phase can encounter errors. After detecting an error, a phase must
handle the error so that compilation can proceed.
• In lexical analysis, errors occur in separation of tokens.
• In syntax analysis, errors occur during construction of syntax tree.
• In semantic analysis, errors occur when the compiler detects constructs
with right syntactic structure but no meaning and during type conversion.
• In code optimization, errors occur when the result is affected by the
optimization.
• In code generation, it shows error when code is missing etc.
• To illustrate the translation of source code through each phase, consider the statement
a=b+c*2. The figure shows the representation of this statement after each phase:
GROUPING OF THE PHASES
• Compiler can be grouped into front and back ends:
- Front end: analysis (machine independent)
• These normally include lexical and syntactic analysis, the
creation of the symbol table, semantic analysis and the
generation of intermediate code. It also includes error handling
that goes along with each of these phases.
- Back end: synthesis (machine dependent)
• It includes code optimization phase and code generation along
with the necessary error handling and symbol table operations.
Compiler passes
• Total number of times the compiler goes through the source
code before converting it into the target machine.
• A collection of phases is done only once (single pass) or
multiple times (multi pass)
• Single pass: a compiler that passes through the source code of
each compilation unit only once.
• Multi pass: a compiler that processes the source code or
abstract syntax tree of a program several times.
Cross compiler
• A cross compiler that runs one platform and generate target
code for other platform.
• A compiler that runs on platform (A) and is capable of
generating executable code for platform (B) is called a cross-
compiler.
• The process of creating executable code for different machines
is called retargeting.
COMPILER CONSTRUCTION TOOLS
These are specialized tools that have been developed for helping implement various
phases of a compiler. The following are the compiler construction tools:
1) Parser Generators:
-These produce syntax analyzers, normally from input that is based on a context-free
grammar.
-It consumes a large fraction of the running time of a compiler. -Example-YACC
(Yet Another Compiler-Compiler).
2) Scanner Generator:
-These generate lexical analyzers, normally from a specification based on regular
expressions. -The basic organization of lexical analyzers is based on finite automation.
3) Syntax-Directed Translation:
-These produce routines that walk the parse tree and as a result generate intermediate
code. -Each translation is defined in terms of translations at its neighbor nodes in the
tree.
4) Automatic Code Generators:
-It takes a collection of rules to translate intermediate language into machine language.
The rules must include sufficient details to handle different possible access methods for
data.
5) Data-Flow Engines:
-It does code optimization using data-flow analysis, that is, the gathering of information
about how values are transmitted from one part of a program to each other part.
Thank you

Weitere ähnliche Inhalte

Ähnlich wie Chapter 1.pptx

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

Ähnlich wie Chapter 1.pptx (20)

Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
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 an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
 
Phases of Compiler.pdf
Phases of Compiler.pdfPhases of Compiler.pdf
Phases of Compiler.pdf
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Chap01-Intro.ppt
Chap01-Intro.pptChap01-Intro.ppt
Chap01-Intro.ppt
 
Chapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialChapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course Material
 
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
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 

Chapter 1.pptx

  • 2. Computer Language Representation • A computer is a logical assembly of Software and Hardware. • The hardware understands a language, which humans cannot understand. • So we write programs in high-level language, which is easier for us to understand and remember. These programs are then fed into a series of tools and OS components to get the desired code that can be used by the machine. This is known as Language Processing System.
  • 3.
  • 4. • The program written in a high-level language is known as a source program, and the program converted into low-level language is known as an object (or target) program. • High-Level Language: If a program contains #define or #include directives such as #include or #define it is called HLL. These (#) tags are called preprocessor directives. They direct the pre-processor about what to do. • Pre-Processor: The pre-processor removes all the #include directives by including the files called file inclusion and all the #define directives using macro(A macro is a piece of code that is given a name) expansion.
  • 5. • Assembly Language: It is an intermediate state that is a combination of machine instructions and some other useful data needed for execution. • Relocatable Machine Code: It can be loaded at any point and can be run. All these files might have been compiled by separate assemblers. • Loader/Linker: It converts the relocatable code into absolute code .Linker loads a variety of object files into a single file to make it executable. Then loader loads it in memory and executes it.
  • 6. Computer Language Representation Translator: It is a program that translates one language to another. source code Translator target code Types of Translator: 1.Interpreter 2.Compiler 3.Assembler 1.Interpreter: It is one of the translators that translate high level language to low level language. high level language Interpreter low level language During execution, it checks line by line for errors. Example: Basic, Lower version of Pascal.
  • 7. 2.Assembler: It translates assembly level language to machine code. assembly language Assembler machine code Example: Microprocessor 8085, 8086. 3.Compiler: It is a program that translates one language(source code) to another language (target code). source code Compiler target code It executes the whole program and then displays the errors. Example: C, C++, COBOL, higher version of Pascal.
  • 8. PARTS OF COMPILATION There are 2 parts to compilation: 1. Analysis 2. Synthesis • Analysis part breaks down the source program into constituent pieces and creates an intermediate representation of the source program. • Synthesis part constructs the desired target program from the intermediate representation.
  • 9. PHASES OF COMPILER A Compiler operates in phases, each of which transforms the source program from one representation into another. The following are the phases of the compiler: Main phases: 1) Lexical analysis 2) Syntax analysis 3) Semantic analysis 4) Intermediate code generation 5)Code optimization 6)Code generation
  • 11. LEXICAL ANALYSIS: • It is the first phase of the compiler. It gets input from the source program and produces tokens as output. • It reads the characters one by one, starting from left to right and forms the tokens. • Token : It represents a logically cohesive sequence of characters such as keywords, operators, identifiers, special symbols etc. Example: a +b =20 Here, a,b,+,=,20 are all separate tokens. • Group of characters forming a token is called the Lexeme. • The lexical analyzer not only generates a token but also enters the lexeme into the symbol table if it is not already there.
  • 12. SYNTAX ANALYSIS: • It is the second phase of the compiler. It is also known as parser. • It gets the token stream as input from the lexical analyzer of the compiler and generates syntax tree as the output. • Syntax tree: • It is a tree in which interior nodes are operators and exterior nodes are operands. Example: For a=b+c*2, syntax tree is
  • 13. • It is the third phase of the compiler. • It gets input from the syntax analysis as parse tree and checks whether the given syntax is correct or not. • It performs type conversion of all the data types into real data types. INTERMEDIATE CODE GENERATION: • It is the fourth phase of the compiler. • It gets input from the semantic analysis and converts the input into output as intermediate code such as three-address code. • The three-address code consists of a sequence of instructions, each of which has at most three operands. • Example: t1=t2+t3 SEMANTIC ANALYSIS
  • 14. CODE OPTIMIZATION • It is the fifth phase of the compiler. • It gets the intermediate code as input and produces optimized intermediate code as output. • This phase reduces the redundant code and attempts to improve the intermediate code so that faster-running machine code will result. • During the code optimization, the result of the program is not affected. • To improve the code generation, the optimization involves : - deduction and removal of dead code (unreachable code). - calculation of constants in expressions and terms. - collapsing of repeated expression into temporary string. - loop unrolling. - moving code outside the loop. - removal of unwanted temporary variables.
  • 15. CODE GENERATION • It is the final phase of the compiler. It gets input from code optimization phase and produces the target code or object code as result. • Intermediate instructions are translated into a sequence of machine instructions that perform the same task. The code generation involves - allocation of register and memory - generation of correct references - generation of correct data types - generation of missing code
  • 16. SYMBOL TABLE MANAGEMENT: • Symbol table is used to store all the information about identifiers used in the program. • It is a data structure containing a record for each identifier, with fields for the attributes of the identifier. • It allows to find the record for each identifier quickly and to store or retrieve data from that record. • Whenever an identifier is detected in any of the phases, it is stored in the symbol table.
  • 17. ERROR HANDLING • Each phase can encounter errors. After detecting an error, a phase must handle the error so that compilation can proceed. • In lexical analysis, errors occur in separation of tokens. • In syntax analysis, errors occur during construction of syntax tree. • In semantic analysis, errors occur when the compiler detects constructs with right syntactic structure but no meaning and during type conversion. • In code optimization, errors occur when the result is affected by the optimization. • In code generation, it shows error when code is missing etc.
  • 18. • To illustrate the translation of source code through each phase, consider the statement a=b+c*2. The figure shows the representation of this statement after each phase:
  • 19. GROUPING OF THE PHASES • Compiler can be grouped into front and back ends: - Front end: analysis (machine independent) • These normally include lexical and syntactic analysis, the creation of the symbol table, semantic analysis and the generation of intermediate code. It also includes error handling that goes along with each of these phases. - Back end: synthesis (machine dependent) • It includes code optimization phase and code generation along with the necessary error handling and symbol table operations.
  • 20. Compiler passes • Total number of times the compiler goes through the source code before converting it into the target machine. • A collection of phases is done only once (single pass) or multiple times (multi pass) • Single pass: a compiler that passes through the source code of each compilation unit only once. • Multi pass: a compiler that processes the source code or abstract syntax tree of a program several times.
  • 21. Cross compiler • A cross compiler that runs one platform and generate target code for other platform. • A compiler that runs on platform (A) and is capable of generating executable code for platform (B) is called a cross- compiler. • The process of creating executable code for different machines is called retargeting.
  • 22. COMPILER CONSTRUCTION TOOLS These are specialized tools that have been developed for helping implement various phases of a compiler. The following are the compiler construction tools: 1) Parser Generators: -These produce syntax analyzers, normally from input that is based on a context-free grammar. -It consumes a large fraction of the running time of a compiler. -Example-YACC (Yet Another Compiler-Compiler). 2) Scanner Generator: -These generate lexical analyzers, normally from a specification based on regular expressions. -The basic organization of lexical analyzers is based on finite automation.
  • 23. 3) Syntax-Directed Translation: -These produce routines that walk the parse tree and as a result generate intermediate code. -Each translation is defined in terms of translations at its neighbor nodes in the tree. 4) Automatic Code Generators: -It takes a collection of rules to translate intermediate language into machine language. The rules must include sufficient details to handle different possible access methods for data. 5) Data-Flow Engines: -It does code optimization using data-flow analysis, that is, the gathering of information about how values are transmitted from one part of a program to each other part.