SlideShare a Scribd company logo
2.4 From Regular Expression To DFAs

    An algorithm: translating a regular expression into a DFA via NFA.

    regular expression         NFA       DFA         program


2.4.1 From a regular expression to an NFA


    The idea of thompson’s construction:
      Use ε-transitions to “glue together” the machine of each piece of a regular
expression to form a machine that corresponds to the whole expression.

1. Basic regular expression:
      a basic regular expression is of the form a, ε,or φ

                 a




                 ε


2. Concatenation: rs
                                                 ε
                           r                                    s
                           ¨¨                                   ¨¨

  We have connected the accepting state of the machine of r to the start state of the
machine of s by anε-transition.
    The new machine has the start stale of the machine of r as its start state and the
accepting state of the machine of j as its accepting state.
    Clearly, this machine accepts L(rs) = L(r)L(s) and so corresponds to the regular
expression rs.

3. Choice among alternatives: r|s
             ε                  r
                                ¨¨                    ε




                                 s
             ε                                        ε
                                 ¨¨



   We have added a new start state and a new accepting state and connected them as
shown usingε-transitions.
   Clearly, this machine accepts the language L(r|s) =L(r )UL ( s).
                                             1
4. Repetition: construct a machine that corresponds to r*
       Given a machine that corresponds to r.




      We added two new states, a start state and an accepting state.

      The repetition in this machine is afforded by the newε-transition from the
 accepting state of the machine of r to its start state.

      We most also draw an ε-transition from the new start state to the new
 accepting state.

   This construction is not unique, simplifications are possible in the many cases.

Example 1: The regular expression ab|a

                                     a




                                         b




                                             2
Example 2: regular expression letter(letter|digit)*




  The machine for the choice letter |digit:
                            letter                              digit




 The NFA for the repetition (letter|digit) * as follows:
                                                      ε




    Finally, the NFA for the letter(letter|digit)*:        ε




2.4.2 from an NFA to a DFA


    Given an arbitrary NFA, construct an equivalent DFA. (i.e., one that accepts
precisely the same strings)
                                                3
We need some methods for:
    (1) Eliminating ε-transitions

         ε-closure: the set of all states reachable by ε-transitions from a state or states.

    (2) Eliminating multiple transitions from a state on a single input character.
        Keeping track of the set of states that are reachable by matching a single
         character.

    Both these processes lead us to consider sets of states instead of single states.
Thus, it is not surprising that the DFA we construct has as its states sets of states of the
original NFA.

The algorithm is called the subset construction.

1. The ε-closure of a Set of states:
   The ε-closure of a single state s is the set of states reachable by a series of zero or
more ε-transitions, and we write this set as s .
   Example 2.14: regular a*




   1 = { 1,2,4}, 2 ={2}, 3 ={2,3,4}, and 4 ={4}.

 The ε-closure of a set of states : the union of the ε-closures of each individual state.
          S= s
               sin S



 {1,3} = 1 ∪ 3 = {1,2,3}∪{2,3,4}={1,2,3,4}




                                             4
2. The Subset Construction:
   (1) compute the ε-closure of the start state of M; this becomes the start state of M
        .

    (2) For this set, and for each subsequent set, we compute transitions on characters
        a as follows.

              Given a set S of states and a character a in the alphabet,
              Compute the set
                  S′a = { t | for some s in S there is a transition from s to t on a }.
            Then, compute S a ' , the ε-closure of S′a.
           This defines a new state in the subset construction, together with a new
      transition S→ S a ' .

    (3) Continue with this process until no new states or transitions are created.

    (4) Mark as accepting those states constructed in this manner that contain an
     accepting state of M.

Example 2.15: consider the NFA of example 2.14




    M                        ε-closure of M ( S )           S′a
    1                        1,2,4                          2,3,4
    2,3,4                    2,3,4                          2,3,4

                                         a                     a




                                             5

More Related Content

PPT
Regular expressions-Theory of computation
PPT
Chapter Two(1)
PPT
Chapter Three(2)
PPT
Regular expression with DFA
PPT
Regular expressions and languages pdf
PPT
Lecture 03 lexical analysis
PPT
Context free grammars
Regular expressions-Theory of computation
Chapter Two(1)
Chapter Three(2)
Regular expression with DFA
Regular expressions and languages pdf
Lecture 03 lexical analysis
Context free grammars

What's hot (19)

