SlideShare a Scribd company logo
Compiler Construction
Ahmed Raza
Single Pass compiler with example
• What is a Pass?
• What is a Single Pass compiler?
• Difference between Single and Multi Pass Compiler?
• Example
• Issue with Single Pass Compiler
3
The Major “Phases” of a Compiler
Syntax Analysis
Semantic Analysis
Code Generation
Source Program
Abstract Syntax Tree
Decorated Abstract Syntax Tree
Object Code
Error Reports
Error Reports
4
Compiler Passes
• A “pass” is a complete traversal of the source program, or a complete
traversal of some internal representation of the source program (such
as an AST).
• A pass can correspond to a “phase” but it does not have to!
• Sometimes a single pass corresponds to several phases that are
interleaved in time.
• What and how many passes a compiler does over the source program
is an important design decision.
5
Single Pass Compiler
Compiler Driver
Syntactic Analyzer
calls
calls
Contextual Analyzer Code Generator
calls
Dependency diagram of a typical Single Pass Compiler:
A single pass compiler makes a single pass over the source text, parsing, analyzing, and
generating code all at once.
6
Multi Pass Compiler
Compiler Driver
Syntactic Analyzer
calls
calls
Contextual Analyzer Code Generator
calls
Dependency diagram of a typical Multi Pass Compiler:
A multi pass compiler makes several passes over the program. The output of a preceding
phase is stored in a data structure and used by subsequent phases.
input
Source Text
output
AST
input
output
Decorated AST
input
output
Object Code
7
Example: Single Pass Compilation of ...
Source Program
Let var n: integer;
var c: char
in begin
c := ‘&’;
n := n+1
end
Assembly Code
PUSH 2
LOADL 38
STORE 1[SB]
LOAD 0[SB]
LOADL 1
CALL add
STORE 0[SB]
POP 2
HALT
Ident
n
c
Type
int
char
Address
0[SB]
1[SB]
8
Compiler Design Issues
Single Pass Multi Pass
Speed
Memory
Modularity
Flexibility
“Global” optimization
Source Language
better worse
better for
large programs
(potentially) better for
small programs
worse better
betterworse
impossible possible
single pass compilers are not possible for many
programming languages
9
Language Issues
Example Pascal:
Pascal was explicitly designed to be easy to implement with a
single pass compiler:
• Every identifier must be declared before its first use.
var n:integer;
procedure inc;
begin
n:=n+1
end
Undeclared Variable!
procedure inc;
begin
n:=n+1
end;
var n:integer;
?
10
Language Issues
Example Pascal:
• Every identifier must be declared before it is used.
• How to handle mutual recursion then?
procedure ping(x:integer)
begin
... pong(x-1); ...
end;
procedure pong(x:integer)
begin
... ping(x–1); ...
end;
11
Language Issues
Example Pascal:
• Every identifier must be declared before it is used.
• How to handle mutual recursion then?
forward procedure pong(x:integer)
procedure ping(x:integer)
begin
... pong(x-1); ...
end;
procedure pong(x:integer)
begin
... ping(x–1); ...
end;
OK!
Virtual Machine for Compiler
• What is Compiler?
• Phases of Compiler
• What is a Virtual Machine?
• Java Virtual Machine
• Structure of JVM
• Loaders
• Linkers
• Assembler
What is a Compiler?
“A compiler is a piece of software that translates a source program
from source language to equivalent program in target language. An
important feature of the compiler is to identify error in source program
during compilation/translation process. “
Phases of Compiler
Lexical Analyzer
• The lexical analyzer reads the stream of characters making up the
source program and groups the characters into meaningful sequences
called lexem
• For each lexeme, the lexical analyzer produces as output a token of
the form (token-name, attribute-value)
• It is also called Scanner.
Syntax Analyzer
• analyzes the source code (token stream) against the production rules
to detect any errors in the code.
• The output of this phase is a parse tree
• Goal: Recover the structure described by that series of tokens.
• Goal: Report errors if those tokens do not properly encode a structure
Semantic Analyzer
• The semantic analyzer uses the syntax tree and the information in the
symbol table to check the source program for semantic consistency.
• Functions
• Scope checking: verify that all applied occurrences of identifiers are
declared
• Type checking: verify that all operations in the program are used
according to their type rules
• Outputs semantically verified parse tree
Intermediate Code Generations
• This a phase where compiler produces and low level intermediate
representation of the which can have a variety of forms
• Postfix
• Syntax Tree
• Three Address Code
Three Address Code
Three address code is the sequence of statements of the general form
as given
a := b op c
Where a, b and c are identifiers names and op represents operator. And
the symbol “:=” stands for assignment operator
This representation is called three address code because it has three
addresses, two for operands and one for the result
Quadruples
• three address code with four fields
• Op (operator)
• Arg1(argument1)
• Arg2(argument2)
• Result
• Quadruple representation of
Op ARG1 ARG2 Result
(0) + i j t1
(1) + t1 k i
t1 := i + j
i := t1 + k
Triples
• three address code with three fields
• Op (operator)
• Arg1(argument1)
• Arg2(argument2)
Op ARG1 ARG2
(0) + i j
(1) + (0) k
Code Optimization
• It is a program transformation technique, which tries to improve the
code by making it consume less resources (i.e. CPU, Memory) and
deliver high speed
• Optimization depends on various factors like
• Memory
• Algorithm
• Execution Time
• Programming Language and
• Others
Code Generation
• The code generator takes as input an intermediate representation of
the source program and maps it into the target language.
• The target program may be in
• Assembly Language
• Relocatable machine code or
• Absolute machine code
What is a Virtual Machine?
• “A virtual machine (VM) is a software implementation of a machine
that executes programs like a physical machine. It shares physical
hardware resources with the other users but isolates the OS or
application to avoid changing the end-user experience.”
Why use Virtual Machines?
• compilers for languages like Java and C#
• To make language platform independent
• Security ( Sand Boxing)
• strong syntactic and structural constraints
• Garbage Collection
• Robustness
• other
Architecture of JVM
Class Loader Subsystem
Load is the part responsible for loading bytecode into the memory. Class loader loads files from different sources
using different loader such as
 Bootstrap Class Loader responsible for loading java internal classes from rt.jar which is distributed with JVM.
 Extension class loader responsible for loading additional application jars that reside in jre/lib/ext
 Application class loader loads classes from valued specified in your CLASSPATH environment variables and
