0% found this document useful (0 votes)
0 views

Unit 1 Combined

The document introduces the concepts of Automata, Formal Languages, and Logic, explaining the definitions and significance of automata in computer science. It discusses various types of automata, including Finite State Machines (FSM) and Turing Machines, and their applications in fields such as robotics, compiler design, and natural language processing. Additionally, it covers formal languages, their structure, and how they relate to automata theory.

Uploaded by

cahap48948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Unit 1 Combined

The document introduces the concepts of Automata, Formal Languages, and Logic, explaining the definitions and significance of automata in computer science. It discusses various types of automata, including Finite State Machines (FSM) and Turing Machines, and their applications in fields such as robotics, compiler design, and natural language processing. Additionally, it covers formal languages, their structure, and how they relate to automata theory.

Uploaded by

cahap48948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 436

Automata Formal Languages & Logic

Introduction to the Course


Prakash C O
Department of Computer Science & Engineering
Automata Formal Languages & Logic
Introduction

Dictionary Definition of an Automaton


noun (plural automata)
1. A moving mechanical device made in imitation of a human being.

2. A machine that performs a function according to a predetermined set of


coded instructions.
Automata Formal Languages & Logic
Introduction

➢ The term “Automata” comes from the Greek word “αὐτόματα” which implies
“self-acting”. An automaton (Automata in plural) is an abstract self-propelled
machine which follows a predetermined sequence of operations automatically.

➢ What is Automata Theory?

Study of abstract computing devices, or “machines”


Automaton = an abstract computing device
Note: A “device” need not even be a physical hardware!

➢ A fundamental question in computer science:


Find out what different models of machines can do and cannot do
Automata Formal Languages & Logic
Introduction

What is Finite State Machine (FSM)?


➢ An automaton with a finite number of states is named a Finite Automaton (FA)
or Finite State Machine (FSM).

➢ Finite-state machine (FSM) or finite-state automaton (FSA) is an (abstract)


mathematical model of computation. FSM can be in exactly one of a finite number of
states at any given time.
Automata Formal Languages & Logic
Introduction

What is Finite State Machine (FSM)? Cont..

➢ The FSM can change from one state to another in response to sequence of inputs;
the change from one state to another is called a transition.
➢ An FSM is defined by a list of its states, its initial state, its final state(s) and the

inputs that trigger each transition.


Automata Formal Languages & Logic
Introduction

The Turing Machine is a theoretical


model of a simple computing device
that can perform calculations and
manipulate symbols on an infinite
tape.
A Turing machine is an improved
version of the Finite state Machine.

Most widely used model of computation in computability and complexity theory


Automata Formal Languages & Logic
Introduction

Finite State Machine (Finite State Automata)


➢ Example 3: coin-operated turnstile

➢ Example 4: Controller used for an automatic sliding door (simple example of a FSM)
Automata Formal Languages & Logic
Introduction

Finite State Automata (FSA) / Finite State Machine (FSM)


➢ Through automata, computer scientists are able to understand how machines
compute functions and solve problems and more importantly, what it means
for a function to be defined as computable or for a question to be described
as decidable.

➢ There are four major families of automaton:


1. Finite-state machines (Acceptors/Recognizers and Transducers)
1. Pushdown automata
2. Linear-bounded automata
3. Turing machine
Automata Formal Languages & Logic
Introduction

Finite State Automata (FSA) / Finite State Machine (FSM)


Automata Formal Languages & Logic
Introduction

Finite State Automata/ Finite State Machine

Introduced first by two neuro-psychologist Warren S. McCullough and Walter Pitts in


1943 as a model for human brain!

Finite automata can naturally model microprocessors and even software programs working on
variables with bounded domain – capture so-called regular sets of sequences that occur in many
different fields (logic, algebra, regEx).
Applications in digital circuit/protocol verification, compilers, pattern recognition/matching, etc.
Automata Formal Languages & Logic
Introduction – Formal Languages

Formal Languages:

➢In logic, mathematics, computer science, and linguistics, a formal language


consists of words (or strings) whose letters are taken from an finite non-
empty alphabet and are well-formed according to a specific set of rules
called a formal grammar.

➢Words that belong to a particular formal language are sometimes called well-
formed words or well-formed formulas. A formal language is often defined by
means of a formal grammar such as a regular grammar or context-free
grammar, which consists of its formation rules.
Automata Formal Languages & Logic
Introduction – Formal Languages

Formal Languages:
➢Automata theory is closely related to formal language theory. In this context,
automata are used as finite representations of formal languages that may be infinite.

➢Automata(or FSMs) are often classified by the class of formal languages they can
recognize, as in the Chomsky hierarchy, which describes a nesting relationship
between major classes of automata.

➢A formal language is designed for use in situations in which natural language is


unsuitable, as for example in mathematics, logic, or computer programming.
The symbols and formulas of such languages stand in precisely specified syntactic and
semantic relations to one another.
Automata Formal Languages & Logic
Introduction – Formal Languages

➢ Formal languages are languages which only consider about the well-formedness nothing
else. That means the sufficient and necessary condition is to adhere to its rules.

➢ Here are some examples of formal languages:

❑ L = Σ*, the set of all words over Σ;

❑ L = {a}* = {an}, where n ranges over the natural numbers and "an" means "a" repeated n times (this
is the set of words consisting only of the symbol "a");

❑ The set of syntactically correct programs in a given programming language (the syntax of which is
usually defined by a context-free grammar);

❑ The set of inputs upon which a certain Turing machine halts; or

❑ The set of maximal strings of alphanumeric ASCII characters on this line, i.e.,
the set {the, set, of, maximal, strings, alphanumeric, ASCII, characters, on, this, line, i, e}.
Automata Formal Languages & Logic
Introduction – Formal Languages

➢ Formal languages are normally defined in one of three ways, all of which
can be described by automata theory:

1) Regular expressions (only for regular languages)


• Regular Expressions are an algebraic way to describe regular languages.
2) Standard Automata - that accepts (or recognizes) the language.

3) A formal grammar system - that generates the language


• Regular (Type-3) grammars - generate the regular languages - recognized by FSM.

• Context-free (Type-2) grammars - generate the context-free languages - recognized by PDA.


• Context-sensitive (Type-1) grammars - generate context-sensitive languages - recognized by LBA.
• Recursively enumerable (Type-0 or Unrestricted grammar) grammars - generate recursively
enumerable or Turing-recognizable languages - recognized by TM.
Automata Formal Languages & Logic
Introduction – Formal Languages

➢ Formal languages are normally defined in one of three ways, all of which
can be described by automata theory:

Formal Language L = {, a, aa, aaa, aaaa, … }

➢1) Regular expressions (only for regular languages)


Regex a*

2) Standard Automata - that accepts (or recognizes) the language

Automata

3) A formal grammar system S → aS


Grammar
S→
Automata Formal Languages & Logic
Applications

1. Robotics and Control Systems:


FSMs are used to model the behaviour of Robots and Control systems.
2. Compiler Design:
Finite automata are extensively used in Compiler Design for Lexical analysis, which
involves tokenizing and scanning the source code of a programming language.
Lexical analyzers employ finite automata to recognize and classify different lexical
units such as keywords, identifiers, operators, literals and punctation symbols.

➢ Lexical analysis (Finite state automata),


➢ Syntactical analysis (Pushdown automata),
➢ Code selection (Tree automata)
Automata Formal Languages & Logic
Applications

3. Pattern Recognition: Finite automata are employed in pattern-matching and


recognition tasks. Applications include text processing, string matching, and searching
algorithms.

4. Implementing artificially intelligent agents in games, but many games include


characters with simple, state-based behaviors that are easily and effectively modeled
using state machines.

5. Natural Language Processing: Finite automata are used in natural language processing
for tasks such as text tokenization, morphological analysis, and part-of-speech tagging.
They help in parsing and understanding the structure of natural language sentences.
Automata Formal Languages & Logic
Applications

• 6. Digital Circuit Design: Finite automata can model the behavior of digital circuits and
aid in their design and testing. Sequential circuits, such as counters and state machines,
can be represented and analyzed using finite automata.

• 7. Network Protocol Design and Analysis: Network Protocol Design and Analysis: FSMs
are employed to model and analyze communication protocols (e.g., TCP/IP, HTTP, …),
They help to ensure correct message sequencing and behaviour.

• 8. Software Engineering: State machines are extensively used in software


development to model the behaviour of complex systems, such as user interfaces,
protocol implementations, and control systems.

9. ….
Automata Formal Languages & Logic
Introduction - Course Textbook
Automata Formal Languages & Logic
Introduction - Course Evaluation Policy
Automata Formal Languages & Logic
Introduction - Additional Info

In automata theory, combinational logic is a type of digital logic that is implemented


by Boolean circuits, where the output is a pure function of the present input only. This
is in contrast to sequential logic, in which the output depends not only on the present
input but also on the history of the input. In other words, sequential logic
has memory while combinational logic does not.
Automata Formal Languages & Logic
Introduction – Additional Info

ω-automaton (or stream automaton)


➢ In automata theory, an ω-automaton (or stream automaton) is a variation of a finite
automaton that runs on infinite, rather than finite, strings as input.

➢ An ω-automaton either accepts or rejects infinite inputs. Since ω-automata do not


stop, they have a variety of acceptance conditions rather than simply a set of
accepting states.

➢ Classes of ω-automata include the Büchi automata, Rabin automata, Streett


automata, parity automata and Muller automata, each deterministic or non-
deterministic. These classes of ω-automata differ only in terms of acceptance
condition.
Automata Formal Languages & Logic
Introduction – Additional Info

Tree Automata:
➢ Most machine models like Turing machines and finite automata, work on inputs that are
strings over some finite alphabet. However, often problem descriptions have inputs
that more structured than strings. For example, the input could be a graph, or a tree,
or a partial order, etc.
Automata Formal Languages & Logic
Introduction – Additional Info

Automata and Logic:


➢ The connection between automata and logic goes back to work of Biichi [Bii60] and
Elgot [EI61], who showed that finite automata and monadic second-order
logic (interpreted over finite words) have the same expressive power, and that the
transformations from logic formulas to automata and vice versa are effective.

➢ Mathematical logic and automata theory are two scientific disciplines with a
fundamentally close relationship.
Automata Formal Languages & Logic
Introduction – Additional Info

Automata and Logic:


➢ Example:
Automata Formal Languages & Logic
Unit 1 – Basic Notations

Prakash C O
Department of Science & Engineering
Automata Formal Languages & Logic
Unit 1 – Basic Notations

▪ Alphabet (𝜮 )
▪ String or word (w)
▪ Empty String [ (“epsilon”) or λ (“lambda”)]
▪ Length of a String ( |w| )
▪ Powers of an Alphabet (𝜮k )
▪ Kleene Closure or Kleene Star or Kleene operator(*)
▪ Kleene Plus (+)
▪ Zero or one occurrence (?)
▪ Language ( L )
Automata Formal Languages & Logic
Unit 1 – Basic Notations

