SlideShare a Scribd company logo
Introduction to Compilation
Lecture 01
What is a compiler?
• Programming problems are easier to solve in high-level
languages
– Languages closer to the level of the problem domain, e.g.,
• SmallTalk: OO programming
• JavaScript: Web pages
• Solutions are usually more efficient (faster, smaller) when
written in machine language
– Language that reflects to the cycle-by-cycle working of a processor
• Compilers are the bridges:
– Tools to translate programs written in high-level languages to
efficient executable code
What is a compiler?
A program that reads a program written in one language
and translates it into another language.
Source language Target language
Traditionally, compilers go from high-level languages to low-level
languages.
Compilation task is full of variety??
• Thousands of source languages
– Fortran, Pascal, C, C++, Java, ……
• Thousands of target languages
– Some other lower level language (assembly language),
machine language
• Compilation process has similar variety
– Single pass, multi-pass, load-and-go, optimizing….
• Variety is overwhelming……
• Good news is:
– Few basic techniques is sufficient to cover all variety
– Many efficient tools are available
Requirement
• In order to translate statements in a language, one
needs to understand both
– the structure of the language: the way “sentences" are
constructed in the language, and
– the meaning of the language: what each “sentence"
stands for.
• Terminology:
Structure ≡ Syntax
Meaning ≡ Semantics
Analysis-Synthesis model of compilation
Front End –
language specific
Back End –
machine specific
Source
Language
Target Language
Intermediate
Language
• Two parts
– Analysis
• Breaks up the source program into constituents
– Synthesis
• Constructs the target program
Software tools that performs analysis
• Structure Editors
– Gives hierarchical structure
– Suggests keywords/structures automatically
• Pretty Printer
– Provides an organized and structured look to program
– Different color, font, indentation are used
• Static Checkers
– Tries to discover potential bugs without running
– Identify logical errors, type-checking, dead codes
identification etc
• Interpreters
– Does not produce a target program
– Executes the operations implied by the program
Compilation Steps/Phases
Scanner
(lexical
analysis)
Parser
(syntax
analysis)
Code
Optimizer
Semantic
Analysis
(IC generator)
Code
Generator
Symbol
Table
Source
language
tokens Syntactic
structure
Intermediate
Language
Target
language
Intermediate
Language
Compilation Steps/Phases
• Lexical Analysis Phase: Generates the “tokens” in the source
program
• Syntax Analysis Phase: Recognizes “sentences" in the program
using the syntax of the language
• Semantic Analysis Phase: Infers information about the program
using the semantics of the language
• Intermediate Code Generation Phase: Generates “abstract” code
based on the syntactic structure of the program and the semantic
information from Phase 2
• Optimization Phase: Refines the generated code using a series of
optimizing transformations
• Final Code Generation Phase: Translates the abstract intermediate
code into specific machine instructions
Lexical Analysis
• Convert the stream of characters representing input
program into a sequence of tokens
• Tokens are the “words" of the programming language
• Lexeme
– The characters comprising a token
• For instance, the sequence of characters “static int" is
recognized as two tokens, representing the two words
“static" and “int"
• The sequence of characters “*x++" is recognized as
three tokens, representing “*", “x" and “++“
• Removes the white spaces
• Removes the comments
Lexical Analysis
• Input: result = a + b * 10
• Tokens:
‘result’, ‘=‘, ‘a’, ‘+’, ‘b’, ‘*’, ‘10’
identifiers
operators
Syntax Analysis (Parsing)
• Uncover the structure of a sentence in the program from a
stream of tokens.
• For instance, the phrase “x = +y", which is recognized as
four tokens, representing “x", “=“ and “+" and “y", has the
structure =(x,+(y)), i.e., an assignment expression, that
operates on “x" and the expression “+(y)".
• Build a tree called a parse tree that reflects the structure of
the input sentence.
Syntax Analysis: Grammars
• Expression grammar
Exp ::= Exp ‘+’ Exp
| Exp ‘*’ Exp
| ID
| NUMBER
Assign ::= ID ‘=‘ Exp
Syntax Tree
Assign
result +
a *
b 10
Input: result = a + b * 10
Semantic Analysis
• Concerned with the semantic (meaning) of the
program
• Performs type checking
– Operator operand compitability
Intermediate Code Generation
• Translate each hierarchical structure decorated as
tree into intermediate code
• A program translated for an abstract machine
• Properties of intermediate codes
– Should be easy to generate
– Should be easy to translate
• Intermediate code hides many machine-level details,
but has instruction-level mapping to many assembly
languages
• Main motivation: portability
• One commonly used form is “Three-address Code”
Code Optimization
• Apply a series of transformations to improve the time
and space efficiency of the generated code.
• Peephole optimizations: generate new instructions by
combining/expanding on a small number of
consecutive instructions.
• Global optimizations: reorder, remove or add
instructions to change the structure of generated code
• Consumes a significant fraction of the compilation
time
• Optimization capability varies widely
• Simple optimization techniques can be vary valuable
Code Generation
• Map instructions in the intermediate code to specific
machine instructions.
• Memory management, register allocation, instruction
selection, instruction scheduling, …
• Generates sufficient information to enable symbolic
debugging.
Symbol Table
• Records the identifiers used in the source program
– Collects various associated information as attributes
• Variables: type, scope, storage allocation
• Procedure: number and types of arguments method of
argument passing
• It’s a data structure with collection of records
– Different fields are collected and used at different
phases of compilation
Error Detection, Recovery and Reporting
• Each phase can encounter error
• Specific types of error can be detected by specific
phases
– Lexical Error: int abc, 1num;
– Syntax Error: total = capital + rate year;
– Semantic Error: value = myarray [realIndex];
• Should be able to proceed and process the rest of the
program after an error detected
• Should be able to link the error with the source
program
Error Detection, Recovery and Reporting
Scanner
(lexical
analysis)
Parser
(syntax
analysis)
Code
Optimizer
Semantic
Analysis
(IC generator)
Code
Generator
Symbol
Table
Source
language
tokens Syntactic
structure
Target
language
Error
Handler
Translation of a statement
Lexical Analyzer
result = a + b * 10
Syntax Analyzer
id1 = id2 + id3 * 10
Assign
id1 +
id2 *
id3 10
…….b
…….a
…….result
Symbol Table
Translation of a statement
Assign
id1 +
id2 *
id3 10 Semantic Analyzer
Assign
id1 +
id2 *
id3 INTTOREAL
10
Translation of a statement
Intermediate Code Generator
temp1 := INTTOREAL (10)
temp2 := id3 * temp1
temp3 := id2 + temp2
Id1 := temp3
Code Optimizer
temp1 := id3 * 10.0
Id1 := id2 + temp1
Code Generator
MOVF id3, R2
MULF #10.0, R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
Syntax Analyzer versus Lexical Analyzer
• Which constructs of a program should be recognized by
the lexical analyzer, and which ones by the syntax
analyzer?
– Both of them do similar things; But the lexical analyzer deals
with simple non-recursive constructs of the language.
– The syntax analyzer deals with recursive constructs of the
language.
– The lexical analyzer simplifies the job of the syntax analyzer.
– The lexical analyzer recognizes the smallest meaningful
units (tokens) in a source program.
– The syntax analyzer works on the smallest meaningful units
(tokens) in a source program to recognize meaningful
structures in our programming language.
Cousins of the Compiler
• Preprocessor
– Macro preprocessing
• Define and use shorthand for longer constructs
– File inclusion
• Include header files
– “Rational” Preprocessors
• Augment older languages with modern flow-of-control or
data-structures
– Language Extension
• Add capabilities to a language
• Equel: query language embedded in C
Assemblers
Compiler
Assembler
Source program
Assembly program
Relocatable machine code
Loader/link-editor
Absolute machine code
Two-Pass Assembly
• Simplest form of assembler
• First pass
– All the identifiers are stored in a symbol table
– Storage is allocated
• Second pass
– Translates each operand code in the machine language
MOV a, R1
ADD #2, R1
MOV R1, b
Identifier Address
a 0
b 4
0001 01 00 00000000 *
0011 01 10 00000010
0010 01 00 00000100 *
Source After First Pass After 2nd Pass
Instruction
code Register
Addressing
Mode Address
Relocation
bit
Loaders and Link-Editors
• Convert the relocatable machine code into absolute
machine code
– Map the relocatable address
• Place altered instructions and data in memory
• Make a single program from several files of
relocatable machine code
– Several files of relocatable codes
– Library files
0001 01 00 00000000 *
0011 01 10 00000010
0010 01 00 00000100 *
0001 01 00 00001111
0011 01 10 00000010
0010 01 00 00010011
Address space of data
to be loaded starting
at location L=00001111
Multi Pass Compilers
• Passes
– Several phases of compilers are grouped in to passes
– Often passes generate an explicit output file
– In each pass the whole input file/source is processed
Syntax Analyzer
Lexical Analyzer Intermediate Code Generator
• Semantic analysis
How many passes?
• Relatively few passes is desirable
– Reading and writing intermediate files take time
– It may require to keep the entire file in memory
• One phase generate information in different order than
that is needed by the next phase
• Memory space is not trivial in some cases
• Grouping into same pass incurs some problems
– Intermediate code generation and code generation in
the same pass is difficult
• e.g. Target of ‘goto’ that jumps forward is now known
• ‘Backpatching’ can be a remedy
Issues Driving Compiler Design
• Correctness
• Speed (runtime and compile time)
– Degrees of optimization
– Multiple passes
• Space
• Feedback to user
• Debugging
Other Applications
• In addition to the development of a compiler, the techniques
used in compiler design can be applicable to many problems in
computer science.
– Techniques used in a lexical analyzer can be used in text editors,
information retrieval system, and pattern recognition programs.
– Techniques used in a parser can be used in a query processing
system such as SQL.
– Many software having a complex front-end may need techniques
used in compiler design.
• A symbolic equation solver which takes an equation as input.
That program should parse the given input equation.
– Most of the techniques used in compiler design can be used in
Natural Language Processing (NLP) systems.

