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

Acd Unit1

compiler design

Uploaded by

SANJAY N
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)
5 views

Acd Unit1

compiler design

Uploaded by

SANJAY N
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/ 105

Vallurupalli Nageswara Rao Vignana Jyothi Institute of

Engineering &Technology
Department of Computer Science & Engineering

SUBJECT: Automata and Compiler


Design
Subject Code: 22PC1CB303

Topic Name: Finite Automata


III year-I sem, sections- B
Dr.M.Gangappa
Associate Professor
Email: gangappa_m@vnrvjiet
<Web link of your created resource if any>

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 1


Unit – 1
▪ Syllabus:
Formal Languages and Regular Expressions: Definition of
Languages, Finite Automata – DFA, NFA, regular expressions,
Conversion of regular expression to NFA, NFA to DFA, Pumping Lemma
for regular languages, lex tools.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 2


▪ Symbols: Symbols are basic build blocks in any Formal Languages. A
symbol can be anything such as a letter, digit, special character or picture.
▪ E.g., a,b,c,A,B,C,0,1,2,$,# etc.,
▪ Alphabets ( Σ ): Finite collection of symbols. In general, These are used as
input symbols.
▪ E.g.,
Σ = { 0,1} Binary alphabet
Σ = { a,b,c,d,e,f,g,h,I,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z} English alphabets
Σ = {0,1,2,3,4,5,6,7,8,9} decimal alphabets

Prepared by M.Gangappa ,CSE department September 16, 2024 3


▪ Strings (or word or sentence) : Sequence of symbols taken from input alphabet
Σ.

▪ E.g., : a,b,ab,aab,abc, …………………….

▪ Length of a String : Number of symbols present in a string and is denoted by |S|


where S is the string.

▪ E.g., : S = abbcbdae, then |S| = 8

▪ Empty String: If |S| = 0 , then S is called empty string and is denoted by ε or λ.

Prepared by M.Gangappa ,CSE department September 16, 2024 4


▪ Let an alphabet Σ = {a,b}. How many strings of length 2 can be possible?
▪ Solution: aa,ab,ba,bb (22 = 4)
▪ If the number of symbols in Σ is |Σ|, then the number of strings of length
n over the alphabet |Σ| is |Σ|𝑛 .
▪ If Σ = {0,1}, then the valid strings are 0,1,00,01,10,11,1110,0000.
▪ Σ∗ 𝒅𝒆𝒏𝒐𝒕𝒆𝒔 𝒕𝒉𝒆 𝒔𝒆𝒕 𝒐𝒇 𝒔𝒕𝒓𝒊𝒏𝒈𝒔 𝒐𝒇 𝒂𝒏𝒚 𝒍𝒆𝒏𝒈𝒕𝒉 𝒇𝒓𝒐𝒎 Σ.
▪ Σ𝒌 𝒅𝒆𝒏𝒐𝒕𝒆𝒔 𝒕𝒉𝒆 𝒔𝒆𝒕 𝒐𝒇 𝒔𝒕𝒓𝒊𝒏𝒈𝒔 𝒐𝒇 𝒍𝒆𝒏𝒈𝒕𝒉 𝒌 𝒇𝒓𝒐𝒎 Σ.

Prepared by M.Gangappa ,CSE department September 16, 2024 5


▪ Σ∗ = Σ0 ∪ Σ1 ∪ Σ2 ∪ … … … … … … ∪ Σ𝑘 ∪ … … …
▪ Σ+ = Σ1 ∪ Σ2 ∪ … … … … … … ∪ Σ𝑘 ∪ … … …
▪ If Σ = { a, b} , then (i) Σ2 = { aa,ab,ba,bb}
(ii) Σ3 = { aaa,aab,aba,baa,bba,abb,bab,bbb}
▪ Σ∗ = a set of all strings of any length over the input alphabet Σ. Therefore
Σ∗ is considered as universal set. This set is infinite set.
▪ Find Σ∗ , if Σ = {a,b}.
Ans : Σ∗ contains set of all possible strings over {a,b} of all lengths.

Prepared by M.Gangappa ,CSE department September 16, 2024 6


▪ Concatenation : The concatenation of strings U and V is given by
appending the symbols of V to the right end symbols of U, denoted by UV.
▪ E.g., If x = 001100 and y = 1111 then xy = 0011001111.
▪ Substring : The substring of a string w is any string of consecutive symbols
in w.
▪ E.g., If x = 011101110111, then the valid substrings are the following:
▪ 01,11011,111,111011,0111.

