SlideShare a Scribd company logo
1 of 32
Download to read offline
Compiler Design
Dr. K ADISESHA
Introduction
to
Introduction
Problem Statement
Queries
Compilers
General Model
2
Compilers
Dr. K. Adisesha
Phases of Compiler
Introduction
Dr. K. Adisesha
3
Language Processing System:
The computer system is made of hardware and software.
➢ The programs written in high-level language, which is
easier for us to understand and remember which cannot be
understandable to hardware.
➢ These programs are then fed into a series of tools and OS
components to get the desired code that can be used by the
machine.
➢ This is known as Language Processing System.
Introduction
Dr. K. Adisesha
4
Preprocessor:
A preprocessor produce input to compilers.
They may perform the following functions.
➢ Macro processing: A preprocessor may allow a user to define macros that are short
hands for longer constructs.
➢ File inclusion: A preprocessor may include header files into the program text.
➢ Rational preprocessor: these preprocessors augment older languages with more
modern flow-of control and data structuring facilities.
➢ Language Extensions: These preprocessor attempts to add capabilities to the
language by certain amounts to build-in macro
Introduction
Dr. K. Adisesha
5
Compiler:
A compiler translates the code written in one language to some other language without
changing the meaning of the program.
➢ It is also expected that a compiler should make the target code efficient and optimized
in terms of time and space..
➢ Compiler design principles provide an in-depth view of translation and optimization
process.
➢ Compiler design covers basic translation mechanism and error detection & recovery.
Introduction
Dr. K. Adisesha
6
Compiler:
A compiler translates the code written in one language to some other language without
changing the meaning of the program.
Compilers
Dr. K. Adisesha
7
Compiler:
Before diving straight into the concepts of compilers, we should understand a few other
tools that work closely with compilers.
Let us understand how a program, using C compiler, is executed on a host machine.
➢ User writes a program in C language (high-level language).
➢ The C compiler, compiles the program and translates it to assembly program (low-level
language).
➢ An assembler then translates the assembly program into machine code (object).
➢ A linker tool is used to link all the parts of the program together for execution
(executable machine code).
➢ A loader loads all of them into memory and then the program is executed.
Compilers
Dr. K. Adisesha
8
Types of compiler:
➢ Native code compiler: A compiler may produce binary output to run /execute on the same computer and
operating system. This type of compiler is called as native code compiler.
➢ Cross Compiler: A cross compiler is a compiler that runs on one machine and produce object code for
another machine.
➢ Bootstrap compiler: If a compiler has been implemented in its own language . self-hosting compiler.
➢ One pass compiler: The compilation is done in one pass over the source program, hence the compilation
is completed very quickly. This is used for the programming language PASCAL, COBOL,FORTAN.
➢ Multi-pass compiler(2 or 3 pass compiler): In this compiler , the compilation is done step by step . Each
step uses the result of the previous step and it creates another intermediate result. Example:- gcc , TC++
➢ JIT Compiler: This compiler is used for JAVA programming language and Microsoft .NET
➢ Source to source compiler: It is a type of compiler that takes a high level language as a input and its
output as high level language. Example Open MP
Compilers
Dr. K. Adisesha
9
LIST OF COMPILERS:
➢ Ada compilers
➢ ALGOL compilers
➢ BASIC compilers
➢ C# compilers
➢ C compilers
➢ C++ compilers
➢ COBOL compilers
➢ Common Lisp compilers
➢ ECMAScript interpreters
➢ Fortran compilers
➢ Java compilers
➢ Pascal compilers
➢ PL/I compilers
➢ Python compilers
➢ Smalltalk compilers
Introduction
Dr. K. Adisesha
10
Compiler Design:
The high-level language is converted into binary language in various phases. A
compiler is a program that converts high-level language to assembly language.
➢ A compiler can broadly be divided into two phases based on the way they compile.
❖ Analysis Phase: Known as the front-end of the compiler, the
analysis phase of the compiler reads the source program, divides
it into core parts and then checks for lexical, grammar and
syntax errors.
❖ Synthesis Phase: Known as the back-end of the compiler, the
synthesis phase generates the target program with the help of
intermediate source code representation and symbol table.
Introduction
Dr. K. Adisesha
11
Interpreter:
An interpreter is a program that appears to execute a source program as if it were
machine language.
➢ Languages such as BASIC, SNOBOL, LISP can be translated using interpreters. JAVA
also uses interpreter.
➢ The process of interpretation can be carried out in following phases.
❖ 1. Lexical analysis
❖ 2. Syntax analysis
❖ 3. Semantic analysis
❖ 4. Direct Execution.
Compiler Design
Dr. K. Adisesha
12
Phases of Compiler:
Programming language - formal language used to give instructions to computers.
➢ Analysis Phase: An intermediate representation is created from the given source code
➢ Synthesis Phase: Equivalent target program is created from
the intermediate representation. It has two parts :
Compiler Design
Dr. K. Adisesha
13
Phases of Compiler:
Programming language - formal language used to give instructions to computers.
➢ Analysis Phase: An intermediate representation is created from
the given source code
❖ Lexical Analyzer
❖ Syntax Analyzer
❖ Semantic Analyzer
❖ Intermediate Code Generator
➢ Synthesis Phase: Equivalent target program is created from
the intermediate representation. It has two parts :
❖ Code Optimizer
❖ Code Generator
Compiler Design
Dr. K. Adisesha
14
Phases of Compiler:
Programming language - formal language used to give instructions to computers.
Compiler Design
Dr. K. Adisesha
15
Phases of Compiler:
➢ Lexical Analysis :
❖ The first phase of scanner works as a text scanner.
❖ This phase scans the source code as a stream of characters and converts it into lexemes.
❖ Lexical analyzer represents these lexemes in the form of tokens as:
<token-name, attribute-value>
➢ Example: x = y + 10
❖ Tokens
X identifier
= Assignment operator
Y identifier
+ Addition operator
10 Number
Compiler Design
Dr. K. Adisesha
16
Phases of Compiler:
➢ Syntax Analysis:
❖ The next phase is also called the parsing.
❖ It takes the token produced by lexical analysis as input and generates a parse tree
❖ In this phase, token arrangements are checked against the source code grammar, i.e. the
parser checks if the expression made by the tokens is syntactically correct.
➢ Consider parse tree for the following example
(a+b)*c
Compiler Design
Dr. K. Adisesha
17
Phases of Compiler:
➢ Limitations of Syntax Analyzers: Syntax analyzers receive their inputs, in the form of
tokens, from lexical analyzers.
➢ Lexical analyzers are responsible for the validity of a token supplied by the syntax
analyzer.
➢ Syntax analyzers have the following drawbacks:
❖ it cannot determine if a token is valid,
❖ it cannot determine if a token is declared before it is being used,
❖ it cannot determine if a token is initialized before it is being used,
❖ it cannot determine if an operation performed on a token type is valid or not.
Compiler Design
Dr. K. Adisesha
18
Phases of Compiler:
Semantic Analysis : Semantic analysis checks whether the parse tree constructed follows the
rules of language. Also, the semantic analyzer keeps track of identifiers, their types and
expressions; whether identifiers are declared before use or not etc.
➢ The semantic analyzer produces an annotated syntax tree as an output.
➢ Collects type information and checks for type compatibility
➢ Checks if the source language permits the operands or not
➢ The following tasks should be performed in semantic analysis:
❖ Scope resolution
❖ Type checking
❖ Array-bound checking
➢ Allows you to perform type checking float x = 20.2;
float y = x*30;
Compiler Design
Dr. K. Adisesha
19
Phases of Compiler:
Semantic Errors: We have mentioned some of the semantics errors that the semantic analyzer is
expected to recognize.
➢ Type mismatch
➢ Undeclared variable
➢ Reserved identifier misuse.
➢ Multiple declaration of variable in a scope.
➢ Accessing an out of scope variable.
➢ Actual and formal parameter mismatch.
Compiler Design
Dr. K. Adisesha
20
Phases of Compiler:
Intermediate Code Generation: After semantic analysis the compiler generates an intermediate
code of the source code for the target machine.
➢ It represents a program for some abstract machine.
➢ It is in between the high-level language and the machine language.
➢ This intermediate code should be generated in such a way that it makes it easier to be translated
into the target machine code.
➢ Example: total = count + rate * 5
❖ Intermediate code with the help of address code method is:
t1 := int_to_float(5)
t2 := rate * t1
t3 := count + t2
total := t3
Compiler Design
Dr. K. Adisesha
21
Phases of Compiler:
Code Optimization: Optimization can be assumed as something that removes unnecessary code
lines, and arranges the sequence of statements in order to speed up the program execution without
wasting resources (CPU, memory).
➢ It helps you to establish a trade-off between execution and compilation speed
➢ Improves the running time of the target program
➢ Removing unreachable code and getting rid of unused variables
➢ Removing statements which are not altered from the loop
➢ Example: Consider the following code
a = intofloat(10)
b = c * a
d = e + b
f = d
➢ Can become
b =c * 10.0
f = e+b
Compiler Design
Dr. K. Adisesha
22
Phases of Compiler:
Code Generation: In this phase, the code generator takes the optimized representation of the
intermediate code and maps it to the target machine language.
➢ The code generator translates the intermediate code into a sequence of (generally) re-locatable
machine code.
➢ Sequence of instructions of machine code performs the task as the intermediate code would do.
➢ Example:
a = b + 60.0
Would be possibly translated to registers.
MOVF a, R1
MULF #60.0, R2
ADDF R1, R2
Compiler Design
Dr. K. Adisesha
23
Phases of Compiler:
Symbol Table: It is a data-structure maintained throughout all the phases of a compiler. All the
identifier's names along with their types are stored here.
➢ The symbol table makes it easier for the compiler to quickly search the identifier record and
retrieve it. The symbol table is also used for scope management.
Error Handling Routine: In the compiler design process error may occur in all the below-given
phases:
➢ Lexical analyzer: Wrongly spelled tokens
➢ Syntax analyzer: Missing parenthesis
➢ Intermediate code generator: Mismatched operands for an operator
➢ Code Optimizer: When the statement is not reachable
➢ Code Generator: When the memory is full or proper registers are not allocated
➢ Symbol tables: Error of multiple declared identifiers
Compiler Design
Dr. K. Adisesha
24
➢ Lexical Analyzer : It takes modified source code from language
preprocessors that are written in the form of sentences.
❖ The lexical analyzer breaks these syntaxes into a series of tokens,
by removing any whitespace or comments in the source code.
❖ lexical analyzer can identify tokens with the help of regular
expressions and pattern rules.
➢ Syntax Analyzer : this phase uses context-free grammar
(CFG), which is recognized by push-down automata.
➢ Semantic Analyzer : Semantic analysis judges whether the
syntax structure constructed in the source program derives any
meaning or not.
Compiler Design
Dr. K. Adisesha
25
➢ Intermediate Code Generator: Intermediate code can be either language
specific (e.g., Byte Code for Java) or language independent (three-address code).
➢ Intermediate codes can be represented in a variety of ways and they have their own
benefits.
❖ High Level IR - High-level intermediate code representation is very close to the
source language itself.
❖ Low Level IR - This one is close to the target machine, which makes it suitable
for register and memory allocation, instruction set selection, etc. It is good for
machine-dependent optimizations.
➢ Code Optimizer: In optimization, high-level general programming constructs
are replaced by very efficient low-level programming codes.
➢ Code Generator: The code generated by the compiler is an object code of some
lower-level programming language, for example, assembly language.
Compiler Passes
Dr. K. Adisesha
26
Types of Compilers:
A Compiler pass refers to the traversal of a compiler through the entire program.
➢ Compiler pass are two types:
❖ Single Pass Compiler: If we combine or group all the phases of compiler design in a
single module known as single pass compiler.
❖ Two Pass Compiler or Multi Pass Compiler: A Two pass/multi-pass Compiler is a
type of compiler that processes the source code or abstract syntax tree of a program
multiple times. In multipass Compiler we divide phases in two pass.
Compiler Passes
Dr. K. Adisesha
27
Single Pass Compiler :
If we combine or group all the phases of compiler design in a single module known as
single pass compiler.
➢ All 6 phases are grouped in a single module, some points of single pass compiler is as:
❖ A one pass/single pass compiler is that type of compiler that passes
through the part of each compilation unit exactly once.
❖ Single pass compiler is faster and smaller than the multi pass
compiler.
❖ As a disadvantage of single pass compiler is that it is less efficient in
comparison with multipass compiler.
❖ Single pass compiler is one that processes the input exactly once, so
going directly from lexical analysis to code generator, and then going
back for the next read.
One-Pass Compiler Multi-Pass Compiler
It reads the code only once and translates it at a similar
time.
It reads the code multiple times, each time changing it into numerous
forms.
They are faster. They are "slower." As more number of passes means more execution
time.
Less efficient code optimization and code generation. Better code optimization and code generation.
It is also called a "Narrow compiler." It has limited scope. It is also called a "wide compiler." As they can scan every portion of
the program.
The compiler requires large memory. The memory occupied by one pass can be reused by a subsequent pass;
therefore, small memory is needed by the compiler.
Example − Pascal & C languages use one-pass
compilation.
Example − Modula -2 languages use multi-pass compilation.
Compiler Passes
Dr. K. Adisesha
28
Difference between One & Multi Pass Compiler:
Compiler Passes
Dr. K. Adisesha
29
Compiler pass :
A Compiler pass refers to the traversal of a compiler through the entire program.
Compiler Passes
Dr. K. Adisesha
30
Compiler pass :
A Compiler pass refers to the traversal of a compiler through the entire program.
Compiler Passes
Dr. K. Adisesha
31
Syntax Definition:
Discussion
Dr. K. Adisesha
32
Queries ?
Dr. K. Adisesha
9449081542