More Related Content

What's hot

Compiler design
Compiler designCompiler design
Compiler design
Thakur Ganeshsingh Thakur
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
Huawei Technologies
 
Compiler Design Unit 1
Compiler Design Unit 1Compiler Design Unit 1
Compiler Design Unit 1
Jena Catherine Bel D
 
Assembly Language
Assembly LanguageAssembly Language
Assembly Language
Ibrahimcommunication Al Ani
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Basic programming concepts
Basic programming conceptsBasic programming concepts
Basic programming concepts
salmankhan570
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
Akhil Kaushik
 
Parsing
ParsingParsing
Parsing
khush_boo31
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
Tanzeela_Hussain
 
Yacc
YaccYacc
Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
Radhakrishnan Chinnusamy
 
Compilers
CompilersCompilers
Compilers
Bense Tony
 
Compiler vs interpreter
Compiler vs interpreterCompiler vs interpreter
Compiler vs interpreter
Paras Patel
 
Language processing activity
Language processing activityLanguage processing activity
Language processing activity
Dhruv Sabalpara
 
Assembler
AssemblerAssembler
Assembler
manpreetgrewal
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
Akhil Kaushik
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
desta_gebre
 

What's hot (20)

Compiler design
Compiler designCompiler design
Compiler design
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Compiler Design Unit 1
Compiler Design Unit 1Compiler Design Unit 1
Compiler Design Unit 1
 
