SlideShare uma empresa Scribd logo
1 de 27
CHAPTER ONE:
INTRODUCTION TO COMPILING
For 3rd Year Computer Science
1
WHAT IS COMPILER
 a program that reads a program written in one
language and translates it into an equivalent
program in another language.
2
COMPILER VS INTERPRETER
 Compiler: convert human readable instructions to
computer readable instructions one time.
 Interpreter: converts human instructions to machine
instructions each time the program is run.
3
Adv. Dis. Adv.
Fast Not cross platform
Source code private Requires extra compiling step
Ready to run Inflexible
Adv. Dis. Adv.
Cross platform Slower
Simple to test Public source code
Simple to debug Interpreter required
WHY COMPILER DESIGN
 Many applications for compiler technology
 Parsers for HTML in web browser
 Machine code generation for high level
languages
 Software testing
 Program optimization
 Malicious code detection
 Design of new computer architectures
4
COMPLEXITY OF COMPILER TECHNOLOGY
 Required to map a programmer’s requirements (in
a HLL program) to architectural details.
 Uses algorithms and techniques from a very large
number of areas in computer science.
 Translates intricate theory into practice - enables
tool building.
5
LANGUAGE PROCESSING SYSTEM
6
COUSINS OF THE COMPILER
 There are other programs used with the compiler,
these are:
Preprocessor
Interpreter
Assembler
Linker
Loader
Cross-Compiler
Source-to-source Compiler
7
COUSINS OF THE COMPILER (CONT…)
 Preprocessor:
 A tool that produces input for compiler and considered
as part of a compiler.
 deals with macro-processing, file inclusion,
language extension, etc.
 Assembler
 translates assembly language programs into machine
code
 output of an assembler is called an object file
(combination of machine instructions as well as the data
required to place these instructions in memory).
8
COUSINS OF THE COMPILER (CONT…)
 Linker
 A program that links and merges various object
files together in order to make an executable file.
 All these files might have been compiled by
separate assemblers.
 Search and locate referenced
module/routines in a program
 Determine the memory location where these
codes will be loaded
9
 Loader
 Part of operating system and is responsible for loading executable files
into memory and execute them.
 It calculates the size of a program (instructions and
data) and creates memory space for it.
 It initializes various registers to initiate execution.
 Cross-Compiler
 A compiler that runs on platform (A) and is capable of
generating executable code for another platform (B).
 Source-to-source Compiler
 A source-to-source compiler is a compiler that takes the
source code of one programming language and translates it
into the source code of another programming language.
10
PHASES OF A COMPILER
 The two important parts in compilation are:
 Analysis (Machine Independent/Language Dependent)
 Synthesis (Machine Dependent/Language
independent)
11
12
ANALYSIS AND SYNTHESIS
 Analysis:
 breaks up the source program into constituent pieces
and creates an intermediate representation of the
source program.
 Synthesis:
 constructs the (target) program from the intermediate
representation.
13
ANALYSIS OF THE SOURCE PROGRAM
1. Lexical / Linear Analysis (scanning)
 Scans the source code as a stream of characters
and converts it into meaningful lexemes.
 Lexical analyzer represents these lexemes in the
form of tokens as:
<token-name, attribute-value>
 Token is the basic lexical unit which is the smallest
meaningful element that a compiler understands.
 Examples of tokens are: Identifiers, Keywords,
Literals, Operators and Special symbols.
 Blanks, newlines, comments , tabulation marks will
be removed from the source program.
14
15
 Lexical analyzers can be generated automatically
from regular expression specifications.
LEX and Flex are two such tools.
 Lexical analyzer is a deterministic finite state
automaton.
2. Syntax / Hierarchical Analysis – Parsing
 Tokens are grouped hierarchically into nested
collections with collective meaning.
 The result is generally a parse tree.
 In this phase expressions, statements, declarations
etc... are identified by using the results of lexical
analysis.
 Most syntactic errors in the source program are
caught in this phase.
 Syntactic rules of the source language are given via
a Grammar.
16
 Parsers are deterministic push-down automata.
17
3. Semantic Analysis
 Certain checks are performed to make sure that the
components of the program fit together
meaningfully.
 Unlike parsing, this phase checks for semantic
errors in the source program (e.g. type mismatch)
Type checking of various programming language
constructs is one of the most important tasks.
 Stores type information in the symbol table or the
syntax tree.
Types of variables, function parameters, array
dimensions, etc.
18
19
INTERMEDIATE CODE GENERATION
 While generating machine code directly from
source code is possible, it entails two problems:
 With m languages and n target machines, we
