Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
1
Compiler Design
Unit – 4
Pushdown
Automata
Chapter One
Introduction to Compiler
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
2
Topics to be covered
 Translator
 Analysis synthesis model of compilation
 Phases of compiler
 Difference between compiler & interpreter
 Types of compiler
 Context of compiler (Cousins of compiler)
 Pass structure
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
3
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)
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
4
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
0000 1100 0010
0100
0111 1000 0001
1111 0101 1110
1100 0000 1000
1011
Target
Program
(If any)
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
5
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)
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
6
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)
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
7
Analysis synthesis model of
compilation
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
8
Analysis synthesis model of compilation
 There are two part 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
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
9
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 consist 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
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
10
Phases of compiler
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
11
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
12
Lexical analysis
Position = initial + rate*60
Lexical analysis
id1
 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)
= id2 + id3 * 60
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
13
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
14
Syntax analysis
Position = initial + rate*60
Syntax analysis
id1 = id2 + id3 * 60
 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.
Lexical analysis
=
id1 +
id2 *
id3 60
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
15
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
16
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
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
17
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
18
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
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
19
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
20
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
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
21
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
22
Code generation
 The intermediate code instructions are
translated into sequence of machine
instruction.
Code generation
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
ADDF R2,R1
MOVF R1, id1
Code optimization
t1= id3 * 60.0
id1 = id2 + t1
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
23
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
Sr. Variable
Name
Type Address
1 Position Float 0001
2 Initial Float 0005
3 Rate Float 0009
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
24
Exercise
 Write output of all the phases of compiler for following
statements:
1. x = b-c*2
2. I=p*n*r/100
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
25
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
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
26
Context of Compiler
(Cousins of compiler)/Language Processing
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
27
Context of compiler (Cousins of compiler)
Preprocessor
Skeletal
Source
Program
It performs the following functions:
1. Macro processing
2. File inclusion
3. Rational preprocessor
4. Language extensions
Preprocessor
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
28
Context of compiler (Cousins of compiler)
Preprocessor
Skeletal
Source
Program
1. Macro processing: Allows user to define macros. Macro is shorthand
for longer constructs.
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.
Preprocessor
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
29
Context of compiler (Cousins of compiler)
Preprocessor
Skeletal
Source
Program
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.
Preprocessor
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
30
Context of compiler (Cousins of compiler)
Preprocessor Compiler
Skeletal
Source
Program
Source
Program
• A compiler is a program that reads a program written in source
language and translates it into an equivalent program in target
language.
Compiler
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
31
Context of compiler (Cousins of compiler)
Preprocessor Compiler Assembler
Skeletal
Source
Program
Source
Program
Target
Assembly
Program
• Assembler is a translator which takes the assembly program
(mnemonic) as an input and generates the machine code as an
output.
Assembler
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
32
Context of compiler (Cousins of compiler)
Preprocessor Compiler Assembler
Skeletal
Source
Program
Source
Program
Target
Assembly
Program
Relocatable
Object Code
Libraries &
Object Files
• 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.
Linker
Linker / Loader
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
33
Context of compiler (Cousins of compiler)
Preprocessor Compiler Assembler
Skeletal
Source
Program
Source
Program
Target
Assembly
Program
Relocatable
Object Code
Absolute
Machine
Code
Libraries &
Object Files
• The process of loading consists of:
1. Taking relocatable machine code.
2. Altering the relocatable address.
3. Placing the altered instructions and
data in memory at the proper
location.
Loader
Linker / Loader
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
34
Front end & back end (Grouping of phases)
Front end: 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
Back end: Depends on target machine and do not depend on source program.
It includes following phases:
6. Code optimization
7. Code generation phase
8. Error handling and symbol table operation
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
35
Pass structure
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
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
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
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.
 In Pass I: Perform analysis of the source program and note
relevant information.
 In Pass II: Generate target code using information noted in pass I.
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
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.
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
39
Types of compiler
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
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.
Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
41
End of Unit-1

More Related Content

PDF
Compiler Design Introduction
PPTX
Unit1.pptx of compiler design students subjects
PDF
unit1pdf__2021_12_14_12_37_34.pdf
PPTX
1-Phases of compiler-26-04-2023.pptx
PPTX
Phases of Compiler
PDF
Chapter1pdf__2021_11_23_10_53_20.pdf
PPTX
PPTX
Pros and cons of c as a compiler language
Compiler Design Introduction
Unit1.pptx of compiler design students subjects
unit1pdf__2021_12_14_12_37_34.pdf
1-Phases of compiler-26-04-2023.pptx
Phases of Compiler
Chapter1pdf__2021_11_23_10_53_20.pdf
Pros and cons of c as a compiler language