Assembly Language
Assembly LanguageAssembly Language
Assembly Language
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Basic programming concepts
Basic programming conceptsBasic programming concepts
Basic programming concepts
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Parsing
ParsingParsing
Parsing
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Yacc
YaccYacc
Yacc
 
Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
 
Compilers
CompilersCompilers
Compilers
 
Compiler vs interpreter
Compiler vs interpreterCompiler vs interpreter
Compiler vs interpreter
 
Language processing activity
Language processing activityLanguage processing activity
Language processing activity
 
Assembler
AssemblerAssembler
Assembler
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
 

Viewers also liked

Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to Compiler
Iffat Anjum
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
adilmehmood93
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
Iffat Anjum
 
Phases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems Programming
Mukesh Tekwani
 
What is Compiler?
What is Compiler?What is Compiler?
What is Compiler?
Huawei Technologies
 
Fog computing ( foggy cloud)
Fog computing  ( foggy cloud)Fog computing  ( foggy cloud)
Fog computing ( foggy cloud)
Iffat Anjum
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane
 
Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.
Md Hossen
 
Translators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreterTranslators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreter
baabtra.com - No. 1 supplier of quality freshers
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
Iffat Anjum
 
Classification of Compilers
Classification of CompilersClassification of Compilers
Classification of Compilers
Sarmad Ali
 