from –cp parameterized folder.
• Link is the phase where much of the work is done. It consists of three
parts
• Verify This is the part where the bytecode is verified according to the JVM
class specifications.
• Prepare This is the part where the memory is allocated for the static variables
inside the class file. The memory locations are than initialized with the default
values.
• Resolve In this part all the symbolic references to the current classes are
resolved with actual reference. For example one class has reference to other
class.
• Initialization This is the phase where the actual values of the static
variable define in source code are set unlike prepare where the
default value are set.
Runtime Data Areas
• Method Areas The place where metadata corresponding to
class is stored.
• Heap Areas The place where object data is stored
• PC Registers They are called program counter registers i.e.
point to the next instruction to be executed per thread.
• Stack Areas Contain stack frame corresponding to the
current method execution per thread
• Native Method Stacks Contains stack for the native method
execution per thread. The stack contains memory portions
for different parts of functions like parameters or local
variables etc
Execution Engine
Assembler
• It is a program which converts assembly language into machine code.
Assembler performs the translation in similar way as compiler. But
assembler is used to translate low-level programming language
whereas compiler is used to translate high-level programming
language.
• An assembler performs the following functions
• Convert mnemonic operation codes to their machine language codes
• Convert symbolic (e.g., jump labels, variable names) operands to their
machine addresses
• Use proper addressing modes and formats to build efficient machine
instructions
• Translate data constants into internal machine representations
• Output the object program and provide other information (e.g., for linker and
loader)
Two Pass Assembler
• Pass 1
• Assign addresses to all statements in the program
• Save the values (addresses) assigned to all labels (including label and
variable names) for use in Pass 2 (deal with forward references)
• Perform some processing of assembler directives (e.g., BYTE, RESW,
these can affect address assignment)
• Pass 2
• Assemble instructions (generate opcode and look up addresses)
• Generate data values defined by BYTE, WORD
• Perform processing of assembler directives not done in Pass 1
• Write the object program and the assembly listing
Linker
• A programming tool which combines one or more partial Object Files
and libraries into a (more) complete executable object file
Linker (cont.)
• Three tasks
• Searches the program to find library routines used by program, e.g. printf(),
math routines,…
• Determines the memory locations that code from each module will occupy
and relocates its instructions by adjusting absolute references
• Resolves references among files
Loader
“Part of the OS that brings an executable file residing on disk into
memory and starts it running “
• Steps
• Read executable file’s header to determine the size of text and data segments
• Create a new address space for the program
• Copies instructions and data into address space
• Copies arguments passed to the program on the stack
• Initializes the machine registers including the stack ptr
• Jumps to a startup routine that copies the program’s arguments from the
stack to registers and calls the program’s main routine
Loader (cont.)
References
• http://www.slideshare.net/tousifirshad/virtual-machine-45872339
• http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-1.html#jvms-1.2
• https://www.tutorialspoint.com/compiler_design/compiler_design_intermediate_code_generati
ons.htm
• https://www.quora.com/Why-dont-C-and-Java-compile-to-machine-code
• http://www.artima.com/insidejvm/ed2/jvmP.html
• https://www.youtube.com/watch?v=ZBJ0u9MaKtM
• https://en.wikipedia.org/wiki/Source_code_editor
• http://www.slideshare.net/BinYang7/linker-and-loader-upload?qid=a64e8e07-b14a-445b-9507-
8b4a5dbccf30&v=&b=&from_search=6
• http://www.slideshare.net/akshaykhatri125/linker-and-loader-18359420
• Compilers : principles, techniques, and tools 1 Alfred V. Aho ... [et al.]. -- 2nd ed.
• M._Joseph_-_Elements_of_Compiler_Design