More Related Content

Similar to COMPILER DESIGN.pdf

Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
انشال عارف
 

Similar to COMPILER DESIGN.pdf (20)

COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
COM1407: Introduction to C Programming
COM1407: Introduction to C Programming COM1407: Introduction to C Programming
COM1407: Introduction to C Programming
 
compiler construction tool in computer science .
compiler construction tool in computer science .compiler construction tool in computer science .
compiler construction tool in computer science .
 
aditya malkani compiler.pptx
aditya malkani compiler.pptxaditya malkani compiler.pptx
aditya malkani compiler.pptx
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptx
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Introduction to programming language (basic)
Introduction to programming language (basic)Introduction to programming language (basic)
Introduction to programming language (basic)
 
4_5802928814682016556.pptx
4_5802928814682016556.pptx4_5802928814682016556.pptx
4_5802928814682016556.pptx
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Cd ch1 - introduction
Cd   ch1 - introductionCd   ch1 - introduction
Cd ch1 - introduction
 
CD - CH1 - Introduction to compiler design.pptx
CD - CH1 - Introduction to compiler design.pptxCD - CH1 - Introduction to compiler design.pptx
CD - CH1 - Introduction to compiler design.pptx
 
Unit 1_Evaluation Criteria_session 3.pptx
Unit 1_Evaluation Criteria_session 3.pptxUnit 1_Evaluation Criteria_session 3.pptx
Unit 1_Evaluation Criteria_session 3.pptx
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals master
 
