SlideShare a Scribd company logo
: Introduction 1
Introduction
2
What is a Compiler?
• A compiler is a computer
program that translates a
program in a source language
into an equivalent program in a
target language.
• A source program/code is a
program/code written in the
source language, which is
usually a high-level language.
• A target program/code is a
program/code written in the
target language, which often is a
machine language or an
intermediate code.
compiler
Source
program
Target
program
Error
message
3
Process of Compiling
scanner
parser
Semantic analyzer
Intermediate code generator
Code optimization
Code generator
Code optimization
Stream of characters
Stream of tokens
Parse/syntax tree
Annotated tree
Intermediate code
Intermediate code
Target code
Target code
4
Some Data Structures
• Symbol table
• Literal table
• Parse tree
5
Symbol Table
• Identifiers are names of variables,
constants, functions, data types, etc.
• Store information associated with identifiers
– Information associated with different types of
identifiers can be different
• Information associated with variables are name, type,
address,size (for array), etc.
• Information associated with functions are name,type
of return value, parameters, address, etc.
6
Symbol Table (cont’d)
• Accessed in every phase of compilers
– The scanner, parser, and semantic analyzer put
names of identifiers in symbol table.
– The semantic analyzer stores more information
(e.g. data types) in the table.
– The intermediate code generator, code
optimizer and code generator use information in
symbol table to generate appropriate code.
• Mostly use hash table for efficiency.
7
Literal table
• Store constants and strings used in program
– reduce the memory size by reusing constants
and strings
• Can be combined with symbol table
8
Parse tree
• Dynamically-allocated, pointer-based
structure
• Information for different data types
related to parse trees need to be stored
somewhere.
– Nodes are variant records, storing
information for different types of data
– Nodes store pointers to information stored
in other data structure, e.g. symbol table
9
Scanning
• A scanner reads a stream of characters and
puts them together into some meaningful
(with respect to the source language) units
called tokens.
• It produces a stream of tokens for the next
phase of compiler.
10
Parsing
• A parser gets a stream of tokens from the
scanner, and determines if the syntax
(structure) of the program is correct
according to the (context-free) grammar of
the source language.
• Then, it produces a data structure, called a
parse tree or an abstract syntax tree, which
describes the syntactic structure of the
program.
11
Semantic analysis
• It gets the parse tree from the parser together with
information about some syntactic elements
• It determines if the semantics or meaning of the
program is correct.
• This part deals with static semantic.
– semantic of programs that can be checked by reading
off from the program only.
– syntax of the language which cannot be described in
context-free grammar.
• Mostly, a semantic analyzer does type checking.
• It modifies the parse tree in order to get that
(static) semantically correct code.
12
Intermediate code generation
• An intermediate code generator
– takes a parse tree from the semantic analyzer
– generates a program in the intermediate
language.
• In some compilers, a source program is
translated into an intermediate code first and
then the intermediate code is translated into
the target language.
• In other compilers, a source program is
translated directly into the target language.
13
Intermediate code generation (cont’d)
• Using intermediate code is beneficial when
compilers which translates a single source
language to many target languages are
required.
– The front-end of a compiler – scanner to
intermediate code generator – can be used for
every compilers.
– Different back-ends – code optimizer and code
generator– is required for each target language.
• One of the popular intermediate code is
three-address code. A three-address code
instruction is in the form of x = y op z.
14
Code optimization
• Replacing an inefficient sequence of
instructions with a better sequence of
instructions.
• Sometimes called code improvement.
• Code optimization can be done:
– after semantic analyzing
• performed on a parse tree
– after intermediate code generation
• performed on a intermediate code
– after code generation
• performed on a target code
15
Code generation
• A code generator
– takes either an intermediate code or a parse
tree
– produces a target program.
16
Error Handling
• Error can be found in every phase of
compilation.
– Errors found during compilation are called static
(or compile-time) errors.
– Errors found during execution are called
dynamic (or run-time) errors
• Compilers need to detect, report, and
recover from error found in source programs
• Error handlers are different in different
phases of compiler.
Chapter 1 2301373: Introduction 17
• a compiler which generates target code for a
different machine from one on which the
compiler runs.
• A host language is a language in which the
compiler is written.
– T-diagram
• Cross compilers are used very often in
practice.
Cross Compiler
S
H
T
18
Cross Compilers (cont’d)
• If we want a compiler from
language A to language B on a
machine with language E,
– write one with E
– write one with D if you have a
compiler from D to E on some
machine
• It is better than the former approach
if D is a high-level language but E is
a machine language
– write one from G to B with E if we
have a compiler from A to G
written in E
A
E
B
D
?
E
A
D
B
G
E
BA
E
G
19
Porting
• Porting: construct a compiler between a
source and a target language using one
host language from another host language
A
A
K
A
H
H A
H
K
A
A
K
A
H
K A
K
K
20
Bootstrapping
• If we have to implement, from
scratch, a compiler from a
high-level language A to a
machine, which is also a host,
language,
– direct method
– bootstrapping
A
H
H
A
A1
H
A1
A2
H
A2
A3
H
A3
H
H
21
Cousins of Compilers
• Linkers
• Loaders
• Interpreters
• Assemblers
Chapter 1 2301373: Introduction 22
History (1930’s -40’s)
• 1930’s
– John von Neumann invented the concept of
stored-program computer.
– Alan Turing defined Turing machine and
computability.
• 1940’s
– Many electro-mechanic, stored-program
computers were constructed.
• ABC (Atanasoff Berry Computer) at Iowa
• Z1-4 (by Zuse) in Germany
• ENIAC (programmed by a plug board)

