Download as pdf or txt
Download as pdf or txt
You are on page 1of 422

19CSE214 – Theory of Computation

Introduction to Automata and Languages


by
KAVITHA C.R. & SREEBHA BHASKARAN
Dept. of Computer Science and Engineering
Amrita School of Engineering, Bengaluru
Amrita Vishwa Vidyapeetham
19CSE214 THEORY OF COMUTATION 3003
Unit 1
Finite State machines –Deterministic finite state machine – Non-Deterministic finite state
machine- Equivalence of NFA and DFA –Minimization of Finite State Machine – Regular
Expression -Regular Language – Properties of Regular Languages.

Unit 2
Context Free Grammar -Pushdown Automata – Variants of Pushdown automata –
Equivalence between PDA and CFG- Context Free Languages – Properties of CFL – Normal
Forms.
Unit 3
Context Sensitive Language- Linear Bound Automata- Turing Machine – Variants of Turing
Machine – Decidability- Post correspondence problem – Introduction to undecidable problems

Also known as Automata Theory and Computation or Formal Languages & Automata
12-03-2022 2
Text Books:
1. Linz P. An introduction to formal languages and automata. Sixth edition, Jones and
Bartlett Publishers; 2016.

References:
• Hopcroft JE, Motwani R, Ullman JD. Introduction to Automata Theory, Languages and
Computation. Third Edition, Pearson; 2006.
• Sipser M. Introduction to the Theory of Computation. Third Edition Cengage Publishers;
2005.
• Martin JC. Introduction to Languages and the Theory of Computation. Fourth Edition
McGraw-Hill; 2010.

12-03-2022
3
Course Objectives
● This course provides an overview of the problems that can be solved by various
kinds of abstract machines such as finite state machine, pushdown automata and
Turing machine.
● This course deals with how efficiently problems can be solved on a model of
computation, using an algorithm.

CO Course Outcomes

CO1 Design and develop various finite state machines

CO2 Demonstrate the push down automata model for a given language

CO3 Understand and design the various types of Turing machine

CO4 Analyze the properties of different languages

CO5 Understand the concepts of undecidability

12-03-2022 4
Outline
• Introduction
• Example
• Applications

12-03-2022 5
What is automata theory?
Automata theory is the study of abstract computational devices
Abstract devices are (simplified) models of real computations
Computations happen everywhere: On your laptop, on your
cell phone, in nature, …

Why do we need abstract models?

12-03-2022 6
A simple computer

BATTERY

input: switch
output: light bulb
actions: flip switch
states: on, off

12-03-2022 7
A simple “computer”
f

BATTERY start off on

input: switch
output: light bulb bulb is on if and only if
there was an odd
actions: f for “flip switch” number of flips
states: on, off

12-03-2022 8
Another “computer” 1
1 start off off
1

2 2
2 2
BATTERY
1
2
off on
1

inputs: switches 1 and 2


bulb is on if and only if
actions: 1 for “flip switch 1” both switches were
actions: 2 for “flip switch 2” flipped an odd number
of times
states: on, off

12-03-2022 9
A design problem
1 4

?
5
BATTERY

Can you design a circuit where the light is on if


and only if all the switches were flipped exactly
the same number of times??

12-03-2022 10
A design problem
Such devices are difficult to reason about, because they can
be designed in an infinite number of ways
By representing them as abstract computational devices, or
automata, we will learn how to answer such questions

12-03-2022 11
These devices can model many things
They can describe the operation of any “small
computer”, like the control component of an alarm clock
or a microwave
They are also used in lexical analyzers to recognize well
formed expressions in programming languages:

ab1 is a legal name of a variable in C


5u= is not

12-03-2022 12
Applications of Finite Automata
• Software for designing and checking the behavior of digital
circuits
• Lexical analyzer of a typical compiler
• Software for scanning large bodies of text (e.g., web pages)
for pattern finding
• Software for verifying systems of all types that have a finite
number of states (e.g., stock market transaction,
communication/network protocol)

12-03-2022 13
Application in Compiler
Design

Regular Grammar
Expression
12-03-2022 14
Different kinds of automata
We will look at different devices, and look at the following questions:

What can a given type of device compute, and what are its
limitations?

Is one type of device more powerful than another?

12-03-2022 15
Some devices …
Finite Automata Devices with a finite amount of memory
Used to model “small” computers
Push-Down Devices with infinite memory that can be
Automata accessed in a restricted way.
Used to model parsers
Turing Machines Devices with infinite memory
Used to model any computer
time-bounded Infinite memory, but bounded running time
Turing Machines Used to model any computer program that
runs in a “reasonable” amount of time
12-03-2022 16
Some highlights of the course
Finite Automata(FA)
• Understand what kinds of things a device with finite
memory can do, and what it cannot do
• Introduce simulation: the ability of one device to “imitate”
another device
• Introduce nondeterminism: the ability of a device to make
arbitrary choices
Push-Down Automata(PDA)
These devices are related to grammars, which describe the
structure of programming (and natural) languages
12-03-2022 17
Some highlights of the course
Turing Machines
This is a general model of a computer, capturing anything we
could ever hope to compute
Surprisingly, there are many things that we cannot compute,
for example:

Write a program that, given the code of another


program in C, tells if this program ever outputs
the word “hello”

12-03-2022 18
Some highlights of the course
Time-bounded Turing Machines
Many problems are possible to solve on a computer in principle, but take
too much time in practice
Traveling salesman: Given a list of cities, find the shortest way to visit
them exactly once and come back home

Easy in principle: Try the cities in every possible order


Hard in practice: For 100 cities, this would take more time even on the
fastest computer!
12-03-2022 19
Preliminaries of automata theory
How do we formalize the question
Can device A solve problem B?

First, we need a formal way of describing the problems


that we are interested in solving

12-03-2022 20
Problems
Examples of problems we will consider
Given a word s, does it contain the subword “fool”?
Given a number n, is it divisible by 7?
Given a pair of words s and t, are they the same?
Given an expression with brackets, e.g. (()()), does every left bracket match
with a subsequent right bracket?
Automata as an
1) All of these have “yes/no” answers. Acceptor

2) There are other types of problems, that ask “Find this” or “How many of that”
but we won’t look at those.

Automata as a
Transducer
12-03-2022 21
Finite Automata as an Acceptor

Rule:
Identifiers must
2 start with a letter or
underscr,
followed by any
no. of letters or digits
or underscr

To recognize C identifier
12-03-2022 22
Finite Automata as a Transducer
a b s c
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

A Binary Half adder


12-03-2022 23
Finite Automata : Examples
On/Off switch
action

state

Modeling recognition of the word “then”

Start state Transition Intermediate Final state


state

12-03-2022 24
Alphabets and strings
A common way to talk about words, number, pairs of words, etc. is by
representing them as strings
To define strings, we start with an alphabet

An alphabet is a finite set of symbols.

Examples S1 = {a, b, c, d, …, z}: the set of letters in English


