SlideShare a Scribd company logo
1 of 41
BCSE307L – Compiler Design
1
Module:1 INTRODUCTION TO COMPILATION AND LEXICAL ANALYSIS
7 hours
Introduction to LLVM - Structure and Phases of a Compiler-Design Issues-Patterns-Lexemes-Tokens-Attributes-Specification of Tokens-Extended
Regular Expression- Regular expression to Deterministic Finite Automata (Direct method) - Lex - A Lexical Analyzer Generator.
Module:2 SYNTAX ANALYSIS 8
hours
Role of Parser- Parse Tree - Elimination of Ambiguity – Top Down Parsing – Recursive Descent Parsing - LL (1) Grammars – Shift Reduce
Parsers- Operator Precedence Parsing - LR Parsers, Construction of SLR Parser Tables and Parsing- CLR Parsing- LALR Parsing.
Module:3 SEMANTICS ANALYSIS 5
hours
Syntax Directed Definition – Evaluation Order - Applications of Syntax Directed Translation - Syntax Directed Translation Schemes -
Implementation of L-attributed Syntax Directed Definition.
Module:4 INTERMEDIATE CODE GENERATION 5
hours
Variants of Syntax trees - Three Address Code- Types – Declarations - Procedures - Assignment Statements - Translation of Expressions -
Control Flow - Back Patching- Switch Case Statements.
Module:5 CODE OPTIMIZATION 6 hours
Loop optimizations- Principal Sources of Optimization -Introduction to Data Flow Analysis - Basic Blocks - Optimization of Basic Blocks -
Peephole Optimization- The DAG Representation of Basic Blocks -Loops in Flow Graphs - Machine Independent Optimization- Implementation of
a naïve code generator for a virtual Machine- Security checking of virtual machine code.
Module:6 CODE GENERATION 5
hours
Issues in the design of a code generator- Target Machine- Next-Use Information – Register Allocation and Assignment- Runtime Organization-
Activation Records.
Module:7 PARALLELISM 7
2
Course Objectives:
1. To provide fundamental knowledge of various language translators.
2. To make students familiar with lexical analysis and parsing techniques.
3. To understand the various actions carried out in semantic analysis.
4. To make the students get familiar with how the intermediate code is generated.
5. To understand the principles of code optimization techniques and code generation.
6. To provide foundation for study of high-performance compiler design.
Course Outcomes:
1. Apply the skills on devising, selecting, and using tools and techniques towards compiler
design
2. Develop language specifications using context free grammars (CFG).
3. Apply the ideas, the techniques, and the knowledge acquired for the purpose of
developing software systems.
4. Constructing symbol tables and generating intermediate code.
5. Obtain insights on compiler optimization and code generation.
3
Module:1
INTRODUCTION TO COMPILATION AND LEXICAL
ANALYSIS
4
5
Structure and Phases of a
Compiler
Translator
• A translator is a program that takes one form of program as input and
converts it into another form.
• Types of translators are:
1. Compiler
2. Interpreter
3. Assembler
Error
Messages
Translator
Source
Program
Target
Program
(If any)
6
Compiler
• A compiler is a program that reads a program written in source language and
translates it into an equivalent program in target language.
Error
Messages
Compiler
void main()
{
int a=1,b=2,c;
c=a+b;
printf(“%d”,c);
}
Source
Program
Target
Program
(If any)
10100100101
1001010
7
Interpreter
• Interpreter is also program that reads a program written in source language and
translates it into an equivalent program in target language
Interpreter
Source
Program
Target
Program
line by line.
Void main()
{
int a=1,b=2,c;
c=a+b;
printf(“%d”,c);
}
0000 1100 0010
0000
1111
1010 1100 0010
0011 1100 0010
1111 1100 0010
Error
Messages (If any)
8
Assembler
• Assembler is a translator which takes the assembly code as an input and
generates the machine code as an output.
Assembler
MOV id3, R1
MUL #2.0, R1
MOV id2, R2
MUL R2, R1
MOV id1, R2
ADD R2, R1
MOV R1, id1
Assembly Code
0000 1100 0010
0100
0111 1000 0001
1111 0101 1110
1100 0000 1000
1011
1100 0000 1000
Machine Code
Error
Messages (If any)
9
Analysis Synthesis model of compilation
10
Analysis synthesis model of compilation
• There are two parts of compilation.
1. Analysis Phase
2. Synthesis Phase
Analysis
Phase
Synthesis
Phase
Intermediate
Representation
void main()
{
int a=1,b=2,c;
c=a+b;
printf(“%d”,c);
}
Source Code
0000 1100
0111 1000
0001
1111 0101
1000
1011
Target Code
11
Analysis phase & Synthesis phase
Analysis Phase
• Analysis part breaks up the source
program into constituent pieces and
creates an intermediate
representation of the source
program.
• Analysis phase consists of three sub
phases:
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
Synthesis Phase
 The synthesis part constructs the desired