More Related Content

What's hot

Programming paradigm
Programming paradigmProgramming paradigm
Programming paradigm
busyking03
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language Processing
Hemant Sharma
 
Programming Fundamental Presentation
Programming Fundamental PresentationProgramming Fundamental Presentation
Programming Fundamental Presentation
fazli khaliq
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1Manoj Patil
 
Computer Language Translator
Computer Language TranslatorComputer Language Translator
Computer Language Translator
Ranjeet Kumar
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
Akhil Kaushik
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
Akhil Kaushik
 
Types of Compilers
Types of CompilersTypes of Compilers
Types of Compilers
Hemant Chetwani
 
loaders and linkers
 loaders and linkers loaders and linkers
loaders and linkers
Temesgen Molla
 
Computer registers
Computer registersComputer registers
Computer registers
DeepikaT13
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
desta_gebre
 
Loaders
LoadersLoaders
Programming Fundamentals lecture 1
Programming Fundamentals lecture 1Programming Fundamentals lecture 1
Programming Fundamentals lecture 1
REHAN IJAZ
 
Linker and Loader
Linker and Loader Linker and Loader
Linker and Loader
sonalikharade3
 
Introduction to basic programming
Introduction to basic programmingIntroduction to basic programming
Introduction to basic programming
Jordan Delacruz
 

What's hot (20)

Programming paradigm
Programming paradigmProgramming paradigm
Programming paradigm
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language Processing
 
Programming Fundamental Presentation
Programming Fundamental PresentationProgramming Fundamental Presentation
Programming Fundamental Presentation
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
 
Computer Language Translator
Computer Language TranslatorComputer Language Translator
Computer Language Translator
 
Assembler
AssemblerAssembler
Assembler
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Types of operating system
Types of operating systemTypes of operating system
Types of operating system
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Types of Compilers
Types of CompilersTypes of Compilers
Types of Compilers
 
loaders and linkers
 loaders and linkers loaders and linkers
loaders and linkers
 
Computer registers
Computer registersComputer registers
Computer registers
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
 
Loaders
LoadersLoaders
Loaders
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
Programming Fundamentals lecture 1
Programming Fundamentals lecture 1Programming Fundamentals lecture 1
Programming Fundamentals lecture 1
 
Linker and Loader
Linker and Loader Linker and Loader
Linker and Loader
 
Introduction to basic programming
Introduction to basic programmingIntroduction to basic programming
Introduction to basic programming
 

Viewers also liked

Programing paradigm & implementation
Programing paradigm & implementationPrograming paradigm & implementation
Programing paradigm & implementation
Bilal Maqbool ツ
 
Object Oriented Programing - Inheritance
Object Oriented Programing - InheritanceObject Oriented Programing - Inheritance
Object Oriented Programing - Inheritance
Bayu Firmawan Paoh
 
