You are on page 1of 46

AUTOMATA THEORY

and
COMPILER DESIGN
(CC3203 )
Introduction to Automata Theory

Why Study Automata Theory?


Introduction to Automata Theory
Introduction to Automata Theory

 Automata theory is the basis for the theory of formal languages.


 It is the study of abstract machines and the computation problems that can
be solved using these machines.
 The abstract machine is called the automata.
 The main motivation behind developing the automata theory was to develop
methods to describe and analyze the dynamic behavior of discrete systems.
Introduction to Automata Theory

 In 1930, A. Turing studied an abstract machine


that had all the capabilities of today’s computers,
at least as far as what they could compute.
 Turing’s goal was to describe precisely the
boundary between what a computing machine
could do and what it could not do.
Introduction to Automata Theory

 His conclusions apply not only to his abstract Turing machines but to today’s
real machines.
Introduction to Automata Theory

 The Applications of Automata are given as follows:


 For the designing of lexical analysis of a compiler.
 For recognizing the pattern using regular expressions.
 For the designing of the combination and sequential circuits using Mealy
and Moore Machines.
 Used in text editors.
 For the implementation of spell checkers.
Introduction to Automata Theory

 The figure shows the following features of automata:


 Input Tape for Input
 States of automata
 State relation
 Finite Control
 Head
Introduction to Automata Theory

 A Finite Automata consists of the five tuples { Q, Σ, q, F, δ }


Q: Finite set of states.
Σ: a set of Input Symbols.
Q: Initial state.
F: a set of Final States.
δ: Transition Function
Introduction to Automata Theory

 Symbol: A symbol (often also called a character) is the smallest building block,
which can be any alphabet, letter, or picture.
 Example: a,b,c…..

 Alphabets (Σ): Alphabets are a set of symbols, which are always finite.
 Σ={a,b}
 Σ={0,1}
Introduction to Automata Theory

 String: A string is a finite sequence of symbols from some alphabet. A string is


generally denoted as w and the length of a string is denoted as |w|.
 Σ* is a set of all possible strings
 Empty string is the string with zero occurrences of symbols, represented as
ε.
Introduction to Automata Theory

 Language: A language is a collection of appropriate strings. A language that is


formed over Σ can be Finite or Infinite.
 Example of Finite Language:
 L1 = { set of string of length 2 }
 L2= { xy, yx, xx, yy }

 Example of Infinite Language:


 L1 = { set of all strings starts with 'b' }
 L2 = { babb, baa, ba, bbb, baab, ....... }
Introduction to Automata Theory

 Grammar: It is a finite set of formal rules for generating syntactically correct


sentences or meaningful correct sentences.
 Constitute Of Grammar: Grammar is composed of two basic elements –
 Terminal Symbols: Terminal symbols are those that are the components of
the sentences generated using grammar and are represented using small
case letters like a, b, c, etc.
 Non-terminal symbols: Non-terminal symbols are those symbols that take
part in the generation of the sentence but are not the component of the
sentence. Non-Terminal Symbols are also called Auxiliary Symbols and
Variables. These symbols are represented using capital letters like A, B, C,
etc.
Introduction to Automata Theory

 The figure shows the following features of automata:


 Input Tape for Input
 States of automata
 State relation
 Finite Control
 Head
Introduction to Automata Theory

 Representation of Automata
 Transition Table
 Transition Function
 Transition Diagram
Introduction to Automata Theory

 Type of Automata
 Deterministic Finite Automata (DFA).
 Non-Deterministic Finite Automata (NFA or NDFA).
 Non-Deterministic Finite Automata with epsilon moves (e-NFA or e-
NDFA).
 Non-Deterministic Finite Automata without epsilon moves
Introduction to Automata Theory

 Definition of different automata (DFA)


 A Deterministic Finite automata is defined as a 5-tuples
 M=(Q, Σ, δ,q0,F)
 Where:
Q: Finite set called states.
Σ: Finite set called alphabets.
δ: Q × Σ → Q is the transition function.
q0 ∈ Q is the start or initial state.
F: Final or accept state.
Introduction to Automata Theory

 Construction Of DFA:
 Problems: In this problem, we will discuss the construction of DFA for languages