Introduction to compilers
Introduction to compilersIntroduction to compilers
Introduction to compilers
 
How a Compiler Works ?
How a Compiler Works ?How a Compiler Works ?
How a Compiler Works ?
 

More from AdiseshaK

More from AdiseshaK (20)

Introduction to Java Programming.pdf
Introduction to Java Programming.pdfIntroduction to Java Programming.pdf
Introduction to Java Programming.pdf
 
SP_Solutions_-Adi.pdf
SP_Solutions_-Adi.pdfSP_Solutions_-Adi.pdf
SP_Solutions_-Adi.pdf
 
CNS_Solutions-Adi.pdf
CNS_Solutions-Adi.pdfCNS_Solutions-Adi.pdf
CNS_Solutions-Adi.pdf
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
 
INPUT OUTPUT MEMORY DEVICE.pdf
INPUT OUTPUT MEMORY DEVICE.pdfINPUT OUTPUT MEMORY DEVICE.pdf
INPUT OUTPUT MEMORY DEVICE.pdf
 
AI-ML.pdf
AI-ML.pdfAI-ML.pdf
AI-ML.pdf
 
Macros & Loaders sp.pdf
Macros & Loaders sp.pdfMacros & Loaders sp.pdf
Macros & Loaders sp.pdf
 
Introd to loaders_sp Adi.pdf
Introd to loaders_sp Adi.pdfIntrod to loaders_sp Adi.pdf
Introd to loaders_sp Adi.pdf
 