S2 = {0, 1, …, 9}: the set of (base 10) digits
S3 = {a, b, …, z, #}: the set of letters plus the
special symbol #
S4 = {(, )}: the set of open and closed brackets
12-03-2022 25
Strings
A string over alphabet S is a finite sequence
of symbols in S.

The empty string will be denoted by e or λ

Examples
abfbz is a string over S1 = {a, b, c, d, …, z}
9021 is a string over S2 = {0, 1, …, 9}
ab#bc is a string over S3 = {a, b, …, z, #}
))()(() is a string over S4 = {(, )}

12-03-2022 26
Languages
A language is a set of strings over an alphabet.

Languages can be used to describe problems with “yes/no” answers.

For example:
L1 = The set of all strings over S1 that contain the substring “fool”
L2 = The set of all strings over S2 that are divisible by 7
= {7, 14, 21, …}
L3 = The set of all strings of the form s#s where s is any
string over {a, b, …, z}
L4 = The set of all strings over S4 where every ( can be
matched with a subsequent )
12-03-2022 27
Languages & Grammars
• Languages: “A language is a
collection of sentences or strings of
finite length all constructed from a
finite alphabet of symbols”
• Grammars: “A grammar can be
regarded as a device that
enumerates the sentences of a
language” - nothing more, nothing
less

• N. Chomsky, Information and


Control, Vol 2, 1959
12-03-2022 28
The Chomsky Hierarchy
• A containment hierarchy of classes of formal languages
DFA- Deterministic Finite Automata
PDA-Push Down Automata
LBA-Linear Bounded Automata
TM- Turing Machine

Regular
Context- Recursively-
(DFA) Context-
free enumerable
(PDA) sensitive (TM)
(LBA)

12-03-2022 29
This course covers Concepts…
• Languages
• Grammar
• Automata

12-03-2022 30
Sets and Set Operations

12-03-2022 31
Introduction
A set is a collection of objects.
The objects in a set are called elements of the set.
A well – defined set is a set in which we know for sure if an element
belongs to that set.

Example:
The set of all Prime Minister’s of India
The set of all Hindi movies released in 2020
The set of best TV shows of all time - is not well – defined (It is a
matter of opinion)

12-03-2022 32
Notation
When talking about a set we usually denote the set with a capital letter.

Roster notation is the method of describing a set by listing each element


of the set.
Example:
Let C = The set of all days in a week. The Roster notation would be
C={Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

Example:
Let set A = The set of odd numbers greater than zero, and less than 10.
The roster notation of A={1, 3, 5, 7, 9}

12-03-2022 33
More on Notation
•Sometimes we can’t list all the elements of a set.
•For instance, Z = The set of integer numbers. We can’t write out all
the integers, there infinitely many integers. So we adopt a
convention using dots …

•The dots mean continue on in this pattern forever and ever.


•Z = { …-3, -2, -1, 0, 1, 2, 3, …}

•W = {0, 1, 2, 3, …} = This is the set of whole numbers.

12-03-2022 34
Set – Builder Notation
When it is not convenient to list all the elements of a set, we use a
notation that employs the rules in which an element is a member of the
set.
This is called set – builder notation.

V = { people | citizens registered to vote in India}

A = {x | x > 5} This is the set A that has all real numbers greater than 5.

The symbol “|” is read as “such that”.


12-03-2022 35
Special Sets of Numbers
•N = The set of natural numbers.
= {1, 2, 3, …}.
•W = The set of whole numbers.
={0, 1, 2, 3, …}
•Z = The set of integers.
= { …, -3, -2, -1, 0, 1, 2, 3, …}
•Q = The set of rational numbers.
={x| x=p/q, where p and q are elements of Z and q ≠ 0 }
•H = The set of irrational numbers.
•R = The set of real numbers.
•C = The set of complex numbers.
12-03-2022 36
Universal Set, Subsets, Powerset
The Universal Set denoted by U is the set of all possible elements
used in a problem.
When every element of one set is also an element of another set, we
say the first set is a subset.
Example A={1, 2, 3, 4, 5} and B={2, 3}

We say that B is a subset of A. The notation we use is B A.


Let S={1,2,3}, list all the subsets of S.
The subsets of S are  , {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}.
The powerset of S is
{  , {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}
12-03-2022 37
The Empty Set
The empty set is a special set. It contains no elements.
It is usually denoted as { } or  .
The empty set is always considered a subset of any set.
Do not be confused by this question:
Is this set {0} empty?
It is not empty! It contains the element zero.

12-03-2022 38
Intersection of sets
When an element of a set belongs to two or more sets we say the sets
will intersect.
The intersection of a set A and a set B is denoted by A ∩ B.
A ∩ B = {x| x is in A and x is in B}

Note the usage of and. This is similar to conjunction A ^ B.

Example A={1, 3, 5, 7, 9} and B={1, 2, 3, 4, 5}


Then A ∩ B = {1, 3, 5}. Note that 1, 3, 5 are in both A and B.
Mutually Exclusive Sets
We say two sets A and B are mutually exclusive if A ∩ B =  .
12-03-2022
Think of this as two events that can not happen at the same time. 39
Union of sets
The union of two sets A, B is denoted by A U B.
A U B = {x| x is in A or x is in B}

Note the usage of or. This is similar to disjunction A v B.

Using the set A and the set B from the previous slide,
Example A={1, 3, 5, 7, 9} and B={1, 2, 3, 4, 5}
then the union of A, B is A U B = {1, 2, 3, 4, 5, 7, 9}.

The elements of the union are in A or in B or in both. If elements are in


both sets, we do not repeat them.
12-03-2022 40
Complement of a Set
The complement of set A is denoted by A’ or A .
C

A’ = {x| x is not in set A}


=U-A

The complement set operation is analogous to the negation operation


in logic.

Example Say U={1,2,3,4,5}, A={1,2}, then A’ = {3,4,5}.

12-03-2022 41
Cardinal Number
The Cardinal Number of a set is the number of elements in the set and
is denoted by n(A) or |A|

Let A={2,4,6,8,10}, then n(A)=5

The Cardinal Number formula for the union of two sets is


n(A U B)= n(A) + n(B) – n(A∩B)

The Cardinal number formula for the complement of a set is


n(A) + n(A’) = n(U)
12-03-2022 42
Strings and Languages

12-03-2022 43
Languages
A language is a set of strings

String: A sequence of letters

Examples: “cat”, “dog”, “house”, …

Defined over an alphabet:

S  a, b, c,, z
12-03-2022 44
Alphabets and Strings
We will use small alphabets: S  a, b
Strings
a
u  ab
ab
v  bbbaaa
abba
w  abba
baba
12-03-2022
aaabbbaabab 45
String Operations
w  a1a2 an abba
v  b1b2 bm bbbaaa
Concatenation
Concatenation is denoted by dot operator(not compulsory to use dot) Eg. w.v = wv
wv  a1a2 anb1b2 bm abbabbbaaa
Reverse
w  a1a2 an ababaaabbb
w  an  a2a1
R
bbbaaababa
12-03-2022 46
String Length
w  a1a2 an
Length:
w n

Examples:

abba  4
aa  2
a 1
12-03-2022 47
Length of Concatenation
uv  u  v
Example:

u  aab, u  3
v  abaab, v  5

uv  aababaab  8
uv  u  v  3  5  8
12-03-2022 48
Empty String 
A string with no letters:
 0

Observations:
w  w  w

abba  abba  abba


12-03-2022 49
Substring
Substring of string:
a subsequence of consecutive characters

String

abbab Substring ab
abbab abba
abbab b
abbab bbab
12-03-2022 abbab 50
Proper Substring
Substring of string:
a subsequence of consecutive characters excluding the
empty string and the given string

String Proper Substring


abbab ab
abbab abba
abbab b
abbab bbab
12-03-2022 51
Prefix and Suffix
Prefix: A substring with the sequence of beginning symbols of a given string is called a “prefix”.

Suffix: A substring with the sequence of ending symbols of a given string is called a “suffix”.

abbab
w  uv
Prefixes
 Suffixes
abbab
a bbab prefix

ab bab suffix

abb ab
abba b
12-03-2022
abbab  52
Proper Prefix and Proper Suffix
Proper Prefix: A substring with the sequence of beginning symbols of a given string excluding the empty and the whole
string
Proper Suffix: A substring with the sequence of ending symbols of a given string excluding the empty and the whole
string

abbab w  uv
Proper  Proper abbab
Prefixes Suffixes
a bbab prefix

suffix
ab bab
abb ab
abba b
12-03-2022 abbab  53
Examples
Substrings of “computer”
‘λ’, 'c', 'co', 'com', 'comp', 'compu', 'comput', 'compute', 'computer', 'o',
'om', 'omp', 'ompu', 'omput', 'ompute', 'omputer', 'm', 'mp', 'mpu',
'mput', 'mpute', 'mputer', 'p', 'pu', 'put', 'pute', 'puter', 'u', 'ut', 'ute',
'uter', 't', 'te', 'ter', 'e', 'er', ‘r’

Proper Substrings of “computer”


‘λ’, 'c', 'co', 'com', 'comp', 'compu', 'comput', 'compute', 'computer', 'o',
'om', 'omp', 'ompu', 'omput', 'ompute', 'omputer', 'm', 'mp', 'mpu',
'mput', 'mpute', 'mputer', 'p', 'pu', 'put', 'pute', 'puter', 'u', 'ut', 'ute',
'uter', 't', 'te', 'ter', 'e', 'er', ‘r’

12-03-2022 54
Examples
Prefix of the string “computer”
‘λ’, 'c', 'co', 'com', 'comp', 'compu', 'comput', 'compute', 'computer’
Proper Prefix of the string “computer”
‘λ’, 'c', 'co', 'com', 'comp', 'compu', 'comput', 'compute', 'computer’
Suffix of the string “computer”
‘λ’, ‘r', ‘er', ‘ter', 'uter', ‘puter', 'mputer', 'omputer', 'computer’
Proper Suffix of the string “computer”
‘λ’, ‘r', ‘er', ‘ter', 'uter', ‘puter', 'mputer', 'omputer', 'computer’

12-03-2022 55
Another Operation
w  ww
n

 
w
n
Example:
abba   abbaabba
2

Definition:
w 
0

abba   
0

12-03-2022 56
The * Operation (zero or more occurrences)
S * : the set of all possible strings from alphabet S

S  a, b
S*   , a, b, aa, ab, ba, bb, aaa, aab,

12-03-2022 57
The + Operation (one or more occurrences)
 : the set of all possible strings from
S
alphabet S except 

S  a, b
S*   , a, b, aa, ab, ba, bb, aaa, aab,


S  S * 

S  a, b, aa, ab, ba, bb, aaa, aab,
12-03-2022 58
Language
A language is any subset of S*

S  a, b
Example:

S*   , a, b, aa, ab, ba, bb, aaa,


Languages:

a, aa, aab
{ , abba, baba, aa, ab, aaaaaa}
12-03-2022 59
Another Example
L  {a b : n  0}
n n
An infinite language


ab
L abb  L
aabb
aaaaabbbbb
12-03-2022 60
Operations on Languages
The usual set operations
a, ab, aaaa  bb, ab  {a, ab, bb, aaaa}
a, ab, aaaa  bb, ab  {ab}
a, ab, aaaa  bb, ab  a, aaaa
Complement:
L  S * L
a, ba   , b, aa, ab, bb, aaa,
12-03-2022 61
Reverse
Definition: L  {w : w  L}
R R

Examples:
ab, aab, baba  ba, baa, abab
R

L  {a b : n  0}
n n

L  {b a : n  0}
R n n
12-03-2022 62
Concatenation
Concatenation is denoted by dot operator Eg.: L1.L2 or L1L2

Definition:
L1L2  xy : x  L1, y  L2 

Example:

a, ab, bab, aa

 ab, aaa, abb, abaa, bab, baaa


12-03-2022 63
Another Operation
Definition: L 
n

LL L
n
a, b  a, ba, ba, b 
3

aaa, aab, aba, abb, baa, bab, bba, bbb


Special case: L  
0

12-03-2022
a , bba, aaa 0   64
More Examples
L  {a b : n  0}
n n

L  {a b a b : n, m  0}
2 n n m m

2
aabbaaabbb L

12-03-2022 65
Star-Closure (Kleene *)
Definition:
L*  L  L  L 
0 1 2

Example:
 , 
a, bb, 
 
a, bb*   
 aa , abb, bba , bbbb, 
aaa, aabb, abba, abbbb,
12-03-2022 66
Kleene Closure
Say, L, or L1 ={a, abc, ba}, on Σ ={a,b,c}

Then, L2 = {aa, aabc, aba, abca, abcabc, abcba, baa, baabc, baba}

L3= {a, abc, ba}. L2

…..

But, L0 = {ε}

Kleene closure of L, L* = {ε, L1, L2, L3, . . .}

12-03-2022 67
Positive Closure

Definition:
L  L  L 
1 2

 L *  

a, bb, 
  
a, bb  aa, abb, bba, bbbb, 
aaa, aabb, abba, abbbb,
 

12-03-2022 68
Examples
Concatenating a language with itself: L0 = {ε}
Li = LLi-1, for all i >= 1

If L={1, 2, 3} find the length of L2


L2= L.L
L2 = {11, 12, 13, 21, 22, 23, 31, 32, 33}
|L2| = 9

12-03-2022 69
Examples
If L1 = {a, b, c}, L2 = {1, 2}, If L1UL2, L1ՈL2, L1-L2, L1*, L2’, L11.

L1UL2 = {a, b, c, 1, 2} |L1UL2|=5


L1ՈL2 = φ |L1ՈL2|=0
L1-L2 = {a, b, c}
L1* = {λ, a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc, aaa…….}
L2’ = {λ, 11, 12, 21, 22, 111, 112, 121, 122, 211, 212, 221, 222,…..}
L11 = {a, b, c}

12-03-2022 70
Examples

• Let L={ab, baa, aa}. Which of the following are in L*?

a. abaabaaabaa
b. aaaabaaaa
c. baaaaabaaaab
d. baaaaabaa
e. aaabbaaaaaa

71
Examples

12-03-2022 72
L = { w | na(w) = nb(w), w ϵ { a, b}* }

L= {λ, ab, ba, aabb, abab, baba, bbaa, …….}

L| = {a, b, aab, baa, aba, abb, ………..}

L*= {λ,
ab, ba, aabb, ….
abab, …….
}

12-03-2022 73
L = {absba | s ϵ {a, b}+ }
L= {ababa, abbba, ababba, abaaba, abbaba, abbbba, …..}

abaaaab not
babba not
abababbaba accepted
aaba not
abababab not

12-03-2022 74
L = { w | w contains even no. of a’s and odd no. of b’s }

L= {b, aab, aba, baa, aabbb,


aaaab,
aaaabbb,
…..}
Thank You
18-01-2021 Dept. of CSE 1
Introduction
 We need a mechanism to describe languages
mathematically

 A grammar implies an algorithm that would generate


all legal sentences of the language

 Grammar for the English language tells us whether a


particular sentence is well formed or not

18-01-2021 Dept. of CSE 2


Example:
A grammar that generates a subset of the English
language

<sentence>  <noun_phrase> <predicate>

<noun_phrase>  <article> <noun>

<predicate>  <verb>

18-01-2021 Dept. of CSE 3


Examples:
• The boy sleeps <article> <noun> <verb>
• A dog runs

<sentence>  <noun_phrase> <predicate>

 <article> <noun><predicate>
 The <noun> <predicate>
 The boy <predicate>
 The boy sleep

Derivation
18-01-2021 Dept. of CSE 4
 Language of the Grammar:

L = {“ a boy runs”,
“the boy sleeps”,
“a dog jumps”,
“the dog runs”,

}

18-01-2021 Dept. of CSE 5


Notations
<verb>  runs

Variables Production Terminals


or Rule or
Non-terminals Symbols

18-01-2021 Dept. of CSE 6


Grammar – Definition
A grammar G is defined as a quadruple
G=(V, T, S, P)

where,
V = finite set of objects called variable or non-terminals
T = finite set of objects called terminals
S = start symbol (S ϵ V)
P = finite set of productions (rules) used in grammar

18-01-2021 Dept. of CSE 7


Language
The language accepted by grammar
G = {V, T, S, P}
is defined as
L(G) = { w ϵ T* ; S  * w }

18-01-2021 Dept. of CSE 8


Example:
G={ {S} , {a,b} , S , {SaSb, Sλ} }

SaSb
SaSb | λ
Sλ

It generates:
S λ SaSb SaSb SaSb
ab aaSbb aaSbb
aabb aaaSbbb
aaabbb

L = {λ, ab, aabb, aaabbb, aaaabbbb …. }


L = { anbn ; n>0 }
18-01-2021 Dept. of CSE 9
Example:
 Find the grammar for L={anbn+1 ; n > 0}

L = { b, abb, aabbb, ….. }

SaSb SaSb|A
SaSb|b
Sb Ab

18-01-2021 Dept. of CSE 10


Example:
 Find the grammar for
L = {anb2n+1 ; n > 0}

Sol: ???

L = { b, abbb, aabbbbb, ……. }

S  aSbb
OR S  aSbb | b
Sb

18-01-2021 Dept. of CSE 11


Example:
 Find the grammar with Σ = {a,b} that generates all
strings with exactly one a.
Sol:

L = { a, ab, abb, ba, bba, bab, bbab, …… }

S S a S |b Is this correct???? NO

S X a X
X  bX | λ

18-01-2021 Dept. of CSE 12


Example:
 Find the grammar with Σ = {a,b} that generates all
strings with no more than three a’s.
Sol:

L = { λ, a, b, ba, bba, bab, bbab, …, aaa, bababa …… }

S  B AB A B A B
A a |λ
B bB|λ

19-01-2021 Dept. of CSE 13


Example:
 Find the grammar with Σ = {a,b} that generates all
strings with atleast three a’s.
Sol:

L = { aaa, aaab, abaa,aabab, ababab, …, aabbababa…… }

S  X a Xa X a X
X  bX | aX | λ

18-01-2021 Dept. of CSE 14


Example:
 Find the grammar for
L = {anbm ; n > 0 , m > n}

Sol:

L = { b, bb, abb, abbb, aabbbbb, ……. }

S  AB
A  aAB | λ OR S  aSb | bA
B  bB | b A bA | λ

18-01-2021 Dept. of CSE 15


Example:
 Find the grammar for Σ = {a},
L = {w : |w| mod 3 = 0}

Sol:
L = { λ, aaa, aaaaaa, aaaaaaaaa, ……. }

S  aaaS | λ

18-01-2021 Dept. of CSE 16


Example:
 Find the grammar for Σ = {a},
L = {w : |w| mod 3 > 0}
Workspace:
that is |w| mod 3 =1
|w| mod 3 =2
L = { a , aa, aaaa, aaaaa ……. }

18-01-2021 Dept. of CSE 17


Example:
 Find the grammar for Σ = {a},
L = {w : |w| mod 3 > 0}

Sol:
that is |w| mod 3 =1
|w| mod 3 =2

L = { a , aa, aaaa, aaaaa ……. }

S  aaaS | aa | a

18-01-2021 Dept. of CSE 18


Example
 Are given two grammars equivalent?

SaSb | ab | λ & S aAb | ab


AaAb | λ
Workspace:
Lets try deriving …..
S λ S ab Therefore, except in λ it is
Sab equivalent.
S aAb
aaAbb OR
SaSb
aabb Not equivalent.
aaSbb
aabb
18-01-2021 Dept. of CSE 19
Example
 Find the language:
SaA
AbS
S λ
Sol:
L = {λ, ab, abab, ababab, ………. }

L = { (ab)m, m > 0 }

18-01-2021 Dept. of CSE 20


Example
 Find the language:
SAa
AB
B Aa
Sol: Recursive – not terminating
Therefore, no language

18-01-2021 Dept. of CSE 21


Example
 Find the grammar for L={wwR : w ϵ {a,b}+}
Sol:

f s t …….. t s f

S a S a | b S b | aa | bb

18-01-2021 Dept. of CSE 22


Example
 Find the grammar for L={w : na(w) = nb(w)}
Sol:

S SS | aSb | bSa | λ

18-01-2021 Dept. of CSE 23


Derivation
If w ϵ L(G), then the sequence
Sw1  w2  . . . wn w
is a derivation of the sentence w.

The strings S, w1, w2, …. wn which contains variables as


well as terminals are called sentential forms of the
derivation.

18-01-2021 Dept. of CSE 24


Example -Derivation
 Derive the string “aabb” from the grammar
SaSb | λ
Sol: • Only one substitution at a time
• Left most non-terminal will be expanded first

S  aSb
 aaSbb ;S->aSb Intermediate steps  Sentential forms
 aa λbb ;S-> λ
aabb Sentence

*
S => aabb Derived in 4 steps

18-01-2021 Dept. of CSE 25


18-01-2021 Dept. of CSE 26
25/1/2020 Dept. of CSE 1
Introduction
 A regular expression, often called a pattern, specifies
a set of strings required for a particular purpose.
 Representing regular language by notation
 Notations:
Σ, parenthesis, operators
 Operators:
+  union
 .  concatenation
 * /+  closure (star closure or positive closure)

25/1/2020 Dept. of CSE 2


Definition
 A Regular Expression can be recursively defined as
follows −
Let Σ be a given alphabet. Then
1. ∅, λ, and a ∈ Σ are all regular expressions. These are
called primitive regular expressions.
2. If r1 and r2 are regular expressions, so are r1 + r2,
r1 · r2, r1∗, and (r1).
3. A string is a regular expression if and only if it can be
derived from the primitive regular expressions by a
finite number of applications of the rules in (2).

25/1/2020 Dept. of CSE 3


Languages Associated with Regular
Expressions
 The language L(r) denoted by any regular expression r is
defined by the following rules.
1. ∅ is a regular expression denoting the empty set,
2. λ is a regular expression denoting {λ},
3. For every a ∈ Σ, a is a regular expression denoting {a}.
If r1 and r2 are regular expressions, then
4. L(r1 + r2) = L(r1) ∪ L(r2)
5. L(r1 · r2) = L(r1)L(r2)
6. L((r1)) = L(r1)
7. L(r1*)=(L(r1))*

25/1/2020 Dept. of CSE 4


Languages Associated with Regular
Expressions
∅ = { } RE denoting an empty language
λ /∈ = {∈} language containing an empty string
L(a) = {a}
L(a*) = {a}*= {∈, a, aa, aaa, …….. }
L(a+) = { a, aa, aaa, ………..}
L(a+b) = L(a) + L(b) = {a} + {b} = {a, b}
L(a + bc) = L(a) + L(bc) = {a} +L(b)L(c) = {a}+{bc} = {a,bc}
L((a + b)*) = {λ, a, b, aa, ab, ba, bb, aaa, aab,……….}

25/1/2020 Dept. of CSE 5


Examples
 Generate the regular expression for set of strings of a’s and
b’s of any length including null string.
(a + b)*

 Excluding null string.

(a + b)+

25/1/2020 Dept. of CSE 6


Examples
 Set of strings of a’s and b’s ending with abb

(a+b)*abb
 set of strings of a’s and b’s starting with ba

ba(a+b)*

25/1/2020 Dept. of CSE 7


Examples
 Strings of a’s and b’s having substring aa
(a+b)*aa(a+b)*
(OR) (a + b)*(aa)+(a+b)*
 Any number of a’s, followed by any number of b’s, followed by c’s
a*b*c*
 Atleast one a, followed by atleast one b and one c
a+b+c+ (OR) aa*bb*cc*
 Strings of a’s and b’s ending with either a or bb
(a+b)*(a+bb)

25/1/2020 Dept. of CSE 8


Examples
 Strings consisting of even number of a’s followed by odd number of
b’s
(aa)*(bb)*b
 Language with string 1 or 0 followed by any number of 1’s
(1 + 0)1*
 Strings of 0’s and 1’s without any consecutive ones
(10+0)* (1+ λ)
 All strings with even number of 0’s for Σ ={0,1}
(1*01*01*)* + 1*

25/1/2020 Dept. of CSE 9


Examples
 Strings of a’s and b’s of length 2

ab+ba+aa+bb
 Set of integers

(+ + - + λ)(0+ 1+ …. + 9)+ (OR) (+ + - + λ)(0-9)+

 Real numbers

(+ + - + λ)(0-9)*.(0-9)+ (OR) (+ + - + λ)(0-9)*.?(0-9)+

 Strings of a’s and b’s with alternate a and b.

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

25/1/2020 Dept. of CSE 10


Examples
 L = {w | na(w)mod3=0}, given Σ ={a,b}

(b*ab*ab*ab*)* + b*
 All strings containing no more than three a’s, given Σ ={a,b,c}

(b+c)*+

[(b+c)*a(b+c)*] +
[(b+c)*a(b+c)*a(b+c)*] +
[(b+c)*a(b+c)*a(b+c)*a(b+c)*]

25/1/2020 Dept. of CSE 11


Examples
 L={anbm, n >1, m > 1, nm > 3}

(aaa*bbb*)+(aa*bbbb*)+(aaaa*bb*)

 L={abnw; n > 3, w ∈ {a,b}+ }

abbbb*(a+b)+
 L={anbm, n < 4, m < 3}

(λ + a + aa + aaa)(λ + b + bb + bbb)
 L={anbm, n > 4, m < 3}

(aaaa+) (λ + b + bb + bbb)

25/1/2020 Dept. of CSE 12


Examples
 L={anbm, n + m is even}
(λ + ((aa)*(bb)*) + (a(aa)*b(bb)*))
 If r = (aa)*(bb)*b, find L.
L = {a2nb2m+1 ; m,n > 0} (OR) L= {anbm, n=even, m is odd}
 L = {w | na(w)mod3 ≠ 0} , given Σ ={a,b}
(b*(ab*ab*ab*)*ab*) + (b*(ab*ab*ab*)*ab*ab*)
 All strings with atleast one occurrence of each symbol on Σ ={a,b,c}
((a+b+c)*a(a+b+c)*b(a+b+c)*c(a+b+c)*) + ((a+b+c)*a(a+b+c)*c(a+b+c)*b(a+b+c)*) +
((a+b+c)*b(a+b+c)*a(a+b+c)*c(a+b+c)*) + ((a+b+c)*b(a+b+c)*c(a+b+c)*a(a+b+c)*) +
((a+b+c)*c(a+b+c)*a(a+b+c)*b(a+b+c)*) + ((a+b+c)*c(a+b+c)*b(a+b+c)*a(a+b+c)*)

25/1/2020 Dept. of CSE 13


Examples
 Find the regular expression for strings of 0’s and 1’s with minimum
run length of 0’s as atleast 3.
0 + (1+01+0000*)*(0+ λ)+(1+10+0000*)*
 Strings of 0’s and 1’s with atmost one pair of consecutive 0’s

(1+01)*(λ +0+ 00)(10+1)*


 All strings not ending with 01

(λ+1+0+(0+1)*(00+10+11)) [OR]
(λ+1+0+(0+1)*(0+11)) [OR]
(01+10+0+1)*(10+0)*

25/1/2020 Dept. of CSE 14


Practice
 Strings of a’s and b’s of even length
 Strings of a’s and b’s of odd length
 L={w ; |w|mod3=0}, given Σ ={a,b}
 All strings with exactly one a, given Σ ={a,b,c}

25/1/2020 Dept. of CSE 15


Finite Automata
Automata
• An automaton is an abstract model of a digital computer.
• It has a mechanism for reading input from the input file.
• It can produce output of some form.
• It may have a temporary storage device, consisting of an unlimited number of cells, each
capable of holding a single symbol from an alphabet.
• The automaton can read and change the contents of the storage cells.
• 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.
• An automaton whose output response is limited to a simple “yes” or “no” is called an
accepter.
• Presented with an input string, an accepter either accepts the string or rejects it.
• A more general automaton, capable of producing strings of symbols as output, is called a
transducer.
Automata
Finite Automata

Deterministic Non-
Finite Acceptor Deterministic
Finite Acceptor
Deterministic Finite Acceptor
DFA Example
String Acceptance or Rejection
• Check if 1001 belongs to the language generated by the automata

To solve this, remember the extended transition function ẟ*

(i.e.)
String Acceptance or Rejection

Check if 1001 belongs to the language generated by the automata

ẟ*(q0,1001)
=ẟ(ẟ*(q0, 100),1)
= ẟ(ẟ(ẟ*(q0,10), 0), 1)
= ẟ(ẟ(ẟ(ẟ*(q0,1), 0), 0), 1)
= ẟ(ẟ(ẟ(ẟ(ẟ*(q0,λ), 1), 0), 0), 1)
= ẟ(ẟ(ẟ(ẟ(q0, 1), 0), 0), 1)
= ẟ(ẟ(ẟ(q1, 0), 0), 1)
= ẟ(ẟ(q0, 0), 1)
= ẟ(q0, 1)
= q1 ∈ F q1 is a final state. Hence the string is accepted.
String Acceptance or Rejection

Check if 1011 belongs to the language generated by the automata


ẟ*(q0,1011)
=ẟ(ẟ*(q0, 101),1)
= ẟ(ẟ(ẟ*(q0,10), 1), 1)
= ẟ(ẟ(ẟ(ẟ*(q0,1), 0), 1), 1)
= ẟ(ẟ(ẟ(ẟ(ẟ*(q0,λ), 1), 0), 1), 1)
= ẟ(ẟ(ẟ(ẟ(q0, 1), 0), 1), 1)
= ẟ(ẟ(ẟ(q1, 0), 1), 1)
= ẟ(ẟ(q0, 1), 1)
= ẟ(q1, 1)
= q2 ∉ F q2 is a non-final state. Hence the string is rejected.
Language accepted by DFA
• The language accepted by a dfa M = (Q,Σ, δ, q0, F) is the set
of all strings on Σ accepted by M. In formal notation,

• The language rejected by a dfa M = (Q,Σ, δ, q0, F) is the set of all


strings on Σ rejected by M.
Representing DFA – Example 1
• Find a deterministic finite accepter that recognizes the set of all
strings on Σ = {a, b} starting with the prefix ab.
Language accepted {ab, aba, abb, abaa, ababab, …..}

a
b
a b
q0 q1 q2

b a
qt
a
b
Transition Diagram and Transition Table
a
b
a b
q0 q1 q2

b a
qt
States a b
a
b -> q0 q1 qt
M=({q0, q1, q2, qt}, {a, b}, ẟ, q0, {q2})
where ẟ is given as q1 qt q2
ẟ(q0,a)=q1 ẟ(q0,b)=qt
ẟ(q1,a)=qt ẟ(q1,b)=q2 * q2 q2 q2
ẟ(q2,a)=q2 ẟ(q2,b)=q2
ẟ(qt,a)=qt ẟ(qt,b)=qt qt qt qt
Example 2
• Design a DFA that starts with any number of a’s followed by the string
ba and ending with one or more a’s or b’s.
Language accepted {baa, abab, ababbb, abaaaa,
ababab, …..} a, b
a
b a a, b States a b
q0 q1 q2 q3
-> q0 q0 q1
b q1 q2 qt
qt q2 q3 q3
a, b
* q3 q3 q3

qt qt qt
Example 3
• Draw the DFA to accept all strings on ∑ = {a, b} that ends with abb.
Language accepted {abb, ababb, abbabb, abbaabb,
abababb, …..}
b
a

a b b
q0 q1 q2 q3

a
a

b
Regular Language
• A language L is called regular if and only if there exists some
deterministic finite accepter M such that
L = L(M)
(i.e.) if there exists a valid DFA
Example 4
• Show that 𝐿 = 𝑎𝑤𝑎 𝑤 ∈ 𝑎, 𝑏 ∗ } is regular.
Language accepted { aa, aabaa, abbba, aaaaa,
ababababaa …..}
b a
a a
q0 q1 q2

b
b
qt

a, b
Example 5 Example 6
• All the strings on ∑ = {a, b} with • All the strings on ∑ = {a, b} with
exactly one a. atleast one a.
b b a, b
b a, b

a a
q0 q1 qt a
q0 q1
Example 7 Example 8
• All the strings on ∑ = {a, b} • All the strings on ∑ = {a, b} with
with no more than three a’s. atleast three a’s.
b b b
b b b

a a
a a q0 q1 q2
q0 q1 q2
a
a
a, b q3
b q3

a, b qt
Example 9 & 10
• DFA for 𝐿 = 𝑤 𝑤 ∈ 𝑎, 𝑏 and 𝑤 𝑚𝑜𝑑 5 ≠ 0}
a, b

a, b a, b a, b a, b
q0 q1 q2 q3 q4

• DFA for 𝐿 = 𝑤 𝑤 ∈ 𝑎, 𝑏 and 𝑛𝑎 𝑤 𝑚𝑜𝑑 3 > 1}