Compiler Design Tutorial
Compiler Design Tutorial Compiler Design Tutorial
Compiler Design Tutorial
Sarit Chakraborty
 
Ch5a
Ch5aCh5a
Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)
guest251d9a
 
Different phases of a compiler
Different phases of a compilerDifferent phases of a compiler
Different phases of a compiler
Sumit Sinha
 
Introduction to Functional Languages
Introduction to Functional LanguagesIntroduction to Functional Languages
Introduction to Functional Languages
suthi
 
Passescd
PassescdPassescd
Passescd
BBDITM LUCKNOW
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptx
Iffat Anjum
 

Viewers also liked (18)

Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to Compiler
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Phases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems Programming
 
What is Compiler?
What is Compiler?What is Compiler?
What is Compiler?
 
Fog computing ( foggy cloud)
Fog computing  ( foggy cloud)Fog computing  ( foggy cloud)
Fog computing ( foggy cloud)
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.
 
Translators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreterTranslators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreter
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Classification of Compilers
Classification of CompilersClassification of Compilers
Classification of Compilers
 
Compiler Design Tutorial
Compiler Design Tutorial Compiler Design Tutorial
Compiler Design Tutorial
 
Ch5a
Ch5aCh5a
Ch5a
 
Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)
 
Different phases of a compiler
Different phases of a compilerDifferent phases of a compiler
Different phases of a compiler
 
Introduction to Functional Languages
Introduction to Functional LanguagesIntroduction to Functional Languages
Introduction to Functional Languages
 
Passescd
PassescdPassescd
Passescd
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptx
 

Similar to Lecture 01 introduction to compiler

Compilers.pptx
Compilers.pptxCompilers.pptx
Compilers.pptx
MohammedMohammed578197
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
Thapar Institute
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
NesredinTeshome1
 
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 compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
Abha Damani
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptx
PmarkNorcio
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
kazi_aihtesham
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
انشال عارف
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
Ashok Raj
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
Rebaz Najeeb
 
Session01 basics programming
Session01 basics programmingSession01 basics programming
Session01 basics programming
HarithaRanasinghe
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
DrIsikoIsaac
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
amudha arul
 
4_5802928814682016556.pptx
4_5802928814682016556.pptx4_5802928814682016556.pptx
4_5802928814682016556.pptx
AshenafiGirma5
 
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
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
ssuser3b4934
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
Radhika Talaviya
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
abdulbaki3
 

Similar to Lecture 01 introduction to compiler (20)

Compilers.pptx
Compilers.pptxCompilers.pptx
Compilers.pptx
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
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 compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptx
 