Prepared by M.Gangappa ,CSE department September 16, 2024 7


▪ Prefix and suffix of a string: A prefix of a string is any number of leading
symbols of that string. A suffix is any number of trailing symbols of that
string.
▪ Example: String abc
▪ Prefix : ε, a, ab, abc
▪ Suffix : ε, c, bc, abc
▪ A prefix or suffix of a string, other than the string itself, is called a proper
prefix of proper suffix.

Prepared by M.Gangappa ,CSE department September 16, 2024 8


▪ A formal language L over an input alphabet Σ is a subset of Σ∗ that is, the
set of strings over an input alphabet Σ.
▪ Some examples:
1) Φ
2) Let Σ = {a,b} and 𝑎𝑛 | 𝑛 ≥ 0 . 𝑡ℎ𝑒𝑛
L = { ε, a, aa, aaa, …. }
3) Let Σ = {a,b} and 𝑎𝑛 𝑏 𝑛 | 𝑛 ≥ 0, then
L = {ε,ab,aabb,aaabbb,……}
Prepared by M.Gangappa ,CSE department September 16, 2024 9
▪ Since the languages are represented as sets, the operations on languages
can be defined in terms of set operations.

▪ Union: The union is the simple operation on two languages. If 𝐿1 and 𝐿2


are two languages, then the union, denoted by 𝐿1 ∪ 𝐿2 is a language
containing all strings (w) from both languages.

▪ 𝐿1 ∪ 𝐿2 = 𝑤 𝑤 𝑖𝑛 𝐿1 𝑜𝑟 𝑤 𝑖𝑛 𝐿2 }

Prepared by M.Gangappa ,CSE department September 16, 2024 10


Union operation
Example 1: Let 𝐿1 = 0,01,10,11 and 𝐿2 = { 0,01,001}.

𝐿1 ∪ 𝐿2 = { 0, 01,10,11,001}

Example 2: Let Σ = 𝟎 𝐚𝐧𝐝 𝑳𝟏 = 𝒆𝒗𝒆𝒏 𝒏𝒊𝒎𝒃𝒆𝒓 𝒐𝒇 𝒛𝒆𝒓𝒐𝒔 𝒂𝒏𝒅 𝑳𝟐 =


{ 𝒐𝒅𝒅 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒛𝒆𝒓𝒐𝒔}. Find the union of 𝑳𝟏 𝒂𝒏𝒅 𝑳𝟐 .

Prepared by M.Gangappa ,CSE department September 16, 2024 11


Concatenation
▪ The concatenation of languages 𝐿1 and 𝐿2 is given by appending the
strings of 𝐿2 to the right end strings of 𝐿1 , denoted by 𝐿1 𝐿2 .

▪ 𝐿1 𝐿2 = 𝑢𝑣 𝑢 ∈ 𝐿1 𝑎𝑛𝑑 𝑣 ∈ 𝐿2 }

▪ Example : Let Σ = { 0,1}, A and B are languages over Σ . A = { 00,11} and

B = { 01,10} . Then the concatenation of A and b is

AB = { 0001,1101,0010,1110}

Prepared by M.Gangappa ,CSE department September 16, 2024 12


Closure operations
i) Kleene / star closure :
𝐿∗ , the Kleene Closure of L: is a set of strings formed by taking any number of
strings (possibly none) from L, possibly with repetitions and concatenating all of
them.
𝐿∗ = 𝐿0 ∪ 𝐿1 ∪ 𝐿2 … … …
Example: if L = { 0} then
𝐿∗ = { ε, 0,00,000,0000…}
ii) Positive closure (𝑳+ ) 𝑖𝑠 𝑎 𝑠𝑒𝑡 𝑜𝑓 strings formed by taking any number of strings
except null string from L, possibly with repetitions and concatenating all of them.
𝐿+ = 𝐿1 ∪ 𝐿2 … … …
Example: if L = { 0} then
𝐿+ = { 0,00,000,0000…}

Prepared by M.Gangappa ,CSE department September 16, 2024 13


Review Questions
1. Define a word or a string from a set S and give an example.