Dynamics Telephony Dialer Intro
Dynamics Telephony Dialer IntroDynamics Telephony Dialer Intro
Dynamics Telephony Dialer Intro
Eddie Steede
 
Object Oriented Programing - Polymrphism
Object Oriented Programing - PolymrphismObject Oriented Programing - Polymrphism
Object Oriented Programing - Polymrphism
Bayu Firmawan Paoh
 
Object Oriented Programing - Intro
Object Oriented Programing - IntroObject Oriented Programing - Intro
Object Oriented Programing - Intro
Bayu Firmawan Paoh
 
Aspect Oriented Programing - Introduction
Aspect Oriented Programing - IntroductionAspect Oriented Programing - Introduction
Aspect Oriented Programing - Introduction
Venkaiah Chowdary Koneru
 
ELIXIR Webinar: Introducing TeSS
ELIXIR Webinar: Introducing TeSSELIXIR Webinar: Introducing TeSS
ELIXIR Webinar: Introducing TeSS
Niall Beard
 
Spark as a distributed Scala
Spark as a distributed ScalaSpark as a distributed Scala
Spark as a distributed Scala
Alex Fruzenshtein
 
WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011
WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011
WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011
Mustafa TURAN
 
나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발
나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발
나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발
Changwook Park
 
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
Elixir Club
 
Control flow in_elixir
Control flow in_elixirControl flow in_elixir
Control flow in_elixir
Anna Neyzberg
 
Spring IO for startups
Spring IO for startupsSpring IO for startups
Spring IO for startups
Alex Fruzenshtein
 
Flowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Flowex: Flow-Based Programming with Elixir GenStage - Anton MishchukFlowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Flowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Elixir Club
 
Phoenix: Inflame the Web - Alex Troush
Phoenix: Inflame the Web - Alex TroushPhoenix: Inflame the Web - Alex Troush
Phoenix: Inflame the Web - Alex Troush
Elixir Club
 
IS 151 Lecture 3
IS 151 Lecture 3IS 151 Lecture 3
IS 151 Lecture 3
wajanga
 
IS 139 Lecture 7
IS 139 Lecture 7IS 139 Lecture 7
IS 139 Lecture 7
wajanga
 
GenStage and Flow - Jose Valim
GenStage and Flow - Jose Valim GenStage and Flow - Jose Valim
GenStage and Flow - Jose Valim
Elixir Club
 

Viewers also liked (20)

Programing paradigm & implementation
Programing paradigm & implementationPrograming paradigm & implementation
Programing paradigm & implementation
 
Object Oriented Programing - Inheritance
Object Oriented Programing - InheritanceObject Oriented Programing - Inheritance
Object Oriented Programing - Inheritance
 
Dynamics Telephony Dialer Intro
Dynamics Telephony Dialer IntroDynamics Telephony Dialer Intro
Dynamics Telephony Dialer Intro
 
Object Oriented Programing - Polymrphism
Object Oriented Programing - PolymrphismObject Oriented Programing - Polymrphism
Object Oriented Programing - Polymrphism
 
Object Oriented Programing - Intro
Object Oriented Programing - IntroObject Oriented Programing - Intro
Object Oriented Programing - Intro
 
Aspect Oriented Programing - Introduction
Aspect Oriented Programing - IntroductionAspect Oriented Programing - Introduction
Aspect Oriented Programing - Introduction
 
ELIXIR Webinar: Introducing TeSS
ELIXIR Webinar: Introducing TeSSELIXIR Webinar: Introducing TeSS
ELIXIR Webinar: Introducing TeSS
 
Spark as a distributed Scala
Spark as a distributed ScalaSpark as a distributed Scala
Spark as a distributed Scala
 
Big Data eBook
Big Data eBookBig Data eBook
Big Data eBook
 
WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011
WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011
WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011
 
나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발
나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발
나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발
 
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
 
Control flow in_elixir
Control flow in_elixirControl flow in_elixir
Control flow in_elixir
 
Spring IO for startups
Spring IO for startupsSpring IO for startups
Spring IO for startups
 
Flowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Flowex: Flow-Based Programming with Elixir GenStage - Anton MishchukFlowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Flowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
 
