SlideShare a Scribd company logo
1 of 24
System Software (5KS03)
Unit 1 : Introduction to Compiling
Lecture : 6 Design of lexical analyzer generator,
State minimization of a DFA
A S Kapse,
Assistant Professor,
Department Of Computer Sci. & Engineering
Anuradha Engineering College, Chikhli
Contents…
 Design of lexical analyzer generator,
 State minimization of a DFA
Objectives…
 Upon completion of this lecture, you will be able
 To understand the Lexical Analysis
 To understand Role of lexical analyses
 To understand input buffering & state minimization of
DFA
Review…./ Concepts
 What do you mean by State?
 What do you mean by NFA & DFA?
 What do you mean by Parse tree?
5
Deterministic and Nondeterministic
Automata
 Deterministic Finite Automata (DFA)
 One transition per input per state
 No -moves
 Nondeterministic Finite Automata (NFA)
 Can have multiple transitions for one input in a given state
 Can have -moves
 Finite automata have finite memory
 Need only to encode the current state
6
Execution of Finite Automata
 A DFA can take only one path through the state
graph
 Completely determined by input
 NFAs can choose
 Whether to make -moves
 Which of multiple transitions for a single input to take
7
Acceptance of NFAs
 An NFA can get into multiple states
• Input:
0
1
1
0
1 0 1
• Rule: NFA accepts if it can get in a final state
8
NFA vs. DFA (1)
 NFAs and DFAs recognize the same set of
languages (regular languages)
 DFAs are easier to implement
 There are no choices to consider
9
NFA vs. DFA (2)
 For a given language the NFA can be simpler than
the DFA
0
1
0
0
0
1
0
1
0
1
NFA
DFA
• DFA can be exponentially larger than NFA
10
Regular Expressions to Finite Automata
 High-level sketch
Regular
expressions
NFA
DFA
Lexical
Specification
Table-driven
Implementation of DFA
11
Regular Expressions to NFA (1)
 For each kind of rexp, define an NFA
 Notation: NFA for rexp A
A
• For 

• For input a
a
12
Regular Expressions to NFA (2)
 For AB
A B

• For A | B
A
B




13
Regular Expressions to NFA (3)
 For A*
A



14
Example of RegExp -> NFA conversion
 Consider the regular expression
(1 | 0)*1
 The NFA is

1C E
0D F


B


G



A H
1
I J
15
Next
Regular
expressions
NFA
DFA
Lexical
Specification
Table-driven
Implementation of DFA
16
NFA to DFA. The Trick
 Simulate the NFA
 Each state of resulting DFA
= a non-empty subset of states of the NFA
 Start state
= the set of NFA states reachable through -moves from
NFA start state
 Add a transition S a S’ to DFA iff
 S’ is the set of NFA states reachable from the states in S
after seeing the input a
 considering -moves as well
17
NFA -> DFA Example
1
0
1
 






A B
C
D
E
F
G H I J
ABCDHI
FGABCDHI
EJGABCDHI
0
1
0
1
0 1
18
NFA to DFA. Remark
 An NFA may be in many states at any time
 How many different states ?
 If there are N states, the NFA must be in some
subset of those N states
 How many non-empty subsets are there?
 2N - 1 = finitely many, but exponentially many
19
Implementation
 A DFA can be implemented by a 2D table T
 One dimension is “states”
 Other dimension is “input symbols”
 For every transition Si a Sk define T[i,a] = k
 DFA “execution”
 If in state Si and input a, read T[i,a] = k and skip to state
Sk
 Very efficient
20
Table Implementation of a DFA
S
T
U
0
1
0
1
0 1
0 1
S T U
T T U
U T U
21
Implementation (Cont.)
 NFA -> DFA conversion is at the heart of tools such
as flex or jflex
 But, DFAs can be huge
 In practice, flex-like tools trade off speed for space in
the choice of NFA and DFA representations
Video on Compilers
1. Design of lexical analyzer generator,
Questions..
1. What are the steps associated with transformation from
NFA to DFA?
2.
Homework..
1. What is parser?
2. Define a translator and categorize it.

More Related Content

What's hot

24 Hours Later - NDC London 2014
24 Hours Later -  NDC London 201424 Hours Later -  NDC London 2014
24 Hours Later - NDC London 2014Phillip Trelford
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 
Assembly language i
Assembly language iAssembly language i
Assembly language iVivek Kumar
 