2. Consider the words x = 110 , y = 0110. Find i) xy ii) yx iii) 𝑥 2 iv) εy

3. Define a language over the input alphabet S.

4. Let 𝐿1 = 𝑥 𝑥 𝑐𝑜𝑛𝑠𝑖𝑠𝑡𝑠 𝑜𝑓 𝑜𝑛𝑙𝑦 0′ 𝑠 } = { 0, 00, 000, … } and 𝐿2 =


𝑥 𝑥 𝑐𝑜𝑛𝑠𝑖𝑠𝑡𝑠 𝑜𝑓 𝑜𝑛𝑙𝑦 0′ 𝑠 or only 1′ 𝑠 and have an odd length} = {
0,1,000,111,….}. Find the intersection of 𝐿1 𝑎𝑛𝑑 𝐿2 .

Prepared by M.Gangappa ,CSE department September 16, 2024 14


▪ In 1936, Allan Turing introduced the concept of abstract computing machines
(theoretical model of a computer).

▪ Automata theory is the study of abstract computing devices.

▪ Formal languages and Automata theory is a theoretical branch of computer science


and mathematics, which mainly deals with the logics of computation with respect to
simple machines, referred to as automata.

▪ Automaton ( automata in plural) is a self computing device.

▪ An automaton with finite number of states is called Finite Automaton (FA) or Finite
State Machines (FSM).

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 15


Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 17
▪ Computations happen everywhere: on your laptop, on your cell phone etc.
▪ An automata is an abstract computing device. Abstract devices are
simplified models of real computations.
▪ As such every automaton includes some essential features.
▪ It has a mechanism for reading input. It will be assumed that input is a
string over a given alphabet, written on an input file, which the automaton
can read. The input file is divided into cells, each of which can hold one
symbol. The input mechanism can read the input file from left to right, one
symbol at a time. The input mechanism can also detect the end of the
input string.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 18


▪ The mechanism may have a temporary storage device.
▪ The automaton can produce output of some form.
▪ The automaton has a control unit, which can be in any one of a finite
number of internal states and which can change state in some defined
manner. The Figure 1. shows a schematic representation of a general
automaton.
▪ The automaton can be of two types: acceptors and transducers.
▪ Acceptors give the results in the form of accepted or rejected.
▪ Transducers are the one which produce outputs.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 19


▪ A Finite Automata (FA) is a mathematical model for the study of abstract
computing devices (machines).
▪ Definition: A finite automata is defined by a 5-tuple M =(Q,Σ,δ,𝑞0 ,F)
where
Q is the finite set of non-empty states.
Σ is a finite non empty set of symbols called input alphabets
δ is the transition function, defines the transition from one state to
another for a given input alphabet. δ : Q X Σ Q
𝑞0 is the initial state, 𝑞0 ∈ Q .
F is the final (accepting) state, F ⊆ Q.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 20


A finite automata can be represented in two ways: Transition diagrams and
transition Tables.
1. Transition Diagram or state graph:
It is the graphical representation of a Finite automata. It is a directed
graph in which Nodes represent the state of the computation and directed
edges illustrate the transition with label as input symbols from one state to
another.
It contains two distinguishable states. They are initial state which is
labelled with arrow symbol and the final or accepting states are represented
as double circles.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 21


▪ If there is a transition from state 𝑞𝑚 to state 𝑞𝑛 on input alphabet a, then
there is an edge labelled a from state 𝑞𝑚 to 𝑞𝑛 in the transition diagram.
▪ For each input symbol a in Σ, if δ(𝒒𝒎 ,a) = 𝒒𝒏 , then there is an edge from
𝑞𝑚 to 𝑞𝑛 .
▪ An arrow into the state 𝑞0 corresponds to start state or initial state.
▪ Nodes marked by double circle corresponding to accepting state or final
state.
▪ The FA(Finite automata) accepts the string w if the sequence of transitions
corresponding to the symbols of w leads from the initial state to an
accepting state.
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 22
▪ Example :
a
𝑞0 is the initial state
a
𝑞0 𝑞1 𝑞1 and 𝑞2 are the final states
σ = { a,b} input alphabets
b

𝑞2

Figure 1.2

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 23


▪ A transition table is a conventional tabular representation of transition function
δ that takes two arguments ( state and input alphabet) and returns new state or
the self state.
▪ The row of a table corresponds to the states and the column of a table
corresponds to the input symbols.
▪ The transition Table for the Figure 1.2