b b b

a a
q0 q1 q2

a
Example 11 & 12
• Show that 𝐿 = 𝑎𝑛 𝑛 ≥ 4 is regular on ∑={a}.
a

a a a a
q0 q1 q2 q3 q4

• Show that 𝐿 = 𝑎𝑛 𝑛 ≥ 0 and 𝑛 ≠ 4 is regular on ∑={a}.


a

a a a a a
q0 q1 q2 q3 q4 q5
Example 13
• DFA with condition left most symbol differs from right most symbol
on ∑={0, 1}
0 1
1
q1 q2
0
0
q0 1 0

1 0
q3 q4

1
Example 14
• Find a dfa that accepts all the strings on {0, 1}, except those containing
the substring 001.
1 0, 1

0 0 1
q0 q1 q2 q3

1 0
Runs – Example 15
• A run in a string is a substring of length atleast 2, as long as possible
and consisting entirely of same symbol.
• Example: Design a DFA for L={w | every run of a’s has length either 2
or 3} on ∑={a, b}

a, b
b

a a a a
q0 q1 q2 q3 qt
b
b
b
Example 16
• L={w | w has no runs of length less than 4} on ∑={a, b}
Modulo Problems – Example 1
• L={w | na(w)mod 2=0 and nb(w) mod 2=0} (i.e.) na(w) and nb(w) are
both even.
Language accepted {λ, abab, ababbb, abbabb, ababaa, abababba,
…..}

a
00 a 10

b b b b
a

01 11
a
Example 2
• L={w | na(w) mod 3 >= nb(w) mod 2}
a a
00 10 20

b b b b b b

a a
01 11 21

a
Division by K problems
• Draw a DFA to accept the binary strings divisible by 5.
∑={0, 1}
ẟ(qi, d) = qj
j=((r*i)+d) mod k
where r is the radix (ex: decimal, binary..}
i is the current state from the set of states
d is the set of digits
k is the division factor
Example 1
• Draw a DFA to accept the binary strings divisible by 5.
∑={0, 1} i d j ẟ
d={0, 1} 0 0 0 ẟ(q0, 0) = q0
1 1 ẟ(q0, 1) = q1
k=5
1 0 2 ẟ(q1, 0) = q2
i = 0 to k-1 1 3 ẟ(q1, 1) = q3
r=2 2 0 4 ẟ(q2, 0) = q4
j=((r*i)+d) mod k 1 0 ẟ(q2, 1) = q0
3 0 1 ẟ(q3, 0) = q1
1 2 ẟ(q3, 1) = q2
4 0 3 ẟ(q4, 0) = q3
1 4 ẟ(q4, 1) = q4

Example 1 ẟ(q0, 0) = q0
ẟ(q0, 1) = q1
• Draw a DFA to accept the binary strings divisible by 5. ẟ(q1, 0) = q2
1 ẟ(q1, 1) = q3
0
ẟ(q2, 0) = q4
0
1 q1
q2
ẟ(q2, 1) = q0
q0
ẟ(q3, 0) = q1
0 1 1 0 ẟ(q3, 1) = q2
ẟ(q4, 0) = q3
1
ẟ(q4, 1) = q4
q3 0 q4
Example 2
• Obtain a DFA that accepts set of all strings when interpreted in
reverse as a binary string is divisible by 5
∑={0, 1} 1

d={0, 1} 0
0
k=5 1 q 1 q2
q0
i = 0 to k-1
1 0 1 0
r=2
1
j=((r*i)+d) mod k
q3 0 q4
Example 3
• DFA to accept the decimal strings divisible by 3.
Example strings: 393, 915, 7254 are accepted
Example strings: 593, 785, 9346 are rejected
d={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} It could be noted that, digits,
0, 3, 6, 9 mod 3 is 0
k=3
1, 4, 7 mod 3 is 1
i = 0 to k-1 2, 5, 8 mod 3 is 2
r=10 Hence digits can be grouped as
D1={0, 3, 6, 9}
j=((r*i)+d) mod k D ={1, 4, 7}
2
D3={2, 5, 8}
Example 3
• Draw a DFA to accept the binary strings divisible by 3.
j=((r*i)+d) mod k
i d j ẟ
r=10
D1 0 ẟ(q0, D1) = q0
k=3 0 D2 1 ẟ(q0, D2) = q1
• D1={0, 3, 6, 9} D3 2 ẟ(q0, D3) = q2
• D2={1, 4, 7} D1 1 ẟ(q1, D1) = q1
1 D2 2 ẟ(q1, D2) = q2
• D3={2, 5, 8} D3 0 ẟ(q1, D3) = q0
D1 2 ẟ(q2, D1) = q2
2 D2 0 ẟ(q2, D2) = q0
D3 1 ẟ(q2, D3) = q1
Example 3

• Draw a DFA to accept the binary strings divisible by 3. ẟ(q0, D1) = q0
ẟ(q0, D2) = q1
D1 ẟ(q0, D3) = q2
D1
D2 ẟ(q1, D1) = q1
q1 ẟ(q1, D2) = q2
q0 D1={0, 3, 6, 9} ẟ(q1, D3) = q0
D3
D2={1, 4, 7}
D2 D3
D3={2, 5, 8} ẟ(q2, D1) = q2
D3 ẟ(q2, D2) = q0
D2
ẟ(q2, D3) = q1
q2

D1
Example strings: 393, 915, 7254 are accepted
Example strings: 593, 785, 9346 are rejected
Exercises
1. L={aw1aaw2a : w1, w2 ∈ {a, b}*}
2. L={w1abbw2 : w1, w2 ∈ {a, b}*}
3. L={ban : n ≥ 1, n ≠ 4}
4. L={w | na(w) mod 3 = 0 and nb(w) mod 2 = 0}
5. L={w | na(w) mod 4 < nb(w) mod 6}
6. L={w | na(w) mod 2 <= nb(w) mod 4}
7. L = {w : there are exactly two runs of a’s of length 3} on {a, b}}
8. All strings on {0, 1} where the leftmost two symbols and the rightmost
two symbols are identical
9. All strings with atleast one b and exactly two a’s on {a, b}
10. All strings that contain substring 000, but not 0000 on {0,1}
19CSE214 – Theory of Computation

Non Deterministic
Finite Automata(NFA)
by
KAVITHA C.R. & SREEBHA BHASKARAN
Dept. of Computer Science and Engineering

Amrita School of Engineering, Bengaluru


Amrita Vishwa Vidyapeetham
Introduction
• Non determinism is difficult concept
w • Computers are completely deterministic
A B
•ie. from a state will reach a unique state for an input
DFA

Why Studying NFA???


λ
w B
• An exhaustive search with backtracking
A 1 •Eg: playing chess
w
•When several alternatives are possible, we choose one
B2
and follow it until it becomes clear whether or not it was
.
w .
best.
. •If not, we retreat to the last decision point and explore
the other choices
Bn
• NFA machines can serve as models of search-and-
backtrack algorithms
φ • NFA is helpful in solving problems easily
NFA
12-03-2022 2
Definition
• A Nondeterministic Finite Accepter or NFA is defined
by the quintuple
M = (Q, Σ, δ, q0, F),
where
Q is a finite set of internal states
Σ is a finite set of symbols called input alphabet
q0 ϵ Q is the initial state
F C Q is a set of final states
δ : Q × (Σ ∪ {λ}) → 2Q [Transition function]

12-03-2022 3
Example 1
• Construct an NFA for language that ends with “a” for ∑ = {a,b}.
Sol:
L = {a, aa, ba, aba, baa, …..}
a,b a a b
X X String can be X X X
accepted if atleast
a Y Y φ
X Y one state is Final
String is rejected
Transition Function
Q*Σ 2Q (Power set of Q)
δ: Q * Σ  2Q DFA:
(X , a) φ δ:Q×Σ →Q
Q={X,Y}
(X , b) {X}
Σ ={a,b} Every DFA is NFA
(Y , a) {Y}
(Y , b) {X,Y}

12-03-2022 4
Language accepted by NFA
• The language L accepted by an nfa M = (Q, Σ, δ, q0, F) is
defined as the set of all strings accepted.
Formally,

L(M) = {w ∈ Σ∗ : δ∗ (q0, w) ∩ F ≠ ∅}

In words, the language consists of all strings w for which


there is a walk labelled w from the initial vertex of the
transition graph to some final vertex.

12-03-2022 5
Example 2
0 0,1
Ʃ= {0, 1}

q0
1 q1
0,1 q2

• NFA allows a transformation to a new state without consuming any input symbols.
• Transformations to new states without consuming an input symbol are
called lambda transitions or epsilon transitions.
Language: Transition Table: 1010
L = {λ, 10, 1010,
0 1
….}
q0 Φ q1
L = {(10)n : n > 0}
q1 {q0,q2} q2
q2 q2 q2

12-03-2022 6
Example 3
• Draw an NFA with 3 states that accepts the language
{ab, abc}*

q0
a q1
b q2

c,λ

OR
b

q0
a q1
b q2

c,λ

12-03-2022 7
Example 4
• Draw an NFA with no more than 5 states for
L = {ababn : n>0} U {aban : n>0}

q0
a q1
b q2
a q4
λ
q5 a

12-03-2022 8
Example 5
• Draw an NFA with 3 states for
L = {an : n>1} U { bmak : m>0, k>0}
a

q0
a q1

λ a

q2

12-03-2022 9
Example 6
• Draw an NFA for
L = {a3} U {a2n : n>1}

q1
a q2
a q3
a
q0
a
a
q4 q5

12-03-2022 10
Example 7
• Draw an NFA with 4 states
L = {an : n > 0} U {bna : n > 1}

λ q1 a

q0

b
b

q2
a q3

12-03-2022 11
Example 8
• Given L = {a} U {bn : n > 1}, find NFA without λ
transition and with a single final state that accepts
this language.

a, b q1

q0
b
b

q2

12-03-2022 12
Example 9
• Draw an NFA that accepts a* and when one λ edge is
removed the resulting automation accepts only a.

a q1

q0

λ
q2 a

12-03-2022 13
Assignment
1. Given ={a,b}, draw the NFA’s for the following
1. L = {set of all strings starts with a}
2. L = {set of all strings that contains a}
3. L = {set of all strings that ends with a}
4. L = {set of all strings starts with ab}
5. L = {set of all strings that contains ab}
6. L = {set of all strings that ends with ab}
2. Which of the strings 00, 01001, 10010, 000, 0000
are accepted by the following NFA?

12-03-2022 14
12-03-2022 15
19CSE214 – Theory of Computation

Regular Expressions to
Non Deterministic
Finite Automata(NFA)
by
KAVITHA C.R. & SREEBHA BHASKARAN
Dept. of Computer Science and Engineering

Amrita School of Engineering, Bengaluru


Amrita Vishwa Vidyapeetham

12-03-2022 1
Thompson’s representation
• To represent input a

• To represent input ab (or a.b)

a b

a b
12-03-2022 2
contd…
• To represent a+b

12-03-2022 3
contd…
• To represent a*

12-03-2022 4
Example 1
(a+b)*abb

a
a b b

12-03-2022 5
Example 2
(a*+ b*)* a

12-03-2022 6
Example 3
(a*b*)*

12-03-2022 7
Precedence of Operators

• In regular expressions, the ' * ', ' + ', and ' ? ' operators, as well
as the braces ' { ' and ' } ', have the highest precedence,
followed by concatenation, and finally by ‘|’ or ‘+’. As in
arithmetic, parenthesis can change how operators are
grouped.

12-03-2022 8
Example 4
(a+b+c)*

12-03-2022 9
Example 5
(a+bc)(x+λ)

12-03-2022 10
Example 6
ab+bc*

12-03-2022 11
Exercises
1. ab*bc+a
2. ab(b+c)*ab*
3. ab+bc(a+b*)
4. a*bca*
5. abc+d(a+b)

12-03-2022 12
19CSE214 – Theory of Computation

Non Deterministic Finite Automata(NFA)


to Deterministic Finite Automata (DFA)
and Minimized DFA
by
KAVITHA C.R. & SREEBHA BHASKARAN
Dept. of Computer Science and Engineering

Amrita School of Engineering, Bengaluru


Amrita Vishwa Vidyapeetham

12-03-2022 1
RE to DFA
• To convert RE to NFA use – Thompson’s algorithm
• To convert NFA to DFA use – Subset Construction algorithm
• To minimize the obtained DFA use – Tabulation method (also
called Mark/Reduce procedure)

12-03-2022 2
Subset construction algorithm
Step 1: Find the λ-closure of all the states
Step 2: Let the λ-closure of the initial state be named as A
Step 3: Now find the transitions of each input symbol on this state A.
Then, find the λ-closure of the above set and name it as A if it appears to
be same otherwise give a new name as B.
Step 4: Repeat Step 3 for each of the new states added until there are no
new states in the queue.
Step 5: Convert the above representation to a DFA. The initial state of the
DFA will be A. Mark the final states of DFA.

λ-closure(state q) is the set of states that are reachable through only λ-transitions from state q.
Note: λ-closure(state q) contains itself that is q
12-03-2022 3
Example 1
• Convert the RE (a+b)*abb to a minimized DFA
NFA M1 = ({0,1,2,3,4,5,6,7,8,9,10},{a, b}, ᵟ , 0, {10})

λ-closure(0) = {0, 1, 2, 4, 7} λ-closure(4) = {4} λ-closure(8) = {8}