PPT
Chapter Eight(2)
PDF
regular expressions (Regex)
PPT
Chapter 6 intermediate code generation
PDF
Converting A Subset of LTL Formula to Buchi Automata
PDF
CONVERTING A SUBSET OF LTL FORMULA TO BUCHI AUTOMATA
PDF
Automata
PDF
AI Lesson 13
PDF
AI Lesson 16
PPT
context free language
PPT
Intermediate code generation
PDF
Unit i
PPTX
Lecture 12 intermediate code generation
PPT
Unit05 dbms
PPTX
Join dependency
DOC
AUTOMATA THEORY - SHORT NOTES
PDF
Unit ii
PPTX
Managing I/O & String function in C
PPT
Context free grammar
Chapter Eight(2)
regular expressions (Regex)
Chapter 6 intermediate code generation
Converting A Subset of LTL Formula to Buchi Automata
CONVERTING A SUBSET OF LTL FORMULA TO BUCHI AUTOMATA
Automata
AI Lesson 13
AI Lesson 16
context free language
Intermediate code generation
Unit i
Lecture 12 intermediate code generation
Unit05 dbms
Join dependency
AUTOMATA THEORY - SHORT NOTES
Unit ii
Managing I/O & String function in C
Context free grammar
Ad

Viewers also liked (15)

DOC
Principles of Compiler Design
PPT
Chapter Three(1)
PPT
Theory of computing
PPTX
Two-way Deterministic Finite Automata
PDF
Regular language and Regular expression
PPT
Automata 3
PDF
Lecture 01 introduction to compiler
PPT
Automata 1
PPT
Automata 2
PPT
Automata 4
PPT
Automata 5
PPT
Deterministic Finite Automata
PPT
Compiler Design
PPT
02. chapter 3 lexical analysis
PPTX
Compiler Chapter 1
Principles of Compiler Design
Chapter Three(1)
Theory of computing
Two-way Deterministic Finite Automata
Regular language and Regular expression
Automata 3
Lecture 01 introduction to compiler
Automata 1
Automata 2
Automata 4
Automata 5
Deterministic Finite Automata
Compiler Design
02. chapter 3 lexical analysis
Compiler Chapter 1
Ad

Similar to Chapter 2 2 1 1 (20)

DOC
Chapter 2 2 1 2
PDF
Hwsoln03 toc
PDF
CS 162 Fall 2015 Homework 1 Problems September 29, 2015 Timothy Johnson 1. Ex...
DOC
Compiler Design Material 2
DOC
PCD ?s(MCA)
DOC
Pcd(Mca)
DOC
Pcd(Mca)
PDF
Automata_Theory_and_compiler_design_UNIT-1.pptx.pdf
PDF
Chapter 3 REGULAR EXPRESSION.pdf
PPTX
Unit2 Toc.pptx
PDF
Nondeterministic Finite Automata AFN.pdf
PPT
To lec 03
PDF
TOC 5 | Regular Expressions
PPT
0227 regularlanguages
PDF
Complier Design - Operations on Languages, RE, Finite Automata
PDF
Lex analysis
DOCX
unit 2 part b.docx
PDF
Problemes chap1 toc
PPT
String Matching with Finite Automata,Aho corasick,
PPT
Finite automata(For college Seminars)
Chapter 2 2 1 2
Hwsoln03 toc
CS 162 Fall 2015 Homework 1 Problems September 29, 2015 Timothy Johnson 1. Ex...
Compiler Design Material 2
PCD ?s(MCA)
Pcd(Mca)
Pcd(Mca)
Automata_Theory_and_compiler_design_UNIT-1.pptx.pdf
Chapter 3 REGULAR EXPRESSION.pdf
Unit2 Toc.pptx
Nondeterministic Finite Automata AFN.pdf
To lec 03
TOC 5 | Regular Expressions
0227 regularlanguages
Complier Design - Operations on Languages, RE, Finite Automata
Lex analysis
unit 2 part b.docx
Problemes chap1 toc
String Matching with Finite Automata,Aho corasick,
Finite automata(For college Seminars)

More from bolovv (7)

PPT
Chapter Seven(2)
PPT
Chapter Eight(3)
PPT
Chapter Five(2)
PPT
Chapter One
PPT
Chapter Seven(1)
PPT
Chapter Eight(1)
DOC
Chapter 1 1
Chapter Seven(2)
Chapter Eight(3)
Chapter Five(2)
Chapter One
Chapter Seven(1)
Chapter Eight(1)
Chapter 1 1