More Related Content

What's hot

Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Akshaya Arunan
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
Akhil Kaushik
 
Register & flags
Register & flagsRegister & flags
Register & flags
fazli khaliq
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
Iffat Anjum
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
Ms.SHANTHI.S CSE
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
Huawei Technologies
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
Radhakrishnan Chinnusamy
 
Introduction
IntroductionIntroduction
Introduction
Hiren Selani
 
Loaders and Linkers
Loaders and LinkersLoaders and Linkers
Loaders and Linkers
kunj desai
 
Intro To Programming Concepts
Intro To Programming ConceptsIntro To Programming Concepts
Intro To Programming ConceptsJussi Pohjolainen
 
Compilers
CompilersCompilers
Compilers
Bense Tony
 
Computer Language Translator
Computer Language TranslatorComputer Language Translator
Computer Language Translator
Ranjeet Kumar
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
Richa Sharma
 
Chapt 02 ia-32 processer architecture
Chapt 02   ia-32 processer architectureChapt 02   ia-32 processer architecture
Chapt 02 ia-32 processer architecture
bushrakainat214
 
Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
Satyamevjayte Haxor
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language Processing
Hemant Sharma
 
Compiler design
Compiler designCompiler design
Compiler design
Thakur Ganeshsingh Thakur
 

What's hot (20)

Computer
ComputerComputer
Computer
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Register & flags
Register & flagsRegister & flags
Register & flags
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
 
Introduction
IntroductionIntroduction
Introduction
 
Loaders and Linkers
Loaders and LinkersLoaders and Linkers
Loaders and Linkers
 