need to write m × n compilers.
The code optimizer cannot be reused.
 By converting source code to an intermediate
code, a machine-independent code optimizer
may be written.
 Intermediate code must be easy to produce and
easy to translate to machine code:
20
21
SYNTHESIS OF THE TARGET PROGRAM
 The synthesis of the target program is
composed of two phases, which are:
1)Code Optimization, and
2)Code Generation.
 The synthesis part of the compilation
process is also called the back-end of
a compiler.
22
CODE OPTIMIZATION
 Changes the IC by removing such inefficiencies
and improves the code so that the code generator
produces a faster and less memory consuming
program.
Improvement may be time, space, or
power consumption.
 It changes the structure of programs,
sometimes of beyond recognition:
Inlines functions, unrolls loops, eliminates
some programmer-defined variables, etc. 23
24
CODE GENERATION
 Converts intermediate code to machine code.
 Each intermediate code instruction may result
in many machine instructions or vice-versa.
 Must handle all aspects of machine
architecture
Registers, pipelining, cache, multiple function
units, etc.
 Storage allocation decisions are made here
Register allocation and assignment are the most
important problems.
25
26
COMPILER CONSTRUCTION TOOLS
Various tools are used in the construction
of the various parts of a compiler.
1) Scanner generators
e.g. Lex, flex, JLex
These tools generate a scanner /lexical
analyzer given a regular expression.
2) Parser Generators
e.g. Yacc, Bison, CUP
These tools produce a parser /syntax
analyzer given a Context Free Grammar
(CFG) that describes the syntax of the
source language. 27

Mais conteúdo relacionado

Semelhante a Ch 1.pptx

Language translators
Language translatorsLanguage translators
Language translators
Aditya Sharat
 
Compilerdesignnew 091219090526-phpapp02
Compilerdesignnew 091219090526-phpapp02Compilerdesignnew 091219090526-phpapp02
Compilerdesignnew 091219090526-phpapp02
Anil Thakral
 

Semelhante a Ch 1.pptx (20)

Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
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
 
design intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdfdesign intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdf
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
WEBSITE DEVELOPMENT
WEBSITE DEVELOPMENTWEBSITE DEVELOPMENT
WEBSITE DEVELOPMENT
 
Language translators
Language translatorsLanguage translators
Language translators
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
basics of compiler design
basics of compiler designbasics of compiler design
basics of compiler design
 
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(Nanthu)
Compiler Design(Nanthu)Compiler Design(Nanthu)
Compiler Design(Nanthu)
 
Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)
 
Compilerdesignnew 091219090526-phpapp02
Compilerdesignnew 091219090526-phpapp02Compilerdesignnew 091219090526-phpapp02
Compilerdesignnew 091219090526-phpapp02
 
Chap01-Intro.ppt
Chap01-Intro.pptChap01-Intro.ppt
Chap01-Intro.ppt
 
Hm system programming class 1
Hm system programming class 1Hm system programming class 1
Hm system programming class 1
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
 
Insight into progam execution ppt
Insight into progam execution pptInsight into progam execution ppt
Insight into progam execution ppt
 
Compiler design
Compiler designCompiler design
Compiler design
 