Phoenix: Inflame the Web - Alex Troush
Phoenix: Inflame the Web - Alex TroushPhoenix: Inflame the Web - Alex Troush
Phoenix: Inflame the Web - Alex Troush
 
Chap 10(structure and unions)
Chap 10(structure and unions)Chap 10(structure and unions)
Chap 10(structure and unions)
 
IS 151 Lecture 3
IS 151 Lecture 3IS 151 Lecture 3
IS 151 Lecture 3
 
IS 139 Lecture 7
IS 139 Lecture 7IS 139 Lecture 7
IS 139 Lecture 7
 
GenStage and Flow - Jose Valim
GenStage and Flow - Jose Valim GenStage and Flow - Jose Valim
GenStage and Flow - Jose Valim
 

Similar to Introduction to compilers

introduction to computer vision and image processing
introduction to computer vision and image processingintroduction to computer vision and image processing
introduction to computer vision and image processing
pakboy12
 
Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
Radhakrishnan Chinnusamy
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
Thapar Institute
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
amudha arul
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
Akhil Kaushik
 
CD - CH1 - Introduction to compiler design.pptx
CD - CH1 - Introduction to compiler design.pptxCD - CH1 - Introduction to compiler design.pptx
CD - CH1 - Introduction to compiler design.pptx
ZiyadMohammed17
 
Cd ch1 - introduction
Cd   ch1 - introductionCd   ch1 - introduction
Cd ch1 - introduction
mengistu23
 
4_5802928814682016556.pptx
4_5802928814682016556.pptx4_5802928814682016556.pptx
4_5802928814682016556.pptx
AshenafiGirma5
 
COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdf
AdiseshaK
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compilerAbha Damani
 
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
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
Sarmad Ali
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
1 compiler outline
1 compiler outline1 compiler outline
1 compiler outline
ASHOK KUMAR REDDY
 
Presentation1
Presentation1Presentation1
Presentation1
Zarin Tasnim
 
Presentation1
Presentation1Presentation1
Presentation1
Zarin Tasnim
 
Session01 basics programming
Session01 basics programmingSession01 basics programming
Session01 basics programming
HarithaRanasinghe
 

Similar to Introduction to compilers (20)

introduction to computer vision and image processing
introduction to computer vision and image processingintroduction to computer vision and image processing
introduction to computer vision and image processing
 
Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
CD - CH1 - Introduction to compiler design.pptx
CD - CH1 - Introduction to compiler design.pptxCD - CH1 - Introduction to compiler design.pptx
CD - CH1 - Introduction to compiler design.pptx
 
Cd ch1 - introduction
Cd   ch1 - introductionCd   ch1 - introduction
Cd ch1 - introduction
 
4_5802928814682016556.pptx
4_5802928814682016556.pptx4_5802928814682016556.pptx
4_5802928814682016556.pptx
 
COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdf
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
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
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
1 compiler outline
1 compiler outline1 compiler outline
1 compiler outline
 
Presentation1
Presentation1Presentation1
Presentation1
 
Presentation1
Presentation1Presentation1
Presentation1
 
Session01 basics programming
Session01 basics programmingSession01 basics programming
Session01 basics programming
 

More from Bilal Maqbool ツ

Dld lecture module 01
Dld lecture module 01Dld lecture module 01
Dld lecture module 01
Bilal Maqbool ツ
 
Complement
ComplementComplement
Complement
Bilal Maqbool ツ
 
Chapter10
Chapter10Chapter10
Uncdtalk
UncdtalkUncdtalk
Dld lecture module 06
Dld lecture module 06Dld lecture module 06
Dld lecture module 06
Bilal Maqbool ツ
 
Dld lecture module 05
Dld lecture module 05Dld lecture module 05
Dld lecture module 05
Bilal Maqbool ツ
 
Dld lecture module 04 01
Dld lecture module 04  01Dld lecture module 04  01
Dld lecture module 04 01
Bilal Maqbool ツ
 
Dld lecture module 03
Dld lecture module 03Dld lecture module 03
Dld lecture module 03
Bilal Maqbool ツ
 
Dld lecture module 02
Dld lecture module 02Dld lecture module 02
Dld lecture module 02
Bilal Maqbool ツ
 
Lecture 3 report writtng
Lecture 3 report writtngLecture 3 report writtng
Lecture 3 report writtng
Bilal Maqbool ツ
 