λ-closure(1) = {1, 2, 4} λ-closure(5) = {1, 2, 4, 5, 6, 7} λ-closure(9) = {9}
λ-closure(2) = {2} λ-closure(6) = {1, 2, 4, 6, 7} λ-closure(10) = {10}
λ-closure(3) = {1, 2, 3, 4, 6, 7} λ-closure(7) = {7}
12-03-2022 4
Example 1
λ-closure(0) = {0, 1, 2, 4, 7} – A NFA M1
λ-closure(ẟ(A, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(A, b)) = λ-closure(5) = {1, 2, 4, 5, 6, 7} – C
λ-closure(ẟ(B, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(B, b)) = λ-closure(5, 9) = {1, 2, 4, 5, 6, 7, 9} – D
λ-closure(ẟ(C, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(C, b)) = λ-closure(5) = {1, 2, 4, 5, 6, 7} – C
λ-closure(ẟ(D, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(D, b)) = λ-closure(5, 10) = {1, 2, 4, 5, 6, 7, 10} – E
λ-closure(ẟ(E, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(E, b)) = λ-closure(5) = {1, 2, 4, 5, 6, 7} – C 5
12-03-2022
Example 1 – Resultant DFA
DFA M2 = ({A,B,C,D,E}, {a, b}, ᵟ , A, {E})
States a b
-> A B C a
B B D
C B C b
D B E a B D
*E B C
a
b
A a a
DFA transition table
b C E
b

b
DFA M2

12-03-2022 L(M1) = L(M2) Subset Construction Algorithm coverts NFA to its equivalent DFA 6
DFA Minimization – Tabulation Method
States a b
-> A B C Mark/Reduce Procedure
B B D
C B C B
D B E
*E B C C
ẟ(A, a) = B ẟ(A, b) = C
ẟ(D, a) = B ẟ(D, b) = E D
As (C E) is already marked (A D) is also marked
Similarly, (B D) and (C D) are marked E
Because (C D) is marked (B C) and (A B) are marked

Pair – (AC) A B C D
12-03-2022
7
Minimized DFA DFA M3 = ({A,B,D,E},{a, b}, ᵟ , A, {E} )

States a b States a b
-> A B C -> A B A
B B D B B D
a
C B C D B E
D B E *E B A b b
a B D
*E B C
a
A b
a
b E

Minimized DFA M3
12-03-2022 L(M1) = L(M2) = L(M3) 8
Mark/Reduce Procedure

12-03-2022 9
Example 2
• Find the minimal DFA for abb(a+b)*

12-03-2022 10
Example 2

λ-closure(0) = {0} λ-closure(1) = {1} λ-closure(2) = {2}


λ-closure(3) = {3, 4, 5, 7, 10} λ-closure(4) = {4, 5, 7} λ-closure(5) = {5}
λ-closure(6) = {4, 5, 6, 7, 9, 10} λ-closure(7) = {7} λ-closure(8) = {4, 5, 7, 8, 9, 10}
λ-closure(9) = {4, 5, 7, 9, 10} λ-closure(10) = {10} 11
12-03-2022
Example 2 – Subset Alg.

• λ-closure(0) = {0} - A
• λ-closure(ẟ(A, a)) = λ-closure(1) = {1} – B
• λ-closure(ẟ(A, b)) = φ
• λ-closure(ẟ(B, a)) = φ
• λ-closure(ẟ(B, b)) = λ-closure(2) = {2} – C
• λ-closure(ẟ(C, a)) = φ
• λ-closure(ẟ(C, b)) = λ-closure(3) = {3, 4, 5, 7, 10} – D

12-03-2022 12
Example 2

• λ-closure(ẟ(D, a)) = λ-closure(6) = {4, 5, 6, 7, 9, 10} – E


• λ-closure(ẟ(D, b)) = λ-closure(8) = {4, 5, 7, 8, 9, 10} – F
• λ-closure(ẟ(E, a)) = λ-closure(6) = {4, 5, 6, 7, 9, 10} – E
• λ-closure(ẟ(E, b)) = λ-closure(8) = {4, 5, 7, 8, 9, 10} – F
• λ-closure(ẟ(F, a)) = λ-closure(6) = {4, 5, 6, 7, 9, 10} – E
• λ-closure(ẟ(F, b)) = λ-closure(8) = {4, 5, 7, 8, 9, 10} – F

12-03-2022 13
Example 2 - Minimization
States a b B
-> A B φ
B φ C
C
C φ D
*D E F
D
*E E F
*F E F
E

Pairs F
{{DE}{DF}}
{EF} => {DEF} A B C D E
12-03-2022 14
Example 2 – Minimized DFA
States a b

-> A B φ
a, b
B φ C

C φ D T
*D D D a, b
b a
a
a b b
A B C D

12-03-2022 15
Example 3

• Convert to minimized DFA


1
0
0, 1 0, 1
q0 q1 q2

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

12-03-2022 16
Example 3
• λ-closure(q0) = {q0} – A
• λ-closure(ẟ(A, 0)) = λ-closure(q0,q1} = {q0,q1} – B
• λ-closure(ẟ(A, 1)) = λ-closure(q1} = {q1} – C
• λ-closure(ẟ(B, 0)) = λ-closure(q0,q1,q2} = {q0,q1,q2} – D
• λ-closure(ẟ(B, 1)) = λ-closure(q1,q2} = {q1,q2} – E
• λ-closure(ẟ(C, 0)) = λ-closure(q2} = {q2} – F
• λ-closure(ẟ(C, 1)) = λ-closure(q2} = {q2} – F
• λ-closure(ẟ(D, 0)) = λ-closure(q0,q1,q2} = {q0,q1,q2} – D
• λ-closure(ẟ(D, 1)) = λ-closure(q1,q2} = {q1,q2} - E

12-03-2022 17
Example 3
• λ-closure(ẟ(E, 0)) = λ-closure(q2} = {q2} – F
• λ-closure(ẟ(E, 1)) = λ-closure(q2} = {q2} – F States 0 1
• λ-closure(ẟ(F, 0)) = φ -> A B C

• λ-closure(ẟ(F, 1)) = λ-closure(q2} = {q2} - F *B D E


*C F F
*D D E
*E F F
F φ F

12-03-2022 18
B
Example 3
C

E
States 0 1
-> A B C F
*B D E
*C F F
A B C D E
*D D E Pairs:
*E F F
F φ F
(BD), (CE)
12-03-2022 19
Example 3 – Minimized DFA
States 0 1 States 0 1
-> A B C -> A X Y
*B D E *X X Y
*C F F *Y F F
*D D E F φ F
0 0, 1
*E F F
F φ F X
0
F
• Let (BD) be X A 1
0, 1
• Let (CE) be Y 1 Y

12-03-2022 20
Example 4
• Find the minimized DFA for the given NFA
0 1 1

q0 q1
q1 q2

0, λ 0, 1

• λ-closure(q0) = {q0, q1}


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

12-03-2022 21
Example 4
• λ-closure(q0) = {q0, q1} - A
• λ-closure(ẟ(A, 0)) = λ-closure(q0, q1, q2) = {q0, q1, q2} – B
• λ-closure(ẟ(A, 1)) = λ-closure(q1, q2) = {q1, q2} – C
• λ-closure(ẟ(B, 0)) = λ-closure(q0, q1, q2) = {q0, q1, q2} – B
• λ-closure(ẟ(B, 1)) = λ-closure(q1, q2) = {q1, q2} – C
• λ-closure(ẟ(C, 0)) = λ-closure(q0, q2) = {q0, q1, q2} – B
• λ-closure(ẟ(C, 1)) = λ-closure(q1, q2) = {q1, q2} - C

12-03-2022 22
Example 4
States 0 1 B

->* A B C
C All pairs are
indistinguishable
*B B C So, {ABC} is a single
state
A B
*C B C

0, 1

12-03-2022 23
Example 5 - Minimize the given DFA
States 0 1 States 0 1
q1
-> q0 q1 q3 -> q0 X X
q1 q2 q4 X X q4
q2 q1 q4 *q4 q4 q4 q2
q3 q2 q4
*q4 q4 q4 q3

• First draw the DFA and check if all q4


the states are reachable from start
state
q0 q1 q2 q3
• The sets are {q1, q2, q3} and {q2,
q3}.
• So, {q1, q2, q3} can be combined
into one state
12-03-2022 24
Exercises – NFA to Minimized DFA
b c
1. a

A B X
C
λ λ

1
0
2.
A B C
0,1 0,1

12-03-2022 25
Exercises – NFA to Minimized DFA
3. a
b a, b

A B X
C
a, λ b, λ

b, λ

4. RE ab*a(a+b)
5. RE (0+1)+(0+1)*
6. 𝐿 = 𝑎𝑛 𝑏 𝑚 ∶ n ≥ 1, m ≥ 2
7. 𝐿 = 𝑎𝑛 b: n ≥ 1} ⋃ {𝑏 𝑛 a ∶ n ≥ 1
12-03-2022 26
Exercises – Minimize the DFA
8. States 0 1 9.
-> A B F
B G C
*C A C
D C G
E H F
F C G
G G E
H G C

12-03-2022 27
Exercises
10.

12-03-2022 28
19CSE214 – Theory of Computation

Non Deterministic Finite Automata(NFA)


to Deterministic Finite Automata (DFA)
and Minimized DFA
by
KAVITHA C.R. & SREEBHA BHASKARAN
Dept. of Computer Science and Engineering

Amrita School of Engineering, Bengaluru


Amrita Vishwa Vidyapeetham

12-03-2022 1
RE to DFA
• To convert RE to NFA use – Thompson’s algorithm
• To convert NFA to DFA use – Subset Construction algorithm
• To minimize the obtained DFA use – Tabulation method (also
called Mark/Reduce procedure)

12-03-2022 2
Subset construction algorithm
Step 1: Find the λ-closure of all the states
Step 2: Let the λ-closure of the initial state be named as A
Step 3: Now find the transitions of each input symbol on this state A.
Then, find the λ-closure of the above set and name it as A if it appears to
be same otherwise give a new name as B.
Step 4: Repeat Step 3 for each of the new states added until there are no
new states in the queue.
Step 5: Convert the above representation to a DFA. The initial state of the
DFA will be A. Mark the final states of DFA.

λ-closure(state q) is the set of states that are reachable through only λ-transitions from state q.
Note: λ-closure(state q) contains itself that is q
12-03-2022 3
Example 1
• Convert the RE (a+b)*abb to a minimized DFA
NFA M1 = ({0,1,2,3,4,5,6,7,8,9,10},{a, b}, ᵟ , 0, {10})

λ-closure(0) = {0, 1, 2, 4, 7} λ-closure(4) = {4} λ-closure(8) = {8}


λ-closure(1) = {1, 2, 4} λ-closure(5) = {1, 2, 4, 5, 6, 7} λ-closure(9) = {9}
λ-closure(2) = {2} λ-closure(6) = {1, 2, 4, 6, 7} λ-closure(10) = {10}
λ-closure(3) = {1, 2, 3, 4, 6, 7} λ-closure(7) = {7}
12-03-2022 4
Example 1
λ-closure(0) = {0, 1, 2, 4, 7} – A NFA M1
λ-closure(ẟ(A, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(A, b)) = λ-closure(5) = {1, 2, 4, 5, 6, 7} – C
λ-closure(ẟ(B, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(B, b)) = λ-closure(5, 9) = {1, 2, 4, 5, 6, 7, 9} – D
λ-closure(ẟ(C, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(C, b)) = λ-closure(5) = {1, 2, 4, 5, 6, 7} – C
λ-closure(ẟ(D, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(D, b)) = λ-closure(5, 10) = {1, 2, 4, 5, 6, 7, 10} – E
λ-closure(ẟ(E, a)) = λ-closure(3, 8) = {1, 2, 3, 4, 6, 7, 8} – B
λ-closure(ẟ(E, b)) = λ-closure(5) = {1, 2, 4, 5, 6, 7} – C 5
12-03-2022
Example 1 – Resultant DFA
DFA M2 = ({A,B,C,D,E}, {a, b}, ᵟ , A, {E})
States a b
-> A B C a
B B D
C B C b
D B E a B D
*E B C
a
b
A a a
DFA transition table
b C E
b

b
DFA M2

12-03-2022 L(M1) = L(M2) Subset Construction Algorithm coverts NFA to its equivalent DFA 6
DFA Minimization – Tabulation Method
States a b
-> A B C Mark/Reduce Procedure
B B D
C B C B
D B E
*E B C C
ẟ(A, a) = B ẟ(A, b) = C
ẟ(D, a) = B ẟ(D, b) = E D
As (C E) is already marked (A D) is also marked
Similarly, (B D) and (C D) are marked E
Because (C D) is marked (B C) and (A B) are marked

Pair – (AC) A B C D
12-03-2022
7
Minimized DFA DFA M3 = ({A,B,D,E},{a, b}, ᵟ , A, {E} )

States a b States a b
-> A B C -> A B A
B B D B B D
a
C B C D B E
D B E *E B A b b
a B D
*E B C
a
A b
a
b E

Minimized DFA M3
12-03-2022 L(M1) = L(M2) = L(M3) 8
Mark/Reduce Procedure

12-03-2022 9
Example 2
• Find the minimal DFA for abb(a+b)*

12-03-2022 10
Example 2

λ-closure(0) = {0} λ-closure(1) = {1} λ-closure(2) = {2}


λ-closure(3) = {3, 4, 5, 7, 10} λ-closure(4) = {4, 5, 7} λ-closure(5) = {5}
λ-closure(6) = {4, 5, 6, 7, 9, 10} λ-closure(7) = {7} λ-closure(8) = {4, 5, 7, 8, 9, 10}
λ-closure(9) = {4, 5, 7, 9, 10} λ-closure(10) = {10} 11
12-03-2022
Example 2 – Subset Alg.

• λ-closure(0) = {0} - A
• λ-closure(ẟ(A, a)) = λ-closure(1) = {1} – B
• λ-closure(ẟ(A, b)) = φ
• λ-closure(ẟ(B, a)) = φ
• λ-closure(ẟ(B, b)) = λ-closure(2) = {2} – C
• λ-closure(ẟ(C, a)) = φ
• λ-closure(ẟ(C, b)) = λ-closure(3) = {3, 4, 5, 7, 10} – D

12-03-2022 12
Example 2

• λ-closure(ẟ(D, a)) = λ-closure(6) = {4, 5, 6, 7, 9, 10} – E


• λ-closure(ẟ(D, b)) = λ-closure(8) = {4, 5, 7, 8, 9, 10} – F
• λ-closure(ẟ(E, a)) = λ-closure(6) = {4, 5, 6, 7, 9, 10} – E
• λ-closure(ẟ(E, b)) = λ-closure(8) = {4, 5, 7, 8, 9, 10} – F
• λ-closure(ẟ(F, a)) = λ-closure(6) = {4, 5, 6, 7, 9, 10} – E
• λ-closure(ẟ(F, b)) = λ-closure(8) = {4, 5, 7, 8, 9, 10} – F

12-03-2022 13
Example 2 - Minimization
States a b B
-> A B φ
B φ C
C
C φ D
*D E F
D
*E E F
*F E F
E

Pairs F
{{DE}{DF}}
{EF} => {DEF} A B C D E
12-03-2022 14
Example 2 – Minimized DFA
States a b

-> A B φ
a, b
B φ C

C φ D T
*D D D a, b
b a
a
a b b
A B C D

12-03-2022 15
Example 3

• Convert to minimized DFA


1
0
0, 1 0, 1
q0 q1 q2

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

12-03-2022 16
Example 3
• λ-closure(q0) = {q0} – A
• λ-closure(ẟ(A, 0)) = λ-closure(q0,q1} = {q0,q1} – B
• λ-closure(ẟ(A, 1)) = λ-closure(q1} = {q1} – C
• λ-closure(ẟ(B, 0)) = λ-closure(q0,q1,q2} = {q0,q1,q2} – D
• λ-closure(ẟ(B, 1)) = λ-closure(q1,q2} = {q1,q2} – E
• λ-closure(ẟ(C, 0)) = λ-closure(q2} = {q2} – F
• λ-closure(ẟ(C, 1)) = λ-closure(q2} = {q2} – F
• λ-closure(ẟ(D, 0)) = λ-closure(q0,q1,q2} = {q0,q1,q2} – D
• λ-closure(ẟ(D, 1)) = λ-closure(q1,q2} = {q1,q2} - E

12-03-2022 17
Example 3
• λ-closure(ẟ(E, 0)) = λ-closure(q2} = {q2} – F
• λ-closure(ẟ(E, 1)) = λ-closure(q2} = {q2} – F States 0 1
• λ-closure(ẟ(F, 0)) = φ -> A B C

• λ-closure(ẟ(F, 1)) = λ-closure(q2} = {q2} - F *B D E


*C F F
*D D E
*E F F
F φ F

12-03-2022 18
B
Example 3
C

E
States 0 1
-> A B C F
*B D E
*C F F
A B C D E
*D D E Pairs:
*E F F
F φ F
(BD), (CE)
12-03-2022 19
Example 3 – Minimized DFA
States 0 1 States 0 1
-> A B C -> A X Y
*B D E *X X Y
*C F F *Y F F
*D D E F φ F
0 0, 1
*E F F
F φ F X
0
F
• Let (BD) be X A 1
0, 1
• Let (CE) be Y 1 Y

12-03-2022 20
Example 4
• Find the minimized DFA for the given NFA
0 1 1

q0 q1
q1 q2

0, λ 0, 1

• λ-closure(q0) = {q0, q1}


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

12-03-2022 21
Example 4
• λ-closure(q0) = {q0, q1} - A
• λ-closure(ẟ(A, 0)) = λ-closure(q0, q1, q2) = {q0, q1, q2} – B
• λ-closure(ẟ(A, 1)) = λ-closure(q1, q2) = {q1, q2} – C
• λ-closure(ẟ(B, 0)) = λ-closure(q0, q1, q2) = {q0, q1, q2} – B
• λ-closure(ẟ(B, 1)) = λ-closure(q1, q2) = {q1, q2} – C
• λ-closure(ẟ(C, 0)) = λ-closure(q0, q2) = {q0, q1, q2} – B
• λ-closure(ẟ(C, 1)) = λ-closure(q1, q2) = {q1, q2} - C

12-03-2022 22
Example 4
States 0 1 B

->* A B C
C All pairs are
indistinguishable
*B B C So, {ABC} is a single
state
A B
*C B C

0, 1

12-03-2022 23
Example 5 - Minimize the given DFA
States 0 1 States 0 1
q1
-> q0 q1 q3 -> q0 X X
q1 q2 q4 X X q4
q2 q1 q4 *q4 q4 q4 q2
q3 q2 q4
*q4 q4 q4 q3

• First draw the DFA and check if all q4


the states are reachable from start
state
q0 q1 q2 q3
• The sets are {q1, q2, q3} and {q2,
q3}.
• So, {q1, q2, q3} can be combined
into one state
12-03-2022 24
Exercises – NFA to Minimized DFA
b c
1. a

A B X
C
λ λ

1
0
2.
A B C
0,1 0,1

12-03-2022 25
Exercises – NFA to Minimized DFA
3. a
b a, b

A B X
C
a, λ b, λ

b, λ

4. RE ab*a(a+b)
5. RE (0+1)+(0+1)*
6. 𝐿 = 𝑎𝑛 𝑏 𝑚 ∶ n ≥ 1, m ≥ 2
7. 𝐿 = 𝑎𝑛 b: n ≥ 1} ⋃ {𝑏 𝑛 a ∶ n ≥ 1
12-03-2022 26
Exercises – Minimize the DFA
8. States 0 1 9.
-> A B F
B G C
*C A C
D C G
E H F
F C G
G G E
H G C

12-03-2022 27
Exercises
10.

12-03-2022 28
19CSE214 – Theory of Computation
RE to DFA and Finite Automata to RE
by
KAVITHA C.R. & SREEBHA
BHASKARAN
Dept. of Computer Science and
Engineering

Amrita School of Engineering, Bengaluru

Amrita Vishwa Vidyapeetham

12-03-2022 1
Basic RE to DFA
• For regular expression “a”

a
X Y

• For regular expression “ab”


a b
X Y Z

• For regular expression “a+b”


a
X Y a,b
X Y
b
Basic RE to DFA
• For regular expression “a*”

• For regular expression “(a+b)*”

a a,b
Example 1
• Construct DFA for a*bc

b c
X Y Z

a,b
c a,b,c

T
Example 2
• Construct DFA for a+bc

a b c
W X Y Z

a,b
b,c c a,b,c

T
Example 3
• Construct DFA for (ab)*b

b
A B

a b b

C D
a
Example 4
1) Construct DFA for Even number of a’s, when Σ ={a,b}.

RE  ( b + ab*ab* ) *

DFA a

A a
B

b b
Exercise
2) Construct DFA for string with “ab” as substring , when Σ ={a,b}.

RE  (a+b)* ab (a+b)*

DFA  b a a, b

a b
X Y Z
FA to RE Use generalized transition graph where the label of the
edge can be regular expression

• State elimination method –


• Remove all intermediate states
• Remove all the trap states and thus solve
a b
X Y Z RE = ab

a
X Y RE = a+b
b

b
a b*a
X Y X Y RE = b*a
Example 1
a a a,b

b b
1 2 3

a a+b

b a*b
1 2 3

a a+b

ba*b
1 3

a*ba*b(a+b)* RE= a*ba*b(a+b)*


1 3
Example 2
a d
c
1 2
b

c d

1 2
a*b
d

1 a*b 2

ca*b

RE = a*b(d + ca*b)*
Example 3
0
0
A B
1 A B
0 1 Removing trap state
0 1 0
0 1
C 1 D
C 1 D
0
A B A 01 A 01 A 01 + 10
1
0 1 0 1
10
C C
RE = (01+10)*
Example 4
0 1 0,1

1 0
q0 q1 q2

0 1 0,1

1 0
q0 q1 q2

0 1 0*1*

1
q0 q1 q0

RE= 0* + 0*11*
RE = 0*1*
= 0*(λ + 11*)
Example 5
1 1 0,1
0
1
q0 q1 q2

0
1 0,1 1 0,1
1*0
1*1 01*1
q0 q1 q2 q0 q2

0
0+1 01*0
1+01*0

01*1
q0 q2
RE= (1+01*0)*01*1(0+1)*
Example 6
a λ

a,b a b bb
1 2 3 4

a
a λ a ab

a+b ab bb (a+b)ab bb
1 2 4 1 4

a a

1 a*(a+b)ab 4 ab+bb
1 a*(a+b)ab 4 ab+bb

aa*(a+b)ab
a

RE= a*(a+b)ab ( (ab+bb) + (aa*(a+b)ab) )*


Example 7
aa aa
a bb ab(bb)*ba
00 10 00 ab 00
a
ba 11
b b b b b b b b a(bb)*ba
a
a ab(bb)*a
01 11 01 a 01
a

a(bb)*a
aa
ab(bb)*ba
00

b+a(bb)*ba
ab(bb)*a+b RE = (aa+ab(bb)*ba)* (ab(bb)*a+b)
01 ( a(bb)*a +
(b+a(bb)*ba) (aa+ab(bb)*ba)* (ab(bb)*a+b) )*
a(bb)*a
Example 8
a a+b a+b*
a a+b
0 1 2

a*b+c
a a+b* a a+b*
a (a+b)*(a+b) a(a+b)*(a+b)
0 1 2 0 2

a*b+c a*b+c
a a+b*
(a*b+c)+(a(a+b)+ ) a*(a*b+c)+(a(a+b)+ )(a+b*)*
0 2 0 2

RE = a*(a*b+c)+(a(a+b)+ )(a+b*)*
Exercise
1) Construct DFA for the string with regular expression (111 + 11111)*
Strings accepted are
3 (111), 5 (11111), 6 (111 twice), 8 (111 once and 11111 once),

9 (111 trice), 10 (11111 twice),Count of 1 afterwards

1 1 1 1 1 1 1

1
1
Exercise
2) Construct DFA for the strings of 0’s and 1’s without any consecutive
ones
(10+0)* (1+ λ) 0,1

3
0 0
1

1 0
0 1 2

1
Assignment
RE to DFA:
1) String with a divisible by 3, when Σ ={a,b}.
2) Odd number of a’s, when Σ ={a,b}.
3) Construct DFA for :
1) (a+b)ba
2) (a+b)*ba
3) (a*b*)*
FA to RE: a e b
1. 0 1 2
d c
19CSE214 – Theory of Computation
Regular Grammar
by
KAVITHA C.R. & SREEBHA
BHASKARAN
Dept. of Computer Science and
Engineering