Assembler by ADI.pdf
Assembler by ADI.pdfAssembler by ADI.pdf
Assembler by ADI.pdf
 
C programming notes.pdf
C programming notes.pdfC programming notes.pdf
C programming notes.pdf
 
VB PPT by ADI PART4.pdf
VB PPT by ADI PART4.pdfVB PPT by ADI PART4.pdf
VB PPT by ADI PART4.pdf
 
VB PPT by ADI PART3.pdf
VB PPT by ADI PART3.pdfVB PPT by ADI PART3.pdf
VB PPT by ADI PART3.pdf
 
WP Solutions- Adi.pdf
WP Solutions- Adi.pdfWP Solutions- Adi.pdf
WP Solutions- Adi.pdf
 
TOC Solutions-Adi.pdf
TOC Solutions-Adi.pdfTOC Solutions-Adi.pdf
TOC Solutions-Adi.pdf
 
SP Solutions -Adi.pdf
SP Solutions -Adi.pdfSP Solutions -Adi.pdf
SP Solutions -Adi.pdf
 
CNS Solutions-Adi.pdf
CNS Solutions-Adi.pdfCNS Solutions-Adi.pdf
CNS Solutions-Adi.pdf
 
VB PPT by ADI PART2.pdf
VB PPT by ADI PART2.pdfVB PPT by ADI PART2.pdf
VB PPT by ADI PART2.pdf
 
