Professional Documents
Culture Documents
Introduction To Automata and Languages: 19CSE214 - Theory of Computation
Introduction To Automata and Languages: 19CSE214 - Theory of Computation
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
CO2 Demonstrate the push down automata model for a given language
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, …
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
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
12-03-2022 9
A design problem
1 4
?
5
BATTERY
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:
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?
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:
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
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
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
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.
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
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.
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 …
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.
A = {x | x > 5} This is the set A that has all real numbers greater than 5.
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}
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}.
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|
12-03-2022 43
Languages
A language is a set of strings
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
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
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’
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:
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:
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}
…..
But, L0 = {ε}
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
12-03-2022 69
Examples
If L1 = {a, b, c}, L2 = {1, 2}, If L1UL2, L1ՈL2, L1-L2, L1*, L2’, L11.
12-03-2022 70
Examples
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, …….
}
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 }
<predicate> <verb>
<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”,
…
}
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
SaSb
SaSb | λ
Sλ
It generates:
S λ SaSb SaSb SaSb
ab aaSbb aaSbb
aabb aaaSbbb
aaabbb
SaSb SaSb|A
SaSb|b
Sb Ab
Sol: ???
S aSbb
OR S aSbb | b
Sb
S S a S |b Is this correct???? NO
S X a X
X bX | λ
S B AB A B A B
A a |λ
B bB|λ
S X a Xa X a X
X bX | aX | λ
Sol:
S AB
A aAB | λ OR S aSb | bA
B bB | b A bA | λ
Sol:
L = { λ, aaa, aaaaaa, aaaaaaaaa, ……. }
S aaaS | λ
Sol:
that is |w| mod 3 =1
|w| mod 3 =2
S aaaS | aa | a
L = { (ab)m, m > 0 }
f s t …….. t s f
S a S a | b S b | aa | bb
S aSb
aaSbb ;S->aSb Intermediate steps Sentential forms
aa λbb ;S-> λ
aabb Sentence
*
S => aabb Derived in 4 steps
(a + b)+
(a+b)*abb
set of strings of a’s and b’s starting with ba
ba(a+b)*
ab+ba+aa+bb
Set of integers
Real numbers
(ab)*(a+ λ) + (ba)*(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)*]
(aaa*bbb*)+(aa*bbbb*)+(aaaa*bb*)
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)
(λ+1+0+(0+1)*(00+10+11)) [OR]
(λ+1+0+(0+1)*(0+11)) [OR]
(01+10+0+1)*(10+0)*
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
(i.e.)
String Acceptance or Rejection
ẟ*(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
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
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
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
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 ≠ ∅}
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
12-03-2022 1
Thompson’s representation
• To represent input a
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
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})
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} - 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
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
• λ-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
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
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
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
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})
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} - 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
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
• λ-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
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
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
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
12-03-2022 1
Basic RE to DFA
• For regular expression “a”
a
X Y
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
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
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
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),
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
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
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)∗ ).
12-03-2022 6
Example 5
Construct right- and left-linear grammars for the language
L = {anbm : n ≥ 3, m ≥ 2} .
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?
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, ....
12-03-2022 24
Example 2
Prove that the language L = {wwR : w ∈{a,b}*} is not regular.
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
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 | λ
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
SX|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 | λ
Derivation Tree
E E
E + E E * E
a E * E c
E + E
b c Ambiguous grammar a b
Example:
EE+E|E*E|(E)|a|b|c Derive the string “a+b*c”.
Sol:
LMD:
LMD:
EE+E
EE*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:
SaSbA | λ
Aa Derive the string “aababa”.
Sol:
LMD: RMD:
SaSbA
SaSbA
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:
SAB|aaB
Aa | Aa
B b Derive the string “aab”.
Sol: RMD:
SaaB
RMD: aab [by B->b]
SAB
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:
SaSbS| bSaS | λ Derive the string “abab”.
Sol:
LMD:
LMD:
SaSbS
SaSbS 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
Bb
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:
EE+T|T
TT*F|F
F(E)|a|b|c
Derive (((a+b)*c))+a
SaSbS|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
3
Eliminate left recursion
• A -> Ab | Acd | x | y • A -> xA’ | yA’
A’ -> bA’ | cdA’ | λ
4
Eliminate left recursion
• A -> Aab | a | b • A -> aA’ | bA’
A’ -> abA’ | λ
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
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.
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
Xa
Yb
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
Xa Xa
Yb Yb
Zc
Zc
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 Xa
Yb
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
Aa
Bb
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
Aa S ABY | AA S aBY | aA
X AB X aB
Bb Y ABYB | AAB Y aBYB | aAB
Aa Aa
Bb Bb
23-10-2020 13
Example-4
S ABb | a
A aaA | B
B bAb
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
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
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
{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} 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
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
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:
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
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
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
{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
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 | ∈ Bb
Y AY | a B bbA b) S aSb | bSa | a | b
Aa
a) W = babaa
b) b) W = baaaa
23-10-2020 36
Push Down Automata (PDA)
1
NPDA - Definition
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
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
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, λ)
17
Example 10
• Find the language accepted by the NPDA
M=({q0, q1, q2, q3, q4, q5}, {a, b}, {1}, δ, q0, Z, {q4, q5}}
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
LCSG
LCFG
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 = {SXA, A AB|b, bBBbb, XBX, 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 = {SaSBC | abC, CBDB, DBDE, DEDC, DCBC, bBbb, Cc }
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 = {SaSb | ∈}
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 = {SaS | ∈}
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
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
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}
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}
q0q2 q1
q0
q2
q2 q1
6
Example 1
• Design a TM to accept the language L={anbn : n>=1}
q0q2 q1
q0
q2 q1
7
Example 1
• Design a TM to accept the language L={anbn : n>=1}
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}*}
δ(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
δ(q2, a) = (q2, a, R)
DFA
δ(q2, b) = (q3, b, R)
δ(q3, B) = (q4, B, L)
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)
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
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
24
Offline TM
Input File read-only (once)
a b c
Input string
Input string Appears on
input file only
Control Unit
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
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