Concept of compiler in details
Concept of compiler in detailsConcept of compiler in details
Concept of compiler in details
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
 
Session01 basics programming
Session01 basics programmingSession01 basics programming
Session01 basics programming
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
4_5802928814682016556.pptx
4_5802928814682016556.pptx4_5802928814682016556.pptx
4_5802928814682016556.pptx
 
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
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 

More from Iffat Anjum

Cognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationCognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentation
Iffat Anjum
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
Iffat Anjum
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
Iffat Anjum
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
Iffat Anjum
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
Iffat Anjum
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
Iffat Anjum
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05
Iffat Anjum
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
Iffat Anjum
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
Iffat Anjum
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
Iffat Anjum
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
Iffat Anjum
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
Iffat Anjum
 
Distributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioDistributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radio
Iffat Anjum
 
On qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareOn qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcare
Iffat Anjum
 
Data link control
Data link controlData link control
Data link control
Iffat Anjum
 
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Iffat Anjum
 
Qo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksQo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networks
Iffat Anjum
 
A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...
Iffat Anjum
 
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Iffat Anjum
 
Quality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networksQuality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networks
Iffat Anjum
 

More from Iffat Anjum (20)

Cognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationCognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentation
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
Distributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioDistributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radio
 
On qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareOn qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcare
 
Data link control
Data link controlData link control
Data link control
 
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
 
Qo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksQo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networks
 
A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...
 
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
 
Quality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networksQuality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networks
 

Recently uploaded

Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxA Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
OH TEIK BIN
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
Payaamvohra1
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Ch-4 Forest Society and colonialism 2.pdf
Ch-4 Forest Society and colonialism 2.pdfCh-4 Forest Society and colonialism 2.pdf
Ch-4 Forest Society and colonialism 2.pdf
lakshayrojroj
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
ShwetaGawande8
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
 
How to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in useHow to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in use
Celine George
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
Celine George
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 

Recently uploaded (20)

Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxA Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Ch-4 Forest Society and colonialism 2.pdf
Ch-4 Forest Society and colonialism 2.pdfCh-4 Forest Society and colonialism 2.pdf
Ch-4 Forest Society and colonialism 2.pdf
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
 
How to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in useHow to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in use
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 