Amrita School of Engineering, Bengaluru

Amrita Vishwa Vidyapeetham

12-03-2022 1
Regular Grammar
• Two Types
• Right Linear Grammar
• Left Linear Grammar
• A grammar G = (V, T, S, P) is said to be right-linear if all productions
are of the form
• A → xB
• A→x where A, B ∈ V , and x ∈ T∗
• A grammar G = (V, T, S, P) is said to be left-linear if all productions are
of the form
• A → Bx
• A→x where A, B ∈ V , and x ∈ T∗
• A regular grammar is one that is either right-linear or left-linear
12-03-2022 2
Example 1
The grammar G1 = ({S} , {a, b} , S, P1), with P1 given as
S → abS|a
is right-linear

The grammar G2 = ({S, A, B} , {a, b} , S, P2), with productions


S → Aab
A → Aab|B
B→a
is left-linear

12-03-2022 3
Example 2
The grammar G = ({S, A,B} , {a, b} , S, P) with
productions
S→A
A → aB|λ
B → Ab
is not regular.
Why?
But this is an example for a Linear Grammar (i.e. The
RHS of the productions should have only one non-
terminal)
12-03-2022 4
Example 3
Find a regular grammar that generates the language L(aa∗ (ab +
a)∗).
Right Linear Grammar Left Linear
S -> aA Grammar
A -> aA | B S -> Sab | Sa | A
B -> abB | aB | λ A -> Aa | a

12-03-2022 5
Example 4
Construct a right-linear and left-linear grammar for the language
L((aaab∗ab)∗ ).

Right Linear Grammar Left Linear


S -> aaaA | λ Grammar
A -> bA | abS S -> Aab | λ
A -> Ab | Saaa

12-03-2022 6
Example 5
Construct right- and left-linear grammars for the language
L = {anbm : n ≥ 3, m ≥ 2} .

Right Linear Grammar Left Linear


S -> aaaA Grammar
A -> aA | bbB S -> Abb
B -> bB | λ A -> Ab | Baaa
B -> Ba | λ

12-03-2022 7
Example 6
Find the language generated by the given grammar. Generate the left-
linear grammar for the same.
S -> abA
A -> baB Language Left Linear
B -> aA | bb abbabb Grammar
abbaababb S -> Abb
abbaabaababb A -> Aaba | abba
L(abb(aab)*abb)
(or)
L(abba(aba)*bb)
12-03-2022 8
Example 7
Generate the right-linear grammar
for

Right Linear
a b a Grammar
A B C D A -> aB A -> abC
b B -> bC C -> aD
a or
C -> aD D -> abC |
E
D -> aE | λ λ
E -> bC

12-03-2022 9
Example 8
Write the Regular grammar to recognize the strings with even number
of a’s and even no of b’s in any order. [Regular => right-linear grammar]
Right Linear Grammar
a S -> aA | bB | λ
00
a
10 A -> aS | bC
(S) (A)
B -> aC | bS
b b b b C -> aB | bA
a
01 11
(B) a (C)

12-03-2022 10
Properties of Regular Languages
• If L1 and L2 are regular languages, then so are L1 ∪ L2, L1 ∩ L2, L1 L2,
L1, and L∗. We say that the family of regular languages is closed
under union, intersection, concatenation, complementation, and star-
closure.
• The family of regular languages is closed under reversal (L1R).
• Let h be a homomorphism. If L is a regular language, then its
homomorphic image h(L) is also regular. The family of regular
languages is therefore closed under arbitrary homomorphisms.
• If L1 and L2 are regular languages, then L1 /L2 is also regular. We say
that the family of regular languages is closed under right quotient
with a regular language.
• If L1 and L2 are regular languages, then L1 - L2 is also regular.
(i.e) L1 − L2 = L1 ∩ L2.
12-03-2022 11
Example 1
Prove the properties of regular languages on L1 = L(a∗baa∗), L2 = L(ab∗).
Solution:
DFA for L1 DFA for L2

a a b

b a a
A B C P Q
C

b b a
b
T R

a, b a, b
12-03-2022 12
L1 ⋃ L2
• ẟ({A,P},a)={A,Q} • ẟ({B,Q},a)={C,R}
• ẟ({A,P},b)={B,R} • ẟ({B,Q},b)={T,Q}
• ẟ({A,Q},a)={A,R} • ẟ({C,R},a)={C,R} a
b a
• ẟ({A,Q},b)={B,Q} • ẟ({C,R},b)={T,R} A,P B,R C
C,R

• ẟ({B,R},a)={C,R} • ẟ({T,R},a)={T,R} a b b b
• ẟ({B,R},b)={T,R} • ẟ({T,R},b)={T,R} A,Q a B,Q
A,R T,R

• ẟ({A,R},a)={A,R} • ẟ({T,Q},a)={T,R} b
a a a a, b
• ẟ({A,R},b)={B,R} • ẟ({T,Q},b)={T,Q} B,Q
b
T,Q
b
12-03-2022 13
L1.L2
• Concatenate both DFAs by combining the final state of L1 with initial
state of L2 using lambda (is lambda necessary always??)
• If there are more than one final states in L1 then ???
• If the resultant diagram is a DFA, then stop the process else convert
the NFA to DFA using the known procedure

Remove the
final state

12-03-2022 14
L1 ⋂ L2
• ẟ({A,P},a)={A,Q} • ẟ({B,Q},a)={C,R}
• ẟ({A,P},b)={B,R} • ẟ({B,Q},b)={T,Q}
• ẟ({A,Q},a)={A,R} • ẟ({C,R},a)={C,R} a
b a
• ẟ({A,Q},b)={B,Q} • ẟ({C,R},b)={T,R} A,P B,R C
C,R

• ẟ({B,R},a)={C,R} • ẟ({T,R},a)={T,R} a b b b
• ẟ({B,R},b)={T,R} • ẟ({T,R},b)={T,R} A,Q a B,Q
A,R T,R

• ẟ({A,R},a)={A,R} • ẟ({T,Q},a)={T,R} b
a a a a, b
• ẟ({A,R},b)={B,R} • ẟ({T,Q},b)={T,Q} B,Q
b
T,Q
b
12-03-2022
Since there are no final states, L1 ⋂ L2 = φ 15
L1
• For the given DFA, complement the states (i.e.) make all the final
states as non-final states and vice versa
• Check if the resultant diagram is a DFA, if not convert it to DFA using
the procedure. Complete the resultant DFA.
• What if you have a trap state in the given language?

a a a a
b a b a
A B C A B C
b b
b b
T T

12-03-2022 a, b a, b 16
L1*
• Apply the closure property to the DFA and convert to DFA
• Add lambda transitions with two added states

a a
b a
A B C
b
b
T

a, b

12-03-2022 17
L1R
• Change the initial state to final state, final state(s) to initial state(s)
• Reverse the arrows in the diagram. Convert to DFA if necessary.
• Add the trap state if necessary
• What if there are more final states?

a a
a a
b a
b a A B C
A B C
b
b b
b T
T
a, b
12-03-2022
a, b 18
Homomorphism
Ex. 1:
If R = (0∪1)∗001(0∪1)∗ and h(0) = ab and h(1) = bc then
h(R) = (ab ∪ bc)∗ababbc(ab ∪ bc)*

Ex. 2:
Let L = {0n1n |n ≥ 0} and h(0) = ab and h(1) = ba. Then
h(L) = {(ab)n(ba)n |n ≥ 0}

Ex. 3:
If h : {0, 1} → {a, b}* where h(0) = ab and h(1) = ba. Then
h(0011) = ababbaba

12-03-2022 19
L1/L2
Prove the right quotient property of regular languages on L1 = L(a∗baa∗), L2 =
∗).
L(abDFA for L1
L(A) ⋂ L2 = Φ
a a
L(B) ⋂ L2 ≠ Φ
b a L(C) ⋂ L2 ≠ Φ
A B C L(T) ⋂ L2 = Φ
b Hence the right quotient of two languages is
b
DFA for L2 T a a
b
a a, b b a
P Q
C A B C
b
b a b
R T
a, b
20
12-03-2022 a, b
Example 2
• If L1 = {anbm : n ≥ 1,m ≥ 0} ∪ {ba} and L2 = {bm : m ≥ 1} , then prove that
L1/L2 = {anbm : n ≥ 1,m ≥ 0}.

L(q0) ⋂ L2 = Φ
L(q1) ⋂ L2 ≠ Φ
L(q2) ⋂ L2 ≠ Φ
L(q3) ⋂ L2 = Φ
L(q4) ⋂ L2 = Φ
L(q5) ⋂ L2 = Φ Hence the right quotient
of two languages is same
as the given DFA but
with final states as q1
Non-final and q2 alone

12-03-2022 21
Non-Regular languages
• Is the language L = {anbn : n ≥ 0} regular?
No
But why?

To prove the non-regularity of languages we have Pumping


Lemma (i.e) pumping lemma for regular languages

12-03-2022 22
Pumping Lemma
Let L be an infinite regular language. Then there exists some positive integer
m such that any w ∈ L with |w| ≥ m can be decomposed as
w = xyz
with
|xy| ≤ m,
and
|y| ≥ 1,
such that
wi = xyiz,
is also in L for all i = 0, 1, 2, ....

Use Proof by Contradiction


12-03-2022 23
Example 1
Prove that the language L = {anbn : n ≥ 0} is not regular.

Solution: 1. Assume that L is an infinite regular language and


2. let m=5
3. Let w = aaaabbbb ∈ L where |w|>=m (assuming m=5)
4.Decompose w as xyz (i.e.) a a a a b b b b
x Y Z

where, |xy| <= 5 and |y|>=1

5. Now, wi = xyiz = aaa(a)ibbbb ∉ L ∀ i (for example: it fails when i=0)


Hence the assumption is incorrect.
(i.e.) the given language is not regular.

12-03-2022 24
Example 2
Prove that the language L = {wwR : w ∈{a,b}*} is not regular.

Solution: Assume that L is an infinite regular language and let


m=4
Let w = aabbaa ∈ L where |w|>=m (assuming m=4)
Decompose w as xyz (i.e.) x a aY b Z b a a

where, |xy| <= 4 and |y|>=1

Now, wi = xyiz = aab(b)iaa ∉ L ∀ i (for example: it fails when i=0)


Hence the assumption is incorrect. What if the middle 2 b’s
(i.e.) the given language is not regular. were taken as y?
12-03-2022 25
Example 3
Prove that the language L = {anbm : n ≠ m} is not regular.

Solution: Assume that L is an infinite regular language and let


m=5
Let w = aaabbbbb ∈ L where |w|>=m (assuming m=5)
Decompose w as xyz (i.e.) x a aY a bZ b b b b

where, |xy| <= 5 and |y|>=1

Now, wi = xyiz = aaa(bb)ibbb ∉ L ∀ i (for example: it fails when i=0)


Hence the assumption is incorrect.
(i.e.) the given language is not regular.
12-03-2022 26
Example 4
Prove that the language L = {an! : n >=1} is not regular.

Solution: Assume that L is an infinite regular language and let


m=5
Let w = an! ∈ L where |w|>=m (assuming m=5)
Decompose w as xyz (i.e.) x Y ap aq a(n!-p-q)
Z

where, |xy| <= 5 and |y|>=1

Now, wi = xyiz = ap(aq)i a(n!-p-q) ∉ L ∀ i (for example: it fails when i=0)


Hence the assumption is incorrect.
(i.e.) the given language is not regular.
12-03-2022 27
Example 5
Show that the language L = {anbkcn+k : n ≥ 0, k ≥ 0} is not regular.
Solution:
• It is not difficult to apply the pumping lemma directly, but it is
even easier to use closure under homomorphism.
• Assume h (a) = a, h (b) = a, h (c) = c, then
h (L) = {an+kcn+k : n + k ≥ 0} = {aici : i ≥ 0}
• But we know this language is not regular; therefore, L cannot be
regular either.

12-03-2022 28
Exercises
1. Find a right linear and left linear grammar for the language L(abba(ab*a)ba*)
2. Find a regular grammar that generates the language
L = {w ∈ {a, b}∗ : na (w) + 3nb (w) is odd} .
3. Find regular grammar for the following languages on {a, b}:
L = {w : (na (w) − nb (w)) mod 3 = 1} and L = {anbm : n + m is odd}
4. Let L1 = L(a*baa*) and L2 = L(aba*). Prove union, intersection, and right quotient.
5. If 𝐿1 = 𝐿(011∗ ) and 𝐿2 = 𝐿(1∗ 0). Show that L1 R , L1 , L1 L2 is regular.
6. Show that the language L = {anbkcndk : n ≥ 0, k > n} is not regular.
7. Show that the language L = {w : na (w) = nb (w)} is not regular. Is L* regular?
8. Determine whether or not the following language on Σ = {a} is regular. Prove
your
answer.
L = {an : n is not a prime number}.

12-03-2022 29
Identify whether regular or not

1. L = {amblak : m+l+k > 5}


Regular
2. L = {anblak : n > 5, l > 3, k >= 0}
Regular
3. L = {anblak : n >= 5, l > 3, k <= l}
Non-Regular
4. L = {uwwRv : u, v, w ∈ {a, b}+} RE: (a+b)+(aa+bb)(a+b)+
Regular
5. L = {uwwRv : u, v, w ∈ {a, b}+ and |u|<=|v|}
Non-Regular
12-03-2022 30
Introduction
 Context free grammar is a formal grammar which is
used to generate all possible strings in a given formal
language.
 Context free language is a language generated by some
context free grammar
 Set of all CFL is identical to set of languages accepted
by Pushdown Automata
Definition
A grammar G = (V, T, S, P) is said to be context-free if all
productions in P have the form
A → x,
where A ∈ V and x ∈ (V ∪ T)∗

T = finite set of terminal symbols.

V = finite set of non-terminal symbols

P = set of production rules

S = start symbol.
 Example:
CFG to generate equal number of a’s followed by equal
number of b’s.
anbn where, n > 1

S  aXb
X  aXb | λ

G = { {S,X}, {a,b}, S, {S  aXb, X  aXb | λ } }

 Every regular grammar is Context free grammar but


every context free grammar is not regular grammar.
 Example:
L={anbmck : n=m or m < k, n,m,k > 0}

S  AX | Y
A  aAb | λ
X  Xc | λ
Y  aY | Z
Z  bZc|Zc|c | λ
 Example:
L={anbmck : k = |n-m|}
ie, k = n – m or k=m–n
m + k = n or n+k=m

SX|Y
X  aXc | A Y  AB | λ
A  aAb | λ B  bBc | λ
 Example:
L={w ; w ϵ {a,b}*, na(w) = nb(w) + 1 }

S  AaA
A  aAb | bAa | λ
 Example:
Show that L2 is context free if, L = {anbn; n>0}

ie, L1 = anbn
L2 = L1 L1 = anbn ambm , n,m>0}