Lecture 2
Lecture 2Lecture 2
Lecture 1 report writing
Lecture 1 report writingLecture 1 report writing
Lecture 1 report writing
Bilal Maqbool ツ
 
Bill gates
Bill gatesBill gates
Bill gates
Bilal Maqbool ツ
 
Programming assignment 02 (bilal maqbool 10) 2011
Programming assignment 02    (bilal maqbool 10) 2011Programming assignment 02    (bilal maqbool 10) 2011
Programming assignment 02 (bilal maqbool 10) 2011
Bilal Maqbool ツ
 
Programming assignment 30 12-11
Programming assignment 30 12-11Programming assignment 30 12-11
Programming assignment 30 12-11
Bilal Maqbool ツ
 
Internet presentation
Internet presentationInternet presentation
Internet presentation
Bilal Maqbool ツ
 
Presentation internet programming report
Presentation internet programming reportPresentation internet programming report
Presentation internet programming report
Bilal Maqbool ツ
 
Magnetic storage devices
Magnetic storage devicesMagnetic storage devices
Magnetic storage devices
Bilal Maqbool ツ
 

More from Bilal Maqbool ツ (20)

Dld lecture module 01
Dld lecture module 01Dld lecture module 01
Dld lecture module 01
 
Complement
ComplementComplement
Complement
 
Chapter10
Chapter10Chapter10
Chapter10
 
Uncdtalk
UncdtalkUncdtalk
Uncdtalk
 
Okkkkk
OkkkkkOkkkkk
Okkkkk
 
Good
GoodGood
Good
 
Dld lecture module 06
Dld lecture module 06Dld lecture module 06
Dld lecture module 06
 
Dld lecture module 05
Dld lecture module 05Dld lecture module 05
Dld lecture module 05
 
Dld lecture module 04 01
Dld lecture module 04  01Dld lecture module 04  01
Dld lecture module 04 01
 
Dld lecture module 03
Dld lecture module 03Dld lecture module 03
Dld lecture module 03
 
Dld lecture module 02
Dld lecture module 02Dld lecture module 02
Dld lecture module 02
 
Lecture 3 report writtng
Lecture 3 report writtngLecture 3 report writtng
Lecture 3 report writtng
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1 report writing
Lecture 1 report writingLecture 1 report writing
Lecture 1 report writing
 
Bill gates
Bill gatesBill gates
Bill gates
 
Programming assignment 02 (bilal maqbool 10) 2011
Programming assignment 02    (bilal maqbool 10) 2011Programming assignment 02    (bilal maqbool 10) 2011
Programming assignment 02 (bilal maqbool 10) 2011
 
Programming assignment 30 12-11
Programming assignment 30 12-11Programming assignment 30 12-11
Programming assignment 30 12-11
 
Internet presentation
Internet presentationInternet presentation
Internet presentation
 
Presentation internet programming report
Presentation internet programming reportPresentation internet programming report
Presentation internet programming report
 
Magnetic storage devices
Magnetic storage devicesMagnetic storage devices
Magnetic storage devices
 

Recently uploaded

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 