Lecture 01 introduction to compiler

  • 2. What is a compiler? • Programming problems are easier to solve in high-level languages – Languages closer to the level of the problem domain, e.g., • SmallTalk: OO programming • JavaScript: Web pages • Solutions are usually more efficient (faster, smaller) when written in machine language – Language that reflects to the cycle-by-cycle working of a processor • Compilers are the bridges: – Tools to translate programs written in high-level languages to efficient executable code
  • 3. What is a compiler? A program that reads a program written in one language and translates it into another language. Source language Target language Traditionally, compilers go from high-level languages to low-level languages.
  • 4. Compilation task is full of variety?? • Thousands of source languages – Fortran, Pascal, C, C++, Java, …… • Thousands of target languages – Some other lower level language (assembly language), machine language • Compilation process has similar variety – Single pass, multi-pass, load-and-go, optimizing…. • Variety is overwhelming…… • Good news is: – Few basic techniques is sufficient to cover all variety – Many efficient tools are available
  • 5. Requirement • In order to translate statements in a language, one needs to understand both – the structure of the language: the way “sentences" are constructed in the language, and – the meaning of the language: what each “sentence" stands for. • Terminology: Structure ≡ Syntax Meaning ≡ Semantics
  • 6. Analysis-Synthesis model of compilation Front End – language specific Back End – machine specific Source Language Target Language Intermediate Language • Two parts – Analysis • Breaks up the source program into constituents – Synthesis • Constructs the target program
  • 7. Software tools that performs analysis • Structure Editors – Gives hierarchical structure – Suggests keywords/structures automatically • Pretty Printer – Provides an organized and structured look to program – Different color, font, indentation are used • Static Checkers – Tries to discover potential bugs without running – Identify logical errors, type-checking, dead codes identification etc • Interpreters – Does not produce a target program – Executes the operations implied by the program
  • 9. Compilation Steps/Phases • Lexical Analysis Phase: Generates the “tokens” in the source program • Syntax Analysis Phase: Recognizes “sentences" in the program using the syntax of the language • Semantic Analysis Phase: Infers information about the program using the semantics of the language • Intermediate Code Generation Phase: Generates “abstract” code based on the syntactic structure of the program and the semantic information from Phase 2 • Optimization Phase: Refines the generated code using a series of optimizing transformations • Final Code Generation Phase: Translates the abstract intermediate code into specific machine instructions
  • 10. Lexical Analysis • Convert the stream of characters representing input program into a sequence of tokens • Tokens are the “words" of the programming language • Lexeme – The characters comprising a token • For instance, the sequence of characters “static int" is recognized as two tokens, representing the two words “static" and “int" • The sequence of characters “*x++" is recognized as three tokens, representing “*", “x" and “++“ • Removes the white spaces • Removes the comments
  • 11. Lexical Analysis • Input: result = a + b * 10 • Tokens: ‘result’, ‘=‘, ‘a’, ‘+’, ‘b’, ‘*’, ‘10’ identifiers operators
  • 12. Syntax Analysis (Parsing) • Uncover the structure of a sentence in the program from a stream of tokens. • For instance, the phrase “x = +y", which is recognized as four tokens, representing “x", “=“ and “+" and “y", has the structure =(x,+(y)), i.e., an assignment expression, that operates on “x" and the expression “+(y)". • Build a tree called a parse tree that reflects the structure of the input sentence.
  • 13. Syntax Analysis: Grammars • Expression grammar Exp ::= Exp ‘+’ Exp | Exp ‘*’ Exp | ID | NUMBER Assign ::= ID ‘=‘ Exp
  • 14. Syntax Tree Assign result + a * b 10 Input: result = a + b * 10
  • 15. Semantic Analysis • Concerned with the semantic (meaning) of the program • Performs type checking – Operator operand compitability
  • 16. Intermediate Code Generation • Translate each hierarchical structure decorated as tree into intermediate code • A program translated for an abstract machine • Properties of intermediate codes – Should be easy to generate – Should be easy to translate • Intermediate code hides many machine-level details, but has instruction-level mapping to many assembly languages • Main motivation: portability • One commonly used form is “Three-address Code”
  • 17. Code Optimization • Apply a series of transformations to improve the time and space efficiency of the generated code. • Peephole optimizations: generate new instructions by combining/expanding on a small number of consecutive instructions. • Global optimizations: reorder, remove or add instructions to change the structure of generated code • Consumes a significant fraction of the compilation time • Optimization capability varies widely • Simple optimization techniques can be vary valuable
  • 18. Code Generation • Map instructions in the intermediate code to specific machine instructions. • Memory management, register allocation, instruction selection, instruction scheduling, … • Generates sufficient information to enable symbolic debugging.
  • 19. Symbol Table • Records the identifiers used in the source program – Collects various associated information as attributes • Variables: type, scope, storage allocation • Procedure: number and types of arguments method of argument passing • It’s a data structure with collection of records – Different fields are collected and used at different phases of compilation
  • 20. Error Detection, Recovery and Reporting • Each phase can encounter error • Specific types of error can be detected by specific phases – Lexical Error: int abc, 1num; – Syntax Error: total = capital + rate year; – Semantic Error: value = myarray [realIndex]; • Should be able to proceed and process the rest of the program after an error detected • Should be able to link the error with the source program
  • 21. Error Detection, Recovery and Reporting Scanner (lexical analysis) Parser (syntax analysis) Code Optimizer Semantic Analysis (IC generator) Code Generator Symbol Table Source language tokens Syntactic structure Target language Error Handler
  • 22. Translation of a statement Lexical Analyzer result = a + b * 10 Syntax Analyzer id1 = id2 + id3 * 10 Assign id1 + id2 * id3 10 …….b …….a …….result Symbol Table
  • 23. Translation of a statement Assign id1 + id2 * id3 10 Semantic Analyzer Assign id1 + id2 * id3 INTTOREAL 10
  • 24. Translation of a statement Intermediate Code Generator temp1 := INTTOREAL (10) temp2 := id3 * temp1 temp3 := id2 + temp2 Id1 := temp3 Code Optimizer temp1 := id3 * 10.0 Id1 := id2 + temp1 Code Generator MOVF id3, R2 MULF #10.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1
  • 25. Syntax Analyzer versus Lexical Analyzer • Which constructs of a program should be recognized by the lexical analyzer, and which ones by the syntax analyzer? – Both of them do similar things; But the lexical analyzer deals with simple non-recursive constructs of the language. – The syntax analyzer deals with recursive constructs of the language. – The lexical analyzer simplifies the job of the syntax analyzer. – The lexical analyzer recognizes the smallest meaningful units (tokens) in a source program. – The syntax analyzer works on the smallest meaningful units (tokens) in a source program to recognize meaningful structures in our programming language.
  • 26. Cousins of the Compiler • Preprocessor – Macro preprocessing • Define and use shorthand for longer constructs – File inclusion • Include header files – “Rational” Preprocessors • Augment older languages with modern flow-of-control or data-structures – Language Extension • Add capabilities to a language • Equel: query language embedded in C
  • 27. Assemblers Compiler Assembler Source program Assembly program Relocatable machine code Loader/link-editor Absolute machine code
  • 28. Two-Pass Assembly • Simplest form of assembler • First pass – All the identifiers are stored in a symbol table – Storage is allocated • Second pass – Translates each operand code in the machine language MOV a, R1 ADD #2, R1 MOV R1, b Identifier Address a 0 b 4 0001 01 00 00000000 * 0011 01 10 00000010 0010 01 00 00000100 * Source After First Pass After 2nd Pass Instruction code Register Addressing Mode Address Relocation bit
  • 29. Loaders and Link-Editors • Convert the relocatable machine code into absolute machine code – Map the relocatable address • Place altered instructions and data in memory • Make a single program from several files of relocatable machine code – Several files of relocatable codes – Library files 0001 01 00 00000000 * 0011 01 10 00000010 0010 01 00 00000100 * 0001 01 00 00001111 0011 01 10 00000010 0010 01 00 00010011 Address space of data to be loaded starting at location L=00001111
  • 30. Multi Pass Compilers • Passes – Several phases of compilers are grouped in to passes – Often passes generate an explicit output file – In each pass the whole input file/source is processed Syntax Analyzer Lexical Analyzer Intermediate Code Generator • Semantic analysis
  • 31. How many passes? • Relatively few passes is desirable – Reading and writing intermediate files take time – It may require to keep the entire file in memory • One phase generate information in different order than that is needed by the next phase • Memory space is not trivial in some cases • Grouping into same pass incurs some problems – Intermediate code generation and code generation in the same pass is difficult • e.g. Target of ‘goto’ that jumps forward is now known • ‘Backpatching’ can be a remedy
  • 32. Issues Driving Compiler Design • Correctness • Speed (runtime and compile time) – Degrees of optimization – Multiple passes • Space • Feedback to user • Debugging
  • 33. Other Applications • In addition to the development of a compiler, the techniques used in compiler design can be applicable to many problems in computer science. – Techniques used in a lexical analyzer can be used in text editors, information retrieval system, and pattern recognition programs. – Techniques used in a parser can be used in a query processing system such as SQL. – Many software having a complex front-end may need techniques used in compiler design. • A symbolic equation solver which takes an equation as input. That program should parse the given input equation. – Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems.