ๅฐŠๆ•ฌ็š„ ๅพฎไฟกๆฑ‡็Ž‡๏ผš1ๅ†† โ‰ˆ 0.046166 ๅ…ƒ ๆ”ฏไป˜ๅฎๆฑ‡็Ž‡๏ผš1ๅ†† โ‰ˆ 0.046257ๅ…ƒ [้€€ๅ‡บ็™ปๅฝ•]
SlideShare a Scribd company logo
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

More Related Content

Similar to Chapter 1.pptx

Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
kazi_aihtesham
ย 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
ย 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
Anbarasan Radhakrishnan R
ย 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
Rossy719186
ย 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
amudha arul
ย 
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
DrIsikoIsaac
ย 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
Dr. C.V. Suresh Babu
ย 
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
DrIsikoIsaac
ย 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
Tirumala Rao
ย 
Phases of Compiler.pdf
Phases of Compiler.pdfPhases of Compiler.pdf
Phases of Compiler.pdf
SuchandaBanerjee6
ย 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
ย 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
SuchandaBanerjee6
ย 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
NISHASOMSCS113
ย 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq
ย 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
Akhil Kaushik
ย 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
Akhil Kaushik
ย 
Chap01-Intro.ppt
Chap01-Intro.pptChap01-Intro.ppt
Chap01-Intro.ppt
AhmadAbubaker16
ย 
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
gadisaAdamu
ย 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
sivaganesh293
ย 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
sivaganesh293
ย 

Similar to 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
ย 

Recently uploaded

Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
yarusun
ย 
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
biruktesfaye27
ย 
Talking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual AidsTalking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual Aids
MattVassar1
ย 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
MJDuyan
ย 
pol sci Election and Representation Class 11 Notes.pdf
pol sci Election and Representation Class 11 Notes.pdfpol sci Election and Representation Class 11 Notes.pdf
pol sci Election and Representation Class 11 Notes.pdf
BiplabHalder13
ย 
Post init hook in the odoo 17 ERP Module
Post init hook in the  odoo 17 ERP ModulePost init hook in the  odoo 17 ERP Module
Post init hook in the odoo 17 ERP Module
Celine George
ย 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
khabri85
ย 
(T.L.E.) Agriculture: "Ornamental Plants"
(T.L.E.) Agriculture: "Ornamental Plants"(T.L.E.) Agriculture: "Ornamental Plants"
(T.L.E.) Agriculture: "Ornamental Plants"
MJDuyan
ย 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
ย 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
nabaegha
ย 
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
ShwetaGawande8
ย 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Catherine Dela Cruz
ย 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
Kalna College
ย 
bryophytes.pptx bsc botany honours second semester
bryophytes.pptx bsc botany honours  second semesterbryophytes.pptx bsc botany honours  second semester
bryophytes.pptx bsc botany honours second semester
Sarojini38
ย 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
ย 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
Frederic Fovet
ย 
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT KanpurDiversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Quiz Club IIT Kanpur
ย 
What are the new features in the Fleet Odoo 17
What are the new features in the Fleet Odoo 17What are the new features in the Fleet Odoo 17
What are the new features in the Fleet Odoo 17
Celine George
ย 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
Friends of African Village Libraries
ย 
Interprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdfInterprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdf
Ben Aldrich
ย 

Recently uploaded (20)

Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
ย 
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
ย 
Talking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual AidsTalking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual Aids
ย 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
ย 
pol sci Election and Representation Class 11 Notes.pdf
pol sci Election and Representation Class 11 Notes.pdfpol sci Election and Representation Class 11 Notes.pdf
pol sci Election and Representation Class 11 Notes.pdf
ย 
Post init hook in the odoo 17 ERP Module
Post init hook in the  odoo 17 ERP ModulePost init hook in the  odoo 17 ERP Module
Post init hook in the odoo 17 ERP Module
ย 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
ย 
(T.L.E.) Agriculture: "Ornamental Plants"
(T.L.E.) Agriculture: "Ornamental Plants"(T.L.E.) Agriculture: "Ornamental Plants"
(T.L.E.) Agriculture: "Ornamental Plants"
ย 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
ย 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
ย 
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
ย 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
ย 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
ย 
bryophytes.pptx bsc botany honours second semester
bryophytes.pptx bsc botany honours  second semesterbryophytes.pptx bsc botany honours  second semester
bryophytes.pptx bsc botany honours second semester
ย 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
ย 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
ย 
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT KanpurDiversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
ย 
What are the new features in the Fleet Odoo 17
What are the new features in the Fleet Odoo 17What are the new features in the Fleet Odoo 17
What are the new features in the Fleet Odoo 17
ย 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
ย 
Interprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdfInterprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdf
ย 

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.
  ็ฟป่ฏ‘๏ผš