SlideShare uma empresa Scribd logo
1 de 25
Chapter 1 2301373: Introduction 1
Introduction
Chapter 1 2301373: 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
Chapter 1 2301373: Introduction 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
Chapter 1 2301373: Introduction 4
Some Data Structures
• Symbol table
• Literal table
• Parse tree
Chapter 1 2301373: Introduction 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.
Chapter 1 2301373: Introduction 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.
Chapter 1 2301373: Introduction 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
Chapter 1 2301373: Introduction 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
Chapter 1 2301373: Introduction 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.
Chapter 1 2301373: Introduction 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.
Chapter 1 2301373: Introduction 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.
Chapter 1 2301373: Introduction 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.
Chapter 1 2301373: Introduction 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.
Chapter 1 2301373: Introduction 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
Chapter 1 2301373: Introduction 15
Code generation
• A code generator
– takes either an intermediate code or a parse
tree
– produces a target program.
Chapter 1 2301373: Introduction 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 c
ompiler 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
Chapter 1 2301373: Introduction 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 ma
chine
• 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
Chapter 1 2301373: Introduction 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
Chapter 1 2301373: Introduction 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
Chapter 1 2301373: Introduction 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)
Chapter 1 2301373: Introduction 23
History : 1950
• Many electronic, stored-program computers were
designed.
– EDVAC (by von Neumann)
– ACE (by Turing)
• Programs were written in machine languages.
• Later, programs are written in assembly languages
instead.
– Assemblers translate symbolic code and memory
address to machine code.
• John Backus developed FORTRAN (no recursive
call) and FORTRAN compiler.
• Noam Chomsky studied structure of languages
and classified them into classes called Chomsky
hierarchy.
0A 1F 83 90 4B
op code, address,..
LDI B, 4
LDI C, 3
LDI A, 0
ST: ADI A, C
DEC B
JNZ B, ST
STO 0XF0, A
Grammar
Chapter 1 2301373: Introduction 24
History (1960’s)
• Recursive-descent parsing was introduced.
• Nuar designed Algol60, Pascal’s ancestor,
which allows recursive call.
• Backus-Nuar form (BNF) was used to
described Algol60.
• LL(1) parsing was proposed by Lewis and
Stearns.
• General LR parsing was invented by Knuth.
• SLR parsing was developed by DeRemer.
Chapter 1 2301373: Introduction 25
History (1970’s)
• LALR was develpoed by DeRemer.
• Aho and Ullman founded the theory of LR
parsing techniques.
• Yacc (Yet Another Compiler Compiler) was
developed by Johnson.
• Type inference was studied by Milner.

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

basics of compiler design
basics of compiler designbasics of compiler design
basics of compiler design
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Type checking
Type checkingType checking
Type checking
 
Passes of compilers
Passes of compilersPasses of compilers
Passes of compilers
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Language translator
Language translatorLanguage translator
Language translator
 
Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Compilers
CompilersCompilers
Compilers
 
Software Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & SpecificationSoftware Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & Specification
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Software architecture Unit 1 notes
Software architecture Unit 1 notesSoftware architecture Unit 1 notes
Software architecture Unit 1 notes
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
 
Compiler design
Compiler designCompiler design
Compiler design
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Types of Compilers
Types of CompilersTypes of Compilers
Types of Compilers
 

Semelhante a Introduction to Compiler

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 processingpakboy12
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overviewamudha arul
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction Thapar Institute
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
System software module 1 presentation file
System software module 1 presentation fileSystem software module 1 presentation file
System software module 1 presentation filejithujithin657
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdfAdiseshaK
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptxPmarkNorcio
 
4_5802928814682016556.pptx
4_5802928814682016556.pptx4_5802928814682016556.pptx
4_5802928814682016556.pptxAshenafiGirma5
 
Cd ch1 - introduction
Cd   ch1 - introductionCd   ch1 - introduction
Cd ch1 - introductionmengistu23
 
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.pptxZiyadMohammed17
 