S  XX
X  aXb | λ

Therefore, L2 is context free


 A derivation of a string is said to be left most if the
right hand side of the production is always applied to
the left most variable in the sentential form

 A derivation of a string is said to be right most if the


right hand side of the production is always applied to
the right most variable in the sentential form
Eg: for anbn where, n > 1 derive aaabbb.
S  aXb
 aaXbb (by applying XaXb)
 aaaXbbb (by applying XaXb)
 aaabbb (by applying X λ)
Ambiguous Grammar
 If there exists more than one derivation tree for the
given input string.

 In case, the string has -


 More than one tree generated using left most derivation
and right most derivation
 More than one tree generated using left most
derivation.
 More than one tree generated using right most
derivation
Example:
 SAA
AaA | b Derive the string “aabb”.
Sol:
LMD: RMD:
SAA
SAA
Ab [by A->b]
aAA [by A->aA] aAb[by A->aA]
aaAA[by A->aA] aaAb[by A->aA]
aabb [by A->b]
aabA [by A->b]
aabb [by A->b]
Derivation Tree S
S
A A
A A
a A b
a A b
a A
a A Unambiguous grammar
b
b
Example:
 EE+E|E*E|(E)|a|b|c Derive the string “a+b*c”.
Sol:
LMD:
RMD:
EE+E
EE*E
a+E[by E->a] E*c [by E->c]
a+E*E[by E->E*E] E+E*c[by E->E+E]
E+b*c[by E->b]
a+b*E [by E->b]
a+b*c [by E->a]
a+b*c [by E->c]

Derivation Tree
E E
E + E E * E

a E * E c
E + E
b c Ambiguous grammar a b
Example:
 EE+E|E*E|(E)|a|b|c Derive the string “a+b*c”.
Sol:
LMD:
LMD:
EE+E
EE*E
a+E[by E->a] E+E*E [by E->E+E]
a+E*E[by E->E*E] a+E*E[by E->a]
a+b*E[by E->b]
a+b*E [by E->b]
a+b*c [by E->c]
a+b*c [by E->c]

Derivation Tree
E E
E + E E * E

a E * E c
E + E
b c Ambiguous grammar a b
Example:
 SaSbA | λ
Aa Derive the string “aababa”.
Sol:
LMD: RMD:
SaSbA
SaSbA
aSba [by A->a]
aaSbAbA [by S->aSbA] aaSbAba[by S->aSbA]
aabAbA[by S-> λ] aaSbaba[by A->a]
aababa [by S-> λ]
aababA [by A->a]
aababa [by A->a]
Derivation Tree S
S
aS b A
aS b A
aS b A a
aS b A a
Unambiguous grammar λ a
λ a
Example:
 SAB|aaB
Aa | Aa
B b Derive the string “aab”.
Sol: RMD:
SaaB
RMD: aab [by B->b]
SAB
Ab [by B->b]
Aab[by A->Aa] S
aab [by A->a] Derivation Tree a a B
S b
A B

A a b
a ambiguous grammar
Example:
 SaSbS| bSaS | λ Derive the string “abab”.
Sol:
LMD:
LMD:
SaSbS
SaSbS abSaSbS [by S->bSaS]
a λbS [by S-> λ]  ab λ aSbS[by S-> λ]
abaSbS[by S->aSbS] aba λ bS [by S-> λ]
aba λ bS [by S-> λ] abab λ [by S-> λ]
abab
abab λ [by S-> λ]
abab
Derivation Tree
S
S
aS b S
aS b S
aS b S bS a S λ
λ
λ λ ambiguous grammar λ λ
S-Grammar
 Definition:
A context-free grammar G = (V, T, S, P) is said to be a
simple grammar or s-grammar if all its productions are
of the form
A → ax,
where A ∈ V , a ∈ T, x ∈ V∗, and any pair (A, a) occurs at
most once in P.
Example:
 The grammar
S → aS |bSS| c
is an s-grammar.

 The grammar
S → aS |bSS| aSS|c
is not an s-grammar
because the pair (S, a) occurs in the two productions
S → aS and S → aSS.
Example:
 Write S-grammar for the language generated by
regular expression aaa*b + b
Sol:
S  aA | b
A aB
B  aB | b

[OR]

S  aA | b
A  aA | b
Example:
 Write S-grammar for the language
L = {anbn, n > 1}
Sol:
S  aA
A  aAB | b
Bb
Assignment
Write CFG for
 L = {anwwrbn ; w ϵ {a,b}*, n>1}
 L = {uvwvR, where u,v,w {a,b}+, |u|=|w|=2
 Nested parenthesis where Σ =[,],(,),{,}, check for the
validity of parenthesis.
Verify the grammar is ambiguous or not:
 EE+T|T
TT*F|F
F(E)|a|b|c
Derive (((a+b)*c))+a
 SaSbS|bSaS| λ
 Generate S-grammar for
L = {anbn+1, n > 2}
Simplification of CFG

1
Steps in simplification
Eliminate
• Left recursion
• Lambda productions
• Unit productions
• Useless productions
• Apply substitution rule (can be applied after any previous steps,
preferably before useless production elimination)
Need for simplification
• To represent the CFG in standard normal forms
• CNF and GNF
2
Substitution rule
• A -> Ba • A -> ba
B -> b B -> b

• A -> aAb | bAa | S A -> aAb | bAa | a | b


S ->a | b S ->a | b

3
Eliminate left recursion
• A -> Ab | Acd | x | y • A -> xA’ | yA’
A’ -> bA’ | cdA’ | λ

• E -> E+T | E-T | T • E -> TE’


T-> T*F | F E’ -> +TE’ | -TE’ | λ
F->a T -> FT’
T’ -> *FT’ | λ
F -> a

4
Eliminate left recursion
• A -> Aab | a | b • A -> aA’ | bA’
A’ -> abA’ | λ

• S -> Aab • S -> babS’


A -> Sab | b S’ -> ababS’ | λ
A -> Sab | b
• Apply substitution and
eliminate recursion
• S -> Sabab | bab
A -> Sab | b Which way to substitute??
5
Example 1
• Simplify the CFG • No left recursion
S -> ABCa | bD • Eliminate lambda productions
A -> BC | b • Identify nullable variables and
eliminate it
B -> b | λ
C -> c | λ • Nullable variables – B, C and A (indirect)
D -> d S -> ABCa | bD | BCa | ACa | ABa | Ca | Aa | Ba | a
A -> BC | b | B | C
B -> b
C -> c
D -> d
6
Example 1
• Eliminating lambda may introduce Unit productions
• Now eliminate Unit productions
S -> ABCa | bD | BCa | ACa | ABa | Ca | Aa | Ba | a
• Draw the dependency graph
A -> BC | b | B | C
(i.e.) A -> B
B -> b
A -> C
C -> c
Hence apply substitution rule for the above
D -> d dependencies.
S -> ABCa | bD | BCa | ACa | ABa | Ca | Aa | Ba | a
A -> BC | b | b | c
B -> b
If needed, apply the substitution rule
C -> c and useless production elimination
D -> d 7
Example 2
• Simplify the CFG • No left recursion, No unit productions
S -> aA | a | Bb | eC • Try substitution rule
A -> aB • Check for Useless productions
• First, find the NTs that derive terminals and
B -> a | Aa repeat the process till all the productions are
C -> eCD checked for this property
• Next, start from the start symbol and start
D -> ddd writing the productions that are reachable
from the start symbol. Repeat till all
productions are checked.

8
S -> aA | a | Bb | eC
Example 2 A -> aB
B -> a | Aa
Step 1:
C -> eCD
NT list: {Nil}
D -> ddd
S -> a, B -> a, D -> ddd NT list: {S, B, D}
S -> Bb, A -> aB NT list: {S, B, D, A}
S -> aA, B -> Aa NT list: {S, B, D, A}

Step 2:
S -> a | Bb | aA
B -> a | Aa Final set of productions
A -> aB 9
Example 3
Simplify the grammar • Eliminate left recursion
E -> E + T | E – T | T E -> TE’
T -> T * F | T / F | F E’ -> +TE’ | -TE’ | λ
F -> a T -> FT’
T’ -> *FT’ | /FT’ | λ
F -> a

10
Example 3
• Eliminate lambda productions
E -> TE’
• E’ and T’ are nullable
E’ -> +TE’ | -TE’ | λ E -> TE’ | T
T -> FT’ E’ -> +TE’ | -TE’ | +T | -T
T’ -> *FT’ | /FT’ | λ T -> FT’ | F
T’ -> *FT’ | /FT’ | *F | /F
F -> a F -> a

11
Example 3
• Eliminate Unit productions
E -> TE’ | T
• Dependency graph: E -> T -> F
E’ -> +TE’ | -TE’ | +T | -T
• Do the substitution from the right end
T -> FT’ | F E -> TE’ | FT’ | a
T’ -> *FT’ | /FT’ | *F | /F E’ -> +TE’ | -TE’ | +T | -T
F -> a T -> FT’ | a
T’ -> *FT’ | /FT’ | *F | /F
F -> a

12
Example 3
• Eliminate Useless productions
Step 1:
E -> TE’ | FT’ | a E -> a, T -> a, F -> a {E, T, F}
E’ -> +TE’ | -TE’ | +T | -T E’ -> +T | -T, T’ -> *F | /F {E, T, F, E’, T’}
T -> FT’ | a E -> TE’ | FT’, E’ -> +TE’ | –TE’
T’ -> *FT’ | /FT’ | *F | /F T -> FT’, T’ -> *FT’ | /FT’ {E, T, F, E’, T’}
F -> a Step 2:
E -> TE’ | FT’ | a
E’ -> +TE’ | -TE’ | +T | -T All productions
T -> FT’ | a are useful
T’ -> *FT’ | /FT’ | *F | /F
F -> a 13
Example 4
• No left recursion
• Simplify the grammar • Eliminate lambda productions
S -> BAAB • A, B and S are nullable
S -> BAAB | BAA | AAB | BAB | AB | BB | BA | AA | B | A
A -> 0A2 | 2A0 | λ
A -> 0A2 | 2A0 | 02 | 20
B -> AB | 1B | λ B -> AB | 1B | A | B | 1

• Eliminate unit productions


• S -> B, S -> A, B -> A, B -> B (can be avoided)
S -> BAAB | BAA | AAB | BAB | AB | BB | BA | AA | 1B | 1 | 0A2 | 2A0 | 02 | 20 | λ
A -> 0A2 | 2A0 | 02 | 20
B -> AB | 1B | 1 | 0A2 | 2A0 | 02 | 20 14
Exercises
Simplify the given grammars
1) S -> abAB 3) S -> Ra | Aa | a
A -> bAB | λ R -> ab
B -> Baa | A | λ A -> AR | AT | b
2) S -> aA | bB T -> Tb | a
A -> aA | a 4) S -> aS | AB
B -> bB
A -> bA
D -> ab | Ea
B -> AA
E -> ac | d
Find the language after Find the language after
simplification simplification
15
Normal Forms

23-10-2020 1
Chomsky Normal Form
o The number of symbols on the right of a production is strictly limited.
o The string on the right of a production consist of no more than two symbols.
Definition:
oA context-free grammar is in Chomsky normal form if all productions are of the form
A → BC or A → a,
where A, B, C are in V , and a is in T.

oThe grammar should be in simplified form.


oAny context-free grammar G = (V, T, S, P) with λ L(G) has an equivalent grammar G = (V , T, S, P) in
Chomsky normal form.

23-10-2020 2
Example:
oThe grammar
S → AS|a
A → SA|b
is in Chomsky normal form.
oThe grammar
S → AS|AAS
A → SA|aa
is not in Chomsky normal form;
both productions S → AAS and A → aa violate the conditions

23-10-2020 3
Example:
S  AS | ab
A SA | b
To CNF
Sol:
S  AS | XY
A  SA | b
Xa
Yb

23-10-2020 4
Example:
S  ABa
A  aab
B  Ac
Sol:
S  ABX S  AP
A  XQ
A  XXY B  AZ
B  AZ P  BX
Q  XY
Xa Xa
Yb Yb
Zc
Zc

23-10-2020 5
Example:
S  abAB
A  bAB | λ
B  BAa | A | λ
Sol:
1) Eliminating left recurssion: 2) Eliminating λ productions:
S  abAB S  abAB | ab |abA |abB
A  bAB | λ A  b | bA | bB | bAB
B  AC | C | λ
B  AC | C | A
C  AaC | aC | Aa | a
C  AaC | λ

23-10-2020 6
Example:
S  abAB
A  bAB | λ
B  BAa | A | λ
Sol:
2) Eliminating λ productions: 3) Eliminating unit productions:
S  abAB | ab |abA |abB S abAB | ab | abA | abB
A  b | bA | bB | bAB A  bAB | b | bA | bB
B  AC | C | A
B  AC | AaC | a | Aa | aC | bAB | b | bA | bB
C  AaC | a | Aa | aC
C  AaC | aC | Aa | a

23-10-2020 7
Example:
S  abAB 3) Eliminating unit productions:
S abAB | ab | abA | abB
A  bAB | λ
A  bAB | b | bA | bB
B  BAa | A | λ B  AC | AaC | a | Aa | aC | bAB | b | bA | bB
Sol: C  AaC | a | Aa | aC
4) Eliminating useless productions:
S  abAB | abA | abB
A  bA | bB | bAB
B  AC | AaC | aC | Aa | bA | bB | bAB
C  AaC | aC | Aa

23-10-2020 8
Example:
5) CNF:
4) Eliminating useless productions: S  XT | XR | XQ
S  abAB | abA | abB A  YP | YA | YB
A  bA | bB | bAB B  AC | AL | AX | XC | XP | XA | XB
B  AC | AaC | aC | Aa | bA | bB | bAB C  AL | AX | XC
C  AaC | aC | Aa Xa
Yb
P  AB
R  YA
Q  YB
L  XC
T  YP

23-10-2020 9
Greibach Normal Form
o Here restriction is not on the length of the right sides of a production, but on the positions in
which terminals and variables can appear.
Definition:
oA context-free grammar is said to be in Greibach normal form if all productions have the form
A → ax,
where a ∈ T and x ∈ V∗.
o The definition is similar to s-grammar but GNF does not carry the restriction that the pair (A, a)
occur at most once.
oFor every context-free grammar G with λ L(G), there exists an equivalent grammar G in
Greibach normal form.

23-10-2020 10
Example-1:
The grammar
S → abSb|aa
Sol:

S → aBSB|aA
Aa
Bb

23-10-2020 11
Example-2:
The grammar
S → AB
A → aA |bB| b
B→b
Sol:
using the substitution
S → aAB |bBB| bB
A → aA |bB| b
B→b

23-10-2020 12
Example-3:
S  XY | AA
X  AB
Apply Substitution Model Apply Substitution Model
Y  SB
Aa S  ABY | AA S  aBY | aA
X  AB X  aB
Bb Y  ABYB | AAB Y  aBYB | aAB
Aa Aa
Bb Bb

23-10-2020 13
Example-4
S  ABb | a
A  aaA | B
B  bAb

Eliminating Unit Production:


S  ABb | a S  ABY | a S  AP | a S  aQP | bRP | a
A  aaA | bAb A  XXA | YAY A  XQ | YR A  aQ | bR
B  bAb B  YAY B  YR B  bR
Xa P  BY P  bRY
Yb Q  XA Q  aA
R  AY R  aQY | bRY
Xa Yb
Yb