Intro To Programming Concepts
Intro To Programming ConceptsIntro To Programming Concepts
Intro To Programming Concepts
 
Compilers
CompilersCompilers
Compilers
 
Computer Language Translator
Computer Language TranslatorComputer Language Translator
Computer Language Translator
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
 
Chapt 02 ia-32 processer architecture
Chapt 02   ia-32 processer architectureChapt 02   ia-32 processer architecture
Chapt 02 ia-32 processer architecture
 
Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
 
Fundamentals of Language Processing
Fundamentals of Language ProcessingFundamentals of Language Processing
Fundamentals of Language Processing
 
Compiler design
Compiler designCompiler design
Compiler design
 

Similar to Compiler Construction

Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
amudha arul
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
Thapar Institute
 
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
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
NesredinTeshome1
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
Radhika Talaviya
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
MUSHAMHARIKIRAN6737
 
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 Construction
Compiler ConstructionCompiler Construction
Compiler Construction
Sarmad Ali
 
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
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
guest5de1a5
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation file
jithujithin657
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
Rossy719186
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
Anbarasan Radhakrishnan R
 
Assembler
AssemblerAssembler
Assembler
Temesgen Molla
 
C for Engineers
C for EngineersC for Engineers
C for Engineers
Julie Iskander
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
NISHASOMSCS113
 
Ch1 (1).ppt
Ch1 (1).pptCh1 (1).ppt
Ch1 (1).ppt
MDSayem35
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1Manoj Patil
 
Compiler design
Compiler designCompiler design
Compiler design
suganyasanjai
 

Similar to Compiler Construction (20)

Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
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
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
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 Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Lecture 1 introduction to language processors
Lecture 1  introduction to language processorsLecture 1  introduction to language processors
Lecture 1 introduction to language processors
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation file
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
 
Assembler
AssemblerAssembler
Assembler
 
C for Engineers
C for EngineersC for Engineers
C for Engineers
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Ch1 (1).ppt
Ch1 (1).pptCh1 (1).ppt
Ch1 (1).ppt
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
 
Compiler design
Compiler designCompiler design
Compiler design
 

Recently uploaded

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
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
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
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
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
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
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
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
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 

Recently uploaded (20)

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
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
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.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
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
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...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
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
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 