a b
𝑞0 𝑞1 𝑞2
𝑞1 𝑞1
𝑞2 𝑞2

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 24


Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 25
Types of Automata Models

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 26


Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 27
▪ A Finite automata is said to be deterministic finite automata (DFA) if the
corresponding to the input symbol, there is only one resultant state (ie.,
there is only one transition).
▪ FA (DFA or NFA) are used in pattern recognition in compiler design.
▪ Example:
int x, y;
For the above example, FA (or Regular Expressions) can be used to
recognize the words “int”, “x”, ”y”, and “;”
FA recognizes the word “int” as a keyword in C language. Similarly, x and y
as an identifiers and ; as statement end marker in C language.
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 29
▪ Design a DFA that accepts one or more number of ones in a Language L.
▪ Solution:
First, identify input alphabet Σ
Σ = {1}
Second, identify the language L
L = { 1, 11, 111, 1111, … … … }
Take the small string: 1
Draw the diagram

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 30


Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 31
▪ A string x is accepted by a finite automata M = (Q,Σ,δ,𝑞0 , F) if δ (𝑞0 ,x) = q for
some q∈ F. this is basically the acceptability of a string by the final state.
▪ How a FA processes a string?
▪ FA (DFA or NFA) starts with the initial state 𝑞0 . Let 𝑎1 , 𝑎2 , 𝑎3 … … 𝑎𝑛 is a
sequence of input symbols and δ is a transition function.
▪ Step1 : δ(𝑞0 , 𝑎1 ) = 𝑞1 , FA in state 𝑞0 on input 𝑎1 , enters state 𝑞1 .
▪ Step2 : δ(𝑞1 , 𝑎2 ) = 𝑞2 , FA in state 𝑞1 on input 𝑎2 , enters state 𝑞2 .
▪ …. ….. ….. …..
▪ δ(𝑞𝑛−1 , 𝑎𝑛 ) = 𝑞𝑛 .
▪ If 𝑞𝑛 is a member of F, then input 𝑎1 , 𝑎2 , 𝑎3 … … 𝑎𝑛 is accepted, otherwise not
accepted by FA.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 32


Determine the string 10010 is accepted by
DFA or not.
Solution:
Transition Table
Q = { 𝑞0 , 𝑞1 , 𝑞2 } , Σ = { 0,1} and F = {𝑞2 }.
The given input string : 10010
δ (𝑞0 , 10010) |− δ (𝑞0 , 0010)
|− δ (𝑞1 , 010)
|− δ (𝑞1 , 10)
|− δ (𝑞2 , 0)
|− δ (𝑞2 , ε)
|− 𝑞2 , Accepted
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 33
▪ Property 1: δ(q, ε ) = q , This means that the state of the system can be
changed only on input symbol.
▪ Property 2: For a string ω and input symbol a,
δ(q, aω) = δ(δ(q, a), ω)
δ(q, ωa) = δ(δ(q, ω), a)

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 34


1. DFA that accepts 01.
2. DFA to accept the even number of zeros. Assuming that Σ = { 0,1}.
3. DFA to accept all strings ending with 00. Assuming that Σ = { 0,1}.
4. DFA to accept all strings with three consecutive 1’s. Assuming that
Σ = { 0,1}.
5. DFA that accepts all strings such that every string contains prefix ab over Σ = { a,
b}.
6. DFA that accepts all strings with exactly one 1 over Σ = { 0,1}.
7. DFA that accepts all strings over Σ = { a, b, c} containing “bca” as substring.
8. DFA that accepts language L = { awa | w∈{a, b}* }
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 35
Design a DFA that accepts all strings starting with 0. Assume that Σ = { 0,1}

L = { 0,01,001,00,001,0111….}

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 36


The Following DFA accepts all strings beginning with 0

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 37


▪ Definition: A non-deterministic finite automata (NFA) is defined by a 5-
tuple M =(Q,Σ,δ,𝑞0 ,F) where
Q is the finite set of non-empty states.
Σ is a finite non empty set of symbols called input alphabets
δ is the transition function, defines the transition from one state to
another for a given input alphabet. δ : Q X Σ 2Q
𝑞0 is the initial state, 𝑞0 ∈ Q .
F is the final (accepting) state, F ⊆ Q.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 38