Último

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Último (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

Ch 1.pptx

  • 1. CHAPTER ONE: INTRODUCTION TO COMPILING For 3rd Year Computer Science 1
  • 2. WHAT IS COMPILER  a program that reads a program written in one language and translates it into an equivalent program in another language. 2
  • 3. COMPILER VS INTERPRETER  Compiler: convert human readable instructions to computer readable instructions one time.  Interpreter: converts human instructions to machine instructions each time the program is run. 3 Adv. Dis. Adv. Fast Not cross platform Source code private Requires extra compiling step Ready to run Inflexible Adv. Dis. Adv. Cross platform Slower Simple to test Public source code Simple to debug Interpreter required
  • 4. WHY COMPILER DESIGN  Many applications for compiler technology  Parsers for HTML in web browser  Machine code generation for high level languages  Software testing  Program optimization  Malicious code detection  Design of new computer architectures 4
  • 5. COMPLEXITY OF COMPILER TECHNOLOGY  Required to map a programmer’s requirements (in a HLL program) to architectural details.  Uses algorithms and techniques from a very large number of areas in computer science.  Translates intricate theory into practice - enables tool building. 5
  • 7. COUSINS OF THE COMPILER  There are other programs used with the compiler, these are: Preprocessor Interpreter Assembler Linker Loader Cross-Compiler Source-to-source Compiler 7
  • 8. COUSINS OF THE COMPILER (CONT…)  Preprocessor:  A tool that produces input for compiler and considered as part of a compiler.  deals with macro-processing, file inclusion, language extension, etc.  Assembler  translates assembly language programs into machine code  output of an assembler is called an object file (combination of machine instructions as well as the data required to place these instructions in memory). 8
  • 9. COUSINS OF THE COMPILER (CONT…)  Linker  A program that links and merges various object files together in order to make an executable file.  All these files might have been compiled by separate assemblers.  Search and locate referenced module/routines in a program  Determine the memory location where these codes will be loaded 9
  • 10.  Loader  Part of operating system and is responsible for loading executable files into memory and execute them.  It calculates the size of a program (instructions and data) and creates memory space for it.  It initializes various registers to initiate execution.  Cross-Compiler  A compiler that runs on platform (A) and is capable of generating executable code for another platform (B).  Source-to-source Compiler  A source-to-source compiler is a compiler that takes the source code of one programming language and translates it into the source code of another programming language. 10
  • 11. PHASES OF A COMPILER  The two important parts in compilation are:  Analysis (Machine Independent/Language Dependent)  Synthesis (Machine Dependent/Language independent) 11
  • 12. 12
  • 13. ANALYSIS AND SYNTHESIS  Analysis:  breaks up the source program into constituent pieces and creates an intermediate representation of the source program.  Synthesis:  constructs the (target) program from the intermediate representation. 13
  • 14. ANALYSIS OF THE SOURCE PROGRAM 1. Lexical / Linear Analysis (scanning)  Scans the source code as a stream of characters and converts it into meaningful lexemes.  Lexical analyzer represents these lexemes in the form of tokens as: <token-name, attribute-value>  Token is the basic lexical unit which is the smallest meaningful element that a compiler understands.  Examples of tokens are: Identifiers, Keywords, Literals, Operators and Special symbols.  Blanks, newlines, comments , tabulation marks will be removed from the source program. 14
  • 15. 15  Lexical analyzers can be generated automatically from regular expression specifications. LEX and Flex are two such tools.  Lexical analyzer is a deterministic finite state automaton.
  • 16. 2. Syntax / Hierarchical Analysis – Parsing  Tokens are grouped hierarchically into nested collections with collective meaning.  The result is generally a parse tree.  In this phase expressions, statements, declarations etc... are identified by using the results of lexical analysis.  Most syntactic errors in the source program are caught in this phase.  Syntactic rules of the source language are given via a Grammar. 16
  • 17.  Parsers are deterministic push-down automata. 17
  • 18. 3. Semantic Analysis  Certain checks are performed to make sure that the components of the program fit together meaningfully.  Unlike parsing, this phase checks for semantic errors in the source program (e.g. type mismatch) Type checking of various programming language constructs is one of the most important tasks.  Stores type information in the symbol table or the syntax tree. Types of variables, function parameters, array dimensions, etc. 18
  • 19. 19
  • 20. INTERMEDIATE CODE GENERATION  While generating machine code directly from source code is possible, it entails two problems:  With m languages and n target machines, we need to write m × n compilers. The code optimizer cannot be reused.  By converting source code to an intermediate code, a machine-independent code optimizer may be written.  Intermediate code must be easy to produce and easy to translate to machine code: 20
  • 21. 21
  • 22. SYNTHESIS OF THE TARGET PROGRAM  The synthesis of the target program is composed of two phases, which are: 1)Code Optimization, and 2)Code Generation.  The synthesis part of the compilation process is also called the back-end of a compiler. 22
  • 23. CODE OPTIMIZATION  Changes the IC by removing such inefficiencies and improves the code so that the code generator produces a faster and less memory consuming program. Improvement may be time, space, or power consumption.  It changes the structure of programs, sometimes of beyond recognition: Inlines functions, unrolls loops, eliminates some programmer-defined variables, etc. 23
  • 24. 24
  • 25. CODE GENERATION  Converts intermediate code to machine code.  Each intermediate code instruction may result in many machine instructions or vice-versa.  Must handle all aspects of machine architecture Registers, pipelining, cache, multiple function units, etc.  Storage allocation decisions are made here Register allocation and assignment are the most important problems. 25
  • 26. 26
  • 27. COMPILER CONSTRUCTION TOOLS Various tools are used in the construction of the various parts of a compiler. 1) Scanner generators e.g. Lex, flex, JLex These tools generate a scanner /lexical analyzer given a regular expression. 2) Parser Generators e.g. Yacc, Bison, CUP These tools produce a parser /syntax analyzer given a Context Free Grammar (CFG) that describes the syntax of the source language. 27