Noida Institute of Engineering and Technology, Greater Noida Noida Institute of Engineering and Technology, Greater Noida
Noida Institute of Engineering and Technology, Greater Noida Noida Institute of Engineering and Technology, Greater Noida
Unit: 1
Subject:
Compiler Design (RCS-602)
Nishant Kumar Hind
Course Details Assistant Professor
B Tech CSE 6th Sem CSE Department
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10PO11PO12
RCS-602.1 3 3 3 3 3 1 1 1 3 1 2 2
RCS-602.2 3 3 3 3 3 1 1 1 3 1 2 2
RCS-602.3 3 3 3 3 3 1 1 1 3 1 2 2
RCS-602.4 3 2 3 3 3 1 1 1 3 1 2 2
RCS-602.5 3 2 3 3 3 1 1 1 3 1 2 2
AVG 3 2.6 3 3 3 1 1 1 3 1 2 2
• Theory of Automata
• Algorithms
• Languages and machines
• Operating systems
• Computer architectures
Output
• Compiler:
– A program that translates a
source program written in high High level language
level language into
target/executable program
written in low level language is
called compiler.
• High Level Language: Compiler
– Which closer to human
understanding. Ex- C, C++, Java,
Pascal etc
• Low Level Language: Low level language
– Which is closer to computer
understanding. Ex- Machine
Language and assembly
language.
• Process of compilation
involves two major phases.
Source program
– Analysis Phase:
Where Source program is Source program
analyzed for errors. Analysis
– Synthesis Phase: Compiler
Where source is Synthesis
converted into target Target program
program. Target program
Lexical Analysis
Syntax Analysis
Code Optimization
Target Program
Nishant Kumar Hind KCS-502 CD Unit -1
08/11/2021 16
Phases of Compiler : Analogy of Natural language
Pre-Processor
Pre-Processed Code {#include, #define etc
Compiler
Target Assembly Code
Assembler
Re-locatable machine Code
Linker
Executable Machine Code {More obj. files
Loader
Memory
Processor
Nishant Kumar Hind KCS-502 CD Unit -1
08/11/2021 19
Pass of Compiler
• Pass: A Compiler Pass refers to the traversal of a compiler through the
entire program.
• Pass also refers to the grouping of phases in different module.
• Compiler pass are of two types:
– Single Pass Compiler
– Two Pass Compiler or Multi Pass Compiler
• Single Pass: If we combine or group all the phases of compiler design
in a single module known as single pass compiler.
• Multi Pass: 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 multi pass Compiler we divide phases in two or
more Module.
Lexical Analysis
Syntax Analysis
Semantic Analysis
Single Pass: All the
units are in one
Intermediate Code Generation module
Code Optimization
Lexical Analysis
Syntax Analysis
Code Optimization
Second Pass: Back
End
Target Code Generation
Lexical Analysis
S T
I
L M
P P M
C C M
A A M
M
S N
M Cross Compiler
S M
M
– Desired: S N
N
S N S N
S S N N
Desired Compiler is created
M
S M
M
– Desired: P N
N
P N
P
P N P N
P P N N
Desired Compiler is created
M
δ: Q X ∑ Q
QX∑ Q
(q0,a)
q0
(q0, b) One transition per input
q1 per state and no -moves
(q1, a)
(q1, b)
08/11/2021 Nishant Kumar Hind KCS-502 CD Unit -1 36
Nondeterministic Finite Automata (NFA)
(q0,a)
{}
(q0, b) {q0} Multiple transitions for
{q1} one input in a given state
(q1, a) {q0, q1}
(q1, b)
08/11/2021 Nishant Kumar Hind KCS-502 CD Unit -1 37
Representation of Finite Automata
0
q0 q1
1
1 0 Q ∑ 0 1
0
q0 q1 q0 q1 q0
1
q1 q1 q0
• R.S is a regular expression R S
08/11/2021 Nishant Kumar Hind KCS-502 CD Unit -1 41
Precedence of Operators in Regular
Expression
D_States ∑ 0 1
Move(A,0) = { 5 }
Move(A,1) = { 3,9 } ε-Closure(0) = {0,1,2,4,7,8} =A B C
Move(B,0) = { 5 }
Move(B,1) = { 3 ,9} ε-Closure(5) = {1,2,4,5,6,7,8} =B B C
Move(C,0) = { 5 } B C
ε-Closure(3,9)= {1,2,3,4,6,7,8,9} = C
Move(C,1) = { 3 ,9}
NFA
DFA
Regular
expressions
Table-driven
Lexical Implementation of DFA
Specification
token
Source Lexical Syntax To semantic
program Analyzer Analysis analysis
getNextToken
Symbol
table
• List out all the alphabets, characters and tokens with their pattern
allowed in the language:
digit -> [0-9]
Digits -> digit+
number -> digit(.digits)? (E[+-]? Digit)?
letter -> [A-Za-z_]
id -> letter (letter|digit)*
If -> if
Then -> then
Else -> else
Relop -> < | > | <= | >= | = | <>
White_space -> (blank | tab | newline)+
• Lex Source
program
• LEX • lex.yy.c
• lex.l Compiler
• lex.yy.c • C • a.out
• compiler
• Sequen
• Input • a.out ce of
stream
tokens
declarations
%%
translation rules Pattern {Action}
%%
auxiliary functions
• Terminals
• Non terminals expression -> expression + term
• Start symbol expression -> expression – term
• productions expression -> term
term -> term * factor
term -> term / factor
term -> factor
factor -> (expression)
factor -> id
• -(id+id)
• E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
• For some strings there exist more than one parse tree
• Or more than one leftmost derivation
• Or more than one rightmost derivation
• Example: id+id*id
• NPTEL
• https://youtu.be/trocRZqxZFM
• https://youtu.be/-Ut1b1xEbCo
• https://youtu.be/UMnllso8znw
Thank You