23-10-2020 14
CYK algorithm
Originators J. Cocke, D. H. Younger, and T. Kasami.
Specific for Context free grammar
The algorithm works only if the grammar is in Chomsky normal form
Succeeds by breaking one problem into a sequence of smaller ones
Is w in L(G)?
Uses a “dynamic programming” or “table-filling algorithm”

23-10-2020 15
Construct a Triangular Table
Each row corresponds to one length of substrings
– Bottom Row – Strings of length 1
– Second from Bottom Row – Strings of length 2
.
.
– Top Row – string ‘w’
Xi, j is the set of variables A such that
A  wi is a production of G
Compare at most n pairs of previously computed sets:
(Xi, j , Xi+1, j ), (Xi, j+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )

23-10-2020 16
Construct a Triangular Table
Xn,1 wn

X4,n X4,1 w4

j X3,n X3,2 X3,1 w3

X2,n X2,3 X2,2 X2,1 w2

X1,n X1, 4 X1,3 X1,2 X1,1 w1

23-10-2020 17
Construct a Triangular Table
Grammar: 1) is the grammar in CNF?
S  AB YES
A  BB | a
B  AB | b 2) |w| = 5
X5,1 b
W= aabbb
X4,2 X4,1 b

X3,3 X3,2 X3,1 b

X2,4 X2,3 X2,2 X2,1 a

X1,5 X1, 4 X1,3 X1,2 X1,1 a

23-10-2020 18
Construct a Triangular Table
S  AB
A  BB | a
B  AB | b
{B} b
W= aabbb
b
{B}

{B} b

{A} a

{A} a

23-10-2020 19
Construct a Triangular Table
S  AB X1,2 = (X1,1 , X2,1)
A  BB | a aa = {A}{A} = {AA}
B  AB | b
• Steps:
W= aabbb
b – Look for production rules to generate AA
{B}
b – There is no production which produces
{B} AA. Therefore, ∅

{B} b

{A} a

∅ {A} a

23-10-2020 20
Construct a Triangular Table
S  AB X2,2 = (X2,1 , X3,1)
A  BB | a
ab = {A}{B} = {AB}
B  AB | b
• Steps:
W= aabbb
{B} b – Look for production rules to generate
AB
b
{B} – There are two: {S and B}
{B} b

{S,B} {A} a

∅ {A} a

23-10-2020 21
Construct a Triangular Table
S  AB X3,2 = (X3,1 , X4,1)
A  BB | a
bb= {B}{B} = {BB}
B  AB | b
• Steps:
W= aabbb
{B} b – Look for production rules to generate
BB
b
{B} – There is one: {A}
{A} {B} b

{S,B} {A} a

∅ {A} a

23-10-2020 22
Construct a Triangular Table
S  AB X4,2 = (X4,1 , X5,1)
A  BB | a
bb = {B}{B} = {BB}
B  AB | b
• Steps:
W= aabbb
{B} b – Look for production rules to generate
BB
b
{A} {B} – There is one: {A}
{A} {B} b

{S,B} {A} a

∅ {A} a

23-10-2020 23
Construct a Triangular Table
S  AB X1,3 = (X1,2 , X2,2)
A  BB | a {aab = {a}{ab} , {aa}{b}
B  AB | b = {AS, AB}, {∅}
W= aabbb • Steps:
{B} b
– Look for production rules to generate AS,AB,
b
{A} {B} – There are two: {S,B}

{A} {B} b

{S,B} {A} a

{S,B} ∅ {A} a

23-10-2020 24
Construct a Triangular Table
S  AB X2,3 = (X2,2 , X3,2)
A  BB | a {abb = {a}{bb} , {ab}{b}
B  AB | b = {AA}, {SB, BB}
W= aabbb • Steps:
{B} b – Look for production rules to generate AA,SB,
BB
{A} {B}
b
– There is one: {A}

{A} {B} b

{A} {S,B} {A} a

{S,B} ∅ {A} a

23-10-2020 25
Construct a Triangular Table
S  AB X3,3 = (X3,2 , X4,2)
A  BB | a {bbb = {b}{bb} , {bb}{b}
B  AB | b = {BA}, {AB}
W= aabbb • Steps:
{B} b
– Look for production rules to generate BA,AB
b
{A} {B} – There are two: {S, B}

{S,B} {A} {B} b

{A} {S,B} {A} a

{S,B} ∅ {A} a

23-10-2020 26
Construct a Triangular Table
S  AB X1,4 = (X1,3 , X2,3)
A  BB | a {aabb = {a}{abb}, {aa}{bb}, {aab}{b}
B  AB | b = {A}{A}, {∅}{A}, {S,B}{B}
W= aabbb = {AA, ∅, SB, BB}
{B} b • Steps:
b – Look for production rules to generate AA, SB,
{A} {B} BB

{S,B} {A} {B} b – There is one: {A}

{A} {S,B} {A} a

{A} {S,B} ∅ {A} a

23-10-2020 27
Construct a Triangular Table
S  AB X2,4 = (X2,3 , X3,3)
A  BB | a {abbb = {a}{bbb}, {ab}{bb}, {abb}{b}
B  AB | b = {A}{S,B}, {S,B}{A}, {A}{B}
W= aabbb ={AS, AB, SA, BA, AB}
{B} b • Steps:
b – Look for production rules to generate AS, AB,
{A} {B} SA, BA

{S,B} {A} {B} b – There are two: {S, B}

{S,B} {A} {S,B} {A} a

{A} {S,B} ∅ {A} a

23-10-2020 28
Construct a Triangular Table
S  AB X1,5 = (X1,4 , X2,4)
A  BB | a aabbb = {a}{abbb}, {aa}{bbb}, {aab}{bb}, {aabb}{b}
B  AB | b = {A}{S,B}, {∅}{S,B}, {S,B}{A}, {A}{B}
W= aabbb ={AS, AB, ∅, SA, BA, AB}
{B} b • Steps:

b – Look for production rules to generate AS, AB, SA,


{A} {B} BA, AB

b – There are two: {S, B}


{S,B} {A} {B}

{S,B} {A} {S,B} {A} a

{S,B} {A} {S,B} ∅ {A} a

23-10-2020 29
Construct a Triangular Table
Check if S is in (1,n), then
Therefore, aabbb ∈ L(G)
Accept the string
Else
{B} b Reject the string
{A} {B}
b

{S,B} {A} {B} b

{S,B} {A} {S,B} {A} a

{S,B} {A} {S,B} ∅ {A} a

23-10-2020 30
Construct a Triangular Table
Grammar: 1) is the grammar in CNF?
S  AB YES
A  BB | a
B  AB | b 2) |w| = 4
W= aabb X4,1 b

X3,1 X3,2 b

X2,1 X2,2 X2,3 a

X1,1 X2,1 X3,1 X4,1 a

23-10-2020 31
Construct a Triangular Table
Grammar: Identify the production rule where
S  AB these terminals occur

A  BB | a
B  AB | b
W= aabb b
{B}

{B} b

{A} a

{A} a

23-10-2020 32
Construct a Triangular Table
Grammar: Identify for {aa}, {ab}, {bb}
S  AB
A  BB | a
B  AB | b
W= aabb b
{B}

{A} {B} b

{S,B} {A} a

∅ {A} a

23-10-2020 33
Construct a Triangular Table
Grammar: Identify for {aab}, {abb}
S  AB
A  BB | a
B  AB | b
W= aabb b
{B}

{A} {B} b

{A} {S,B} {A} a

{S,B} ∅ {A} a

23-10-2020 34
Construct a Triangular Table
Grammar: Identify for {aabb}
S  AB
Therefore, aabb does not belong to L(G)
A  BB | a
B  AB | b
W= aabb b
{B}

{A} {B} b

{A} {S,B} {A} a

{A} {S,B} ∅ {A} a

23-10-2020 35
Assignment
1) Apply CYK Algorithm 2) Apply CNF 3) Apply GNF
S  XY S  AB | aB a) S  aSb |ab
X  XA | a | b A  aab | ∈ Bb
Y  AY | a B  bbA b) S  aSb | bSa | a | b
Aa
a) W = babaa
b) b) W = baaaa

23-10-2020 36
Push Down Automata (PDA)

1
NPDA - Definition

δ(q0, a, Z) = (q0, aZ)


δ(q0, a, a) = (q0, aa)
δ(q0, a, Z) = (q0, λ)
δ(q0, a, Z) = (q1, bZ)
δ(q0, a, Z) = (q1, λ)
δ(q0, a, Z) = (q0, Z)
δ(q0, a, a) = (q0, b)
δ(q1, λ, Z) = (q2, Z)
δ(q1, λ, Z) = (q2, λ)
2
Example 1 Example: aaaabbbb λ
Design a PDA to accept L={anbn:n>=0}

a
a
a
a
accept
Z

3
Example 1 Example: aaaabbbb λ
q0 q0 q0 q0q0 q1 q1 q1 q1 q2
Design a PDA to accept L={anbn:n>=0}
δ(q0, a, Z) = (q0, aZ) What if n>=1 ???
δ(q0, a, a) = (q0, aa) a

δ(q0, b, a) = (q1, λ) a

δ(q1, b, a) = (q1, λ) a
a
δ(q1, λ, Z) = (q2, Z)
Final state accept
Z
To accept empty string
δ(q0, λ, Z) = (q2, Z)
M=({q0, q1, q2}, {a, b}, {a}, δ, q0, Z, {q2}}
Same stack symbol and same state, but different
input symbols, so given automata is NPDA
4
Example 1

Accept by
final state

δ(q1, λ, Z) = (q2, λ) Accept by


δ(q0, λ, Z) = (q2, λ) Empty stack

5
Example 1
Check if aaabbb is part of the
language generated by the
automata
Initial ID Check if aab is part of the language
(q0, aaabbb, Z) Ͱ (q0, aabbb, aZ) generated by the automata
Ͱ (q0, abbb, aaZ) Ͱ (q0, bbb, aaaZ) Initial ID
Ͱ (q1, bb, aaZ) Ͱ (q1, b, aZ) (q0, aab, Z) Ͱ (q0, ab, aZ)
Ͱ (q1, λ, Z) Ͱ (q2, λ, Z) Ͱ (q0, b, aaZ) Ͱ (q1, λ, aZ)
Accept Transition not defined
Reject the string
6
Language accepted by PDA

• This is called Instantaneous description (ID)


• (q0, w, z) is called Initial ID
7
Example 2
Design a PDA to accept L={anbn+1:n>=0}
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, λ)
δ(q1, b, a) = (q1, λ)
δ(q1, b, Z) = (q2, Z) (make sure that only one extra b is there)
δ(q2, λ, Z) = (q3, Z) (final state)
δ(q0, b, Z) = (q4, Z) (minimum string)
δ(q4, λ, Z) = (q3, Z) (minimum string, final state)
NPDA or DPDA?
DPDA
8
Example 3
Design a PDA to accept L={an+1bn:n>=0}
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, λ)
δ(q1, b, a) = (q1, λ)
δ(q1, λ, a) = (q2, λ) (make sure that only one extra a is there)
δ(q2, λ, Z) = (q3, Z) (final state)
δ(q0, λ, a) = (q4, λ) (minimum string) How to do..
δ(q4, λ, Z) = (q3, Z) (minimum string, final state) L={ambn:m>n}
NPDA or DPDA? mbn:m<n}
NPDA bcoz of rule 2 and 7
L={a
9
Example 4
• PDA for L={wcwR : w ∈ (a+b)*}
δ(q0, a, Z) = (q0, aZ)
δ(q0, b, Z) = (q0, bZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb) δ(q1, a, a) = (q1, λ)
δ(q0, a, b) = (q0, ab) δ(q1, b, b) = (q1, λ)
δ(q0, b, a) = (q0, ba) δ(q1, λ, Z) = (q2, Z) (final state)
δ(q0, c, a) = (q1, a) δ(q0, c, Z) = (q3, Z)
δ(q0, c, b) = (q1, b)
δ(q3, λ, Z) = (q2, Z) (Minimum string)
10
Example 4
• Check if abbcbba is accepted by the PDA
(q0, abbcbba, Z) Ͱ (q0, bbcbba, aZ)
Ͱ (q0, bcbba, baZ) Ͱ (q0, cbba, bbaZ)
Ͱ (q1, bba, bbaZ) Ͱ (q1, ba, baZ) • Check if abcbba is accepted by the PDA
Ͱ (q1, a, aZ) Ͱ (q1, λ, Z) Ͱ (q2, λ, Z) (q0, abcbba, Z) Ͱ (q0, bcbba, aZ)
q2 ∈ F hence, accept the string Ͱ (q0, cbba, baZ) Ͱ (q1, bba, baZ)
Ͱ (q1, ba, aZ)
no transition defined
Hence reject the string
11
Example 5
• PDA for L={wwR : w ∈ (a+b)*}
δ(q0, a, Z) = (q0, aZ)
δ(q1, a, a) = (q1, λ)
δ(q0, b, Z) = (q0, bZ)
δ(q0, a, a) = (q0, aa) δ(q1, b, b) = (q1, λ)
δ(q0, b, b) = (q0, bb) δ(q1, λ, Z) = (q2, Z) (final state)
δ(q0, a, b) = (q0, ab) δ(q0, λ, Z) = (q2, Z) (Minimum string)
δ(q0, b, a) = (q0, ba)
δ(q0, λ, a) = (q1, a)
δ(q0, λ, b) = (q1, b) Imaginary λ
NPDA or DPDA??
NPDA – rules 1&12 or 2&12 or 3&7 or 4 &8 12
• Check if abba is accepted by the PDA
Example 5 (q0, abba, Z) Ͱ (q0, bba, aZ)
Ͱ (q0, ba, baZ) Ͱ (q0, λa, bbaZ)
• Check if abba is accepted by the PDA
Ͱ (q1, a, bbaZ)
(q0, abba, Z) Ͱ (q0, bba, aZ)
No transition defined
Ͱ (q0, λba, baZ)
reject the string (as per this)
Ͱ (q1, ba, baZ) Ͱ (q1, a, aZ)
Ͱ (q1, λ, Z) Ͱ (q2, λ, Z)
• Check if abba is accepted by the PDA
q2 ∈ F hence, accept the string
(q0, abba, Z) Ͱ (q0, λbba, aZ)
But if any one derivation Ͱ (q1, bba, aZ)
accepts the string, then the No transition defined
string has to be accepted reject the string (as per this) 13
Example 6
• L={anb2n:n>=0}
• For such problems, push 2 a’s for a single a, and match the b’s
δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
δ(q0, b, a) = (q1, λ)
δ(q1, b, a) = (q1, λ)
δ(q1, λ, Z) = (q2, Z) (final state)
δ(q0, λ, Z) = (q2, Z) (to accept minimum string)

14
Example 7
• L={a2nbn:n>=0}
• For such problems, push 1 a for every 2 a’s, and match the b’s (easier to
implement/understand)
δ(q0, a, Z) = (q3, Z)
δ(q3, a, Z) = (q0, aZ)
δ(q0, a, a) = (q3, a)
δ(q3, a, a) = (q0, aa)
δ(q0, b, a) = (q1, λ)
δ(q1, b, a) = (q1, λ)
δ(q1, λ, Z) = (q2, Z) (final state)
δ(q0, λ, Z) = (q2, Z) (to accept minimum string)

15
Example 8
• Design a PDA for L(ab*ab)
• Keep accepting inputs as per the requirement. Change the state if
necessary
δ(q0, a, Z) = (q0, aZ)
δ(q0, b, a) = (q0, ba)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q1, ab)
δ(q0, a, a) = (q1, aa)
δ(q1, b, a) = (q2, ba) (final state)

16
Example 9
• L={w : na(w)=nb(w)}
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q0, λ)

δ(q0, b, Z) = (q0, bZ)


δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, λ)

δ(q0, λ, Z) = (q1, Z) (final state)

17
Example 10
• Find the language accepted by the NPDA
M=({q0, q1, q2, q3, q4, q5}, {a, b}, {1}, δ, q0, Z, {q4, q5}}

δ(q0, b, Z) = (q1, 1Z)


δ(q1, b, 1) = (q2, 11)
δ(q2, a, 1) = (q3, λ)
δ(q3, a, 1) = (q4, λ)
δ(q4, a, Z) = {(q4, Z), (q5, Z)}

L={bban : n>=2}
18
CFG to PDA – Example 1
• PDA can also be generated from CFG
• But the CFG should be in GNF
• Example 1: Equivalent PDA
S -> aAB δ(q0, λ, Z) = (q1, SZ) (always push start symbol from
A -> a the initial state)
B -> b δ(q1, a, S) = (q1, AB)
δ(q1, a, A) = (q1, λ)
δ(q1, b, B) = (q1, λ)
δ(q1, λ, Z) = (q2, Z) (final state)
19
Example 1
• Check if aab belongs to the language generated by PDA
Initial ID (q0, aab, Z) δ(q0, λ, Z) = (q1, SZ)
δ(q1, a, S) = (q1, AB)
δ(q1, a, A) = (q1, λ)
(q0, λaab, Z) Ͱ (q1, aab, SZ) δ(q1, b, B) = (q1, λ)
Ͱ (q1, ab, ABZ) Ͱ (q1, b, BZ) δ(q1, λ, Z) = (q2, Z)

Ͱ (q1, λ, Z) Ͱ (q2, λ, Z)
Accept the string

20
Example 2
• Obtain the PDA for
S -> aSBB | a Equivalent PDA
B -> b δ(q0, λ, Z) = (q1, SZ) (always push start symbol from
the initial state)
δ(q1, a, S) = {(q1, SBB), (q1, λ)}
δ(q1, b, B) = (q1, λ)
δ(q1, λ, Z) = (q2, Z) (final state)

21
Problems that PDA cannot solve
• L={anbncn : n>=0}
• L={ww : w ∈ (a+b)*}
• L={anbmcndm : n, m>=0}
Basically the problems that need extra memory other than stack
memory….