▪ A Non-deterministic Finite-state Automata (NFA) is a language recognizing
system similar to DFA.
▪ A Finite Automata(FA) is said to be non-deterministic, if there are more than one
possible transitions from current state on the current input symbol.
▪ When an NFA receives an input symbol a, it can make a transition to zero, one,
two, or even more states.
– each state can have multiple edges labeled with the same symbol.
An NFA accepts a string w iff there exists a path labeled w from the initial state to
one of the final states.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 39


Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 40
▪ Design NFA that accepts all strings ending with 00 over Σ = {0,1} .
NFA M = ({𝑞0 , 𝑞1 , 𝑞2 }, {0,1},δ, 𝑞0 , {𝑞2 }) is the following

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 41


▪ Design NFA that accepts all strings over {0,1} such that string contains two
consecutive zeros or two consecutive ones.
0,1 0,1

0 0
𝑞0 𝑞1 𝑞2

𝑞3
1
0,1
𝑞4

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 42


NFA with ε-transitions
▪ NFA with ε-moves allows transitions on empty string.
▪ From a particular state, on receiving without input symbol(ε), the transitions are
made in a FA, such transitions are called ε-transitions or null transitions .
▪ The NFA with ε-moves is defined by a 5-tuple M =(Q,Σ,δ,𝑞0 ,F) where
Q is the finite set of non-empty states.
Σ is a finite non empty set of symbols called input alphabets
δ is the transition function, defines the transition from one state to
another for a given input alphabet. δ : Q X (Σ ∪ {ε}) 2Q .
𝑞0 is the initial state, 𝑞0 ∈ Q .
F is the final (accepting) state, F ⊆ Q.
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 43
0 1 2
Transition table
ε ε
𝑞0 𝑞1 𝑞2

Figure: NFA with ε - moves

ε-closure:
ε-closure (q) is a set of all states p
reachable from q to p on ε-moves
including the state q itself.

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 44


▪ Determine the ε-closure on states in the following figure:
ε- closure(𝑞0 ) = {𝑞0 } υ {𝑞1 } υ {𝑞2 }
= {𝑞0 , 𝑞1 , 𝑞2 }

ε- closure(𝑞1 ) = {𝑞1 } υ {𝑞2 }


= {𝑞1 , 𝑞2 }
ε- closure(𝑞2 ) = {𝑞2 }

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 45


▪ Let given NFA is M = (Q, Σ, δ, q0, F ), now to construct DFA M’ = (Q’, Σ, δ’,
[q0], F’) , the procedure is the following:
1. Q’ = 2Q , the set of all subsets of Q.
2. [q0] is the initial state in DFA
3. The input alphabets are same in the NFA and DFA (ie., Σ ).
4. F’ is the set of all subsets of Q which are having final state of given NFA.
5. δ’((q1,q2,…qn),a) = δ(q1,a) U δ(q2,a) U …Uδ(qn,a)
= {p1,p2,…pn}
Add state [p1,p2,…pn] to DFA if it is not there.

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 46


Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 47
Step a:
Consider the start state q0,
Seek all the transitions from state q0 for all input symbols a and b.
We get,
δ(q0, a) = [q0, q1]
δ(q0, b) = [q0]
Now we get,

Here we got new state, [q0, q1].


Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 49
Step b:
Consider the state [q0, q1],
Seek all the transitions from state [q0, q1] for all input symbols a and b.
We get,
δ([q0, q1], a) = δ(q0, a) ∪ δ(q1, a)
= [q0, q1] ∪ q2
= [q0, q1, q2]

δ([q0, q1], b) = δ(q0, b) ∪ δ(q1, b)


= [q0 ] ∪ [q1]
= [q0, q1]

Here we got new state, [q0, q1, q2].


Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 50
Step c:
Consider the state [q0, q1, q2],
Seek all the transitions from state [q0, q1, q2] for all input symbols a and b.
We get,
δ([q0, q1, q2], a) = δ(q0, a) ∪ δ(q1, a) ∪ δ(q2, a)
= [q0, q1] ∪ q2 ∪ q3
= [q0, q1, q2, q3]

δ([q0, q1, q2], b) = δ(q0, b) ∪ δ(q1, b) ∪ δ(q2, b)


