SlideShare a Scribd company logo
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

More Related Content

PPT
1 - Introduction to Compilers.ppt
Rakesh Kumar
Ā 
PDF
Chapter#01 cc
abdulbaki3
Ā 
PPT
Compiler Construction introduction
Rana Ehtisham Ul Haq
Ā 
PPTX
Unit 1 part1 Introduction of Compiler Design.pptx
Neelkaranbind
Ā 
PDF
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
Ā 
PPTX
Pros and cons of c as a compiler language
Ashok Raj
Ā 
PPTX
unit1_cd unit1_cd unit1_cd unit1_cd unit1_cd (1).pptx
shella20221
Ā 
PDF
COMPILER DESIGN.pdf
AdiseshaK
Ā 
1 - Introduction to Compilers.ppt
Rakesh Kumar
Ā 
Chapter#01 cc
abdulbaki3
Ā 
Compiler Construction introduction
Rana Ehtisham Ul Haq
Ā 
Unit 1 part1 Introduction of Compiler Design.pptx
Neelkaranbind
Ā 
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
Ā 
Pros and cons of c as a compiler language
Ashok Raj
Ā 
unit1_cd unit1_cd unit1_cd unit1_cd unit1_cd (1).pptx
shella20221
Ā 
COMPILER DESIGN.pdf
AdiseshaK
Ā 

Similar to Ch 1.pptx (20)

PPTX
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
padmajagrandhe1
Ā 
PPTX
Chapter-1.pptx compiler Design Course Material
gadisaAdamu
Ā 
PPTX
Compiler Design Introduction With Design
rashmishekhar81
Ā 
PPTX
Chapter 1.pptx compiler design lecture note
adugnanegero
Ā 
PPTX
Phases of Compiler
Tanzeela_Hussain
Ā 
PPTX
Unit2_CD.pptx more about compilation of the day
k12196987
Ā 
PPTX
Chapter 2 Program language translation.pptx
dawod yimer
Ā 
PDF
lec00-Introduction.pdf
wigewej294
Ā 
PPTX
16 compiler-151129060845-lva1-app6892-converted.pptx
nandan543979
Ā 
PPT
Introduction to Compiler design
Dr. C.V. Suresh Babu
Ā 
PPTX
Chapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgk
Shemse Shukre
Ā 
PPT
Compiler Design Basics
Akhil Kaushik
Ā 
PPTX
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
Ā 
PDF
Phases of compiler
ahsaniftikhar19
Ā 
PPTX
Compiler
Achyuth Dorasala
Ā 
PPT
A basic introduction to compiler design.ppt
pandaashirbad9
Ā 
PPT
A basic introduction to compiler design.ppt
pandaashirbad9
Ā 
PPT
Introduction to compiler design and phases of compiler
Ranjeet Reddy
Ā 
PPTX
Introduction to Compiler Design Structutre, Phases
Vanitha Alagesan
Ā 
PPTX
Chapter 1.pptx
NesredinTeshome1
Ā 
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
padmajagrandhe1
Ā 
Chapter-1.pptx compiler Design Course Material
gadisaAdamu
Ā 
Compiler Design Introduction With Design
rashmishekhar81
Ā 
Chapter 1.pptx compiler design lecture note
adugnanegero
Ā 
Phases of Compiler
Tanzeela_Hussain
Ā 
Unit2_CD.pptx more about compilation of the day
k12196987
Ā 
Chapter 2 Program language translation.pptx
dawod yimer
Ā 
lec00-Introduction.pdf
wigewej294
Ā 
16 compiler-151129060845-lva1-app6892-converted.pptx
nandan543979
Ā 
Introduction to Compiler design
Dr. C.V. Suresh Babu
Ā 
Chapter 1 Introduction.pptxhjgjghjghjhjhjhgjmjkhgk
Shemse Shukre
Ā 
Compiler Design Basics
Akhil Kaushik
Ā 
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
Ā 
Phases of compiler
ahsaniftikhar19
Ā 
Compiler
Achyuth Dorasala
Ā 
A basic introduction to compiler design.ppt
pandaashirbad9
Ā 
A basic introduction to compiler design.ppt
pandaashirbad9
Ā 
Introduction to compiler design and phases of compiler
Ranjeet Reddy
Ā 
Introduction to Compiler Design Structutre, Phases
Vanitha Alagesan
Ā 
Chapter 1.pptx
NesredinTeshome1
Ā 
Ad

Recently uploaded (20)

PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
Ā 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
Ā 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
Ā 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
Ā 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
Ā 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
Ā 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
Ā 
PDF
BƀI Tįŗ¬P TEST Bį»” TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KƈM BƀI Tįŗ¬P NGHE - TIįŗ¾NG A...
Nguyen Thanh Tu Collection
Ā 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
Ā 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
Ā 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
Ā 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
Ā 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
Ā 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
Ā 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
Ā 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
Ā 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
Ā 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
Ā 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
Ā 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
Ā 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
Ā 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
Ā 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
Ā 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
Ā 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
Ā 
Virat Kohli- the Pride of Indian cricket
kushpar147
Ā 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
Ā 
BƀI Tįŗ¬P TEST Bį»” TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KƈM BƀI Tįŗ¬P NGHE - TIįŗ¾NG A...
Nguyen Thanh Tu Collection
Ā 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
Ā 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
Ā 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
Ā 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
Ā 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
Ā 
Review of Related Literature & Studies.pdf
Thelma Villaflores
Ā 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
Ā 
Care of patients with elImination deviation.pptx
AneetaSharma15
Ā 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
Ā 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
Ā 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
Ā 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
Ā 
Ad

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