22
Exercises
1. L={ambncm+n: m, n>=0}
2. L={aab*aba*}
3. Matching parenthesis for the inputs [, (, ), ]
4. L={anbm: n=m+2, n,m>=0}
5. Find the language accepted by PDA. Assume 0 is the initial stack top symbol.
M=({q0, q1, q2, q3}, {a, b}, {0, 1}, δ, q0, 0, {q3}}
δ(q0, a, 0) = {(q1, 10), (q3, λ)}
δ(q0, λ, 0) = (q3, λ)
δ(q1, a, 1) = (q1, 11)
δ(q1, b, 1) = (q2, λ)
δ(q2, b, 1) = (q2, λ)
δ(q2, λ, 0) = (q3, λ)
6. Generate the PDA for the grammar represented by productions
S -> aSb | bSa | ab | ba
7. Check if the strings aaba and aabb belong to the automata in Example 2 represented by
S -> aSBB | a B -> b
23
The Chomsky Hierarchy

23-04-2021 Dept. of CSE 1


Who is Noam Chomsky?
• Philosopher of Languages
• Professor of Linguistics at MIT
• Constructed the idea that language was not a learned “behavior”, but
that it was cognitive and innate; versus stimulus-response driven
• In an effort to explain these theories, he developed the Chomsky
Hierarchy

23-04-2021 Dept. of CSE 2


Chomsky Hierarchy
LRE

LCSG

LCFG

23-04-2021 Dept. of CSE 3


Type-0 (Unrestricted Grammar)
Definition:
A Grammar G = (V, Σ , S, P), V∩Σ = ɸ is of type-0, if the productions have the form
αβ
For α ∈ (V U Σ)*V(V U Σ)*, β ∈ (V U Σ)*

A language L is type-0 if and only if there is a type-0 grammar G such that L(G) = L

Example:
G = (V, Σ , S, P) where
P = {SXA, A AB|b, bBBbb, XBX, X ∈}

Language: Machine:
Recursively enumerable language Turing Machine
23-04-2021 Dept. of CSE 4
Type-1 (Context Sensitive Grammar)
Definition:
A Grammar G = (V, Σ , S, P), V∩Σ = ɸ is of type-1, if the productions have the form
αβ
For α ∈ (V U Σ)*V(V U Σ)*, β ∈ (V U Σ)*, |α| < |β|

Example:
G = (V, Σ , S, P) where
P = {SaSBC | abC, CBDB, DBDE, DEDC, DCBC, bBbb, Cc }

Language: Machine:
Context Sensitive language Linear bound automata
23-04-2021 Dept. of CSE 5
Type-2 (Context Free Grammar)
Definition:
A Grammar G = (V, Σ , S, P), V∩Σ = ɸ is of type-2, if the productions have the form
Aβ
For A ∈ V, β ∈ (V U Σ)*

A language L is type-2 if and only if there is a type-2 grammar G such that L(G) = L

Example:
G = (V, Σ , S, P) where
P = {SaSb | ∈}

Language: Machine:
Context Free Language Push down automata
23-04-2021 Dept. of CSE 6
Type-3 (Regular Grammar)
Definition:
A Grammar G = (V, Σ , S, P), V∩Σ = ɸ is of type-3 or right linear or left linear, if the productions have the
form
A  xB | x (or) A  Bx | x
For A, B ∈ V, x ∈ Σ*

A language L is type-2 if and only if there is a type-2 grammar G such that L(G) = L

Example:
G = (V, Σ , S, P) where
P = {SaS | ∈}

Language: Machine:
Regular Language Finite automata
23-04-2021 Dept. of CSE 7
Chomsky Hierarchy

LRE
• All Language accepted by Finite
Automata are recognized by
LCSG
PDA & TM
• Language accepted by TM need
LCFG
not be accepted by Finite
automata

23-04-2021 Dept. of CSE 8


Chomsky Hierarchy

23-04-2021 Dept. of CSE 9


Relationship between regular, linear, deterministic context-free, and nondeterministic
context-free languages

23-04-2021 Dept. of CSE 10


Summary
Type Grammar Syntax Language Machine Storage Unit
Type-0 Unrestricted α  β Recursively Turing Tape
grammar α ∈ (V U Σ)*V(V U enumerable machine
Σ)*, language
β ∈ (V U Σ)*
Type-1 Context αβ Context Linear bound Restricted
sensitive α ∈ (V U Σ)*V(V U sensitive automata tape
grammar Σ)*, language
β ∈ (V U Σ)*,
|α| < |β|
Type-2 Context free A  β Context free Push down Stack
grammar A ∈ V, language automata
β ∈ (V U Σ)*
Type-3 Regular A  xB | x (or) Regular Finite Nil
Grammar A  Bx | x language automata
A, B ∈ V,
x ∈ Σ*

23-04-2021 Dept. of CSE 11


Thank You

23-04-2021 Dept. of CSE 12


19CSE214 Theory Computation
Turing Machines

Amrita School of Engineering, Bengaluru


Amrita Vishwa Vidyapeetham

1
Standard Turing Machine

δ(q0, a) = (q0, 0, R)
δ(q0, b) = (q0, b, R)
δ : Q × Γ → Q × Γ × {L, R} δ(q0, 0) = (q0, 0, L)
δ(q0, B) = (q1, B, L)

Note: Students are expected to trace and learn for each example with a sample accepted/rejected strings 2
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape
Infinite tape
B B a X a b Y b B B B

BBaabbBB q0 q0q2 q0q1q2 q1


BBXabbBB q1
BBXabbBB q1
BBXaYbBB q2 Logic:
BBXaYbBB q2 Look for the leftmost a and replace by X, change the state and move right
BBXaYbBB q2 Move towards right in the same state till the leftmost b is encountered
BBXaYbBB q0 Replace leftmost b by Y, Change the state and move left
BBXXYbBB q1 Move towards left till X is encountered in the same state
BBXXYbBB q1 When rightmost X is found, Move right, change the state to q0 and the steps repeats
BBXXYYBB q2
BBXXYYBB q2 Repeat the steps till each a and matching b is replaced into X and Y respectively
BBXXYYBB q0 When all a’s are done, the current state points to Y directly and then change the state
BBXXYYBB q3 Move forward till blank and reach the final state
3
BBXXYYBB q4 Final state
Example 1
• Design a TM to accept the language L={anbn : n>=1}
Infinite tape Infinite tape

B B a X a X b Y b Y B B B

q0 q2 q0q1q2 q1
BBaabbBB q0
BBXabbBB q1
BBXabbBB q1
BBXaYbBB q2
BBXaYbBB q2
BBXaYbBB q2
BBXaYbBB q0
BBXXYbBB q1
BBXXYbBB q1
BBXXYYBB q2
BBXXYYBB q2
BBXXYYBB q0
BBXXYYBB q3
BBXXYYBB q4 Final state 4
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a X b Y b Y B B B

BBaabbBB q0
BBXabbBB q1 q0 q3q4 q3
BBXabbBB q1 String accepted –
BBXaYbBB q2 q4-final state
BBXaYbBB q2
BBXaYbBB q2
BBXaYbBB q0
BBXXYbBB q1
BBXXYbBB q1
BBXXYYBB q2
BBXXYYBB q2
BBXXYYBB q0
BBXXYYBB q3
BBXXYYBB q4 Final state 5
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a b Y b B B B

q0q2 q1
q0
q2
q2 q1

δ(q0, a) = (q1, X, R) δ(q2, a) = (q2, a, L)


δ(q1, a) = (q1, a, R) δ(q2, X) = (q0, X, R)
δ(q1, b) = (q2, Y, L)

6
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a X b Y b Y B B B

q0q2 q1
q0
q2 q1

δ(q0, a) = (q1, X, R) δ(q2, a) = (q2, a, L) δ(q2, Y) = (q2, Y, L)


δ(q1, a) = (q1, a, R) δ(q2, X) = (q0, X, R)
δ(q1, b) = (q2, Y, L) δ(q1, Y) = (q1, Y, R)

7
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a X b Y b Y B B B

q0 q3q4 q3
String accepted –
q4-final state
δ(q0, Y) = (q3, Y, R)
δ(q3, Y) = (q3, Y, R)
δ(q3, B) = (q4, B, L)

8
Example 1
L={anbn : n>=1}
δ(q0, a) = (q1, X, R)
δ(q1, a) = (q1, a, R)
δ(q1, b) = (q2, Y, L)
δ(q2, a) = (q2, a, L)
δ(q2, X) = (q0, X, R)
δ(q1, Y) = (q1, Y, R)
δ(q2, Y) = (q2, Y, L)
δ(q0, Y) = (q3, Y, R)
a b X Y B
δ(q3, Y) = (q3, Y, R)
q0 (q1, X, R) (q3, Y, R)
δ(q3, B) = (q4, B, L)
q1 (q1, a, R) (q2, Y, L) (q1, Y, R)
q4 is the final state q2 (q2, a, L) (q0, X, R) (q2, Y, L)
q3 (q3, Y, R) (q4, B, L)
9
Example 1 - n
L={a b n : n>=1}
• Check if aabb belongs to the automata.
Initial ID
q0w => q0aabb
q0aabb Ͱ Xq1abb Ͱ Xaq1bb Ͱ Xq2aYb
Check for aab
Ͱ q2XaYb Ͱ Xq0aYb Ͱ XXq1Yb q0aab Ͱ Xq1ab Ͱ Xaq1b Ͱ Xq2aY
Ͱ XXYq1b Ͱ XXq2YY Ͱ Xq2XYY Ͱ q2XaY Ͱ Xq0aY Ͱ XXq1Y
Ͱ XXq0YY Ͱ XXYq3Y Ͱ XXYYq3B Ͱ XXYq1B
Ͱ XXYq4YB No B defined on q1
Accept the string Hence reject the string

10
Language accepted by TM

11
Example 2
Design a TM to accept the language
L={anbncn : n>=1}
δ(q0, a) = (q1, X, R) Y;Y,R
δ(q1, a) = (q1, a, R) Y;Y,R
q4

δ(q1, b) = (q2, Y, R)
Z;Z,R
δ(q2, b) = (q2, b, R)
δ(q2, c) = (q3, Z, L) Z;Z,R q5
5
q6
δ(q3, b) = (q3, b, L) 6
δ(q3, Y) = (q3, Y, L)
δ(q1, Y) = (q1, Y, R) δ(q4, Y) = (q4, Y, R)
δ(q3, a) = (q3, a, L)
δ(q2, Z) = (q2, Z, R) δ(q4, Z) = (q5, Z, R)
δ(q3, X) = (q0, X, R)
δ(q3, Z) = (q3, Z, L) δ(q5, Z) = (q5, Z, R)
δ(q0, Y) = (q4, Y, R) δ(q5, B) = (q6, B, L)
12
BBaabbccBB q0

BBXabbccBB q1
BBXabbccBB q1

BBXaYbccBB q2
BBXaYbccBB q2 BBXXYYZZBB q0
BBXXYYZZBB q4
BBXaYbZcBB q3 BBXXYYZZBB q5
BBXaYbZcBB q3 BBXXYYZZBB q5
BBXaYbZcBB q3
BBXaYbZcBB q3
BBXXYYZZBB q6 Final state
BBXaYbZcBB q0

BBXXYbZcBB q1
BBXXYbZcBB q1

BBXXYYZcBB q2
BBXXYYZcBB q2

BBXXYYZZBB q3
BBXXYYZZBB q3
BBXXYYZZBB q3
BBXXYYZZBB q3
13
Example 3
• L={anb2n:n>=0}
δ(q0, a) = (q1, X, R)
δ(q1, a) = (q1, a, R)
δ(q1, b) = (q2, Y, R)
δ(q2, b) = (q3, Y, L)
δ(q3, Y) = (q3, Y, L)
δ(q3, a) = (q3, a, L)
δ(q3, X) = (q0, X, R)
δ(q1, Y) = (q1, Y, R)
δ(q0, Y) = (q4, Y, R)
δ(q4, Y) = (q4, Y, R)
a b X Y B
δ(q4, B) = (q5, B, L)
q0 (q1, X, R) (q4, Y, R)
q5 is the final state q1 (q1, a, R) (q2, Y, R) (q1, Y, R)
q2 (q3, Y, L)
q3 (q3, a, L) (q0, X, R) (q3, Y, L)
q4 (q4, Y, R) (q5, B, L) 14
Example 4
• L={wwR : w Є {a,b}*}

δ(q0, a) = (q1, B, R) δ(q0, b) = (q4, B, R)


δ(q1, a) = (q1, a, R) δ(q4, a) = (q4, a, R)
δ(q1, b) = (q1, b, R) δ(q4, b) = (q4, b, R)
δ(q1, B) = (q2, B, L) δ(q4, B) = (q5, B, L)
δ(q2, a) = (q3, B, L) δ(q5, b) = (q6, B, L) BBabbaBB q0

δ(q6, a) = (q6, a, L)
BBBbbaBB q1
δ(q3, a) = (q3, a, L)
BBBBBBBB q6
BBBbbaBB q1 BBBBBBBB q0
δ(q3, b) = (q3, b, L) δ(q6, b) = (q6, b, L) BBBabaBB
BBBabaBB
q1
q1
BBBBBBBB q7 Final state
BBBabaBB q2
BBBbbBBB q3
δ(q3, B) = (q0, B, R) δ(q6, B) = (q0, B, R) BBBbbBBB q3
BBBbbBBB q3
BBBbbBBB q0

δ(q0, B) = (q7, B, R) //accept


BBBBbBBB q4
BBBBbBBB q4
BBBBbBBB q5
15
Example 5 a b B

• Generate a TM for L(aba*b) q0 (q1, a, R)


q1 (q2, b, R)
δ(q0, a) = (q1, a, R) q2 (q2, a, R) (q3, b, R)

δ(q1, b) = (q2, b, R) q3 (q4, B, L)

δ(q2, a) = (q2, a, R)
DFA
δ(q2, b) = (q3, b, R)
δ(q3, B) = (q4, B, L)

q4 is the final state of TM

If you are able to draw a DFA then using DFA directly convert to a TM.
Add one extra transition for B. Can ignore the trap states while converting.
16
Example 6
• Generate a TM that recognizes the set of all strings on Σ = {a, b}
starting with the prefix ab.
a b B
q0 (q1, a, R)
q1 (q2, b, R)
q2 (q2, a, R) (q2, b, R) (q3, B, L)

δ(q0, a) = (q1, a, R)
δ(q1, b) = (q2, b, R)
δ(q2, a) = (q2, a, R)
δ(q2, b) = (q2, b, R)
q3 is the final state of TM δ(q2, B) = (q3, B, L) //accept
17
Example 7
• Find the language accepted by the TM given below.
M=({q0, q1, q2, q3, q4}, {a, b}, {a, b, B}, , q0, B, {q3})
δ(q0, a) = (q1, a, R)
δ(q0, b) = (q2, b, R)
δ(q1, b) = (q1, b, R)
δ(q1, B) = (q3, B, R)
δ(q2, b) = (q2, b, R)
δ(q2, a) = (q3, a, R)

L={abn : n>=0} ᴜ {bbma : m>=0}


or
L((ab*) ᴜ (bb*a)) 18
Example 8
• Find the language accepted by the TM
δ(q0, a) = (q1, a, R)
δ(q0, b) = (q1, b, R)
δ(q1, a) = (q0, a, R)
δ(q1, b) = (q0, b, R)
δ(q1, B) = (q2, B, R)

L= { w Є ∑* , |w| is odd} on ∑ = {a, b}


19
Example 9

Assume q2 as the
final state

…BB1111BB…

…BB00BB…

20
Example 10

L={w : w Є {0,1}*}

21
Variations of TMs
• Turing Machines with
• Stay-Option
• Semi-Infinite Tape
• Off-Line
• Multi-tape
• Multidimensional
• Nondeterministic

22
Turing Machines with Stay-Option
The head can stay in the same position

L,R,S: possible head moves

 aababbcac a
Left, Right, Stay

δ : Q × Γ → Q × Γ × {L, R, S}

23
TM with Semi-Infinite Tape
The head extends infinitely only to the right
.........
a b a c  

• Initial position is the leftmost cell

• When the head moves left from the border,


it returns to the same position

24
Offline TM
Input File read-only (once)
a b c
Input string
Input string Appears on
input file only
Control Unit

(state machine) δ : Q × ∑ x Γ → Q × Γ × {L, R, S}

Tape read-write
  g d e   25
Multi-track TM
A useful trick to perform more
complicated simulations

One Tape

  a b a b  track 1

  b a c d  track 2

One head
One symbol (a, b)
26
Multi-tape TMs
Control unit
(state machine)

Tape 1 Tape 2
 a b c   e f g 
Input string

27
Multi-dimensional TM
2-dimensional tape y


 c a x
 b

MOVES: L,R,U,D HEAD


U: up D: down Position: +2, -1 28
Non-deterministic TM
• If a Turing machine has transitions specified as the following, it
is non-deterministic

29
Linear Bound Automata (LBA)
• A linear bounded automaton is a nondeterministic Turing machine M = (Q,Σ, Γ, δ,
q0, F), subject to the restriction that Σ must contain two special symbols [ and ],
such that δ (qi, [) can contain only elements of the form (qj , [, R), and δ (qi, ]) can
contain only elements of the form (qj , ], L).

• Example: L = {anbncn : n ≥ 1}
[ e f g ]

30
The language L = {ww | w ∈ {a, b}}. The logic for solving
this problem can be divided into 2 parts:
1. Finding the mid point of the string
Exercises 2. After we have found the mid point we match the
symbols.
First find the mid point by changing a/b to X/Y and
1. Obtain a TM for L = {ww : w ∈ {a,b}*} traverse till the end and do the same for the last character.
Do one step at a time until the mid point is reached. Once
2. L={w1aw2 : w1, w2 ∈ {a, b}*} mid point is reached, change left side symbols back to a/b
3. L={ambncmdn: m, n>=0} accordingly. Now, match left half with right half.
4. L={anbm: n=m+2, n,m>=0}
5. What language is accepted by the Turing machine whose transition graph is in the figure
below?

31

You might also like