Similar to Unit-1compiler design and its lecture note .pptx (20)

PDF
Phases of compiler
PDF
Lecture 01 introduction to compiler
PPTX
Compiler an overview
PPTX
16 compiler-151129060845-lva1-app6892-converted.pptx
PPTX
Compiler Design Introduction
PDF
Chapter#01 cc
PPTX
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
PPT
Introduction to Compiler Construction
PPTX
unit1_cd unit1_cd unit1_cd unit1_cd unit1_cd (1).pptx
PPTX
Ch 1.pptx
PPT
Compiler design lessons notes from Semester
PPTX
Chapter 1.pptx
PDF
Compiler design
PPT
1 - Introduction to Compilers.ppt
PPTX
Chapter-1.pptx compiler Design Course Material
PPTX
Unit 1.pptx
PDF
Compiler design Introduction
PPTX
Lecture 1 introduction to language processors
PPTX
Chapter 2 Program language translation.pptx
PPT
phases of a compiler
Phases of compiler
Lecture 01 introduction to compiler
Compiler an overview
16 compiler-151129060845-lva1-app6892-converted.pptx
Compiler Design Introduction
Chapter#01 cc
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
Introduction to Compiler Construction
unit1_cd unit1_cd unit1_cd unit1_cd unit1_cd (1).pptx
Ch 1.pptx
Compiler design lessons notes from Semester
Chapter 1.pptx
Compiler design
1 - Introduction to Compilers.ppt
Chapter-1.pptx compiler Design Course Material
Unit 1.pptx
Compiler design Introduction
Lecture 1 introduction to language processors
Chapter 2 Program language translation.pptx
phases of a compiler
Ad

More from anwarkade1 (20)

PPT
CH 5 PUSH DOWN AUTOMATA PUSH DOWN AUTOMATA
PPTX
chapter 4 context-freegrammarintroduction context-freegramma
PPT
Chapter-2-lexical-analyser and its property lecture note.ppt
PPTX
chapter 1 automata and complexity theory lecture note.pptx
PPT
context-freelanguages and properties lecture note.ppt
PPTX
Chapter-twoChapter-three automata and complexity theory .pptx
PPT
Chapter-three automata and complexity theory.ppt
PDF
Construction of a predictive parsing table.pdf
PPTX
Deterministic Finite Automata (DFA).pptx
PPTX
INSTAL AND OPTIMIZE OPERATING SYSTEM WINDOWS
PPTX
tvet management system for analysis in school or college
PPTX
Computer Hardware Technology PowerPoint Templates.pptx
PPTX
BUSINESS PLAN PRERARATION GUIDE FINAL PPT.pptx
PPTX
introduction of enter pruner lecture note
PPTX
group 3 bussiness plan FOR MANAUFACTURIN
PPTX
window server 2008 mail configuration
PPT
Information Technology.ppt
PPT
informatics1.ppt
PPT
chapter one Introduction to HCI.ppt
PPT
Chapter 9 TCP IP Reference Model.ppt
CH 5 PUSH DOWN AUTOMATA PUSH DOWN AUTOMATA
chapter 4 context-freegrammarintroduction context-freegramma
Chapter-2-lexical-analyser and its property lecture note.ppt
chapter 1 automata and complexity theory lecture note.pptx
context-freelanguages and properties lecture note.ppt
Chapter-twoChapter-three automata and complexity theory .pptx
Chapter-three automata and complexity theory.ppt
Construction of a predictive parsing table.pdf
Deterministic Finite Automata (DFA).pptx
INSTAL AND OPTIMIZE OPERATING SYSTEM WINDOWS
tvet management system for analysis in school or college
Computer Hardware Technology PowerPoint Templates.pptx
BUSINESS PLAN PRERARATION GUIDE FINAL PPT.pptx
introduction of enter pruner lecture note
group 3 bussiness plan FOR MANAUFACTURIN
window server 2008 mail configuration
Information Technology.ppt
informatics1.ppt
chapter one Introduction to HCI.ppt
Chapter 9 TCP IP Reference Model.ppt
Ad

Recently uploaded (20)

PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PPTX
20th Century Theater, Methods, History.pptx
PDF
International_Financial_Reporting_Standa.pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Empowerment Technology for Senior High School Guide
PDF
Complications of Minimal Access-Surgery.pdf
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Computer Architecture Input Output Memory.pptx
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
Introduction to pro and eukaryotes and differences.pptx
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
My India Quiz Book_20210205121199924.pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Uderstanding digital marketing and marketing stratergie for engaging the digi...
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
20th Century Theater, Methods, History.pptx
International_Financial_Reporting_Standa.pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
Empowerment Technology for Senior High School Guide
Complications of Minimal Access-Surgery.pdf
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Unit 4 Computer Architecture Multicore Processor.pptx
Paper A Mock Exam 9_ Attempt review.pdf.
Computer Architecture Input Output Memory.pptx
B.Sc. DS Unit 2 Software Engineering.pptx