consisting of strings starting with a particular substring.
 Steps To Construct DFA-
 The following steps are followed to construct a DFA -
 Step-01:
 Determine the minimum number of states required in the DFA.
 Draw those states.
 Use the following rule to determine the minimum number of states-
 Calculate the length of the substring.
 All strings starting with ‘n’ length substring will always require minimum
(n+2) states in the DFA.
Introduction to Automata Theory

 Construction Of DFA:
 Step-02:
 Decide the strings for which DFA will be constructed.
 Step-03:
 Construct a DFA for the strings decided in Step-02.
 While constructing a DFA,
 Always prefer to use the existing path.
 Create a new path only when there exists no path to go with.
 Step-04:
 Send all the left possible combinations to the dead state.
 Do not send the left possible combinations over the starting state
Introduction to Automata Theory

 Designing of DFA:
 Design a DFA that accept string w={a} over the Σ={a}
 Design a DFA that accept string w={a} over the Σ={a,b}
 Design a DFA that accept string of length |w|=2 over the Σ={a,b}
 Design a DFA that accept string of length |w|<=3 over the Σ={a,b}
 Design a DFA that accept string of length |w|>=3 over the Σ={a,b}
 Design a DFA that accepts an even number of “a” over the Σ={a,b}
 Design a DFA that accepts an odd number of “a” over the Σ={a,b}
Introduction to Automata Theory

 Designing of DFA :
 Design a DFA that accepts an odd number of “a” over the Σ = { a, b}
 Design a DFA that accepts an even number of “a” and an even number of
“b” over the Σ={a, b}
 Design a DFA that accepts string starting with “a” over the Σ={a, b}
 Design a DFA that accepts a string ending with “a” over the Σ={a, b}
 Design a DFA that accepts a string starting with “a” ending with “b” over
the Σ={a, b}
Introduction to Automata Theory

 Designing of DFA :
 Design a DFA that accepts a string starting with “a” ending with “b” over
the Σ={a, b}
 Design a DFA that accepts a string starting and ending with the same
symbol over the Σ={a, b}
 Design a DFA that accepts a string starting and ending with a different
symbol over the Σ={a, b}
 Design a DFA that accepts a string containing “ab” as a sub-string over the
Σ={ a, b}
 Design a DFA that accepts a string containing “a” is divisible by 2 over the
Σ={ a, b}
Introduction to Automata Theory

 Designing of DFA :
 Design a DFA that accepts a string containing “a” is divisible by 2 over the
Σ={ a, b}
 Design a DFA that accepts a string containing “a” is divisible by 3 over the
Σ={ a, b}
 Design a DFA that accepts binary numbers divisible by 2 over the Σ={ 0, 1}
 Design a DFA that accepts a language L={ anbm | m>1, n>2} over the Σ={ a,
b}
Introduction to Automata Theory

 Type of Automata
 Deterministic Finite Automata (DFA).
 Non-Deterministic Finite Automata (NFA or NDFA).
 Non-Deterministic Finite Automata with epsilon moves (e-NFA or e-
NDFA).
 Non-Deterministic Finite Automata without epsilon moves
Introduction to Automata Theory

 Non-Deterministic Finite Automata (NFA or NDFA).


 NFA stands for non-deterministic finite automata. It is easier to construct an
NFA than a DFA for a given problem.
 The finite automata are called NFA when there exist many paths for specific
input from the current state to the next state.
 Every NFA is not DFA, but each NFA can be translated into DFA.
 NFA is defined in the same way as DFA but with the following two
exceptions, it contains multiple next states, and it contains ε transition.
Introduction to Automata Theory

 Non-deterministic finite automata (NFA):


 NFA also has five states which are the same as DFA, but with different
transition functions, as shown as follows:
δ: Q X Σ -> 2Q
 M=(Q, Σ, δ,q0,F)

 Where:
Q: Finite set of states
Σ: Finite set of the input symbol
q0: Initial state
F: Final state
δ: Transition function: Q X Σ -> 2Q
Introduction to Automata Theory

 Non-deterministic finite automata (NFA):


 Example 1:
 NFA with ∑ = {0, 1} accepts all strings starting with 01.
 Solution:

 Example 2:
 Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
 Solution:
Introduction to Automata Theory

 Conversion from NFA to DFA


 In NFA, when a specific input is given to the current state, the machine goes
to multiple states.
 It can have zero, one, or more than one move on a given input symbol.
 On the other hand, in DFA, when a specific input is given to the current state,
the machine goes to only one state. DFA has only one move on a given input
symbol.
 Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M). There
should be equivalent DFA denoted by M' = (Q', ∑', q0', δ', F') such that L(M) =
L(M').
Introduction to Automata Theory

 Conversion from NFA to DFA


 Steps for converting NFA to DFA:
 Step 1: Initially Q' = ϕ
 Step 2: Add q0 of NFA to Q'. Then find the transitions from this start state.
 Step 3: In Q', find the possible set of states for each input symbol. If this set
of states is not in Q', then add it to Q'.
 Step 4: In DFA, the final state will be all the states which contain F(final
states of NFA)
Introduction to Automata Theory

 Conversion from NFA to DFA


 Example 1:

State 0 1 State 0 1

→q0 q0 q1 →[q0] [q0] [q1]


NFA to DFA
q1 {q1, q2} q1 [q1] [q1, q2] [q1]
*q2 q2 {q1, q2} *[q1, q2] [q1, q2] [q1, q2]
Introduction to Automata Theory
 NFA with Null or ∈ move

NFA with (null) or


∈ move : If any
In diagrams, such
The automaton finite automata
transitions are Note that this does
may be allowed to contains ε (null)
depicted by not mean that ε
change its state move or
labeling the has become an
without reading transaction, then
appropriate arcs input symbol.
the input symbol. that finite
with ε.
automata is called
NFA with ∈ moves
Introduction to Automata Theory
 NFA with Null or ∈ move
 Example: L={ an | n is even or divisible by 3 }
Introduction to Automata Theory
 NFA with Null or ∈ move
 Epsilon (∈) – closure:
 Epsilon closure for a given state X is a set of states that can be reached
from the states X with only (null) or ε moves including the state X itself.
 In other words, ε-closure for a state can be obtained by union operation
of the ε-closure of the states which can be reached from X with a single ε
move in a recursive manner.
 Eliminating ε Transitions
 NFA with ε can be converted to NFA without ε, and this NFA without ε can
be converted to DFA.
 To do this, we will use a method, that can remove all the ε transitions from
the given NFA.
Introduction to Automata Theory
 NFA with Null or ∈ move
 Eliminating ε Transitions
 Steps:
1. Find out all the ε transitions from each state from Q. That will be called
ε-closure{q1} where qi ∈ Q.
2. Then δ' transitions can be obtained. The δ' transitions mean a ε-closure
on δ moves.
3. Repeat Step 2 for each input symbol and each state of the given NFA.
4. Using the resultant states, the transition table for equivalent NFA without
ε can be built.
Introduction to Automata Theory
 NFA with Null or ∈ move
 Eliminating ε Transitions
 Example:
 Convert the following NFA with ε to NFA without ε.

 Solutions: We will first obtain ε-closures of q0, q1 and q2 as follows:


 ε-closure(q0) = {q0}
 ε-closure(q1) = {q1, q2}
 ε-closure(q2) = {q2}
Introduction to Automata Theory
 Eliminating ε Transitions
 Example:
 Convert the following NFA with ε to NFA without ε.
 Solutions: We will first obtain ε-closures of q0, q1 and q2 as follows:
 ε-closure(q0) = {q0}
 ε-closure(q1) = {q1, q2}
 ε-closure(q2) = {q2}

Now the δ' transition on each input symbol is obtained as: δ'(q0, b) = ε-closure(δ(δ^(q0, ε),b))
δ'(q0, a) = ε-closure(δ(δ^(q0, ε),a)) = ε-closure(δ(ε-closure(q0),b))
= ε-closure(δ(ε-closure(q0),a)) = ε-closure(δ(q0, b))
= ε-closure(δ(q0, a)) =Ф
= ε-closure(q1)
= {q1, q2}
Introduction to Automata Theory
 Eliminating ε Transitions
 Example:
Now the δ' transition on q1 is obtained as:
δ'(q1, a) = ε-closure(δ(δ^(q1, ε),a))
= ε-closure(δ(ε-closure(q1),a))
= ε-closure(δ(q1, q2), a)
= ε-closure(δ(q1, a) ∪ δ(q2, a))
= ε-closure(Ф ∪ Ф)

δ'(q1, b) = ε-closure(δ(δ^(q1, ε),b))


= ε-closure(δ(ε-closure(q1),b))
= ε-closure(δ(q1, q2), b)
= ε-closure(δ(q1, b) ∪ δ(q2, b))
= ε-closure(Ф ∪ q2)
= {q2}
Introduction to Automata Theory
 Eliminating ε Transitions
 Example:
The δ' transition on q2 is obtained as:
δ'(q2, a) = ε-closure(δ(δ^(q2, ε),a))
= ε-closure(δ(ε-closure(q2),a))
= ε-closure(δ(q2, a))
= ε-closure(Ф)

δ'(q2, b) = ε-closure(δ(δ^(q2, ε),b))


= ε-closure(δ(ε-closure(q2),b))
= ε-closure(δ(q2, b))
= ε-closure(q2)
= {q2}
Introduction to Automata Theory
 Eliminating ε Transitions
 Example:
Now we will summarize all the computed δ' transitions:
δ'(q0, a) = {q0, q1}
δ'(q0, b) = Ф
δ'(q1, a) = Ф
δ'(q1, b) = {q2}
δ'(q2, a) = Ф
δ'(q2, b) = {q2}
Introduction to Automata Theory

 Minimization of DFA:
 DFA minimization is the process of converting a given DFA to its equivalent
DFA with a minimum number of states.
 What is a requirement of DFA Minimization?
 Problem: Design a DFA that accepts an odd number of ‘a’ over the ∑={ a, b}.
Introduction to Automata Theory

 Minimization of DFA:

Method for Minimization of DFA

Equivalence Theorem Myhill-Nerode Theorem


(Partition Method) (Table Filling Method)
Introduction to Automata Theory

 Minimization of DFA:
 Two states will be equivalent
 δ(p,w) ∈ F ⟹ δ(q,w) ∈ F
 δ(p,w) ∉ F ⟹ δ(q,w) ∉ F
 If p and q are equivalent, then we can combine them into 1 state
 Type of equivalence:
 If |w|=0, then p and q are said to be 0 equivalence
 If |w|=1, then p and q are said to be 1 equivalence
 If |w|=2, then p and q are said to be 2 equivalence

 If |w|=n, then p and q are said to be nth equivalence


Introduction to Automata Theory
 Equivalence Theorem:
 Step 1:
 Eliminate all the dead states and inaccessible states from the given DFA (if any).
 Draw a state transition table for the given DFA.
 Step2:
 Take a counter variable k and initialize it with value 0.
 Divide Q (set of states) into two sets such that one set contains all the non-final states, and the other
set contains all the final states.
 This partition is called P0.
 Step 3:
 Increment k by 1.
 Find Pk by partitioning the different sets of Pk-1.
 In each set of Pk-1, consider all the possible pairs of states within each set and if the two states are
distinguishable, partition the set into different sets in Pk.
 Step4:
 Repeat step 3 until no change in partition occurs.
 In other words, when you find Pk = Pk-1, stop.
 Step 5:
 All those states which belong to the same set are equivalent.
 The equivalent states are merged to form a single state in the minimal DFA.
Introduction to Automata Theory
 Equivalence Theorem:
 Example:
Introduction to Automata Theory
 Equivalence Theorem:
 Example:

 Now using the Equivalence Theorem, we have-


 P0 = { A , B , C , E } { D }
 P1 = { A , B , E } { C } { D }
 P2 = { A,E } { B } { C} { D }
 P3 = { A,E } { B } { C} { D }
 Since P3 = P2, so we stop.
 From P3, we infer that states A and E are equivalent and can be merged
Introduction to Automata Theory
 Equivalence Theorem:
 Example:

You might also like