= q0 ∪ q1 ∪ q3
= [q0, q1, q3]
Here we got 2 new
states, [q0, q1, q2, q3]
and [q0, q1, q3].
Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 51
Step d:
Consider each one of these states, [q0, q1, q2, q3] and [q0, q1, q3]
Consider the state [q0, q1, q2, q3],
Seek all the transitions from state [q0, q1, q2, q3] for all input symbols a
and b.
We get
δ([q0, q1,q2,q3], a) = δ(q0, a) ∪ δ(q1, a) ∪ δ(q2, a) ∪ δ(q3, a)
= [q0, q1] ∪ q2 ∪ q3 ∪ φ
= [q0, q1, q2 ,q3]
We get
δ([q0, q1,q2,q3], b) = δ(q0, b) ∪ δ(q1, b) ∪ δ(q2, b) ∪ δ(q3, b)
= q0 ∪ q1 ∪ q3 ∪ q2
= [q0, q1, q2 ,q3]
Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 52
Consider the state [q0, q1, q3],
Seek all the transitions from state [q0, q1, q3] for all input symbols a and b.

We get
δ([q0, q1, q3], a) = δ(q0, a) ∪ δ(q1, a) ∪ δ(q3, a)
= [q0, q1] ∪ q2 ∪ φ
= [q0, q1, q2]
We get
δ([q0, q1, q3], b) = δ(q0, b) ∪ δ(q1, b) ∪ δ(q3, b)
= q0 ∪ q1 ∪ q2
= [q0, q1, q2]

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 53


Now there are no new states generated. Therefore, above is the transition
table corresponding to the new DFA.

In the above DFA, the start state is q0.


The final states of the DFA are [q0, q1, q2],[q0, q1, q2, q3],[q0, q1,q3], since they
contain at least one final state of the NFA (q2 or q3).
The Final DFA is shown in the next slide.
Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 54
Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 55
▪ Let us say,
[q0] as A,
[q0, q1] as B,
[q0, q1, q2] as C,
[q0, q1, q2, q3] as D, and
[q0, q1,q3] as E
Then the transition table will become,

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 56


▪ Transition Diagram:

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 57


Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 58
Problem 1 Problem 3

Problem 2

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 59


The states of the new NFA will be ε-
closure( 𝑞0 ), ε- closure( 𝑞1 ) , ε−
closure(𝑞3 )

Compute ε-closure of all states.

δ෠ (q0, ε) = ε- closure(𝑞0 ) = {𝑞0 } υ {𝑞1 } υ {𝑞2 }


= {𝑞0 , 𝑞1 , 𝑞2 } = A

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 60


δ෠ (q1, ε) = ε- closure(𝑞1 ) = {𝑞1 } υ {𝑞2 } = {𝑞1 , 𝑞2 } = 𝐵

δ෠ (q2, ε) = ε- closure(𝑞2 ) = {𝑞2 } = C

The start state of the new NFA will be [q0, q1, q2].

The final states of the new NFA are {q0, q1, q2}, {q1, q2}, {q2}, since
they contain the final state q2 of the old NFA.

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 61


Next we need to find the transitions of these new states on input
symbols 0 and 1.
Consider state {q0, q1, q2} = A and compute the extended transition
over inputs as the following:

𝜹(A,0) ෡
and 𝜹(A,1)

𝜹(A,0) = ε-closure(δ(ε-closure(A),0))
= ε-closure(δ({q0,q1,q2},0))
= ε-closure(δ(q0,0) U δ(q1,0) U δ(q2,0))
= ε-closure( {q0} U φ U φ )
= ε-closure( q0 )
= [q0,q1,q2] = A

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 62



𝜹(A,1) = ε-closure(δ(ε-closure(A),1))
= ε-closure(δ(q0,1) U δ(q1,1) U δ(q2,1))
= ε-closure( φ U q1 U φ )
= [q1,q2] = B
DFA Table

Now the new state is B ie.,[q1,q2]


Next we need to find the transitions of this new state on input symbols
0 and 1.

𝜹(B,0) ෡
and 𝜹(B,1)

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 63



𝜹(B,0) = ε-closure(δ(ε-closure(B),0))
= ε-closure(δ(q1,0) U δ(q2,0))
= ε-closure(φ U q2)
= ε-closure(q2)
= [q2] = C

𝜹(B,1) = ε-closure(δ(ε-closure(B),1))
= ε-closure(δ(q1,1) U δ(q2,1))
= ε-closure(q1 U φ)
= ε-closure(q1) = [q1,q2] = B DFA Table

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 64