Recently uploaded (20)

PPTX
Machine Learning_overview_presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Mushroom cultivation and it's methods.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
1. Introduction to Computer Programming.pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
August Patch Tuesday
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Getting Started with Data Integration: FME Form 101
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Machine Learning_overview_presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mushroom cultivation and it's methods.pdf
NewMind AI Weekly Chronicles - August'25-Week II
1. Introduction to Computer Programming.pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
TLE Review Electricity (Electricity).pptx
August Patch Tuesday
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
OMC Textile Division Presentation 2021.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Spectral efficient network and resource selection model in 5G networks
Getting Started with Data Integration: FME Form 101
A comparative analysis of optical character recognition models for extracting...
Accuracy of neural networks in brain wave diagnosis of schizophrenia

Chapter 2 2 1 1

  • 1. 2.4 From Regular Expression To DFAs An algorithm: translating a regular expression into a DFA via NFA. regular expression NFA DFA program 2.4.1 From a regular expression to an NFA The idea of thompson’s construction: Use ε-transitions to “glue together” the machine of each piece of a regular expression to form a machine that corresponds to the whole expression. 1. Basic regular expression: a basic regular expression is of the form a, ε,or φ a ε 2. Concatenation: rs ε r s ¨¨ ¨¨ We have connected the accepting state of the machine of r to the start state of the machine of s by anε-transition. The new machine has the start stale of the machine of r as its start state and the accepting state of the machine of j as its accepting state. Clearly, this machine accepts L(rs) = L(r)L(s) and so corresponds to the regular expression rs. 3. Choice among alternatives: r|s ε r ¨¨ ε s ε ε ¨¨ We have added a new start state and a new accepting state and connected them as shown usingε-transitions. Clearly, this machine accepts the language L(r|s) =L(r )UL ( s). 1
  • 2. 4. Repetition: construct a machine that corresponds to r* Given a machine that corresponds to r. We added two new states, a start state and an accepting state. The repetition in this machine is afforded by the newε-transition from the accepting state of the machine of r to its start state. We most also draw an ε-transition from the new start state to the new accepting state. This construction is not unique, simplifications are possible in the many cases. Example 1: The regular expression ab|a a b 2
  • 3. Example 2: regular expression letter(letter|digit)* The machine for the choice letter |digit: letter digit The NFA for the repetition (letter|digit) * as follows: ε Finally, the NFA for the letter(letter|digit)*: ε 2.4.2 from an NFA to a DFA Given an arbitrary NFA, construct an equivalent DFA. (i.e., one that accepts precisely the same strings) 3
  • 4. We need some methods for: (1) Eliminating ε-transitions ε-closure: the set of all states reachable by ε-transitions from a state or states. (2) Eliminating multiple transitions from a state on a single input character. Keeping track of the set of states that are reachable by matching a single character. Both these processes lead us to consider sets of states instead of single states. Thus, it is not surprising that the DFA we construct has as its states sets of states of the original NFA. The algorithm is called the subset construction. 1. The ε-closure of a Set of states: The ε-closure of a single state s is the set of states reachable by a series of zero or more ε-transitions, and we write this set as s . Example 2.14: regular a* 1 = { 1,2,4}, 2 ={2}, 3 ={2,3,4}, and 4 ={4}. The ε-closure of a set of states : the union of the ε-closures of each individual state. S= s sin S {1,3} = 1 ∪ 3 = {1,2,3}∪{2,3,4}={1,2,3,4} 4
  • 5. 2. The Subset Construction: (1) compute the ε-closure of the start state of M; this becomes the start state of M . (2) For this set, and for each subsequent set, we compute transitions on characters a as follows. Given a set S of states and a character a in the alphabet, Compute the set S′a = { t | for some s in S there is a transition from s to t on a }. Then, compute S a ' , the ε-closure of S′a. This defines a new state in the subset construction, together with a new transition S→ S a ' . (3) Continue with this process until no new states or transitions are created. (4) Mark as accepting those states constructed in this manner that contain an accepting state of M. Example 2.15: consider the NFA of example 2.14 M ε-closure of M ( S ) S′a 1 1,2,4 2,3,4 2,3,4 2,3,4 2,3,4 a a 5