VB PPT by ADI part-1.pdf
VB PPT by ADI part-1.pdfVB PPT by ADI part-1.pdf
VB PPT by ADI part-1.pdf
 
8085 microprocessor notes
8085 microprocessor notes8085 microprocessor notes
8085 microprocessor notes
 
Introduction to 8085_by_adi_ppt
Introduction to 8085_by_adi_pptIntroduction to 8085_by_adi_ppt
Introduction to 8085_by_adi_ppt
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

COMPILER DESIGN.pdf

  • 3. Introduction Dr. K. Adisesha 3 Language Processing System: The computer system is made of hardware and software. ➢ The programs written in high-level language, which is easier for us to understand and remember which cannot be understandable to hardware. ➢ These programs are then fed into a series of tools and OS components to get the desired code that can be used by the machine. ➢ This is known as Language Processing System.
  • 4. Introduction Dr. K. Adisesha 4 Preprocessor: A preprocessor produce input to compilers. They may perform the following functions. ➢ Macro processing: A preprocessor may allow a user to define macros that are short hands for longer constructs. ➢ File inclusion: A preprocessor may include header files into the program text. ➢ Rational preprocessor: these preprocessors augment older languages with more modern flow-of control and data structuring facilities. ➢ Language Extensions: These preprocessor attempts to add capabilities to the language by certain amounts to build-in macro
  • 5. Introduction Dr. K. Adisesha 5 Compiler: A compiler translates the code written in one language to some other language without changing the meaning of the program. ➢ It is also expected that a compiler should make the target code efficient and optimized in terms of time and space.. ➢ Compiler design principles provide an in-depth view of translation and optimization process. ➢ Compiler design covers basic translation mechanism and error detection & recovery.
  • 6. Introduction Dr. K. Adisesha 6 Compiler: A compiler translates the code written in one language to some other language without changing the meaning of the program.
  • 7. Compilers Dr. K. Adisesha 7 Compiler: Before diving straight into the concepts of compilers, we should understand a few other tools that work closely with compilers. Let us understand how a program, using C compiler, is executed on a host machine. ➢ User writes a program in C language (high-level language). ➢ The C compiler, compiles the program and translates it to assembly program (low-level language). ➢ An assembler then translates the assembly program into machine code (object). ➢ A linker tool is used to link all the parts of the program together for execution (executable machine code). ➢ A loader loads all of them into memory and then the program is executed.
  • 8. Compilers Dr. K. Adisesha 8 Types of compiler: ➢ Native code compiler: A compiler may produce binary output to run /execute on the same computer and operating system. This type of compiler is called as native code compiler. ➢ Cross Compiler: A cross compiler is a compiler that runs on one machine and produce object code for another machine. ➢ Bootstrap compiler: If a compiler has been implemented in its own language . self-hosting compiler. ➢ One pass compiler: The compilation is done in one pass over the source program, hence the compilation is completed very quickly. This is used for the programming language PASCAL, COBOL,FORTAN. ➢ Multi-pass compiler(2 or 3 pass compiler): In this compiler , the compilation is done step by step . Each step uses the result of the previous step and it creates another intermediate result. Example:- gcc , TC++ ➢ JIT Compiler: This compiler is used for JAVA programming language and Microsoft .NET ➢ Source to source compiler: It is a type of compiler that takes a high level language as a input and its output as high level language. Example Open MP
  • 9. Compilers Dr. K. Adisesha 9 LIST OF COMPILERS: ➢ Ada compilers ➢ ALGOL compilers ➢ BASIC compilers ➢ C# compilers ➢ C compilers ➢ C++ compilers ➢ COBOL compilers ➢ Common Lisp compilers ➢ ECMAScript interpreters ➢ Fortran compilers ➢ Java compilers ➢ Pascal compilers ➢ PL/I compilers ➢ Python compilers ➢ Smalltalk compilers
  • 10. Introduction Dr. K. Adisesha 10 Compiler Design: The high-level language is converted into binary language in various phases. A compiler is a program that converts high-level language to assembly language. ➢ A compiler can broadly be divided into two phases based on the way they compile. ❖ Analysis Phase: Known as the front-end of the compiler, the analysis phase of the compiler reads the source program, divides it into core parts and then checks for lexical, grammar and syntax errors. ❖ Synthesis Phase: Known as the back-end of the compiler, the synthesis phase generates the target program with the help of intermediate source code representation and symbol table.
  • 11. Introduction Dr. K. Adisesha 11 Interpreter: An interpreter is a program that appears to execute a source program as if it were machine language. ➢ Languages such as BASIC, SNOBOL, LISP can be translated using interpreters. JAVA also uses interpreter. ➢ The process of interpretation can be carried out in following phases. ❖ 1. Lexical analysis ❖ 2. Syntax analysis ❖ 3. Semantic analysis ❖ 4. Direct Execution.
  • 12. Compiler Design Dr. K. Adisesha 12 Phases of Compiler: Programming language - formal language used to give instructions to computers. ➢ Analysis Phase: An intermediate representation is created from the given source code ➢ Synthesis Phase: Equivalent target program is created from the intermediate representation. It has two parts :
  • 13. Compiler Design Dr. K. Adisesha 13 Phases of Compiler: Programming language - formal language used to give instructions to computers. ➢ Analysis Phase: An intermediate representation is created from the given source code ❖ Lexical Analyzer ❖ Syntax Analyzer ❖ Semantic Analyzer ❖ Intermediate Code Generator ➢ Synthesis Phase: Equivalent target program is created from the intermediate representation. It has two parts : ❖ Code Optimizer ❖ Code Generator
  • 14. Compiler Design Dr. K. Adisesha 14 Phases of Compiler: Programming language - formal language used to give instructions to computers.
  • 15. Compiler Design Dr. K. Adisesha 15 Phases of Compiler: ➢ Lexical Analysis : ❖ The first phase of scanner works as a text scanner. ❖ This phase scans the source code as a stream of characters and converts it into lexemes. ❖ Lexical analyzer represents these lexemes in the form of tokens as: <token-name, attribute-value> ➢ Example: x = y + 10 ❖ Tokens X identifier = Assignment operator Y identifier + Addition operator 10 Number
  • 16. Compiler Design Dr. K. Adisesha 16 Phases of Compiler: ➢ Syntax Analysis: ❖ The next phase is also called the parsing. ❖ It takes the token produced by lexical analysis as input and generates a parse tree ❖ In this phase, token arrangements are checked against the source code grammar, i.e. the parser checks if the expression made by the tokens is syntactically correct. ➢ Consider parse tree for the following example (a+b)*c
  • 17. Compiler Design Dr. K. Adisesha 17 Phases of Compiler: ➢ Limitations of Syntax Analyzers: Syntax analyzers receive their inputs, in the form of tokens, from lexical analyzers. ➢ Lexical analyzers are responsible for the validity of a token supplied by the syntax analyzer. ➢ Syntax analyzers have the following drawbacks: ❖ it cannot determine if a token is valid, ❖ it cannot determine if a token is declared before it is being used, ❖ it cannot determine if a token is initialized before it is being used, ❖ it cannot determine if an operation performed on a token type is valid or not.
  • 18. Compiler Design Dr. K. Adisesha 18 Phases of Compiler: Semantic Analysis : Semantic analysis checks whether the parse tree constructed follows the rules of language. Also, the semantic analyzer keeps track of identifiers, their types and expressions; whether identifiers are declared before use or not etc. ➢ The semantic analyzer produces an annotated syntax tree as an output. ➢ Collects type information and checks for type compatibility ➢ Checks if the source language permits the operands or not ➢ The following tasks should be performed in semantic analysis: ❖ Scope resolution ❖ Type checking ❖ Array-bound checking ➢ Allows you to perform type checking float x = 20.2; float y = x*30;
  • 19. Compiler Design Dr. K. Adisesha 19 Phases of Compiler: Semantic Errors: We have mentioned some of the semantics errors that the semantic analyzer is expected to recognize. ➢ Type mismatch ➢ Undeclared variable ➢ Reserved identifier misuse. ➢ Multiple declaration of variable in a scope. ➢ Accessing an out of scope variable. ➢ Actual and formal parameter mismatch.
  • 20. Compiler Design Dr. K. Adisesha 20 Phases of Compiler: Intermediate Code Generation: After semantic analysis the compiler generates an intermediate code of the source code for the target machine. ➢ It represents a program for some abstract machine. ➢ It is in between the high-level language and the machine language. ➢ This intermediate code should be generated in such a way that it makes it easier to be translated into the target machine code. ➢ Example: total = count + rate * 5 ❖ Intermediate code with the help of address code method is: t1 := int_to_float(5) t2 := rate * t1 t3 := count + t2 total := t3
  • 21. Compiler Design Dr. K. Adisesha 21 Phases of Compiler: Code Optimization: Optimization can be assumed as something that removes unnecessary code lines, and arranges the sequence of statements in order to speed up the program execution without wasting resources (CPU, memory). ➢ It helps you to establish a trade-off between execution and compilation speed ➢ Improves the running time of the target program ➢ Removing unreachable code and getting rid of unused variables ➢ Removing statements which are not altered from the loop ➢ Example: Consider the following code a = intofloat(10) b = c * a d = e + b f = d ➢ Can become b =c * 10.0 f = e+b
  • 22. Compiler Design Dr. K. Adisesha 22 Phases of Compiler: Code Generation: In this phase, the code generator takes the optimized representation of the intermediate code and maps it to the target machine language. ➢ The code generator translates the intermediate code into a sequence of (generally) re-locatable machine code. ➢ Sequence of instructions of machine code performs the task as the intermediate code would do. ➢ Example: a = b + 60.0 Would be possibly translated to registers. MOVF a, R1 MULF #60.0, R2 ADDF R1, R2
  • 23. Compiler Design Dr. K. Adisesha 23 Phases of Compiler: Symbol Table: It is a data-structure maintained throughout all the phases of a compiler. All the identifier's names along with their types are stored here. ➢ The symbol table makes it easier for the compiler to quickly search the identifier record and retrieve it. The symbol table is also used for scope management. Error Handling Routine: In the compiler design process error may occur in all the below-given phases: ➢ Lexical analyzer: Wrongly spelled tokens ➢ Syntax analyzer: Missing parenthesis ➢ Intermediate code generator: Mismatched operands for an operator ➢ Code Optimizer: When the statement is not reachable ➢ Code Generator: When the memory is full or proper registers are not allocated ➢ Symbol tables: Error of multiple declared identifiers
  • 24. Compiler Design Dr. K. Adisesha 24 ➢ Lexical Analyzer : It takes modified source code from language preprocessors that are written in the form of sentences. ❖ The lexical analyzer breaks these syntaxes into a series of tokens, by removing any whitespace or comments in the source code. ❖ lexical analyzer can identify tokens with the help of regular expressions and pattern rules. ➢ Syntax Analyzer : this phase uses context-free grammar (CFG), which is recognized by push-down automata. ➢ Semantic Analyzer : Semantic analysis judges whether the syntax structure constructed in the source program derives any meaning or not.
  • 25. Compiler Design Dr. K. Adisesha 25 ➢ Intermediate Code Generator: Intermediate code can be either language specific (e.g., Byte Code for Java) or language independent (three-address code). ➢ Intermediate codes can be represented in a variety of ways and they have their own benefits. ❖ High Level IR - High-level intermediate code representation is very close to the source language itself. ❖ Low Level IR - This one is close to the target machine, which makes it suitable for register and memory allocation, instruction set selection, etc. It is good for machine-dependent optimizations. ➢ Code Optimizer: In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes. ➢ Code Generator: The code generated by the compiler is an object code of some lower-level programming language, for example, assembly language.
  • 26. Compiler Passes Dr. K. Adisesha 26 Types of Compilers: A Compiler pass refers to the traversal of a compiler through the entire program. ➢ Compiler pass are two types: ❖ Single Pass Compiler: If we combine or group all the phases of compiler design in a single module known as single pass compiler. ❖ Two Pass Compiler or Multi Pass Compiler: A Two pass/multi-pass Compiler is a type of compiler that processes the source code or abstract syntax tree of a program multiple times. In multipass Compiler we divide phases in two pass.
  • 27. Compiler Passes Dr. K. Adisesha 27 Single Pass Compiler : If we combine or group all the phases of compiler design in a single module known as single pass compiler. ➢ All 6 phases are grouped in a single module, some points of single pass compiler is as: ❖ A one pass/single pass compiler is that type of compiler that passes through the part of each compilation unit exactly once. ❖ Single pass compiler is faster and smaller than the multi pass compiler. ❖ As a disadvantage of single pass compiler is that it is less efficient in comparison with multipass compiler. ❖ Single pass compiler is one that processes the input exactly once, so going directly from lexical analysis to code generator, and then going back for the next read.
  • 28. One-Pass Compiler Multi-Pass Compiler It reads the code only once and translates it at a similar time. It reads the code multiple times, each time changing it into numerous forms. They are faster. They are "slower." As more number of passes means more execution time. Less efficient code optimization and code generation. Better code optimization and code generation. It is also called a "Narrow compiler." It has limited scope. It is also called a "wide compiler." As they can scan every portion of the program. The compiler requires large memory. The memory occupied by one pass can be reused by a subsequent pass; therefore, small memory is needed by the compiler. Example − Pascal & C languages use one-pass compilation. Example − Modula -2 languages use multi-pass compilation. Compiler Passes Dr. K. Adisesha 28 Difference between One & Multi Pass Compiler:
  • 29. Compiler Passes Dr. K. Adisesha 29 Compiler pass : A Compiler pass refers to the traversal of a compiler through the entire program.
  • 30. Compiler Passes Dr. K. Adisesha 30 Compiler pass : A Compiler pass refers to the traversal of a compiler through the entire program.
  • 31. Compiler Passes Dr. K. Adisesha 31 Syntax Definition:
  • 32. Discussion Dr. K. Adisesha 32 Queries ? Dr. K. Adisesha 9449081542