Alphabet (𝜮):

➢ An alphabet is a finite, non-empty set of symbols.


➢ We use the symbol ∑ (sigma) to denote an alphabet.
3

➢ Examples:
▪ Binary: ∑ = {0,1}
▪ All lower case letters: ∑ = {a,b,c,..z}
▪ Alphanumeric: ∑ = {a-z, A-Z, 0-9}
▪ DNA molecule letters: ∑ = {a,c,g,t}

Automata Formal Languages & Logic
Unit 1 – Basic Notations

String or word (w):


• A string or word is a finite sequence of symbols chosen from ∑

• Empty string is  (“epsilon”) or λ (“lambda”)


4

• Length of a string w, denoted by “|w|”, is equal to the number of


(non- ) characters in the string
E.g., if w = 010100 then |w| = 6
if w = 010100 then |w| = ?
if w =  then |w| = ?
• xy = concatentation of two strings x and y
Automata Formal Languages & Logic
Unit 1 – Basic Notations

Powers of an alphabet:
• Let ∑ be an alphabet.
• ∑k = the set of all strings of length k

Example: if 𝜮 = {a, b} then,


𝜮0 = {𝜆} or {} i.e., the set of all strings of length 0
𝜮1 = {a, b} i.e., the set of all strings of length 1
𝜮2 = {aa, bb, ab, ba} i.e., the set of all strings of length 2

…..
Automata Formal Languages & Logic
Unit 1 – Basic Notations

Kleene star (or Kleene closure): Kleene star is a unary operation, either
on sets of strings or on sets of symbols or characters.
The application of the Kleene star to an alphabet ∑ is written as ∑*
If ∑ is an alphabet, then ∑* is the set of all strings over symbols in ∑,
including the empty string ε.
The definition of Kleene star on ∑ is
∑* = ∑0 U ∑1 U ∑2 U …
Kleene plus: The Kleene plus omits the ∑0 term in the above union.
In other words, the Kleene plus on ∑ is
∑+ = ∑1 U ∑2 U ∑3 U … i.e., the set of strings of length > 0
Automata Formal Languages & Logic
Unit 1 – Basic Notations

Language(L): L is a said to be a language over alphabet ∑, only if L  ∑*


⇒ this is because ∑* is the set of all strings (of all possible length
including 0) over the given alphabet ∑
Examples:
Let L be the language of all strings consisting of n 0’s followed by n 1’s (i.e., 0n1n):
L = {,01,0011,000111,…}
Let L be the language of all strings of with equal number of 0’s and 1’s:
L = {,01,10,0011,1100,0101,1010,1001,…}

Definition: Ø denotes the Empty language, Let L = {}; Is L=Ø?


Automata Formal Languages & Logic
Unit 1 – Basic Notations

Finite or Infinite Languages:

Finite Language has a finite set of words/strings.

L = set of string over 𝜮 = {0, 1} such that |w| = 2

L = {00, 01, 10, 11}

Infinite Language has a infinite set of words/strings.


L = set of string over 𝜮 = {0, 1} such that |w| > 2
L = { 000, 101, 010, 011, 100, 110, 001, 111, 1100, ………………. }
Automata Formal Languages & Logic
Unit 1 – Basic Notations

The Membership Problem


Given a string w∑* and a language L over ∑, decide whether wL or not.
Example:
Let w = 100011 and ∑ ={0,1}
Q) Is w  the language of strings with equal number of 0s and 1s?
Automata Formal Languages & Logic
Unit 1 – DFA

Preet Kanwal and Prakash C O


Department of Computer Science & Engineering
Automata Formal Languages & Logic

Unit 1: Deterministic Finite Automata (DFA)

Preet Kanwal and Prakash C O


Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Formal Definition of a DFA


A DFA can be represented by a 5-tuple M=(Q, ∑, δ, q0, F) where

▪ Q is a finite set of states.

▪ ∑ is a finite non-empty set of input symbols called the alphabet.

▪ δ is the transition function where δ: Q × ∑ → Q

▪ q0 is the initial state from where any input is processed (q0 ∈ Q).

▪ F is a set of final state/states (F ⊆ Q).


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Machine M can be defined in 3 different ways:


1. Transition Diagram or State Transition Diagram or State Diagram
2. Transition Table
3. Transition Function
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

➢ Each finite automaton consists of a set of states connected by


transitions.
➢ Symbols used in Constructing a Finite Automata as Transition Diagram:
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

➢ The below Transition Diagrams are equivalent (or same):


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

DFA:

The automaton in the above figure can be represented as (Q, ∑, δ, q0, F)


where
➢ Q = {q0,q1},
➢ ∑ = {0,1},
➢ q0 = q0,
➢ F = {q0}, and
➢ Transition function δ is such that
δ(q0,0) = q1, δ(q0,1) = q1, and δ(q1,0) = q0, δ(q1,1) = q0.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Let’s draw the state diagram of the following automaton (Q, ∑, δ, q0, F)
Q = {q1, q2, q3}
∑ = {0,1},
δ is given in a tabular form below (i.e., transition table):

q1 is the initial state, and F = { q2 }.

What does it accept?


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

➢ In automata theory, a finite-state machine is called a deterministic


finite automaton (DFA),

1. If each of its transitions is uniquely determined by its source state and


input symbol, and

2. For every state, a transition is required for all the input symbols in ∑.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

The Transition Function of a DFA is δ: Q × ∑ → Q


➢ In DFA, for each state there must be exactly one transition defined for each
symbol in Σ. This is the “deterministic” part of DFA.
or
➢ In DFA, the transition for every state and input symbol is uniquely defined, there
is no ambiguity or choice in the transitions.
or
➢ DFA is a finite-state machine that accepts or rejects a given string of symbols, by
running through a state sequence uniquely determined by the
string. Deterministic refers to the uniqueness of the computation run.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

The Word (or string) acceptance by FA / FSA / FSM :


➢ A finite automaton does not accept as soon as it enters an accepting
state.
➢ A finite automaton accepts if its run ends in an accepting state.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

The Language acceptance by FA / FSA / FSM :


➢ The language accepted by a DFA M = (Q,∑, 𝛿, q0, F) is the set of all
strings on ∑ accepted by M.

➢ So, for an automaton to accept a particular language (e.g., {anbm ∣


n,m≥1}), it must not only accept every string in the language, but
also reject every string not in the language.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Computation or run of a DFA


➢ For an input string, DFA will create an unique ‘computation
sequence’.

➢ A DFA accepts a string if run (or computation) leads to some accept


state.

The Language of a DFA M is L(M) = {w | run of M on w is accepting}.


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Computation or run of a DFA


➢ Example: Given w = aababb and Machine M

A run of M on w is

a a b a b b
q0 → q1 → q1 → q 2 → q1 → q2 → q 2 Accepted run/Computation because q2 ∈ F
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Computation or run of a DFA


➢ Example: Given w = aabbba and Machine M

A run of M on w is

a a b b b a
q0 → q1 → q1 → q 2 → q2 → q2 → q 1 Rejected run/Computation because q1 ∉ F
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Approach to Construct a DFA that recognises a language L:


➢ Step-1: Try to enumerate all the Strings in the language i.e.,
* Specify the minimal string and then the next minimal strings, …

* Enumerate the strings in the order of increasing length

* If possible discover a pattern in the enumerated strings

➢ Step-2: Draw a DFA skeleton of the automata based on the minimal


string or the pattern discovered.

➢ Step-3: Complete the DFA


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 1:
Construct DFA for the Language of strings of length 2 , over Σ ={a,b} .

Solution :

Note Transition diagram is a directed graph.


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 1:
Construct DFA for the Language of strings of length 2 , over Σ ={a,b} .

Solution : L = {aa, bb, ab, ba}

Trap/Dead state

Note1: strings of length 1 are rejected in q1 and strings of length>2 are rejected in q3

Note2: For every state, transition is required for all the input symbols in ∑
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 1a:
Construct DFA for the Language of strings of even length, over Σ ={a,b} .

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 1a:
Construct DFA for the Language of strings of even length, over Σ ={a,b} .

Solution : L = {, aa, bb, ab, ba, aaaa, bbbb, abbb, … }


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 1b:
Construct DFA for the Language of strings having even number of a’s, over Σ ={a, b} .

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 1b:
Construct DFA for the Language of strings having even number of a’s, over Σ ={a, b} .

Solution :
L = {, aa, aaaa, aaaaaa, aaaaaaaa, … , b, bb, bbb, bbbb, bbbbb, …
aab, baa, aba, … … }
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Exercise:
Construct DFA for the Language of strings having odd number of a’s, over Σ ={a, b} .

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 2:
Construct DFA for the language L = { w | w ∈ {a, b}* and |w| ≤ 2 }

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 2:
Construct DFA for the language L = { w | w ∈ {a, b}* and |w| ≤ 2 }

Solution : L = {, a, b, aa, bb, ab, ba}

Trap/Dead state

Trap state is one from which we cannot escape, that is, it has either no transitions defined or
transitions for all symbols goes to itself.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 3:
Construct DFA for the language L = { w | w ∈ {a, b}* and |w| ≥ 2 }

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 3:
Construct DFA for the language L = { w | w ∈ {a, b}* and |w| ≥ 2 }

Solution : L = { aa, bb, ab, ba, aaa, aba, aab, … }


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 4:
Construct DFA for the language of strings starts with a, over Σ={a,b}.

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 4:
Construct DFA for the language of strings starts with a, over Σ={a,b}.

Solution : L = { a, aa, aaa, … , ab, abb, … , aba, aab, … }

Trap/Dead state
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 5:
Construct DFA for the language of strings starts with a and ends with b, over
Σ ={a, b}.

Solution:
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 5:
Construct DFA for the language of strings starts with a and ends with b, over
Σ ={a, b}.

Solution: L = { ab, aab, abb, abab, aaab, abbb, aabb, … }

Trap/Dead state
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 6:
The language with strings over Σ ={a,b} where every string starts with ab
and ends with ab, over Σ ={a,b}.

Solution: L={abab, abaab, abbab, …} If minimal string in the language


is abab then DFA is
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 6:
The language with strings over Σ ={a,b} where every string starts with ab
and ends with ab, over Σ ={a,b}.

Solution: L={abab, abaab, abbab, …} If minimal string in the language


is abab then DFA is

Trap/Dead state
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 6a:
The language with strings over Σ ={a,b} where every string starts with ab
and ends with ab, over Σ ={a,b}.

Solution: L={ab, abab, abaab, abbab, …} If minimal string in the


language is ab then DFA is
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 7:
The language of strings over Σ ={a,b}, where every a in the string is followed
by bb.

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 7:
The language of strings over Σ ={a,b}, where every a in the string is followed
by bb.

Solution : L = {, b, bb, bbb, … , abb, babb, bbabb, babbb, … }

or

Trap/Dead state Trap/Dead state


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 7a:
The language of strings over Σ ={a,b}, where every a in the string is never
followed by bb.

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 8:
The language of strings over Σ ={a,b} where every string must contain
“aa” as the substring.

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 8:
The language of strings over Σ ={a,b} where every string must contain
“aa” as the substring.

Solution : L = {aa, aaa, aaaa, … , baa, aab, aabb, bbaa, baab, … }


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 9:
Construct DFA for the language of strings over Σ = {a,b} of the form anbm
where n, m ≥ 1.

Solution:
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 9:
Construct DFA for the language of strings over Σ = {a,b} of the form anbm
where n, m ≥ 1.

Solution : L = { ab, abb, aab, aabb, abbb, aaab, aaabbb, … }

Trap/Dead state
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 10:
Construct DFA for the language of strings over Σ = {a,b} of the form anbm
where n, m ≥ 0.

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 10:
Construct DFA for the language of strings over Σ = {a,b} of the form anbm
where n, m ≥ 0.

Solution : L = {, a, aa, aaa, …, b, bb, bbb, …, ab, abb, aab, aabb, abbb, aaab,
aaabbb, … }
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 11:
Construct DFA for the language of strings over Σ={a,b} where na(w) mod 2 = 0.
Solution : L = {, aa, aaaa, aaaaaa, …, b, bb, bbb, …, aab, aba, baa, … }
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 11:
Construct DFA for the language of strings over Σ={a,b} where na(w) mod 2 = 0.
Solution : L = {, aa, aaaa, aaaaaa, …, b, bb, bbb, …, aab, aba, baa, … }

Explanation:
➢ In the DFA the states should represent the information
about the string processed so far.
➢ In this example, states have to represent only the number
of a's seen so far is even or odd, so only need two states q0
and q1. One state to represent “even” and another state to
represent “odd”.
➢ Before reading any symbol, the number of a's processed so
far is 0, which is even. Hence, the initial state q0 should
represent even. We want the DFA to accept the strings in L
(strings with even number of a’s). Hence, we should choose
q0 to also represent our accepting state.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 12:
Construct DFA for the language of strings over Σ={a,b} where, na(w)mod 2=0
and nb(w)mod 2=0

Solution:
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 12:
Construct DFA for the language of strings over Σ={a,b} where, na(w)mod 2=0
and nb(w)mod 2=0

Solution:
This state represents, seen so far odd
number of a’s and even number of b’s.

Note: State 00 is the final state.


Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Note:
➢ State 00 is final state for strings having Even number of a’s and Even number of b’s. (i.e., na(w)mod 2=0 and
nb(w)mod 2=0)
L = { ε, aa, aaaa, aaaaaa, …,
bb, bbbb, bbbbbb, …,
aabb, bbaa, abab, baba, abba, baab, … }

➢ State 10 is final state for strings having 0dd number of a’s and Even number of b’s. (i.e., na(w)mod 2=1 and
nb(w)mod 2=0)
L = { a, aaa, aaaaa, aaaaaaa, …,
abb, bab, bba, abbbb, babbb, …,
aaabb, bbaaa, … }
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Note:

➢ State 01 is final state for strings having Even number of a’s and 0dd number of b’s. (i.e., na(w)mod 2=0 and
nb(w)mod 2=1)
L = { b, bbb, bbbbb, bbbbbbb, …,
baa, aba, aab, baaaa, abaaa, …, bbbaa, aabbb, … }

➢ State 11 is final state for strings having Odd number of a’s and 0dd number of b’s. (i.e., na(w)mod 2=1 and
nb(w)mod 2=1)
L = { ab, aaabbb, aaaaabbbbb, aaaaaaabbbbbbb, …,
ba, bbbaaa, bbbbbaaaaa, bbbbbbbaaaaaaa, …,
ababab, ababababab, …, bababa, bababababa, …, aabbba, abbbaa, …,}
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Note:
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 13:
Construct DFA for the language of strings over Σ={a,b} where, na(w)mod 3=0 and
nb(w)mod 2=0.

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 13:
Construct DFA for the language of strings over Σ={a,b} where, na(w)mod 3=0 and
nb(w)mod 2=0.

Solution : This state represents, seen so far


This state represents, seen so far
• number of a’s divisible by 3 and
• number of a’s divisible by 3 + 1 and
• Even number of b’s.
• Even number of b’s.
This state represents, seen so far
• number of a’s divisible by 3 + 2 and
• Even number of b’s.

This state represents, seen so far


• number of a’s divisible by 3 + 2 and
• 0dd number of b’s.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 14:
Construct DFA for the language of strings over Σ={a,b} where, na(w)mod 3=0 and
nb(w)mod 3=0

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 14:
Construct DFA for the language of strings over Σ={a,b} where, na(w)mod 3=0 and
nb(w)mod 3=0

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 15:
Construct DFA for the language of strings over Σ={a,b} where, na(w)mod 3 = 2
and nb(w)mod 3 = 1

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 15:
Construct DFA for the language of strings over Σ={a,b} where, na(w)mod 3 = 2
and nb(w)mod 3 = 1

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 16:
Construct DFA for a binary number divisible by 2 and Σ ={0,1}. i.e., w mod 2 = 0)

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 16:
Construct DFA for a binary number divisible by 2 and Σ ={0,1}. (i.e., w mod 2 = 0)

Solution : L = { 0, 10, 100, 110, 1000, 1010, … }

or
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 16a:
Construct DFA for a binary number where w mod 2 = 1

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 16a:
Construct DFA for a binary number where w mod 2 = 1

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 17:
Construct a DFA for binary number divisible by 3. (i.e., w mod 3 = 0).

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 17:
Construct a DFA for binary number divisible by 3. (i.e., w mod 3 = 0).

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Example 18:
Construct a DFA for binary number divisible by 4. (i.e., w mod 4 = 0).

Solution :
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Exercise:
1. Construct DFAs to recognize C-language
▪ Identifiers,
▪ Integers
▪ Floating point numbers.
2. Construct DFAs to recognize C-language keywords
▪ for
▪ while
▪ switch
3. Construct a DFA to recognize all the relational operators in C-language.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Exercise:
4. Construct DFAs for the validation of
▪ PAN number
▪ Aadhar number
▪ Email-id
▪ Phone number
5. …
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Limitations of DFA (or FSMs):


1. No stored program concept – the machine is the computation. No
auxiliary memory.
2. A DFA is not powerful enough to recognize many context-free
languages because a DFA can't count. But counting is not enough -
consider a language of palindromes, containing strings of the
form wwR. Such a language requires more than an ability to count; it
requires a stack.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Limitations of DFA (or FSMs): cont…

Non-regular languages are those for which no DFA exists. These languages
have patterns or structures that cannot be recognized by a finite
automaton.

Which languages CANNOT be described by any RE or DFA?


• Bit strings with equal number of 0s and 1s.
• Nested parenthesis matching.
• Decimal strings that represent prime numbers.
• Genomic strings that are Watson-Crick complemented palindromes.
• Many more. . . .
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Note:
➢ Every NFA has an equivalent DFA (accepting the same language).
➢ The languages accepted by Finite State Machines (DFA, NFA, ε-NFA …) are
referred to as Regular languages (can be described by regular expressions).
➢ Regular Expression is a concise way to describe a set of strings.
➢ For any DFA, there exists a regular expression to describe the same set of strings; for
any regular expression, there exists a DFA that recognizes the same set.
➢ DFA that accepts nothing has no final states.
➢ There is a unique minimal DFA for every regular language.
Automata Formal Languages and Logic
Unit 1 - Deterministic Finite Acceptor/Automata

Criteria DFA NFA


State transition Each input symbol One input symbol can
leads to exactly one lead to one, more or
state no states
Automata Formal Languages & Logic
Unit 1 – NFA and -NFA

Prakash C O
Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Formal Definition of an NFA


NFA can be represented by a 5-tuple M=(Q, ∑, δ, q0, F) where

▪ Q is a finite set of states.

▪ ∑ is a finite non-empty set of input symbols called the alphabet.

▪ δ is the transition function where δ: Q × ∑ → 2Q

▪ q0 is the initial state from where any input is processed (q0 ∈ Q).

▪ F is a set of final state/states (F ⊆ Q).


Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Why NFA?
➢ NFAs were introduced in 1959 by Michael O. Rabin and Dana Scott, who also
showed their equivalence to DFAs.

➢ NFAs are used in the implementation of Regular Expressions


(i.e., Thompson's construction is used to convert RE to NFA).

➢ Constructing an NFA to recognize a given language is sometimes much


easier than constructing a DFA for that language.

➢ DFAs have been generalized to nondeterministic finite automata (NFA). NFA


provides a nice way of ‘abstraction of information’.
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

The Transition Function of an NFA is δ: Q × ∑ → 2Q

➢ Here the power set (i.e., P(Q)=2Q) has been taken because in case of an NFA,
from a state, transition can occur to any combination of Q states.

➢ Example: 2Q = 23= { {}, {q0}, {q1}, {q2},

{q0,q1}, {q0 ,q2}, {q1 ,q2},

{q0 , q1 ,q2}
}

➢ In an NFA, a (state, input_symbol) combination may lead to several states


simultaneously. δ(q0,a) = {q0 , q1 ,q2}

δ(q2,a) = {}
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Why NFA is called Non-deterministic?


➢ In automata theory, a finite-state machine is called a deterministic finite
automaton (DFA), if

▪ each of its transitions is uniquely determined by its source state and input symbol, and
▪ reading an input symbol is required for each state transition.
➢ A nondeterministic finite automaton (NFA), does not need to obey the above
restrictions of DFA. In particular, every DFA is also an NFA. NFA may have several arrows
of the same label starting from a state.

➢ A Finite Automata(FA) is said to be non-deterministic if there is more than one possible


transition from a state on the same input symbol.
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Computation in NFA
➢ For an input string, NFA will create a ‘computation tree’ rather than a
‘computation sequence’ in case of DFA.
An NFA accepts a string if any one of the paths in the tree leads to some accept
state.

➢ Nondeterminism in NFA causes multiple runs. There can be several or no runs for
a given input word.
If there is at least one run that leads to an accepting state, the machine will
accept the input string.
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

➢ Example 1: Consider L = { x ∈ {0, 1}* | where x ends with 01}

Let 10101 be an input word. The following are potential runs.


Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

➢ Example 1: Consider L = { x ∈ {0, 1}* | where x ends with 01}

Let 10101 be an input word. The following are potential runs.


Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

➢ Example 2: Consider L = {x ∈ {0, 1}* | 2nd symbol from the right is 1}

Computation tree for the input string 11010

➢ The NFA consumes a string of input symbols, one by


one. In each step, whenever two or more
transitions are applicable, it "clones" itself into
appropriately many copies, each one following a
different transition.

➢ If no transition is applicable, the current copy is in a
dead end (or stuck), it "dies". If, after consuming the
complete input, any of the copies is in an accept
state, the input is accepted, else, it is rejected.
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

➢ Example 2: Consider L = {x ∈ {0, 1}* | 2nd symbol from the right is 1}

Computation tree for the input string 11010

➢ The NFA consumes a string of input symbols, one by


one. In each step, whenever two or more
transitions are applicable, it "clones" itself into
appropriately many copies, each one following a
different transition.

➢ If no transition is applicable, the current copy is in a
dead end (or stuck), it "dies". If, after consuming the
complete input, any of the copies is in an accept
state, the input is accepted, else, it is rejected.
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

NFA Construction:
Example 1: Construct NFA for the language of binary strings having at least
a pair of ‘00’ or a pair of ‘11’

Solution:
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

NFA Construction:
Example 1: Construct NFA for the language of binary strings having at least
a pair of ‘00’ or a pair of ‘11’

Solution:

or
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Example 2: Construct NFA for the language of strings ends with a, over ∑ ={a, b}

Solution:
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Example 2: Construct NFA for the language of strings ends with a, over ∑ ={a, b}

Solution: L={ a, aa, ba, bba, baa, aaa, aba, aaaa, aaba, abba, baba, bbba, … }
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Example 3: Construct NFA for the language of strings, where the second symbol
from the RHS of a string is ‘a’ and ∑ = {a, b}

Solution:
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Example 3: Construct NFA for the language of strings, where the second symbol
from the RHS of a string is ‘a’ and ∑ = {a, b}

Solution: L={ aa, ab, aab, baa, bab, aaa, aaaa, aaab, abab, baab, bbab, … }
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Example 4: Design an NFA in which all the string contain a substring 1110
and ∑ = {0, 1}

Solution:
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Example 5: Design an NFA with ∑ = {0, 1} accepts all string in which the
third symbol from the right end is always 0.

Solution:
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

DFA and NFA Equivalence


➢ NFAs and DFAs are equivalent in that if a language is recognized by an NFA, it is
also recognized by a DFA and vice versa.

➢ The establishment of NFA and DFA equivalence is important and useful.

➢ It is useful because constructing an NFA to recognize a given language is sometimes


much easier than constructing a DFA for that language.

➢ It is important because NFAs can be used to reduce the complexity of the mathematical
work required to establish many important properties in automata theory.

➢ For example, it is much easier to prove closure properties of regular languages using
NFAs than DFAs.
Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

Formal Definition of an -NFA (or λ-NFA)

-NFA can be represented by a 5-tuple M=(Q, ∑, δ, q0, F) where

▪ Q is a finite set of states.

▪ ∑ is a finite non-empty set of input symbols called the alphabet.

▪ δ is the transition function where δ: Q × (𝜮 ∪{})→ 2Q or δ: Q × (𝜮 ∪{λ})→ 2Q

▪ q0 is the initial state from where any input is processed (q0 ∈ Q).

▪ F is a set of final state/states (F ⊆ Q).


Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

-transitions (or λ-transitions) (or Null moves):


➢ Let us add another feature to our automaton.
➢ Let us allow it to jump states without reading inputs. We will call such
moves -transitions (or λ-transitions) .

or
Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

-closure (or λ-closure) of a state or set of states:


➢ For a state q, let ε-closure(q) (or λ-closure(q)) denote the set of states that
are reachable from q by ε-transitions (or λ-transitions) including the state q
itself.
Example 1:

λ-closure(q0) = {q0 q1 q2}

λ-closure(q1) = { q1 }

λ-closure(q2) = { q2 }
Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

-closure (or λ-closure) of a state or set of states:


➢ Example 2:

-closure(0) =

-closure(6) =
Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

-closure (or λ-closure) of a state or set of states:


➢ Example 2:

-closure(0) = { 0 1 2 4 7}

-closure(6) = { 6 7 1 2 4 }
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Computation or Run in an -NFA


Example: Consider L = { x ∈ {0, 1}* | where x contains the substring 101 or 11}

Computation tree for the input string 0111


Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Computation or Run in an -NFA


Exercise: Consider the language of strings over Σ = {a, b, c} of the form
anbmck where n, m, k ≥ 0.

Construct Computation tree for the input string aab.


Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Computation or Run in an -NFA


Exercise: Consider the language of strings over Σ = {a, b, c} of the form
anbmck where n, m, k ≥ 0.

Computation tree for the input


string aab.
Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

Example 1: Construct -NFA for a language of strings that begins and ends with
the same symbol, and ∑ = {a, b}

Solution:
Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

Example 1: Construct -NFA for a language of strings that begins and ends with
the same symbol, and ∑ = {a, b}

Solution: L={ aa, bb, aba, bab, aaa, bbb, … }


Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

Example 2: Construct -NFA that recognizes integers without leading zeros.


∑ = { -, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Solution: Example integers: … , -2, -1, 0, 1, 2, … , 101, 102, …


Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

Example 2: Construct -NFA that recognizes integers without leading zeros.


∑ = { -, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Solution: Example integers: … , -2, -1, 0, 1, 2, … , 101, 102, …


Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

Example 3: Construct λ-NFA for the language L={ anbmcp | n,m,p ≥ 0 }

Solution: L={, a, aa, aaa, aaaa, … ,b, bb, bbb, bbbb, … , c, cc, ccc, cccc, …
ab, bc, ac, … , abc, …}
Automata Formal Languages and Logic
Unit 1: -Non-deterministic Finite Automata (-NFA)

Example 3: Construct λ-NFA for the language L={ anbmcp | n,m,p ≥ 0 }

Solution: L={, a, aa, aaa, aaaa, … ,b, bb, bbb, bbbb, … , c, cc, ccc, cccc, …
ab, bc, ac, … , abc, …}
Automata Formal Languages and Logic
Unit 1 – DFAs & NFAs

➢ DFA may be cumbersome to specify for complex systems and may even be
unknown, but NFA provides a nice way of ‘abstraction of information’.
➢ Any language that can be accepted by a DFA can also be accepted by an NFA,
i.e., Every NFA has an equivalent DFA (accepting the same language).
➢ DFA’s, NFA’s, and ε–NFA’s all accept exactly the same set of languages: the
regular languages.
➢ NFA are often more convenient to design than DFA and may have fewer states
compared to DFA.
➢ In real-time, only Deterministic Finite Automata (DFAs) can be implemented.
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

➢ Using subset construction method, every NFA can be translated to a DFA that
recognizes the same language. DFAs, and NFAs as well, recognize exactly the set
of regular languages.

➢ NFAs have been generalized in multiple ways, e.g., ε-NFA, finite-state


transducers, pushdown automata, alternating automata, ω-automata,
and probabilistic automata.
Automata Formal Languages and Logic
Unit 1 - Non-Deterministic Finite Automata (NFA)

Note:
➢ "Deterministic" means "if you put the system in the same situation twice, it is guaranteed to make the same choice both
times".

➢ "Non-deterministic" means "not deterministic", or in other words, "if you put the system in the same situation twice, it might
or might not make the same choice both times".

➢ A non-deterministic finite automaton (NFA) can have multiple transitions out of a state. This means there are multiple
options for what it could do in that situation. It is not forced to always choose the same one; on one input, it might choose the
first transition, and on another input it might choose the same transition.

➢ Here you can think of "situation" as "what state the NFA is in, together with what symbol is being read next from the input".
Even when both of those are the same, a NFA still might have multiple matching transitions that can be taken out of that
state, and it can choose arbitrarily which one to take. In contrast, a DFA only has one matching transition that can be taken in
that situation, so it has no choice -- it will always follow the same transition whenever it is in that.
Automata Formal Languages & Logic

Unit 1
Conversion of NFA to DFA

Preet Kanwal
Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

NFA -> DFA (Subset Construction)

● The initial state is the start state, plus all states reachable from the start state
via 𝜆-transitions (Called 𝜆-closure).

● Transition from a state S on character a is found by following all possible


transitions on a for each state in S, then taking the set of states reachable from
there by 𝜆-transitions.

● Accepting states are any set of states where some state in the set is an
accepting state.
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

NFA -> DFA (Subset Construction)

● In converting an NFA to a DFA, the DFA's states correspond to set of NFA states.

● In the worst-case, the construction can result in a DFA that is exponentially


larger than the original NFA .

● Minimization of a DFA ensures that the resulting DFA (after minimization) has
the least possible states. The advantages of having a minimal DFA are: Faster
Execution: The more the number of states the more time the DFA will take to
process a string, hence minimization ensures faster execution.
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

NFA -> DFA (Subset Construction)

Given an NFA with states Q, inputs Σ, transition function δN, state state q0,
and final states F, construct equivalent DFA with:
* States 2Q (Set of subsets of Q).
* Inputs Σ.
* Start state {q0}.
* Final states = all those with a member of F.
• The DFA states have names that are sets of NFA states.
• But as a DFA state, an expression like {p,q} must be read as a single symbol,
not as a set.
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Example 1: Convert the following NFA,Σ={a,b},L={Strings where the


second symbol from RHS is a} to DFA.
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Solution:

States= [{q0}, {q0q1},{q0q1q2},{q1q2}]


Can rename the states as q0=q0, {q0q1}=q1, {q0,q1,q2}=q2, {q0,q2}=q3
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Example 2: Convert the following NFA to DFA.


Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Solution:

States: [{q0},{q0q1q2},{q0q1q4},Φ}
Rename the states as q0=q0,q0q1q2=q1, q0q1q4=q2, Φ= q3
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Example 3: Convert the following 𝜆-NFA L={anbmck , n,m,k>=0} to DFA.


Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Solution:

Rename the states as:


{q0q1q2} as q0, {q1q2} as q1, q2 as q2, Φ as q3
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Example 4: Convert the following 𝜆-NFA to DFA.


Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Solution:
Transition Table DFA
Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Example 5: Convert the following 𝜆-NFA to DFA.


Automata Formal Languages and Logic
Unit 1 - Conversion of NFA to DFA

Solution:
Transition Table DFA
Automata Formal Languages & Logic

Unit 1
Minimization of DFA

Preet Kanwal
Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 1 - Minimization of DFA

Minimization of DFA (Table Filling Algorithm)

● In converting an NFA to a DFA, the DFA's states correspond to set of NFA


states.
● In the worst-case, the construction can result in a DFA that is
exponentially larger than the original NFA .
● Minimization of a DFA ensures that the resulting DFA (after
minimization) has the least possible states.
● The advantages of having a minimal DFA are: Faster Execution: The more
the number of states the more time the DFA will take to process a string,
hence minimization ensures faster execution.
Automata Formal Languages and Logic
Unit 1 - Minimization of DFA

Minimization of DFA (Table Filling Algorithm)

Step I : Eliminate Unreachable States


Step II : Mark Distinguishable Pair of States
(Final, Non-Final States)
Step III : If there are any Unmarked pairs (M, N) make
the following Check :
if 𝜹(M, a) = X and 𝜹(N, a) = Y && (X, Y) is marked then,
Mark (M, N) also.
Repeat Step III until no new states can be marked.

Step IV : Combine all the unmarked pairs and make them a single state in the
minimized DFA.
Automata Formal Languages and Logic
Unit 1 - Minimization of DFA

Example 1:Minimize the following DFA.


Automata Formal Languages and Logic
Unit 1 - Minimization of DFA

Solution:

Final Table Minimized DFA

States merged :
● {q1, q2}
● {q3, q4}
Automata Formal Languages and Logic
Unit 1 - Minimization of DFA

Example 2 : Minimize the following DFA.


Automata Formal Languages and Logic
Unit 1 - Minimization of DFA

Solution:

Final Table Minimized DFA

States merged :
● {q0,q4}, {q0, q2} and {q2, q4}
● {q1,q5}, {q1,q3} and {q3,q5}
Automata Formal Languages and Logic
Unit 1 - Minimization of DFA

Example 3: Minimize the following DFA.


Automata Formal Languages and Logic
Unit 1 - Minimization of DFA

Solution:
Minimized DFA

Final Table

States merged :
● {q0, q2}
● {q3, q5}
Automata Formal Languages & Logic

Unit 1*
Regular Expression

Preet Kanwal
Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 2 - Regular Expression

Regex is an algebraic way to describe regular languages.

● A
Character/symbol
Atoms ● 𝜙 (L = { })
Regular Expression

● 𝝀 (L = {𝝀})

● (R)
Operators
● R*
● R1.R2 or R1R2
● R1 + R2 or R1 | R2
Automata Formal Languages and Logic
Unit 2 - Example of Regular Expressions

Example

a b* c + d
Automata Formal Languages and Logic
Unit 2 - Example of Regular Expressions

Example

(a b* c)+ d
R1 R2

or
Automata Formal Languages and Logic
Unit 2 - Example of Regular Expressions

Example

(a b* c) + d
Starting with Ending with
an ‘a’ a ‘c’
Followed by 0 or
more no. of b’s
Automata Formal Languages and Logic
Unit 2 - Example of Regular Expressions

Example

(a b* c)+ d String that


contains only d

Starting with Ending with


an ‘a’ a ‘c’
Followed by 0 or
more no. of b’s
Automata Formal Languages and Logic
Unit 2 - Example of Regular Expressions

Example

(a b* c)+ d
L = { ac,
abc,
abbb….c,
d}
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

Identity = 𝜙

Union
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

Identity = 𝜙

R+𝜙=𝜙+R=R

Union
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

Identity = 𝜙

R+𝜙=𝜙+R=R

Union Commutative
R1 + R 2 = R 2 + R 1
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

Identity = 𝜙

R+𝜙=𝜙+R=R

Union Commutative
R1 + R 2 = R 2 + R 1

Associative
(R1 + R2)+R3 =
R1 + (R2+R3)
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

Identity = 𝜖 or 𝝀

Concatenation
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

Identity = 𝜖 or 𝝀

R.𝜖=𝜖.R=R

Concatenation
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

R.𝜙=𝜙.R=𝜙
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

Identity = 𝜖 or 𝝀

R.𝜖=𝜖.R=R
Non-Commutative

Concatenation R1 . R 2 ≠ R 2 . R 1

Example :
R1 = a
R2 = b
then,
ab = ba
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

Identity = 𝜖 or 𝝀

R.𝜖=𝜖.R=R
Non-Commutative

Concatenation R1 . R 2 ≠ R 2 . R 1

Associative
(R1 . R2) . R3 =
R1 . (R2. R3)
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

𝜖* = 𝜖
𝜙* = 𝜖
(R*)* = R*
Closure (R + 𝜖)* = R*
+
R = RR* = R*R
+
(R + 𝜖) = R*
𝜖 + RR* = 𝜖 + R* = R*
Automata Formal Languages and Logic
Unit 2 - Algebraic Laws of Regular Expressions

(a + b)*
= (a* + b*)*
= (a* b*)*
Closure = (a + b*)*
= (a* + b)*
= a*(ba*)*
= b*(ab*)*
Automata Formal Languages and Logic
Unit 2 - Construction of Regular Expressions

Construct a Regular Expression for a given language L


Automata Formal Languages & Logic

Unit 1*
Regular Expression in Practice

Preet Kanwal
Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
(dot)
matches any single character except newline
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
(Star)
0 or more repetitions of preceding regex
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
Example :
a*
Strings matched :
0 or more no. of a’s
Example : 𝝺,a,aa,aaa,aaaa,aaaaa….
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
Example :
a+
Strings matched :
1 or more no. of a’s
Example : a,aa,aaa,aaaa,aaaaa….
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
4. ?
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
4. ?
Example:
ab?c
matches abc or ac
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
4. ?
5. [ ]
Character Class
Matches any single character
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
4. ?
5. [ ]
Example
[cat]
String matched :
c
or a
or t
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
4. ?
5. [ ]
6. ^
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

^ (caret) :

Example:

Example: ^abc matches "a" at the start of the string.

[^abc]: This pattern matches any character except a or b or c.


Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
4. ?
5. [ ]
6. ^
7. $
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

$ (dollar) :
Example :
-> abc$" matches "c" at the end of a line.
-> ^$ matches the empty string.
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
4. ?
5. [ ]
6. ^
7. $
8. { } (Syntax - R{m,n})
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Example of { }

a{0, } - same as a*
a{1, } - same as a+

a{2,3} matches "aa" or “aaa".

a{3} matches aaa only


Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Special characters
1. .
2. *
3. +
4. ?
5. [ ]
6. ^
7. $
8. { } (Syntax - R{m,n})
9. \d - matches any digit between [0-9]
a. \d{9} matches any 9 digits
b. \d{2,3} matches 2 or 3 digits
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

We will discuss how to construct regex for validating the following :


● PAN Card
● Adhaar Card
● Mobile Number
● Date
● Email Address
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

● PAN CARD:

The valid PAN Card number must satisfy the following conditions:
1. It should be 10 characters long.
2. The first five characters should be any upper case alphabets.
3. The next four-characters should be any number from 0 to 9.
4. The last(tenth) character should be any upper case alphabet.
5. It should not contain any white spaces.

https://www.geeksforgeeks.org/how-to-validate-pan-card-number-using-regular-expression/
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

PAN CARD Regex - ^[A-Z]{5}[0-9]{4}[A-Z]$


Input: str = “BNZAA2318J”
Output: true
Explanation:
The given string satisfies all the above mentioned conditions.

Input: str = “23ZAABN18J”


Output: false
Explanation:
The given string does not start with upper case alphabets, therefore it is not a valid PAN
Card number.

https://www.geeksforgeeks.org/how-to-validate-pan-card-number-using-regular-expression/
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

● AADHAR NUMBER:

The valid Aadhar number must satisfy the following conditions:


1. It should have 12 digits.
2. It should not start with 0 and 1.
3. It should not contains any alphabet and special characters.
4. It should have white space after every 4 digits.

https://www.geeksforgeeks.org/how-to-check-aadhar-number-is-valid-or-not-using-regular-expression/#:~:text=Create%20a%20regular%20expression%20to,%2D9%5D%7B4%7
D%24%E2%80%9D%3B&text=%5B0%2D9%5D%7B4%7D,be%20any%20from%200%2D9.
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

AADHAR Number Regex - ^[2-9]{1}\d{3}\s{4}[“ “]\s{4}$”


Input: str = “3675 9834 6012”
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore it is a valid
Aadhar number.

Input: str = “3675 9834 6012 8”


Output: false
Explanation:
The given string contains 13 digits. Therefore it is not a valid Aadhar number.

https://www.geeksforgeeks.org/how-to-check-aadhar-number-is-valid-or-not-using-regular-expression/#:~:text=Create%20a%20regular%20expression%20to,%2D9%5D%7B4%7
D%24%E2%80%9D%3B&text=%5B0%2D9%5D%7B4%7D,be%20any%20from%200%2D9.
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

● INDIAN MOBILE NUMBER:

The valid Mobile number must satisfy the following conditions:


● It is a 10 digits number
● The first digit should contain number between 6 to 9.
● The rest 9 digit can contain any number between 0 to 9.
● The mobile number can have 11 digits also by including 0 at the starting.
● The mobile number can be of 13 digits also by including +91 at the starting

https://www.geeksforgeeks.org/how-to-check-aadhar-number-is-valid-or-not-using-regular-expression/#:~:text=Create%20a%20regular%20expression%20to,%2D9%5D%7B4%7
D%24%E2%80%9D%3B&text=%5B0%2D9%5D%7B4%7D,be%20any%20from%200%2D9.
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

MOBILE Number Regex - ^(0|”+91”)?[6-9]\d{9}$

https://www.geeksforgeeks.org/how-to-check-aadhar-number-is-valid-or-not-using-regular-expression/#:~:text=Create%20a%20regular%20expression%20to,%2D9%5D%7B4%7
D%24%E2%80%9D%3B&text=%5B0%2D9%5D%7B4%7D,be%20any%20from%200%2D9.
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Dates in the year 2020

● It is a leap year

● Let the format be DD-MM-YY

Months with 31 days are : Jan, Mar, May, July, Aug, Oct, Dec
: [1, 3, 5, 7, 8, 10, 12]
Months with 30 days are : Apr, June, Sep, Nov
: [4, 6, 9, 11]
Month with 29 days : Feb
: [2]
● Year : 2020
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Dates in the year 2020

Regex:

(
(0[0-9])|[10-31]”-”(0[13578]|1[0|2])
|
(0[0-9])|[10-30]”-”(0[469]|11)
|
(0[0-9])|[10-29]”-””02”
)”-””2020”
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Email Address:

An email is a string (a subset of ASCII characters) separated into two parts by @ symbol.
a "personal_info" and a domain, that is personal_info@domain.
The length of the personal_info part may be up to 64 characters long and domain name
may be up to 253 characters.

The personal_info part contains the following ASCII characters.


● Uppercase (A-Z) and lowercase (a-z) English letters.
● Digits (0-9).
● Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
● Character . ( period, dot or fullstop) provided that it is not the first or last character
and it will not come one after the other.
The domain name [for example com, org, net, in, us, info] part contains letters, digits,
hyphens, and dots.
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Email Address:

Example of valid email id


[email protected]
[email protected]
[email protected]
Example of invalid email id
● mysite.ourearth.com [@ is not present]
[email protected] [ tld (Top Level domain) can not start with dot "." ]
● @you.me.net [ No character before @ ]

^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.
(([0-9]{1,3})|([a-zA-Z]{2,3})|(com|edu|info|museum|name))$
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Regular Expression in practice: Examples

Example 2: Simple URL Validator


Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Regular Expression in practice: Examples

Example 2: Simple URL Validator

^((ht|f)tp(s?))\://([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(/\S*)?$
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Regular Expression in practice: Examples

Example 5: -mail addresses Validation


Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Regular Expression in practice: Examples

Example 5: -mail addresses Validation

^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.
(([0-9]{1,3})|([a-zA-Z]{2,3})|(com|edu|info|museum|name))$
Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Regular Expression in practice: Examples

Example 6: Ip Address validation:


Automata Formal Languages and Logic
Unit 2 - Regular Expression in Practice

Regular Expression in practice: Examples

Example 6: Ip Address validation:

^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-
5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2
[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4]
[0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$
Automata Formal Languages & Logic

Unit 1*
Regular Expression to Finite
Automata

Preet Kanwal
Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Thompson’s Construction Algorithm

Regular Finite
Expression Kleene’s Automata
R Theorem M

State Elimination Algorithm


Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Thompson’s Construction Algorithm

Regular Finite
Expression Automata
R M
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Thompson’s Construction Algorithm

https://en.wikipedia.org/wiki/Thompson%27s_construction#:~:text=In%20computer%20science%2C%20Thompson's%20co
nstruction,strings%20against%20the%20regular%20expression.
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Thompson’s Construction Algorithm

Union : s|t

https://en.wikipedia.org/wiki/Thompson%27s_construction#:~:text=In%20computer%20science%2C%20Thompson's%20co
nstruction,strings%20against%20the%20regular%20expression.
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Thompson’s Construction Algorithm

Concatenation : s.t

https://en.wikipedia.org/wiki/Thompson%27s_construction#:~:text=In%20computer%20science%2C%20Thompson's%20co
nstruction,strings%20against%20the%20regular%20expression.
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Thompson’s Construction Algorithm

Kleene Star : s*

https://en.wikipedia.org/wiki/Thompson%27s_construction#:~:text=In%20computer%20science%2C%20Thompson's%20co
nstruction,strings%20against%20the%20regular%20expression.
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Construct Finite Automata for the following Regex :

(ab+c)*

https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Construct Finite Automata for the following Regex :

(ab+c)*

a
q0 q1

https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Construct Finite Automata for the following Regex :

(ab+c)*

a
q0 q1

b
q2 q3

https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Construct Finite Automata for the following Regex :

(ab+c)*

a b
q0 q2 q3

https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Construct Finite Automata for the following Regex :

(ab+c)*

a b
q0 q2 q3

c
q4 q5

https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Construct Finite Automata for the following Regex :

(ab+c)*

𝜖 a b
q0 q2 q3

qs
c
q4 q5
𝜖
https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

Construct Finite Automata for the following Regex :

(ab+c)*

𝜖 a b
𝜖
q0 q2 q3

qs
qf
c
q4 q5
𝜖 𝜖
https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

(ab+c)*

𝜖 a b 𝜖
q q
𝜖 q3
0 2
q
S
s qf
c
q q5
𝜖 4 𝜖

https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

(ab+c)*

𝜖 a b 𝜖
q q
𝜖 q3
0 2
q 𝜖
S
s qf F
c
q q5
𝜖 4 𝜖

https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

(ab+c)*

𝜖 a b 𝜖
q q
𝜖 q3
0 2
q 𝜖
S
s qf F
c
q q5
𝜖 4 𝜖

𝜖
https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages and Logic
Unit 2 - Regular Expression to Finite Automata

(ab+c)* 𝜖

𝜖 a b 𝜖
q q
𝜖 q3
0 2
q 𝜖
S
s qf F
c
q q5
𝜖 4 𝜖

𝜖
https://www.cs.york.ac.uk/fp/lsa/lectures/REToC.pdf
Automata Formal Languages & Logic

Unit 1*
Finite Automata to Regular
Expression

Preet Kanwal
Department of Mechanical Engineering
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Regular Finite
Expression Automata
R M

State Elimination Algorithm


Automata Formal Languages and Logic
Unit 2 - Regular Grammar

State Elimination Method


Start with an FA for the language L.
● Add a new start state qs and accept state qf to the FA.
● Add ε-transitions from each original accepting state to qf , then
mark them as not accepting.
● Repeatedly remove states other than qs and qf from the FA by
“shortcutting” them until only two states remain: qs and qf .
● The transition from qs to qf is then a regular expression for the
FA.
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 1 :

b
A B

c
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 1 : a

b
A B a+b
A B

c
c
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 1 :

b a+b+c
A B A B

c
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 2 :
b

a c
A B C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 2 :
b

A
a
xB
c
C

Eliminate B a b* c
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 2 :
b

a c
A B C

A
a b* c C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 3 :

b c

A d
B C

a
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 3 : b

x
ab b
𝜖 d
S A B C

1. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 3 :
b
(c + ab )

S
x B
d
C

b (c + ab)* d
1. Eliminate A
2. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 3 :
b c

A d
B C

S C

b (c + ab)* d
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :
c d
a

A B

b
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :
c d
a
𝜖
S A B

b
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :
c d
a
𝜖 𝜖
S A C
B F

A new Final state F is introduced as there is


an Outgoing edge to the existing Final
State B.
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :
c d

x
a
𝜖 𝜖
S A C
B F

1. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 : c*a

c d

x
a
𝜖
S A C
B F

1. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 : c*a
d + bc*a

𝜖
S C
B F

1. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 : c*a
d + bc*a

S x B
𝜖
F

1. Eliminate A
2. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :

c*a (d + b c* a)*
S F

1. Eliminate A
2. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :
c d
a

A B

c*a (d + b c* a)*
S F
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :
c d
a
𝜖 𝜖
S A C
B F

b
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :
c d

x
a
𝜖 𝜖
S A C
B F

1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

ad*
Example 4 :
c d

x
a
𝜖
S A C
B F

1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

ad*
Example 4 :
c + ad*b

𝜖
S A F

1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

ad*
Example 4 :
c + ad*b

S
𝜖
x A F

1. Eliminate B
2. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 :

(c+ad*b)* ad*
S F

1. Eliminate B
2. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 4 : c d
a

A B

c*a (d + b c* a)* (c+ad*b)* ad*


S F S F

1. Eliminate A 1. Eliminate B
2. Eliminate B 2. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :

0 1 0,1

1 0
A B C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :

0 1 0,1

λ 1 0
S A B C

A new start state (S) is introduced as there is an incoming edge to the existing
start state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :
0 1 0,1

λ 1 0
S A B C

λ λ
F

A new final state (F) is introduced as there is an outgoing edge from the existing
final state and we must have a single accepting state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :
0 1 0,1

λ 1 0
S A B C

λ λ
F

Previous final state is made as non final state


Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :
0 1 0,1

X
λ 1 0
S A B C

λ λ
F

1. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :

0 1
λ 1
S A B

λ λ
F

1. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :

0 1

X
λ 1
S A
B

λ λ
F

1. Eliminate C
2. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :

0
λ
S A
11*

λ
F

1. Eliminate C
2. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :

0
λ 11*+λ
S A F

1. Eliminate C
2. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :

S
λ
X A
11*+λ
F

1. Eliminate C
2. Eliminate B
3. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 5 :

0*(11*+λ)
S F

= 0(11*+λ)
+
=0*(1 +λ)

RE =0*1*
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :

0
A B

1
1 0 0

0,1
C D

1
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :
0
A B

1
1 0 0

C X D
0,1

1
1. Eliminate D
(Dead state)
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression
Example 6 :

0
A B

1
1 0

C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :
0
λ
S A B

1
1 0

A new start state (S) is introduced as there is an incoming edge to the existing
start state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :
F

λ 0
λ
S A B

1
1 0

A new final state (F) is introduced as there is an outgoing edge from the existing
final state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :
F

λ 0

S
λ
A X B

1
1 0

1. Eliminate D
2. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :

01 λ
λ
S A F

1 0

1. Eliminate D
2. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :

01
λ λ
S A F

1 0

X
C

1. Eliminate D
2. Eliminate B
3. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :

01+10
λ λ
S A F

1. Eliminate D
2. Eliminate B
3. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :

01+10
S
λ
XA
λ
F

1. Eliminate D
2. Eliminate B
3. Eliminate C
4. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 6 :

(01+10)*
S F

RE=(01+10)*
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :

1 1 1,0
0
1
A B C

0
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :

1 1 1,0
0
1
S A B C

A new start state (S) is introduced as there is an incoming edge to the existing
start state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :

1 1 1,0
0
λ 1 λ
S A B C F

A new final state (F) is introduced as there is an outgoing edge from the
existing
final state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :

1 1 1,0
0

X
λ 1 λ
S A B C F

1. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :

1 1
0
λ 1(0+1)*
S A B F

1. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :

1 1
0

X
λ 1(0+1)*
S A B F

1. Eliminate C
2. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :
(1+01*0)

1*0 1(0+1)*
S B F

1. Eliminate C
2. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :
(1+01*0)

X
1*0 1(0+1)*
S B F

1. Eliminate C
2. Eliminate A
3. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 7 :

1*0(1+01*0)*1(0+1)*
S F

RE=1*0(1+01*0)*1(0+1)*
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :

0
1
A B

0
1 1

0
C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :

0
1
λ
S A B

0
1 1

0
C

A new start state (S) is introduced as there is an incoming edge to the existing
start state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :
0 1
λ
S A B

0
1 λ
1
0
C F

λ
A new final state (F) is introduced as there is an outgoing edge from the existing
final state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :
0 1
S
λ
A X
B

0
1 λ
1
0
C F

λ
1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :
01*0

λ
S A 01*

01*0+1 1

C F

01*+λ
1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :
01*0

λ
S A
01*

01*0+1 1

X C F

01*+λ
1. Eliminate B
2. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :
01*0+1(1+01*0)

λ 01*
S A F

1(01*+λ)

1. Eliminate B
2. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :
01*0+1(1+01*0)

S
λ
X A
01*
F

1(01*+λ)

1. Eliminate B
2. Eliminate C
3. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :

(01*0+1(1+01*0))*(01*+1(01*+λ))
S F

RE=(01*0+1(1+01*0))*(01*+1(01*+λ))
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :

RE=(01*0+1(1+01*0))*(01*+1(01*+λ))

=(01*0+11+101*0)*(01*+(101*+1))
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :

RE=(01*0+1(1+01*0))*(01*+1(01*+λ))

=(01*0+11+101*0)* (01*+(101*+1))

=(101*0+01*0+11)* (01*(λ+1) +1)


Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :

RE=(01*0+1(1+01*0))*(01*+1(01*+λ))

=(01*0+11+101*0)* (01*+(101*+1))

=(101*0+01*0+11)* (01*(λ+1) +1)

=(01*0(1+λ) + 11)* (01*(λ+1) +1)


Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :

RE=(01*0+1(1+01*0))*(01*+1(01*+λ))

=(01*0+11+101*0)* (01*+(101*+1))

=(101*0+01*0+11)* (01*(λ+1) +1)

=(01*0(1+λ) + 11)* (01*(λ+1) +1)

= (01*0(1+λ))* ( 11)* (01*(λ+1) +1)


Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 8 :

RE=(01*0+1(1+01*0))*(01*+1(01*+λ))

=(01*0+11+101*0)* (01*+(101*+1))

=(101*0+01*0+11)* (01*(λ+1) +1)

=(01*0(1+λ) + 11)* (01*(λ+1) +1)

RE = (01*0(1+λ))* ( 11)* (01*(λ+1) +1)


Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :order of elimination( B,D,C,A)

a
A B

b b b b
a
D C

a
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :

a
λ
S A B

b b b b
a
D C

a
A new start state (S) is introduced as there is an incoming edge to the existing
start state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 : F

λ a
λ
S A B

b b b b
a
D C

A new final state (F) is introduced as there is an outgoing edge from the existing
final state
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
F

X
λ a
λ
S A B

b b b b
a
D C

a
1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
F

λ aa
λ
S A ab

ba
b b
a bb
D C

a
1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
F

aa
λ λ
S A ab

ba
b b

X
a bb
D C

a
1. Eliminate B
2. Eliminate D
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
aa+bb

λ λ
S A F

ab+ba ab+ba

bb+aa
1. Eliminate B
2. Eliminate D
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
aa+bb

λ λ
S A F

ab+ba ab+ba

X C

bb+aa
1. Eliminate B
2. Eliminate D
3. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
(aa+bb )+
(ab+ba)(aa+bb)*(ab+ba)
λ λ
S A F

1. Eliminate B
2. Eliminate D
3. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :

(aa+bb) +
(ab+ba)(aa+bb)*(ab+ba)

X
λ λ
S A F

1. Eliminate B
2. Eliminate D
3. Eliminate C
4. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :

(aa+bb) + (ab+ba)(aa+bb)*(ab+ba))*
S F

RE= ((aa+bb) + (ab+ba)(aa+bb)*(ab+ba))*


Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :

Consider the same example: Order of elimination: B,C, D ,A (Adding new start state and

new final state will remain same)


a
A B

b b b b
a
D C

a
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
F

X
λ a
λ
S A B

b b b b
a
D C

a
1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
F

λ λ aa
S A ab

ba
b b
a bb
D C

a
1. Eliminate B
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
F

λ aa
λ
S A ab

ba
b b
a
X
bb
D C

a
1. Eliminate B
2. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
aa+ab(bb)*ba
λ λ
S A F

b+ab(bb)*a a(bb)*ba+b

1. Eliminate B
a(bb)*a
2. Eliminate C
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
aa+ab(bb)*ba
λ λ
S A F

b+ab(bb)*a a(bb)*ba+b

1. Eliminate B
X D

a(bb)*a
2. Eliminate C
3. Eliminate D
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :

aa+ab(bb)*ba + (b+ab(bb)*a)(a(bb)*a)*(a(bb)*ba+b
λ λ
S A F

1. Eliminate B
2. Eliminate C
3. Eliminate D
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :

(aa+ab(bb)*ba + (b+ab(bb)*a)(a(bb)*a)*(a(bb)*ba+b)

S
λ
X A
λ
F

1. Eliminate B
2. Eliminate C
3. Eliminate D
4. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :

(aa+ab(bb)*ba + (b+ab(bb)*a)(a(bb)*a)*(a(bb)*ba+b)*
S F

1. Eliminate B
2. Eliminate C
3. Eliminate D
4. Eliminate A
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :

((aa+ab(bb)*ba)* + (b+ab(bb)*a)(a(bb)*a)*(a(bb)*ba+b)*
S F

RE = ((aa+ab(bb)*ba) + (b+ab(bb)*a)(a(bb)*a)*(a(bb)*ba+b))*
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Expression

Example 9 :
a
A B
a
b b b b
a
D C

(aa+bb + (ab+ba+(aa+bb)*(ab+ba))* ((aa+ab(bb)*ba)* +


Eliminate : B,D,C,A (b+ab(bb)*a)(a(bb)*a)*(a(bb)*ba+b))*
Eliminate B,C,D,A
Automata Formal Languages & Logic

Unit 1*
Equivalence of two regular
expression

Preet Kanwal
Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Two regular expressions(R1and R2)are equivalent( R1= R2) iff:

L(R1) = L(R2)

We can determine the equivalence using:

1) A Formal method
2) An Informal method:
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Formal method to prove Equivalence of two Regex


Equal
R1 R2

NFA1 NFA2

DFA1 DFA2

Equal
Minimal DFA1 Minimal DFA2
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Informal Method -

We try proving R1 ≠ R2

● Find a string that can be matched with only one of the regex
hence proving that the two regex are not equivalent.

● Faster

● But, based on hit and trail.

● Can only be used to prove inequality!!


Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Let us look at examples and find out whether two regex are
equivalent or not??
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 1:

(0+1)*(0+λ) (1+λ)(1+0)*
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 1: Formal
Method
(0+1)*(0+λ) (1+λ)(1+0)*
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 1: Formal
Method
(0+1)*(0+λ) (1+λ)(1+0)*

0,1 0,1
0,λ 1,λ
NFA A B A B
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 1: Formal
Method
(0+1)*(0+λ) (1+λ)(1+0)*

0,1 0,1
0,λ 1,λ
NFA A B A B

0,1
0,1 0,1
DFA A B
A
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 1: Formal
Method
(0+1)*(0+λ) (1+λ)(1+0)*

0,1 0,1
0,λ 1,λ
NFA A B A B

0,1
0,1 0,1
DFA A B
A

0,1 0,1
Minimal
A A
DFA
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 1: Formal
Method
(0+1)*(0+λ) (1+λ)(1+0)*

0,1 0,1
0,λ 1,λ
NFA A B A B

0,1
0,1 0,1
DFA A B
A

0,1 0,1
Minimal Equal
A A
DFA
Automata Formal Languages and Logic
Unit 2 - Equivalence
Example 1: of two regular expression Formal
Method

Equivalent
(0+1)*(0+λ) (1+λ)(1+0)*

0,1 0,1
0,λ 1,λ
NFA A B A B

0,1
0,1 0,1
DFA A B
A

0,1 0,1
Minimal Equal
A A
DFA
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 2:

(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 2: Informal
Method
(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 2: Informal
Method
(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)

Find a string belongs to only one of the regex


Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 2: Informal
Method
(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)

Find a string belongs to only one of the regex

(1+λ)(011*)(0+λ)
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 2: Informal
Method
(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)

Find a string belongs to only one of the regex

(1+λ)(011*)(0+λ)

011
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 2: Informal
Method
(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)

Find a string belongs to only one of the regex

(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)

011
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Example 2: Informal
Method
(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)

Find a string belongs to only one of the regex

(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)

011 cannot be
011
generated
Automata Formal Languages and Logic
Example 2:
Unit 2 - Equivalence Informal
of two regular expression
Method

(0+λ)(11*0)*(1+λ)
≠ (1+λ)(011*)(0+λ)

Find a string belongs to only one of the regex

(0+λ)(11*0)*(1+λ) (1+λ)(011*)(0+λ)

011 cannot be
011
generated
Automata Formal Languages and Logic
Unit 2 - Equivalence of two regular expression

Can you answer whether the two regex are equivalent


or not??

1) (1+λ) (00*1)* 0* and (0+λ) (11*0)1*

2) 0*(10*)* and (1*0)*1*


Automata Formal Languages & Logic

Unit 1*
Regular Grammar

Preet Kanwal
Department of Computer Science & Engineering
Automata Formal Languages and Logic
Unit 2 - Regular Grammar (outline)

● Formal Definition of a Grammar

● Terminology : Sentence , Sentential Form

● Examples on how to construct a Regular Grammar for a given L


Automata Formal Languages and Logic
Unit 2 - Regular Grammar Definition

V - Variables expr, var, S, A, B…, w,x,y

T - Terminals a, b, c …, 0, 1, … id, num


Grammar

𝜶 or 𝞫 = (V U T)*
P - Production Rules 𝜶→𝞫

L(G) =
S - Start Symbol
{w | S ⇒ w, where w ∈ Σ*}
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

Sentence (or a String)

A sentence is made up only of terminals.

Example:
For the Grammar S->aS | a |𝜆
Sentence (or string) is { a or 𝜆 or aa or aaaa)
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

Sentential Form

A sentential form is made up over set of terminals and


non-terminals (VUT)*

Example:
For the Grammar S->aS | a |𝜆
Sentential form is { a or 𝜆 or aS or aa or or aaS aaaa)
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

Noam Chomsky

Chomsky Hierarchy
https://www.javatpoint.com/automata-chomsky-hierarchy
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

Linear Non linear


Grammars grammar
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

Linear Non
Grammar linear
s grammar

S -> aSa | aS | Sa | 𝜖
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

Linear
Grammar
Linear Non
Grammar linear
s
Regular grammar
Grammar
S -> aSa | aS | Sa | 𝜖 S -> SS | aSS | a | 𝜖
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

Regular
Grammar

Left Linear Right Linear


Grammar Grammar

S -> Sa | 𝜖 S -> aS | 𝜖
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

Construct a regular grammar for a given language description


Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the given language

Language RE RG
L = {a} a S -> a
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the given language

Language RE RG

L = {a, b} a+b S -> a | b


or
S -> a
S -> b
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the given language

Language RE RG
L = {ab} ab S -> ab
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the language L={an|n>=0}

Language RE RG
L = {𝛌,a,aa,aaa,...} a* S -> aS|𝛌
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the language L={an|n>=1}

Language RE RG
L = {a,aa,aaa,...} a+ S -> aS | a
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the regular expression


(a+b)*

Language RE RG
L= (a+b)*
S -> aS | bS | 𝛌
{𝛌,a,b,abb,baaaab,.
.....}
L={any number of
‘as and b’s}
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the regular expression


(a+b)+

Language RE RG
L= (a+b)+
S -> aS | bS | a | b
{𝛌,a,b,abb,baaaab,.
.....}
L={any number of
‘as and b’s}
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the regular expression (ab)*

Language RE RG
L= (ab)*
S -> abS | 𝛌
{𝛌,a,b,ab,ababab,...
...}
L={any number of
ab’s}
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the language L={am bn


|n,m>=0}

Language RE RG
L={𝛌,a,b,bb,abb,..... a*b* S -> A|B
.} A:any number of a’s
L={any number of B:any number of b’s
A->aA|𝛌
a’s followed by any
B->bB|𝛌
number of b’s}
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the given language with


even number of a’s. L={a2n |n>=0}

Language RE RG
L={𝛌, (aa)* S -> aaS | 𝛌
aa,
aaaa,
aaaaaa ….
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the given language with


odd number of a’s. L={a2n+1 |n>=0}

Language RE RG
L={a, (aa)*a S -> aaS | a
aaa,
aaaaa,
aaaaaaa ….
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Construct a regular grammar for the given language with


number of a’s as multiples of 4. L={a4n |n>=0}

Language RE RG
L={𝛌, (aaaa)* S -> aaaaS | 𝛌
aaaa,
aaaaaaaa,
aaaaaaaaaaaa, ….
Automata Formal Languages and Logic
Unit 2 - Construct a regular grammar for the given language

Convert finite automata to regular grammar for the language to


accept at least one ‘a’ over the alphabet 𝝨={a,b}

Language RE RG
L={a, b* a (a+b)* S -> bS | aA
bb... a, A -> aA | bA | 𝛌
ba any # of a’s &
b’s
Automata Formal Languages and Logic
Unit 2 - Equivalence of regular grammar and Finite automata

Regular Finite
Grammar Automata
Automata Formal Languages and Logic
Unit 2 - Equivalence of regular grammar and Finite automata

Let’s look at examples to convert :

1. Finite automata to regular grammar


2. Regular Grammar to Finite Automata
Automata Formal Languages and Logic
Unit 2 - Equivalence of regular grammar and Finite automata

1.Convert finite automata to regular grammar for the


language to accept at most 3 ‘a’s over the alphabet
𝝨={a,b}.
a,b
b b b b
a a a a
S A B C D
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Grammar

Let’s look at each transition and perform the conversion

b b
a
S A

S → bS |aA|𝛌
𝛌 on RHS indicates S
A → bA|𝛌 and A are final states
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Grammar

Let’s look at each transition and perform the conversion

b b b
a a
S A B

S → bS |aA|𝛌
𝛌 on RHS indicates S,A
A → bA|aB |𝛌 and B are final states
B →bB |aC|𝛌
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Grammar

Let’s look at each transition and perform the conversion

b b b b
a a a
S A B C

S → bS |aA|𝛌
C →bC|aD|𝛌
A → bA|aB |𝛌
B →bB |aC|𝛌
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Grammar

Let’s look at each transition and perform the conversion

Since D is a dead state, we will not encode C-> aD and D -> aD | bD as D is a


useless Non-terminal(that means it never terminates)
a,b
b b b b
a a a a
S A B C D

S → bS |aA|𝛌
C →bC |𝛌
A → bA|aB |𝛌
B →bB |aC|𝛌
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Grammar

2.Convert a given finite automata accepting


L={na(w)mod 2=0 and nb(w)mod 2=0 } to regular
grammar.
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Grammar

2.Convert a given finite automata accepting L={na(w)mod 2=0 and


nb(w)mod 2=0 } to regular grammar.

Start state of automata will be the start symbol of the grammar.


We start with S,S on seeing terminal a it moves to state A(S→ aA) and on
seeing terminal b it moves to state B (S→ bB).
Since S is also the final state,we introduce the production S→ 𝜆.
Grammar is
S→ aA|bB| 𝜆
A→ aS|bC
B→ aC|bS
C→ aB|bA
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Grammar

3. Converting a given finite automata accepting L={abw,w∈{a,b}*}


to regular grammar.
Automata Formal Languages and Logic
Unit 2 - Finite Automata to Regular Grammar

3. Converting a given finite automata accepting L={abw,w∈{a,b}*}


to regular grammar.

Grammar is,

S→ aA
A→ bB
B→ aB|bB|𝜆
Automata Formal Languages and Logic
Unit 2 - Equivalence of regular grammar and Finite automata

4. Converting given finite automata accepting L={awa,w∈{a,b}*} to


regular grammar.
Automata Formal Languages and Logic
Unit 2 - Equivalence of regular grammar and Finite automata

4. Converting given finite automata accepting L={awa,w∈{a,b}*} to


regular grammar.

So the grammar is,

S→ aA
A→ aA|bA|aB
B→ 𝜆
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Converting Regular grammar to finite automata


Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 1 :
A→aB|bA|b
B→aC|bB
C→aA|bC|a

Variables->state
terminal symbols ->symbols of finite
automata
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 1 :
A→aB|bA|b
B→aC|bB
C→aA|bC|a

● Initial start symbol will be the start state

b
a
A B
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 1 :
A→aB|bA|b
B→aC|bB
C→aA|bC|a

● Transition A→bA transforms to


A→b,when A→𝜆 A→𝜆 indicates A is a
final state.
b
a
A B
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 1 :
A→aB|bA|b
B→aC|bB
C→aA|bC|a

b b
a a
A B C
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 1 :
A→aB|bA|b
B→aC|bB
C→aA|bC|a
● Transition C→aA to C→a when when A→𝜆
indicates A is a final state.
b b b
a a
A B C

a
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 2 :
S→01A
A→10B
B→0A|11
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 2 :
S→01A
A→10B
B→0A|11

0 1
S A
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 2 :
S→01A
A→10B
B→0A|11

0 1 1 0
S A B
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 2 :
S→01A
A→10B B→11 indicates on state B on
B→0A|11 consuming the input 11 we reach final
state

0 1 1 0 1 1
S A B

0
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 3 :
S→bS|aA
A→aA|bA
A→aB
B→bbB
B→𝜆
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Example 3 :
S→bS|aA
A→aA|bA
A→aB
B→bbB
B→𝜆

b
a
S A
Automata Formal Languages and Logic
Unit 2 - Regular Grammar to Finite Automata

Grammar Automata

S→bS|aA
A→aA|bA|aB
B→bbB|𝜆
Automata Formal Languages and Logic
Unit 2 - Aspects of Grammar

Aspects of grammar

Generative Analytical
(Derivative) Aspect

Generate All Check whether


Strings w 𝛜 L(G) w 𝛜 L(G) ??
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Parse Tree
Automata Formal Languages and Logic
Unit 2 - Parse Tree

What is a Tree?

Leaves

Branches

Root
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Technically we represent a Tree upside down

Root-

Branches

Leaves
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Technically we represent a Tree upside down

Root

Branches

Leaves
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Trees
Edge Node

S Root Node
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Parse Tree / Derivation Tree S Root Node (Start Symbol)


V Non-Terminals - Interior Nodes
T Terminal Symbols - Leaf nodes

Yield of Parse Tree - aab


Automata Formal Languages and Logic
Unit 2 - Parsing

Parsing is the process of determining whether a String


w 𝛜 L(G)??
Automata Formal Languages and Logic
Unit 2 - Types of Parsing

Parsing

Top-Down Bottom-up
Parsing Parsing
Automata Formal Languages and Logic
Unit 2 - Parser

Parse Tree for w


String w if w 𝛜 L(G)
Parser
(program)
Error Message
Grammar G
if w ∉ L(G)
Automata Formal Languages and Logic
Unit 2 - Construct a Parse Tree

Let us look at few examples and generate parse tree for a


given String w and Grammar G.
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Parse Tree
S Root node
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Parse Tree
S Root node
S→aS
a S
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Parse Tree
S Root node
S→aS
a S

b A S→bA
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Parse Tree
S Root node
S→aS
a S

b A S→bA

b A A→bA
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Parse Tree
S Root node
S→aS
a S

b A S→bA

b A A→bA

𝜆 A→𝜆
Automata Formal Languages and Logic
Unit 2 - Parse Tree

Parse Tree
Root node
S
S→aS
* a S
A S→bA
*b
A A→bA
*b

* 𝜆 A→𝜆

Leaf Node:* Yield of the tree:abb


Automata Formal Languages and Logic
Unit 2 - Analytical aspect

2. Analytical aspect

Parsing

Top down Bottom up


Automata Formal Languages and Logic
Unit 2 - Regular Grammar

2. Analytical aspect
S→aS
Parsing S→bA
A→bA
A→𝝺

Top down Bottom up


S
a S
b A

��
Automata Formal Languages and Logic
Unit 2 - Regular Grammar

2. Analytical aspect
S→aS
Parsing S→bA
A→bA
A→𝝺

Top down Bottom up


S S
a S S
b A A

�� ab ��
Automata Formal Languages and Logic
Unit 2 - Converting Right Linear Grammar to Left Linear Grammar

All about Left Linear Grammar


Automata Formal Languages and Logic
Unit 2 - Constructing a Left Linear Grammar

Right Linear
Left Linear
A→aB
B→aB|bB|𝜆 ??
L={starting with ‘a’}

https://www.youtube.com/watch?v=8a2jBm5Syjs
Automata Formal Languages and Logic
Unit 2 - Constructing a Left Linear Grammar

Reverse the Left Linear


Right Linear
Symbols on RHS A→Ba
A→aB
B→aB|bB|𝜆 B→Ba|Bb|𝜆
L={starting with ‘a’} L=?

https://www.youtube.com/watch?v=8a2jBm5Syjs
Automata Formal Languages and Logic
Unit 2 - Constructing a Left Linear Grammar

Reverse the Left Linear


Right Linear
Symbols on RHS A→Ba
A→aB
B→aB|bB|𝜆 B→Ba|Bb|𝜆
L={starting with ‘a’} LR={ending with ‘a’}

When we reverse the RHS in every production in the right linear


grammar for “ L” we get a left linear grammar which will represent LR.

https://www.youtube.com/watch?v=8a2jBm5Syjs
Automata Formal Languages and Logic
Unit 2 - Converting Finite automata to Left Linear Grammar

Converting Finite Automata to Left linear grammar


Automata Formal Languages and Logic
Unit 2 - Constructing a Left Linear Grammar

Finite Automata for L


Change initial to final ,final to initial
Reverse the directions of the transition

Finite Automata for LR

Right Linear grammar for LR


Reverse all symbols on RHS of production

Left Linear grammar (LR)R = L

https://www.youtube.com/watch?v=8a2jBm5Syjs
Automata Formal Languages and Logic
Unit 2 - Constructing a Left Linear Grammar

a,b

FA for L = {aw, w 𝛜 {a,b}*} a


A B

a,b
R a
FA for L = {wa, w 𝛜 {a,b}*} A B

Right Linear Grammar


RLG for LR B→aB|bB|aA
A→𝜆

Left Linear Grammar


LLG for ( LR )R =L B→Ba|Bb|Aa
A→𝜆
Automata Formal Languages and Logic
Unit 2 - Converting Left Linear Grammar to Finite automata

Converting Left linear grammar to Finite automata


Automata Formal Languages and Logic
Unit 2 - Converting left Linear Grammar to finite automata

Left Linear grammar for “L”


Reverse the productions in the
grammar for “L” we get LR
Right linear grammar LR

Construct Finite Automata LR


Reverse the finite automata LR

Finite Automata for( LR )R =L


Automata Formal Languages and Logic
Unit 2 - Constructing a Left Linear Grammar

Left Linear Grammar


LLG for L B→Ba|Bb|Aa
A→𝜆

Right Linear Grammar


RLG for LR B→aB|bB|aA
A→𝜆

a,b
R a
FA for L = {wa, w 𝛜 {a,b}*} A B

a,b

FA for ( LR )R =L = {aw, w 𝛜 a
A B
{a,b}*}
Automata Formal Languages and Logic
Unit 2 - LLG or RLG?

Which one is easier LLG or RLG??


Automata Formal Languages and Logic
Unit 2 - Which one is easier ,Left or Right Linear Grammar?

S
Right Linear
S→abA
A→cB|aC a b A

B→ab
C→b c B

a b
Construct Parse tree for the
String “abcab”
https://www.slideserve.com/kaloni/how-to-convert-a-left-linear-grammar-to-a-right-linear-grammar
Automata Formal Languages and Logic
Unit 2 - Which one is easier,Left or Right Linear Grammar?

S
Left Linear
S→Aabc
A a b c
A→Bb|C
B→a
B b
C→b

a
Construct Parse tree for the
String “ababc”
https://www.slideserve.com/kaloni/how-to-convert-a-left-linear-grammar-to-a-right-linear-grammar
THANK YOU

Preet Kanwal
Department of Computer Science & Engineering
[email protected]
+91 80 6666 3333 Extn 724

You might also like