Instruction set 8085
Instruction set 8085Instruction set 8085
Instruction set 8085varun sukheja
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 80869840596838
 
Lecture 05 pic io port programming
Lecture 05 pic io port programmingLecture 05 pic io port programming
Lecture 05 pic io port programmingVajira Thambawita
 

What's hot (13)

Compiler lesson plan
Compiler lesson planCompiler lesson plan
Compiler lesson plan
 
24 Hours Later - NDC London 2014
24 Hours Later -  NDC London 201424 Hours Later -  NDC London 2014
24 Hours Later - NDC London 2014
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Instruction set
Instruction setInstruction set
Instruction set
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Addressing modes
Addressing modesAddressing modes
Addressing modes
 
Assembly language i
Assembly language iAssembly language i
Assembly language i
 
Instruction set 8085
Instruction set 8085Instruction set 8085
Instruction set 8085
 
Deep speech
Deep speechDeep speech
Deep speech
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Lecture 05 pic io port programming
Lecture 05 pic io port programmingLecture 05 pic io port programming
Lecture 05 pic io port programming
 

Similar to State Minimization DFA Lexical Analyzer

Implementation of lexical analyser
Implementation of lexical analyserImplementation of lexical analyser
Implementation of lexical analyserArchana Gopinath
 
MidtermI-review.pptx
MidtermI-review.pptxMidtermI-review.pptx
MidtermI-review.pptxamara jyothi
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxMeghnadh
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite AutomataArchana Gopinath
 
Finals-review.pptx
Finals-review.pptxFinals-review.pptx
Finals-review.pptxamara jyothi
 
Assembly language (Example with mapping from C++ to Assembly)
Assembly language (Example with mapping from C++ to Assembly)Assembly language (Example with mapping from C++ to Assembly)
Assembly language (Example with mapping from C++ to Assembly)Tish997
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxamara jyothi
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxranjan317165
 
Principles of Compiler Design
Principles of Compiler DesignPrinciples of Compiler Design
Principles of Compiler DesignBabu Pushkaran
 
Introduction to the theory of computation
Introduction to the theory of computationIntroduction to the theory of computation
Introduction to the theory of computationprasadmvreddy
 

Similar to State Minimization DFA Lexical Analyzer (20)

SS UI Lecture 5
SS UI Lecture 5SS UI Lecture 5
SS UI Lecture 5
 
Implementation of lexical analyser
Implementation of lexical analyserImplementation of lexical analyser
Implementation of lexical analyser
 
MidtermI-review.pptx
MidtermI-review.pptxMidtermI-review.pptx
MidtermI-review.pptx
 
TOC question bank.pdf
TOC question bank.pdfTOC question bank.pdf
TOC question bank.pdf
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptx
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Finals-review.pptx
Finals-review.pptxFinals-review.pptx
Finals-review.pptx
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
 
Assembly language (Example with mapping from C++ to Assembly)
Assembly language (Example with mapping from C++ to Assembly)Assembly language (Example with mapping from C++ to Assembly)
Assembly language (Example with mapping from C++ to Assembly)
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptx
 
FiniteAutomata_anim.pptx
FiniteAutomata_anim.pptxFiniteAutomata_anim.pptx
FiniteAutomata_anim.pptx
 
Pcd(Mca)
Pcd(Mca)Pcd(Mca)
Pcd(Mca)
 
PCD ?s(MCA)
PCD ?s(MCA)PCD ?s(MCA)
PCD ?s(MCA)
 
Principles of Compiler Design
Principles of Compiler DesignPrinciples of Compiler Design
Principles of Compiler Design
 
Pcd(Mca)
Pcd(Mca)Pcd(Mca)
Pcd(Mca)
 
Compiler Design Material 2
Compiler Design Material 2Compiler Design Material 2
Compiler Design Material 2
 
COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis: COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis:
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
Introduction to the theory of computation
Introduction to the theory of computationIntroduction to the theory of computation
Introduction to the theory of computation
 

More from Avinash Kapse

More from Avinash Kapse (8)

Presentation1
Presentation1Presentation1
Presentation1
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Presentation1
Presentation1Presentation1
Presentation1
 
SS UII Lecture 1
SS UII Lecture 1SS UII Lecture 1
SS UII Lecture 1
 
SS UI Lecture 4
SS UI Lecture 4SS UI Lecture 4
SS UI Lecture 4
 