Unit-1compiler design and its lecture note .pptx

  • 1. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 1 Compiler Design Unit – 4 Pushdown Automata Chapter One Introduction to Compiler
  • 2. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 2 Topics to be covered  Translator  Analysis synthesis model of compilation  Phases of compiler  Difference between compiler & interpreter  Types of compiler  Context of compiler (Cousins of compiler)  Pass structure
  • 3. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 3 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)
  • 4. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 4 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 0000 1100 0010 0100 0111 1000 0001 1111 0101 1110 1100 0000 1000 1011 Target Program (If any)
  • 5. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 5 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)
  • 6. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 6 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)
  • 7. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 7 Analysis synthesis model of compilation
  • 8. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 8 Analysis synthesis model of compilation  There are two part 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
  • 9. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 9 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 consist 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
  • 10. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 10 Phases of compiler
  • 11. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 11 Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation
  • 12. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 12 Lexical analysis Position = initial + rate*60 Lexical analysis id1  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) = id2 + id3 * 60
  • 13. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 13 Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation
  • 14. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 14 Syntax analysis Position = initial + rate*60 Syntax analysis id1 = id2 + id3 * 60  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. Lexical analysis = id1 + id2 * id3 60
  • 15. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 15 Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation
  • 16. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 16 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
  • 17. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 17 Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation
  • 18. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 18 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
  • 19. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 19 Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation
  • 20. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 20 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
  • 21. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 21 Phases of compiler Compiler Analysis phase Synthesis phase Lexical analysis Syntax analysis Semantic analysis Intermediate code generation Code optimization Code generation
  • 22. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 22 Code generation  The intermediate code instructions are translated into sequence of machine instruction. Code generation MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2,R1 MOVF R1, id1 Code optimization t1= id3 * 60.0 id1 = id2 + t1
  • 23. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 23 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 Sr. Variable Name Type Address 1 Position Float 0001 2 Initial Float 0005 3 Rate Float 0009
  • 24. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 24 Exercise  Write output of all the phases of compiler for following statements: 1. x = b-c*2 2. I=p*n*r/100
  • 25. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 25 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
  • 26. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 26 Context of Compiler (Cousins of compiler)/Language Processing
  • 27. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 27 Context of compiler (Cousins of compiler) Preprocessor Skeletal Source Program It performs the following functions: 1. Macro processing 2. File inclusion 3. Rational preprocessor 4. Language extensions Preprocessor
  • 28. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 28 Context of compiler (Cousins of compiler) Preprocessor Skeletal Source Program 1. Macro processing: Allows user to define macros. Macro is shorthand for longer constructs. 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. Preprocessor
  • 29. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 29 Context of compiler (Cousins of compiler) Preprocessor Skeletal Source Program 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. Preprocessor
  • 30. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 30 Context of compiler (Cousins of compiler) Preprocessor Compiler Skeletal Source Program Source Program • A compiler is a program that reads a program written in source language and translates it into an equivalent program in target language. Compiler
  • 31. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 31 Context of compiler (Cousins of compiler) Preprocessor Compiler Assembler Skeletal Source Program Source Program Target Assembly Program • Assembler is a translator which takes the assembly program (mnemonic) as an input and generates the machine code as an output. Assembler
  • 32. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 32 Context of compiler (Cousins of compiler) Preprocessor Compiler Assembler Skeletal Source Program Source Program Target Assembly Program Relocatable Object Code Libraries & Object Files • 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. Linker Linker / Loader
  • 33. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 33 Context of compiler (Cousins of compiler) Preprocessor Compiler Assembler Skeletal Source Program Source Program Target Assembly Program Relocatable Object Code Absolute Machine Code Libraries & Object Files • The process of loading consists of: 1. Taking relocatable machine code. 2. Altering the relocatable address. 3. Placing the altered instructions and data in memory at the proper location. Loader Linker / Loader
  • 34. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 34 Front end & back end (Grouping of phases) Front end: 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 Back end: Depends on target machine and do not depend on source program. It includes following phases: 6. Code optimization 7. Code generation phase 8. Error handling and symbol table operation
  • 35. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 35 Pass structure
  • 36. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 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. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 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.  In Pass I: Perform analysis of the source program and note relevant information.  In Pass II: Generate target code using information noted in pass I.
  • 38. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 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. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 39 Types of compiler
  • 40. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 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. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology 41 End of Unit-1