Now the new state is C ie.,[q2]
Next we need to find the transitions of this new state on input symbols
0 and 1.

𝜹(C,0) ෡
and 𝜹(C,1)

𝜹(C,0) = ε-closure(δ(ε-closure(C),0))
= ε-closure(δ(q2,0))
= ε-closure(q2) = [q2] = C

𝜹(C,1) = ε-closure(δ(ε-closure(C),1))
Final DFA Table
= ε-closure(δ(q2,1))
= ε-closure(φ) = φ

Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 65


Figure : DFA diagram
Prepared by M.Gangappa, VNRVJIET, Hyderabad September 16, 2024 66
NFA and DFA

Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 67


▪ The regular expression describes languages accepted by Finite Automata

▪ The regular expressions are used to write a pattern. Regular expressions


(patterns) are useful in search, replace and text extraction operations.

▪ Applications:
▪ In web page design, the user name validation, password validation, date
validation, phone number validation are some of the operations
performed by Regular expressions.
▪ In Compiler construction, source code recognition is one of the major
application of Regular expressions.
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 68
▪ A regular expressions over input alphabet Σ can be defined recursively as
follows:

▪ Rule 1 : any terminal symbol defined in Σ is a regular expression.


▪ Φ is a null set and the Φ is a regular expression.
▪ ε is an empty string , then ε is a regular expression.

▪ Rule 2 : if R1 and R2 are two regular expressions, then the union of R1 and
R2 denoted by R1 + R2 or R1 | R2 is also regular expression.
▪ The language associate with R1 and R2 is L(R1) and L(R2). The union of L(R1)
and L(R2) is L(R1) + L(R2) or L(R1)|L(R2).
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 69
▪ Rule 3: if R1 and R2 are two regular expressions, then the concatenation of
R1 and R2 denoted by R1.R2 or R1R2 is also regular expression.
▪ The language associate with R1 and R2 is L(R1) and L(R2). The concatenation
of L(R1) and L(R2) is L(R1).L(R2) .
▪ Rule 4: if R1 is regular expression, then the iteration (Kleene closure or
positive closure) of R1 denoted by 𝑅1 ∗ is also regular expression.
▪ The language associate with R1 is L(R1). The iteration of L(R1) is L(R1)*.

▪ Rule 5: An expression recursively obtained from rules 1 to 4 is also a


regular expression.
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 70
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 71
If L ={ w ∈ {a, b}* : w = {a,b} } ,Then the RE = (a|b) or a+b

If L ={ w ∈ {a, b}* : |w| = even } Then the RE = …….


Sol: L = { ε, aa, ab, ba, bb, aaaa, abab, … … … }
RE = ((a|b)(a|b))*
= (aa,ab,ba,bb)*
If L ={ w ∈ {a, b}* : |w| is odd } Then the RE = …….
Sol: L = { ε, a, aba, bba, bbb, aaaaa, aabab, … … … }
RE = (a|b)((a|b)(a|b))*
= (a|b)(aa|ab|ba|bb)*

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 72