target program from the intermediate
representation.
 Synthesis phase consist of the following sub
phases:
1. Code optimization
2. Code generation
12
Phases of compiler
13
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
14
Lexical analysis
• Lexical Analysis is also called linear analysis or
scanning.
• Lexical Analyzer divides the given source statement
into the tokens.
• Ex: Position = initial + rate * 60 would be grouped
into the following tokens:
Position (identifier)
= (Assignment symbol)
initial (identifier)
+ (Plus symbol)
rate (identifier)
* (Multiplication symbol)
60 (Number)
Lexical analysis
id1 = id2 + id3 * 60
Position = initial + rate*60
15
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
16
Syntax analysis
• Syntax Analysis is also called Parsing or
Hierarchical Analysis.
• The syntax analyzer checks each line of the
code and spots every tiny mistake.
• If code is error free then syntax analyzer
generates the tree. Syntax analysis
id1 = id2 + id3 * 60
Lexical analysis
=
id1 +
id2 *
id3 60
Position = initial + rate*60
17
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
18
Semantic analysis
• Semantic analyzer determines the meaning of
a source string.
• It performs following operations:
1. matching of parenthesis in the expression.
2. Matching of if..else statement.
3. Performing arithmetic operation that are type
compatible.
4. Checking the scope of operation.
=
id1 +
id2 *
id3 60
Semantic analysis
=
id1 +
id2 *
id3 inttoreal
60
int to
real
*Note: Consider id1, id2 and id3 are real
19
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
20
Intermediate code generator
• Two important properties of intermediate code :
1. It should be easy to produce.
2. Easy to translate into target program.
• Intermediate form can be represented using
“three address code”.
• Three address code consist of a sequence of
instruction, each of which has at most three
operands.
=
id1 +
id2 *
id3 inttoreal
60
Intermediate code
t1= int to real(60)
t2= id3 * t1
t3= t2 + id2
id1= t3
t1
t2
t3
21
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
22
Code optimization
• It improves the intermediate code.
• This is necessary to have a faster execution
of code or less consumption of memory.
Intermediate code
t1= int to real(60)
t2= id3 * t1
t3= t2 + id2
id1= t3
Code optimization
t1= id3 * 60.0
id1 = id2 + t1
23
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
24
Code generation
• The intermediate code instructions are
translated into sequence of machine
instruction.
Code generation
MOV id3, R2
MUL #60.0, R2
MOV id2, R1
ADD R2,R1
MOV R1, id1
Code optimization
t1= id3 * 60.0
id1 = id2 + t1
Id3R2
Id2R1
25
Phases of compiler
Symbol table Error detection
and recovery
Lexical analysis
Code optimization
Syntax analysis
Semantic analysis
Intermediate code
Code generation
Target Program
Source program
Analysis Phase
Synthesis Phase
Variable
Name
Type Address
Position Float 0001
Initial Float 0005
Rate Float 0009
26
Grouping of Phases
27
Front end & back end (Grouping of phases)
• Depends primarily on source language and largely independent of the target machine.
• It includes following phases:
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Intermediate code generation
5. Creation of symbol table
Front end
 Depends on target machine and do not depends on source program.
 It includes following phases:
1. Code optimization
2. Code generation phase
3. Error handling and symbol table operation
Back end
28
Difference between compiler & interpreter
Compiler Interpreter
Scans the entire program and translates it
as a whole into machine code.
It translates program’s one statement at a
time.
It generates intermediate code. It does not generate intermediate code.
Memory requirement is more. Memory requirement is less.
An error is displayed after entire program
is checked.
An error is displayed for every instruction
interpreted if any.
Example: C compiler Example: Basic, Python, Ruby
29
Context of Compiler
(Cousins of compiler)
30
Context of compiler (Cousins of compiler)
• In addition to compiler, many other system
programs are required to generate absolute
machine code.
• These system programs are:
• Preprocessor
• Assembler
• Linker
• Loader
Skeletal Source Program
Compiler
Assembler
Source
Program
Target Assembly
Program
Relocatable Object
Code
Absolute Machine
Code
Libraries &
Object Files
Linker / Loader
Preprocessor
31
Context of compiler (Cousins of compiler)
Skeletal Source Program
Compiler
Assembler
Source
Program
Target Assembly
Program
Relocatable Object
Code
Absolute Machine
Code
Libraries &
Object Files
Linker / Loader
Preprocessor
Preprocessor
 Some of the task performed by preprocessor:
1. Macro processing: Allows user to define macros. Ex:
#define PI 3.14159265358979323846
2. File inclusion: A preprocessor may include the header
file into the program. Ex: #include<stdio.h>
3. Rational preprocessor: It provides built in macro for
construct like while statement or if statement.
4. Language extensions: Add capabilities to the
language by using built-in macros.
 Ex: the language equal is a database query
language embedded in C. Statement beginning
with ## are taken by preprocessor to be database
access statement unrelated to C and translated
into procedure call on routines that perform the
database access. 32
Context of compiler (Cousins of compiler)
Skeletal Source Program
Compiler
Assembler
Source
Program
Target Assembly
Program
Relocatable Object
Code
Absolute Machine
Code
Libraries &
Object Files
Linker / Loader
Preprocessor
Compiler
 A compiler is a program that reads a program
written in source language and translates it into an
equivalent program in target language.
33
Context of compiler (Cousins of compiler)
Skeletal Source Program
Compiler
Assembler
Source
Program
Target Assembly
Program
Relocatable Object
Code
Absolute Machine
Code
Libraries &
Object Files
Linker / Loader
Preprocessor
Assembler
 Assembler is a translator which takes the assembly
program (mnemonic) as an input and generates
the machine code as an output.
34
Context of compiler (Cousins of compiler)
Skeletal Source Program
Compiler
Assembler
Source
Program
Target Assembly
Program
Relocatable Object
Code
Absolute Machine
Code
Libraries &
Object Files
Linker / Loader
Preprocessor
Linker
 Linker makes a single program from a several files
of relocatable machine code.
 These files may have been the result of several
different compilation, and one or more library files.
Loader
 The process of loading consists of:
 Taking relocatable machine code
 Altering the relocatable address
 Placing the altered instructions and data in
memory at the proper location.
35
Pass structure
36
Pass structure
• One complete scan of a source program is called pass.
• Pass includes reading an input file and writing to the output file.
• In a single pass compiler analysis of source statement is immediately followed
by synthesis of equivalent target statement.
• While in a two pass compiler intermediate code is generated between analysis
and synthesis phase.
• It is difficult to compile the source program into single pass due to: forward
reference
37
Pass structure
Forward reference: A forward reference of a program entity is a reference to the
entity which precedes its definition in the program.
• This problem can be solved by postponing the generation of target code until
more information concerning the entity becomes available.
• It leads to multi pass model of compilation.
• Perform analysis of the source program and note relevant information.
• In Pass II: Generate target code using information noted in pass I.
Pass I:
Pass II:
38
Effect of reducing the number of passes
• It is desirable to have a few passes, because it takes time to read and write
intermediate file.
• If we group several phases into one pass then memory requirement may be
large.
39
Types of compiler
40
Types of compiler
1. One pass compiler
• It is a type of compiler that compiles whole process in one-pass.
2. Two pass compiler
• It is a type of compiler that compiles whole process in two-pass.
• It generates intermediate code.
3. Incremental compiler
• The compiler which compiles only the changed line from the source code and update the object
code.
4. Native code compiler
• The compiler used to compile a source code for a same type of platform only.
5. Cross compiler
• The compiler used to compile a source code for a different kinds platform.
41

More Related Content

Similar to 1-Phases of compiler-26-04-2023.pptx

unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfDrIsikoIsaac
 
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 languageAshok Raj
 
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.pdfDrIsikoIsaac
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Tirumala Rao
 
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 compileradilmehmood93
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.pptRakesh Kumar
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionSarmad Ali
 
Chapter One
Chapter OneChapter One
Chapter Onebolovv
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introductionRana Ehtisham Ul Haq
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation filejithujithin657
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptxانشال عارف
 

Similar to 1-Phases of compiler-26-04-2023.pptx (20)

unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 
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
 
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
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
 
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
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Chapter One
Chapter OneChapter One
Chapter One
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
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
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Assignment1
Assignment1Assignment1
Assignment1
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation file
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
 

More from venkatapranaykumarGa

5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptxvenkatapranaykumarGa
 
10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptxvenkatapranaykumarGa
 
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...venkatapranaykumarGa
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.pptvenkatapranaykumarGa
 
15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdfvenkatapranaykumarGa
 
11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docx11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docxvenkatapranaykumarGa
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docxvenkatapranaykumarGa
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...venkatapranaykumarGa
 
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...venkatapranaykumarGa
 
6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptxvenkatapranaykumarGa
 
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docxvenkatapranaykumarGa
 
7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptxvenkatapranaykumarGa
 

More from venkatapranaykumarGa (13)

9-Query Processing-05-06-2023.PPT
9-Query Processing-05-06-2023.PPT9-Query Processing-05-06-2023.PPT
9-Query Processing-05-06-2023.PPT
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx
 
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
 
15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf
 
11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docx11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docx
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
 
6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx
 
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
 
7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx
 

Recently uploaded

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 

Recently uploaded (20)

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 

1-Phases of compiler-26-04-2023.pptx

  • 2. Module:1 INTRODUCTION TO COMPILATION AND LEXICAL ANALYSIS 7 hours Introduction to LLVM - Structure and Phases of a Compiler-Design Issues-Patterns-Lexemes-Tokens-Attributes-Specification of Tokens-Extended Regular Expression- Regular expression to Deterministic Finite Automata (Direct method) - Lex - A Lexical Analyzer Generator. Module:2 SYNTAX ANALYSIS 8 hours Role of Parser- Parse Tree - Elimination of Ambiguity – Top Down Parsing – Recursive Descent Parsing - LL (1) Grammars – Shift Reduce Parsers- Operator Precedence Parsing - LR Parsers, Construction of SLR Parser Tables and Parsing- CLR Parsing- LALR Parsing. Module:3 SEMANTICS ANALYSIS 5 hours Syntax Directed Definition – Evaluation Order - Applications of Syntax Directed Translation - Syntax Directed Translation Schemes - Implementation of L-attributed Syntax Directed Definition. Module:4 INTERMEDIATE CODE GENERATION 5 hours Variants of Syntax trees - Three Address Code- Types – Declarations - Procedures - Assignment Statements - Translation of Expressions - Control Flow - Back Patching- Switch Case Statements. Module:5 CODE OPTIMIZATION 6 hours Loop optimizations- Principal Sources of Optimization -Introduction to Data Flow Analysis - Basic Blocks - Optimization of Basic Blocks - Peephole Optimization- The DAG Representation of Basic Blocks -Loops in Flow Graphs - Machine Independent Optimization- Implementation of a naïve code generator for a virtual Machine- Security checking of virtual machine code. Module:6 CODE GENERATION 5 hours Issues in the design of a code generator- Target Machine- Next-Use Information – Register Allocation and Assignment- Runtime Organization- Activation Records. Module:7 PARALLELISM 7 2
  • 3. Course Objectives: 1. To provide fundamental knowledge of various language translators. 2. To make students familiar with lexical analysis and parsing techniques. 3. To understand the various actions carried out in semantic analysis. 4. To make the students get familiar with how the intermediate code is generated. 5. To understand the principles of code optimization techniques and code generation. 6. To provide foundation for study of high-performance compiler design. Course Outcomes: 1. Apply the skills on devising, selecting, and using tools and techniques towards compiler design 2. Develop language specifications using context free grammars (CFG). 3. Apply the ideas, the techniques, and the knowledge acquired for the purpose of developing software systems. 4. Constructing symbol tables and generating intermediate code. 5. Obtain insights on compiler optimization and code generation. 3
  • 4. Module:1 INTRODUCTION TO COMPILATION AND LEXICAL ANALYSIS 4
  • 5. 5 Structure and Phases of a Compiler
  • 6. Translator • A translator is a program that takes one form of program as input and converts it into another form. • Types of translators are: 1. Compiler 2. Interpreter 3. Assembler Error Messages Translator Source Program Target Program (If any) 6
  • 7. Compiler • A compiler is a program that reads a program written in source language and translates it into an equivalent program in target language. Error Messages Compiler void main() { int a=1,b=2,c; c=a+b; printf(“%d”,c); } Source Program Target Program (If any) 10100100101 1001010 7
  • 8. Interpreter • Interpreter is also program that reads a program written in source language and translates it into an equivalent program in target language Interpreter Source Program Target Program line by line. Void main() { int a=1,b=2,c; c=a+b; printf(“%d”,c); } 0000 1100 0010 0000 1111 1010 1100 0010 0011 1100 0010 1111 1100 0010 Error Messages (If any) 8
  • 9. Assembler • Assembler is a translator which takes the assembly code as an input and generates the machine code as an output. Assembler MOV id3, R1 MUL #2.0, R1 MOV id2, R2 MUL R2, R1 MOV id1, R2 ADD R2, R1 MOV R1, id1 Assembly Code 0000 1100 0010 0100 0111 1000 0001 1111 0101 1110 1100 0000 1000 1011 1100 0000 1000 Machine Code Error Messages (If any) 9
  • 10. Analysis Synthesis model of compilation 10
  • 11. Analysis synthesis model of compilation • There are two parts of compilation. 1. Analysis Phase 2. Synthesis Phase Analysis Phase Synthesis Phase Intermediate Representation void main() { int a=1,b=2,c; c=a+b; printf(“%d”,c); } Source Code 0000 1100 0111 1000 0001 1111 0101 1000 1011 Target Code 11
  • 12. Analysis phase & Synthesis phase Analysis Phase • Analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program. • Analysis phase consists of three sub phases: 1. Lexical analysis 2. Syntax analysis 3. Semantic analysis Synthesis Phase  The synthesis part constructs the desired target program from the intermediate representation.  Synthesis phase consist of the following sub phases: 1. Code optimization 2. Code generation 12
  • 14. Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation 14
  • 15. Lexical analysis • Lexical Analysis is also called linear analysis or scanning. • Lexical Analyzer divides the given source statement into the tokens. • Ex: Position = initial + rate * 60 would be grouped into the following tokens: Position (identifier) = (Assignment symbol) initial (identifier) + (Plus symbol) rate (identifier) * (Multiplication symbol) 60 (Number) Lexical analysis id1 = id2 + id3 * 60 Position = initial + rate*60 15
  • 16. Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation 16
  • 17. Syntax analysis • Syntax Analysis is also called Parsing or Hierarchical Analysis. • The syntax analyzer checks each line of the code and spots every tiny mistake. • If code is error free then syntax analyzer generates the tree. Syntax analysis id1 = id2 + id3 * 60 Lexical analysis = id1 + id2 * id3 60 Position = initial + rate*60 17
  • 18. Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation 18
  • 19. Semantic analysis • Semantic analyzer determines the meaning of a source string. • It performs following operations: 1. matching of parenthesis in the expression. 2. Matching of if..else statement. 3. Performing arithmetic operation that are type compatible. 4. Checking the scope of operation. = id1 + id2 * id3 60 Semantic analysis = id1 + id2 * id3 inttoreal 60 int to real *Note: Consider id1, id2 and id3 are real 19
  • 20. Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation 20
  • 21. Intermediate code generator • Two important properties of intermediate code : 1. It should be easy to produce. 2. Easy to translate into target program. • Intermediate form can be represented using “three address code”. • Three address code consist of a sequence of instruction, each of which has at most three operands. = id1 + id2 * id3 inttoreal 60 Intermediate code t1= int to real(60) t2= id3 * t1 t3= t2 + id2 id1= t3 t1 t2 t3 21
  • 22. Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation 22
  • 23. Code optimization • It improves the intermediate code. • This is necessary to have a faster execution of code or less consumption of memory. Intermediate code t1= int to real(60) t2= id3 * t1 t3= t2 + id2 id1= t3 Code optimization t1= id3 * 60.0 id1 = id2 + t1 23
  • 24. Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation 24
  • 25. Code generation • The intermediate code instructions are translated into sequence of machine instruction. Code generation MOV id3, R2 MUL #60.0, R2 MOV id2, R1 ADD R2,R1 MOV R1, id1 Code optimization t1= id3 * 60.0 id1 = id2 + t1 Id3R2 Id2R1 25
  • 26. Phases of compiler Symbol table Error detection and recovery Lexical analysis Code optimization Syntax analysis Semantic analysis Intermediate code Code generation Target Program Source program Analysis Phase Synthesis Phase Variable Name Type Address Position Float 0001 Initial Float 0005 Rate Float 0009 26
  • 28. Front end & back end (Grouping of phases) • Depends primarily on source language and largely independent of the target machine. • It includes following phases: 1. Lexical analysis 2. Syntax analysis 3. Semantic analysis 4. Intermediate code generation 5. Creation of symbol table Front end  Depends on target machine and do not depends on source program.  It includes following phases: 1. Code optimization 2. Code generation phase 3. Error handling and symbol table operation Back end 28
  • 29. Difference between compiler & interpreter Compiler Interpreter Scans the entire program and translates it as a whole into machine code. It translates program’s one statement at a time. It generates intermediate code. It does not generate intermediate code. Memory requirement is more. Memory requirement is less. An error is displayed after entire program is checked. An error is displayed for every instruction interpreted if any. Example: C compiler Example: Basic, Python, Ruby 29
  • 30. Context of Compiler (Cousins of compiler) 30
  • 31. Context of compiler (Cousins of compiler) • In addition to compiler, many other system programs are required to generate absolute machine code. • These system programs are: • Preprocessor • Assembler • Linker • Loader Skeletal Source Program Compiler Assembler Source Program Target Assembly Program Relocatable Object Code Absolute Machine Code Libraries & Object Files Linker / Loader Preprocessor 31
  • 32. Context of compiler (Cousins of compiler) Skeletal Source Program Compiler Assembler Source Program Target Assembly Program Relocatable Object Code Absolute Machine Code Libraries & Object Files Linker / Loader Preprocessor Preprocessor  Some of the task performed by preprocessor: 1. Macro processing: Allows user to define macros. Ex: #define PI 3.14159265358979323846 2. File inclusion: A preprocessor may include the header file into the program. Ex: #include<stdio.h> 3. Rational preprocessor: It provides built in macro for construct like while statement or if statement. 4. Language extensions: Add capabilities to the language by using built-in macros.  Ex: the language equal is a database query language embedded in C. Statement beginning with ## are taken by preprocessor to be database access statement unrelated to C and translated into procedure call on routines that perform the database access. 32
  • 33. Context of compiler (Cousins of compiler) Skeletal Source Program Compiler Assembler Source Program Target Assembly Program Relocatable Object Code Absolute Machine Code Libraries & Object Files Linker / Loader Preprocessor Compiler  A compiler is a program that reads a program written in source language and translates it into an equivalent program in target language. 33
  • 34. Context of compiler (Cousins of compiler) Skeletal Source Program Compiler Assembler Source Program Target Assembly Program Relocatable Object Code Absolute Machine Code Libraries & Object Files Linker / Loader Preprocessor Assembler  Assembler is a translator which takes the assembly program (mnemonic) as an input and generates the machine code as an output. 34
  • 35. Context of compiler (Cousins of compiler) Skeletal Source Program Compiler Assembler Source Program Target Assembly Program Relocatable Object Code Absolute Machine Code Libraries & Object Files Linker / Loader Preprocessor Linker  Linker makes a single program from a several files of relocatable machine code.  These files may have been the result of several different compilation, and one or more library files. Loader  The process of loading consists of:  Taking relocatable machine code  Altering the relocatable address  Placing the altered instructions and data in memory at the proper location. 35
  • 37. Pass structure • One complete scan of a source program is called pass. • Pass includes reading an input file and writing to the output file. • In a single pass compiler analysis of source statement is immediately followed by synthesis of equivalent target statement. • While in a two pass compiler intermediate code is generated between analysis and synthesis phase. • It is difficult to compile the source program into single pass due to: forward reference 37
  • 38. Pass structure Forward reference: A forward reference of a program entity is a reference to the entity which precedes its definition in the program. • This problem can be solved by postponing the generation of target code until more information concerning the entity becomes available. • It leads to multi pass model of compilation. • Perform analysis of the source program and note relevant information. • In Pass II: Generate target code using information noted in pass I. Pass I: Pass II: 38
  • 39. Effect of reducing the number of passes • It is desirable to have a few passes, because it takes time to read and write intermediate file. • If we group several phases into one pass then memory requirement may be large. 39
  • 41. Types of compiler 1. One pass compiler • It is a type of compiler that compiles whole process in one-pass. 2. Two pass compiler • It is a type of compiler that compiles whole process in two-pass. • It generates intermediate code. 3. Incremental compiler • The compiler which compiles only the changed line from the source code and update the object code. 4. Native code compiler • The compiler used to compile a source code for a same type of platform only. 5. Cross compiler • The compiler used to compile a source code for a different kinds platform. 41