Python-L1.pptx
Python-L1.pptxPython-L1.pptx
Python-L1.pptxDukeCalvin
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to CompilersAkhil Kaushik
 

Semelhante a Introduction to Compiler (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 compilers
Introduction to compilersIntroduction to compilers
Introduction to compilers
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
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
 
System software module 1 presentation file
System software module 1 presentation fileSystem software module 1 presentation file
System software module 1 presentation file
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Presentation1
Presentation1Presentation1
Presentation1
 
Presentation1
Presentation1Presentation1
Presentation1
 
COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdf
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptx
 
4_5802928814682016556.pptx
4_5802928814682016556.pptx4_5802928814682016556.pptx
4_5802928814682016556.pptx
 
Cd ch1 - introduction
Cd   ch1 - introductionCd   ch1 - introduction
Cd ch1 - introduction
 
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
 
Compilers.pptx
Compilers.pptxCompilers.pptx
Compilers.pptx
 
Python-L1.pptx
Python-L1.pptxPython-L1.pptx
Python-L1.pptx
 
Lecture1 introduction compilers
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilers
 
Chap01-Intro.ppt
Chap01-Intro.pptChap01-Intro.ppt
Chap01-Intro.ppt
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 

Mais de Radhakrishnan Chinnusamy (11)

Unit 5_Controlling.pptx
Unit 5_Controlling.pptxUnit 5_Controlling.pptx
Unit 5_Controlling.pptx
 
Unit 3_organising.pptx
Unit 3_organising.pptxUnit 3_organising.pptx
Unit 3_organising.pptx
 
Unit 2_Planning.ppt
Unit 2_Planning.pptUnit 2_Planning.ppt
Unit 2_Planning.ppt
 
Unit 1_introduction.pptx
Unit 1_introduction.pptxUnit 1_introduction.pptx
Unit 1_introduction.pptx
 
Chapter 7 Run Time Environment
Chapter 7   Run Time EnvironmentChapter 7   Run Time Environment
Chapter 7 Run Time Environment
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
 
Primitive Recursive Functions
Primitive Recursive FunctionsPrimitive Recursive Functions
Primitive Recursive Functions
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 

Último

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 

Último (20)

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 

Introduction to Compiler

  • 1. Chapter 1 2301373: Introduction 1 Introduction
  • 2. Chapter 1 2301373: 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 4 Some Data Structures • Symbol table • Literal table • Parse tree
  • 5. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 15 Code generation • A code generator – takes either an intermediate code or a parse tree – produces a target program.
  • 16. Chapter 1 2301373: Introduction 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 c ompiler 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. Chapter 1 2301373: Introduction 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 ma chine • 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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. Chapter 1 2301373: Introduction 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)
  • 23. Chapter 1 2301373: Introduction 23 History : 1950 • Many electronic, stored-program computers were designed. – EDVAC (by von Neumann) – ACE (by Turing) • Programs were written in machine languages. • Later, programs are written in assembly languages instead. – Assemblers translate symbolic code and memory address to machine code. • John Backus developed FORTRAN (no recursive call) and FORTRAN compiler. • Noam Chomsky studied structure of languages and classified them into classes called Chomsky hierarchy. 0A 1F 83 90 4B op code, address,.. LDI B, 4 LDI C, 3 LDI A, 0 ST: ADI A, C DEC B JNZ B, ST STO 0XF0, A Grammar
  • 24. Chapter 1 2301373: Introduction 24 History (1960’s) • Recursive-descent parsing was introduced. • Nuar designed Algol60, Pascal’s ancestor, which allows recursive call. • Backus-Nuar form (BNF) was used to described Algol60. • LL(1) parsing was proposed by Lewis and Stearns. • General LR parsing was invented by Knuth. • SLR parsing was developed by DeRemer.
  • 25. Chapter 1 2301373: Introduction 25 History (1970’s) • LALR was develpoed by DeRemer. • Aho and Ullman founded the theory of LR parsing techniques. • Yacc (Yet Another Compiler Compiler) was developed by Johnson. • Type inference was studied by Milner.