If L ={ w ∈ {a, b}* : w starting with string aab. }, Then the RE = …….
Sol: L ={ aab, aaba, aabb, aababa, ……
RE = aab(a|b)*
If L ={ w ∈ {a, b}* : w with substring string aab. }, Then the RE = …….
Sol: L ={ aab, baaba, aaabb, bbaababa, ……
RE = (a|b)*aab(a|b)*
If L ={ w ∈ {a, b}* : w ends with aab. }, Then the RE = …….
Sol: L ={ aab, baab, aaab, bbaab, ……
RE = (a|b)*aab

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 73


L ={ w ∈ {0, 1}* : w has both 00 and 11 has substring}, Then the RE =
…….
Sol: L ={ 0011,001011,11000,001010110 … … }
RE = (0|1)*00(1|0)*11(0|1)* | (0|1)*11(1|0)*00(0|1)*
L ={ w ∈ {0, 1}* : w contains atleast two 0’s}, Then the RE = …….
Sol: L ={ 00,001,11000,001010110 … … }
RE = (0|1)*0(1|0)*0(0|1)*
L ={ w ∈ {0, 1}* : w contains exactly two 0’s}, Then the RE = …….
Sol: L ={ 00,001,110011,0011111 … … }
RE = 1*01*01*

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 74


Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 75
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 76
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 77
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 78
Closure properties (or) Operations on Regular
expressions
Closure properties of Regular expressions or regular languages are the
following:
1. Union 2. Concatenation 3. Kleene closure
▪ 1. Union : Let L1 and L2 be the languages accepted by FA M1 and M2 respectively. M1 =
( Q1,Σ,δ1,q1,F1) and M2 = ( Q2,Σ,δ2,q2,F2).
▪ A Finite Automata(FA) M is constructed which accepts L(M1) U L(M2) as follows:
▪ M = ( Q,Σ,δ,q0,F) where q0 is new start state,
▪ Q = Q1 U Q2 U {q0}
▪ F = F1 U F2
▪ δ = δ1 U δ2

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 79


Operations on Regular expressions
▪ Example: If L1 = { 01,001,11} and L2 = { ε , 011} , then the union of L1 and
L2 is ………………..
▪ L1 U L2 = { ε,01,001,11,011}

▪ Example: If L1 = { 01,101} and L2 = { ε , 10} , then the concatenation of L1


and L2 is ………………..
▪ L1.L2 = { 01,101,0110,10110}

▪ Example: If L1 = { 01}, then the Kleene closure of L1 is ………………..


▪ L1* = { ε,01,0101,…}
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 80
Identities of Regular expressions
▪ Φ+R=R+φ=R ▪ R*R* = R*
▪ Φ.R = R.Φ = Φ ▪ ε + RR* = RR* + ε = R*
▪ ε+R=R+ε=R ▪ (PQ)*P = P(QP)*
▪ ε.R = R.ε = R ▪ (P+Q)* = (P*+Q*)* =(P*Q*)*
▪ ε* = ε ▪ (P+Q)R = PR + QR
▪ Φ* = ε ▪ R(P+Q) = RP + RQ
▪ R+R=R
▪ R*R = RR*
▪ (R*)* = R*

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 81


Arden’s theorem
▪ Let P and Q are two regular expressions over Σ and if P does not contain ε
then the equation 𝑹 = 𝑸 + 𝑹𝑷, has a unique solution given by R = QP*.
▪ Proof:
▪ R = Q + (Q+RP)P = Q+QP+RP2
▪ = Q + QP + (Q+RP)P2
▪ = Q + QP + QP2 + RP3
▪ ….. …… ……
▪ …… …… …….

▪ R = Q ( ε + P + P2 + …. )
▪ R = QP*

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 82


a a

b
q0 qq01

Sol: Write the equation for the states as follows:


q0 = q0a + ε
q1 = q0b + q1a
By Arden’s law, the equation q0 can be re written as
q0 = εa* = a*
Substitute q0 in equation q1 as shown below and solve for q1
q1 = q0b + q1a = a*b + q1a = a*ba*, by Arden’s Law.
The final state is q1, Therefore, the solution is a*ba*
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 83
▪ 1. find the regular expression that denotes the set accepted by FA whose
transition diagram is given below
0 1
0,1

1 0
q0 q1 q2
0 1

1
q0 q1
2. Find the regular expression for
0 0 0 1
1
q3 q2

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 84


S. No Regular Expression Finite automata
1 R=ε ε
q0 q1

2 R=φ
q0 q1

3 R=a
a
q0 q1

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 85


S. No Regular Expression Finite automata
4 R= a+b a
q1 q2
ε ε

q0 q5
ε
ε
b
q3 q4

5 R = a.b
a ε b
q0 q1 q2 q3

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 86


6. R = a*
ε

ε ε a ε ε
q0 q1 q3 q4 q5 q6

Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 87


Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 88
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 89
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 90
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 91
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 92
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 93
Prepared by Dr.M.Gangappa, CSE dept,, VNRVJIET, Hyderabad September 16, 2024 94
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 95
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 96
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 97
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 98
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 99
Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad September 16, 2024 100
September 16, 2024 Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad 101
September 16, 2024 Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad 102
September 16, 2024 Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad 103
September 16, 2024 Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad 104
September 16, 2024 Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad 105
September 16, 2024 Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad 106
September 16, 2024 Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad 107
September 16, 2024 Prepared by M.Gangappa,CSE dept, VNRVJIET, Hyderabad 108

You might also like