Introduction to compilers

  • 2. 2 What is a Compiler? • A compiler is a computer program that translates a program in a source language into an equivalent program in a target language. • A source program/code is a program/code written in the source language, which is usually a high-level language. • A target program/code is a program/code written in the target language, which often is a machine language or an intermediate code. compiler Source program Target program Error message
  • 3. 3 Process of Compiling scanner parser Semantic analyzer Intermediate code generator Code optimization Code generator Code optimization Stream of characters Stream of tokens Parse/syntax tree Annotated tree Intermediate code Intermediate code Target code Target code
  • 4. 4 Some Data Structures • Symbol table • Literal table • Parse tree
  • 5. 5 Symbol Table • Identifiers are names of variables, constants, functions, data types, etc. • Store information associated with identifiers – Information associated with different types of identifiers can be different • Information associated with variables are name, type, address,size (for array), etc. • Information associated with functions are name,type of return value, parameters, address, etc.
  • 6. 6 Symbol Table (cont’d) • Accessed in every phase of compilers – The scanner, parser, and semantic analyzer put names of identifiers in symbol table. – The semantic analyzer stores more information (e.g. data types) in the table. – The intermediate code generator, code optimizer and code generator use information in symbol table to generate appropriate code. • Mostly use hash table for efficiency.
  • 7. 7 Literal table • Store constants and strings used in program – reduce the memory size by reusing constants and strings • Can be combined with symbol table
  • 8. 8 Parse tree • Dynamically-allocated, pointer-based structure • Information for different data types related to parse trees need to be stored somewhere. – Nodes are variant records, storing information for different types of data – Nodes store pointers to information stored in other data structure, e.g. symbol table
  • 9. 9 Scanning • A scanner reads a stream of characters and puts them together into some meaningful (with respect to the source language) units called tokens. • It produces a stream of tokens for the next phase of compiler.
  • 10. 10 Parsing • A parser gets a stream of tokens from the scanner, and determines if the syntax (structure) of the program is correct according to the (context-free) grammar of the source language. • Then, it produces a data structure, called a parse tree or an abstract syntax tree, which describes the syntactic structure of the program.
  • 11. 11 Semantic analysis • It gets the parse tree from the parser together with information about some syntactic elements • It determines if the semantics or meaning of the program is correct. • This part deals with static semantic. – semantic of programs that can be checked by reading off from the program only. – syntax of the language which cannot be described in context-free grammar. • Mostly, a semantic analyzer does type checking. • It modifies the parse tree in order to get that (static) semantically correct code.
  • 12. 12 Intermediate code generation • An intermediate code generator – takes a parse tree from the semantic analyzer – generates a program in the intermediate language. • In some compilers, a source program is translated into an intermediate code first and then the intermediate code is translated into the target language. • In other compilers, a source program is translated directly into the target language.
  • 13. 13 Intermediate code generation (cont’d) • Using intermediate code is beneficial when compilers which translates a single source language to many target languages are required. – The front-end of a compiler – scanner to intermediate code generator – can be used for every compilers. – Different back-ends – code optimizer and code generator– is required for each target language. • One of the popular intermediate code is three-address code. A three-address code instruction is in the form of x = y op z.
  • 14. 14 Code optimization • Replacing an inefficient sequence of instructions with a better sequence of instructions. • Sometimes called code improvement. • Code optimization can be done: – after semantic analyzing • performed on a parse tree – after intermediate code generation • performed on a intermediate code – after code generation • performed on a target code
  • 15. 15 Code generation • A code generator – takes either an intermediate code or a parse tree – produces a target program.
  • 16. 16 Error Handling • Error can be found in every phase of compilation. – Errors found during compilation are called static (or compile-time) errors. – Errors found during execution are called dynamic (or run-time) errors • Compilers need to detect, report, and recover from error found in source programs • Error handlers are different in different phases of compiler.
  • 17. Chapter 1 2301373: Introduction 17 • a compiler which generates target code for a different machine from one on which the compiler runs. • A host language is a language in which the compiler is written. – T-diagram • Cross compilers are used very often in practice. Cross Compiler S H T
  • 18. 18 Cross Compilers (cont’d) • If we want a compiler from language A to language B on a machine with language E, – write one with E – write one with D if you have a compiler from D to E on some machine • It is better than the former approach if D is a high-level language but E is a machine language – write one from G to B with E if we have a compiler from A to G written in E A E B D ? E A D B G E BA E G
  • 19. 19 Porting • Porting: construct a compiler between a source and a target language using one host language from another host language A A K A H H A H K A A K A H K A K K
  • 20. 20 Bootstrapping • If we have to implement, from scratch, a compiler from a high-level language A to a machine, which is also a host, language, – direct method – bootstrapping A H H A A1 H A1 A2 H A2 A3 H A3 H H
  • 21. 21 Cousins of Compilers • Linkers • Loaders • Interpreters • Assemblers
  • 22. Chapter 1 2301373: Introduction 22 History (1930’s -40’s) • 1930’s – John von Neumann invented the concept of stored-program computer. – Alan Turing defined Turing machine and computability. • 1940’s – Many electro-mechanic, stored-program computers were constructed. • ABC (Atanasoff Berry Computer) at Iowa • Z1-4 (by Zuse) in Germany • ENIAC (programmed by a plug board)