SS UI Lecture 1
SS UI Lecture 1SS UI Lecture 1
SS UI Lecture 1
 
Ss ui lecture 2
Ss ui lecture 2Ss ui lecture 2
Ss ui lecture 2
 
Ss ui lecture 1
Ss ui lecture 1Ss ui lecture 1
Ss ui lecture 1
 

State Minimization DFA Lexical Analyzer

  • 1. System Software (5KS03) Unit 1 : Introduction to Compiling Lecture : 6 Design of lexical analyzer generator, State minimization of a DFA A S Kapse, Assistant Professor, Department Of Computer Sci. & Engineering Anuradha Engineering College, Chikhli
  • 2. Contents…  Design of lexical analyzer generator,  State minimization of a DFA
  • 3. Objectives…  Upon completion of this lecture, you will be able  To understand the Lexical Analysis  To understand Role of lexical analyses  To understand input buffering & state minimization of DFA
  • 4. Review…./ Concepts  What do you mean by State?  What do you mean by NFA & DFA?  What do you mean by Parse tree?
  • 5. 5 Deterministic and Nondeterministic Automata  Deterministic Finite Automata (DFA)  One transition per input per state  No -moves  Nondeterministic Finite Automata (NFA)  Can have multiple transitions for one input in a given state  Can have -moves  Finite automata have finite memory  Need only to encode the current state
  • 6. 6 Execution of Finite Automata  A DFA can take only one path through the state graph  Completely determined by input  NFAs can choose  Whether to make -moves  Which of multiple transitions for a single input to take
  • 7. 7 Acceptance of NFAs  An NFA can get into multiple states • Input: 0 1 1 0 1 0 1 • Rule: NFA accepts if it can get in a final state
  • 8. 8 NFA vs. DFA (1)  NFAs and DFAs recognize the same set of languages (regular languages)  DFAs are easier to implement  There are no choices to consider
  • 9. 9 NFA vs. DFA (2)  For a given language the NFA can be simpler than the DFA 0 1 0 0 0 1 0 1 0 1 NFA DFA • DFA can be exponentially larger than NFA
  • 10. 10 Regular Expressions to Finite Automata  High-level sketch Regular expressions NFA DFA Lexical Specification Table-driven Implementation of DFA
  • 11. 11 Regular Expressions to NFA (1)  For each kind of rexp, define an NFA  Notation: NFA for rexp A A • For   • For input a a
  • 12. 12 Regular Expressions to NFA (2)  For AB A B  • For A | B A B    
  • 13. 13 Regular Expressions to NFA (3)  For A* A   
  • 14. 14 Example of RegExp -> NFA conversion  Consider the regular expression (1 | 0)*1  The NFA is  1C E 0D F   B   G    A H 1 I J
  • 16. 16 NFA to DFA. The Trick  Simulate the NFA  Each state of resulting DFA = a non-empty subset of states of the NFA  Start state = the set of NFA states reachable through -moves from NFA start state  Add a transition S a S’ to DFA iff  S’ is the set of NFA states reachable from the states in S after seeing the input a  considering -moves as well
  • 17. 17 NFA -> DFA Example 1 0 1         A B C D E F G H I J ABCDHI FGABCDHI EJGABCDHI 0 1 0 1 0 1
  • 18. 18 NFA to DFA. Remark  An NFA may be in many states at any time  How many different states ?  If there are N states, the NFA must be in some subset of those N states  How many non-empty subsets are there?  2N - 1 = finitely many, but exponentially many
  • 19. 19 Implementation  A DFA can be implemented by a 2D table T  One dimension is “states”  Other dimension is “input symbols”  For every transition Si a Sk define T[i,a] = k  DFA “execution”  If in state Si and input a, read T[i,a] = k and skip to state Sk  Very efficient
  • 20. 20 Table Implementation of a DFA S T U 0 1 0 1 0 1 0 1 S T U T T U U T U
  • 21. 21 Implementation (Cont.)  NFA -> DFA conversion is at the heart of tools such as flex or jflex  But, DFAs can be huge  In practice, flex-like tools trade off speed for space in the choice of NFA and DFA representations
  • 22. Video on Compilers 1. Design of lexical analyzer generator,
  • 23. Questions.. 1. What are the steps associated with transformation from NFA to DFA? 2.
  • 24. Homework.. 1. What is parser? 2. Define a translator and categorize it.