Compiler Construction

  • 2. Single Pass compiler with example • What is a Pass? • What is a Single Pass compiler? • Difference between Single and Multi Pass Compiler? • Example • Issue with Single Pass Compiler
  • 3. 3 The Major “Phases” of a Compiler Syntax Analysis Semantic Analysis Code Generation Source Program Abstract Syntax Tree Decorated Abstract Syntax Tree Object Code Error Reports Error Reports
  • 4. 4 Compiler Passes • A “pass” is a complete traversal of the source program, or a complete traversal of some internal representation of the source program (such as an AST). • A pass can correspond to a “phase” but it does not have to! • Sometimes a single pass corresponds to several phases that are interleaved in time. • What and how many passes a compiler does over the source program is an important design decision.
  • 5. 5 Single Pass Compiler Compiler Driver Syntactic Analyzer calls calls Contextual Analyzer Code Generator calls Dependency diagram of a typical Single Pass Compiler: A single pass compiler makes a single pass over the source text, parsing, analyzing, and generating code all at once.
  • 6. 6 Multi Pass Compiler Compiler Driver Syntactic Analyzer calls calls Contextual Analyzer Code Generator calls Dependency diagram of a typical Multi Pass Compiler: A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases. input Source Text output AST input output Decorated AST input output Object Code
  • 7. 7 Example: Single Pass Compilation of ... Source Program Let var n: integer; var c: char in begin c := ‘&’; n := n+1 end Assembly Code PUSH 2 LOADL 38 STORE 1[SB] LOAD 0[SB] LOADL 1 CALL add STORE 0[SB] POP 2 HALT Ident n c Type int char Address 0[SB] 1[SB]
  • 8. 8 Compiler Design Issues Single Pass Multi Pass Speed Memory Modularity Flexibility “Global” optimization Source Language better worse better for large programs (potentially) better for small programs worse better betterworse impossible possible single pass compilers are not possible for many programming languages
  • 9. 9 Language Issues Example Pascal: Pascal was explicitly designed to be easy to implement with a single pass compiler: • Every identifier must be declared before its first use. var n:integer; procedure inc; begin n:=n+1 end Undeclared Variable! procedure inc; begin n:=n+1 end; var n:integer; ?
  • 10. 10 Language Issues Example Pascal: • Every identifier must be declared before it is used. • How to handle mutual recursion then? procedure ping(x:integer) begin ... pong(x-1); ... end; procedure pong(x:integer) begin ... ping(x–1); ... end;
  • 11. 11 Language Issues Example Pascal: • Every identifier must be declared before it is used. • How to handle mutual recursion then? forward procedure pong(x:integer) procedure ping(x:integer) begin ... pong(x-1); ... end; procedure pong(x:integer) begin ... ping(x–1); ... end; OK!
  • 12. Virtual Machine for Compiler • What is Compiler? • Phases of Compiler • What is a Virtual Machine? • Java Virtual Machine • Structure of JVM • Loaders • Linkers • Assembler
  • 13. What is a Compiler? “A compiler is a piece of software that translates a source program from source language to equivalent program in target language. An important feature of the compiler is to identify error in source program during compilation/translation process. “
  • 15. Lexical Analyzer • The lexical analyzer reads the stream of characters making up the source program and groups the characters into meaningful sequences called lexem • For each lexeme, the lexical analyzer produces as output a token of the form (token-name, attribute-value) • It is also called Scanner.
  • 16. Syntax Analyzer • analyzes the source code (token stream) against the production rules to detect any errors in the code. • The output of this phase is a parse tree • Goal: Recover the structure described by that series of tokens. • Goal: Report errors if those tokens do not properly encode a structure
  • 17. Semantic Analyzer • The semantic analyzer uses the syntax tree and the information in the symbol table to check the source program for semantic consistency. • Functions • Scope checking: verify that all applied occurrences of identifiers are declared • Type checking: verify that all operations in the program are used according to their type rules • Outputs semantically verified parse tree
  • 18. Intermediate Code Generations • This a phase where compiler produces and low level intermediate representation of the which can have a variety of forms • Postfix • Syntax Tree • Three Address Code
  • 19. Three Address Code Three address code is the sequence of statements of the general form as given a := b op c Where a, b and c are identifiers names and op represents operator. And the symbol “:=” stands for assignment operator This representation is called three address code because it has three addresses, two for operands and one for the result
  • 20. Quadruples • three address code with four fields • Op (operator) • Arg1(argument1) • Arg2(argument2) • Result • Quadruple representation of Op ARG1 ARG2 Result (0) + i j t1 (1) + t1 k i t1 := i + j i := t1 + k
  • 21. Triples • three address code with three fields • Op (operator) • Arg1(argument1) • Arg2(argument2) Op ARG1 ARG2 (0) + i j (1) + (0) k
  • 22. Code Optimization • It is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed • Optimization depends on various factors like • Memory • Algorithm • Execution Time • Programming Language and • Others
  • 23. Code Generation • The code generator takes as input an intermediate representation of the source program and maps it into the target language. • The target program may be in • Assembly Language • Relocatable machine code or • Absolute machine code
  • 24. What is a Virtual Machine? • “A virtual machine (VM) is a software implementation of a machine that executes programs like a physical machine. It shares physical hardware resources with the other users but isolates the OS or application to avoid changing the end-user experience.”
  • 25. Why use Virtual Machines? • compilers for languages like Java and C# • To make language platform independent • Security ( Sand Boxing) • strong syntactic and structural constraints • Garbage Collection • Robustness • other
  • 26.
  • 28. Class Loader Subsystem Load is the part responsible for loading bytecode into the memory. Class loader loads files from different sources using different loader such as  Bootstrap Class Loader responsible for loading java internal classes from rt.jar which is distributed with JVM.  Extension class loader responsible for loading additional application jars that reside in jre/lib/ext  Application class loader loads classes from valued specified in your CLASSPATH environment variables and from –cp parameterized folder.
  • 29. • Link is the phase where much of the work is done. It consists of three parts • Verify This is the part where the bytecode is verified according to the JVM class specifications. • Prepare This is the part where the memory is allocated for the static variables inside the class file. The memory locations are than initialized with the default values. • Resolve In this part all the symbolic references to the current classes are resolved with actual reference. For example one class has reference to other class. • Initialization This is the phase where the actual values of the static variable define in source code are set unlike prepare where the default value are set.
  • 30. Runtime Data Areas • Method Areas The place where metadata corresponding to class is stored. • Heap Areas The place where object data is stored • PC Registers They are called program counter registers i.e. point to the next instruction to be executed per thread. • Stack Areas Contain stack frame corresponding to the current method execution per thread • Native Method Stacks Contains stack for the native method execution per thread. The stack contains memory portions for different parts of functions like parameters or local variables etc
  • 32. Assembler • It is a program which converts assembly language into machine code. Assembler performs the translation in similar way as compiler. But assembler is used to translate low-level programming language whereas compiler is used to translate high-level programming language. • An assembler performs the following functions • Convert mnemonic operation codes to their machine language codes • Convert symbolic (e.g., jump labels, variable names) operands to their machine addresses • Use proper addressing modes and formats to build efficient machine instructions • Translate data constants into internal machine representations • Output the object program and provide other information (e.g., for linker and loader)
  • 33. Two Pass Assembler • Pass 1 • Assign addresses to all statements in the program • Save the values (addresses) assigned to all labels (including label and variable names) for use in Pass 2 (deal with forward references) • Perform some processing of assembler directives (e.g., BYTE, RESW, these can affect address assignment)
  • 34. • Pass 2 • Assemble instructions (generate opcode and look up addresses) • Generate data values defined by BYTE, WORD • Perform processing of assembler directives not done in Pass 1 • Write the object program and the assembly listing
  • 35. Linker • A programming tool which combines one or more partial Object Files and libraries into a (more) complete executable object file
  • 36. Linker (cont.) • Three tasks • Searches the program to find library routines used by program, e.g. printf(), math routines,… • Determines the memory locations that code from each module will occupy and relocates its instructions by adjusting absolute references • Resolves references among files
  • 37. Loader “Part of the OS that brings an executable file residing on disk into memory and starts it running “ • Steps • Read executable file’s header to determine the size of text and data segments • Create a new address space for the program • Copies instructions and data into address space • Copies arguments passed to the program on the stack • Initializes the machine registers including the stack ptr • Jumps to a startup routine that copies the program’s arguments from the stack to registers and calls the program’s main routine
  • 39. References • http://www.slideshare.net/tousifirshad/virtual-machine-45872339 • http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-1.html#jvms-1.2 • https://www.tutorialspoint.com/compiler_design/compiler_design_intermediate_code_generati ons.htm • https://www.quora.com/Why-dont-C-and-Java-compile-to-machine-code • http://www.artima.com/insidejvm/ed2/jvmP.html • https://www.youtube.com/watch?v=ZBJ0u9MaKtM • https://en.wikipedia.org/wiki/Source_code_editor • http://www.slideshare.net/BinYang7/linker-and-loader-upload?qid=a64e8e07-b14a-445b-9507- 8b4a5dbccf30&v=&b=&from_search=6 • http://www.slideshare.net/akshaykhatri125/linker-and-loader-18359420 • Compilers : principles, techniques, and tools 1 Alfred V. Aho ... [et al.]. -- 2nd ed. • M._Joseph_-_Elements_of_Compiler_Design