DLC CSC 351 Course Material-2020-Ayorinde
DLC CSC 351 Course Material-2020-Ayorinde
CSC 351
(Formal Languages and Automata Theory)
By
Ibiyinka Temilola AYORINDE (PhD)
Department of Computer Science
University of Ibadan
Ibadan, Nigeria.
FORMAL LANGUAGES AND AUTOMATA THEORY
General Introduction and Course Objective
The objective of this course is to introduce undergraduate students to the theoretical aspects of
computer science, and helps them define infinite languages in finite ways, construct algorithms
for related problems and decide whether a string is in a language or not. In this course, students
will learn how mathematical models like the finite automaton and context-free grammar play
critical roles in several applied arears of computer science. While the finite automaton is used in
text processing, compilers and hardware design, the context-free grammar is used in
programming languages and artificial intelligence.
Table of Content
Study Session 1: Introduction
1.1 Formal Language
1.2 Words/Strings
1.3 Languages
Study Session 2: Regular Expressions (REs) and Regular Languages (RLs)
2.1 Regular Expressions - (REs)
2.2 Regular Languages - (RLs)
2.3 Building Regular Expressions
2.4 Algebraic Laws of RE
Study Session 3: Finite Automata (FA)
3.1 Finite Automata
3.2 Formal Definition of a Finite Automata
Study Session 4: Designing Finite Automata
4.1 Formulate the Idea to Use to Design Automaton
4.2 Design an Automaton
Study Session 5: Nondeterministic Finite Automata (NFA)
5.1 Basic Concept of NFA
5.2 Computation with NFA
5.3 Formal Definition of NFA
5.4 Epsilon NFA (ε-NFA)
5.5 Equivalence of DFA and ε-NFA
Study Session 6: Regular Expressions (REs) Versus Finite Automata s(FA)
6.1 Definitions
6.2 Interchangeability of FA and RE
6.3 The Pumping Lemma for RLs
6.4 Closure Properties of RLs
Study Session 7: Decision Properties of RLs
7.1 Decidability Problems
7.2 Equivalence and Minimization of DFAs
Study Session 8: The Chomsky Hierarchy
8.1 Relationship Between the different Hierarchies
8.2 Closure Properties Under Union, Intersection and Complement
Study Session 9: Context Free Languages (CFLs) and Context Free Grammars (CFGs)
9.1 Overview
9.2 Creating A String From a CFG
9.3 Formal Definition of a CFG
9.4 Syntactic Expressions in Programming Languages
9.5 String Membership
9.6 Leftmost and Rightmost Derivation Styles
9.7 Parse Trees
9.8 Ambiguity in CFGs
9.9 Applications of CFGs
9.10 Closure Properties of CFL
Study Session 10: Normal Forms
10.1 Chomsky Normal Form (CNF)
10.2 Simplification of CFG
10.3 Algorithms to Convert CFG into CNF
10.4 Greibach Normal Form (GNF)
10.5 Pumping Lemma for CFG
sStudy Session 11: Pushdown Automata (PDA)
11.1 Basic Structure of PDA
11.2 Pushdown Automata Acceptance
11.3 PDA and CFG
Study Session 1: Introduction
Expected Duration: 1 Week
Introduction
This chapter discusses several important concepts including definition of a formal language, sets,
symbols, strings and languages.
1.2 Words/Strings
A word (or string) is a finite sequence of items, so called symbols or letters chosen from a
specified finite set called the alphabet. E.g. Common alphabets (a – z), interword space,
punctuation marks and the bits 0 and 1. A word of length one is identified with its only symbol.
A special word is the empty word or null word having no symbols, denoted by λ or ε. In this
course, we will choose the notation ε o represent empty word. The length of the word, w, is the
number of symbols in it, denoted by |w|. The length of the empty word is 0. If there are k
symbols in the alphabet, then there are kn words of length n. Thus there are
n
∑Ki = Kn+1 - 1
i=0 K-1
words of length at most n, if k > 1, and n + 1 words, if k = 1. The set of all words is denumerably
infinite, that is, they can be given as an infinite list, say, by ordering the words first according to
their length.
The basic operation of words is concatenation, that is, writing words as a compound. The
concatenation of the words w1 and w2 is denoted simply by w1w2. Examples of
concatenations in the alphabet {x, y, z} is shown below:
Let w1 = xxzyyx and, w2 = zxxz. Then, w1w2 = xxzyyxzxxz
Let w1 = xxzyyx and w2 = ε. Then, w1w2 = w1 = xxzyyx
Let w1 = ε and, w2 = zxxz. Then, w1w2 = w2 = zxxz
Concatenation is associative in nature i.e., w1(w2w3) = (w1w2)w3. As a con sequence of this,
repeated concatenations can be written without parentheses. On the other hand, concatenation is
usually not commutative, As a rule, w1w2 ≠ w2w1, but not always, and in the case of a unary
alphabet(i.e where we have a single word, like only w1), concatenation is obviously
commutative.
The nth (concatenation) power of the word w is
wn = ww…w (n copies)
Especially w1 = w and w0 = ε. Also, ε n = ε.
The mirror image (or reversal) of the word w = x1x2 · · · xn is the word
wR = xn · · · x2x1,
The reversal of ε , denoted as ε R = ε. Also, we have the reversal of (w1w2)R = (w2)R(w1)R.
A word u is a prefix of the word w, if w = uv.
A word u is a subword or segment of the word w, if w = v1uv2 for some words v1 and v2.
A word u is a scattered subword of the word w, if w = w1u1w2u2 · · ·wnunwn+1
where u = u1u2 · · · un, for some n and some words w1,w2, . . . ,wn+1 and u1u2 · · · un.
1.3 Languages
A language is a collection of sentences of finite length, all constructed from a finite alphabet of
symbols. It can also be defined as a set of sentences. A language is also a set of words over some
alphabets. Examples of languages are:
1. Finite Languages: These languages have only a finite number of words.
2. Cofinite Languages: These are languages that have only a finite number of words missing
3. Empty Language: This is denoted as ∅. It has no words.
4. A Singleton Language {w}: This is identified with its only word w, and the language is
denoted simply by w.
Set-theoretic notations are used for languages. Examples of such notations or symbols are:
1. ⊆ (inclusion or subset)
2. ⊂ (proper inclusion or proper subset)
3. ∪ (union)
4. ∩ (intersection)
5. − (difference) and
6. ′ or ‾ (complement against the set of all words over the alphabet).
Membership: Belonging of a word w in the language L is denoted by w ∈ L.
In formal language theory, defining languages and investigating languages via their definitions is
paramount. There are many other operations of languages in addition to the set-theoretic ones
above. The concatenation of the languages L1 and L2 is given as:
L1L2 = {w1w2 | w1 ∈ L1 and w2 ∈ L2}.
The nth (concatenation) power of the language L is:
Ln = { w1w2 · · ·wn | w1, w2, . . . , wn ∈ L},
Note the following:
i. L1 = L
ii. L0 = {ε}.
iii. ∅0 = {ε}
iv. The concatenation closure or Kleenean star of the language L is
g
L* is the set obtained by concatenating words of L in all possible ways, including the empty
string ε. Similarly, L+ is the set obtained by concatenating words of L in all possible ways,
excluding the empty string. Hence, ∅∗ = {ε}, but ∅+ = ∅. Note that: L+ = L*L = LL*
Example 1.1
Let L = {1, 00}
L0 = {ε}
L1 = {1, 00}
L2 = {1, 00}{1 , 00} = {11,100,001,0000}
L3 = {1, 00}{11,100,001,0000}
= {111, 1100, 1001, 10000, 000000, 00001, 00100, 0011}
L* = L0 ∪ L1 ∪ L2 ∪ L3 ∪ L4 ∪…
L+ = L1 ∪ L2 ∪ L3 ∪ L4 ∪…
The left and right quotients of the languages L1 and L2 are defined as
L1\L2 = {w2 | w1w2 ∈ L2 for some word w1 ∈ L1}
(remove from words of L2 prefixes belonging in L1 in all possible ways) and
L1/L2 = {w1 | w1w2 ∈ L1 for some word w2 ∈ L2}
(remove from words of L1 suffixes belonging in L2 in all possible ways). Note that the
prefix or the suffix can be empty. The mirror image (or reversal) of the language L is the
language L′ = { w′ | w ∈ L}.
References
1. Sipser M. (2006). Introduction to the Theory of Computation. Second Edition. Thomson
Course Technology. ISBN: 0-534-95097-3
2. Jaeger H. (2011). Formal Languages and Logics – Lecture Notes. Jacobs University
Bremen. Fall 2011
Study Session 2: REGULAR EXPRESSIONS (REs) AND REGULAR LANGUAGES
(RLs)
Expected Duration: 1 Week
Introduction
This chapter treats one of the machines that can be used to define languages. Students will learn
how regular expressions can be used to define regular languages. The knowledge of regular
expressions is essential as they are are useful tools to know because most programming
languages are written as plain text and REs are used in search engines, search and replace dialogs
of word processors and text editors, in text processing utilities like analysis.
These operations are combined according to a set of rules, adding parentheses ( and ) when
necessary. The atoms of the formula are symbols of the alphabet, the empty language ∅, the
empty word ε and the braces { } indicating sets. Example of the three language operators
described above is given as follows:
Example 2.1
Example 2.2
Kleen closure (the * operator) of a given language L:
1. L0 = {ε}
2. L1 = {w| w ∈ L}
3. L2 = {w1w2 | w1 ∈ L, w2 ∈ L} – duplicates allowed.
4. Li = {w1w2wi | all w’s chosen are ∈ L} – (duplicates allowed) where i refers
to how many strings to concatenate from the parent language L to
produce strings in the language Li. Note that the choice of each wi is
independent.
Example 2.3
Example 2.4
1. ((R*)*)* = R*
2. (R+S)* = R* + S*
3. (RS + R*)RS = RSRS +R*RS
References
1. Jaeger H. (2011). Formal Languages and Logics – Lecture Notes. Jacobs University
Bremen. Fall 2011
2. Sipser M. (2006). Introduction to the Theory of Computation. Second Edition. Thomson
Course Technology. ISBN: 0-534-95097-3
3. softwareengineering.stackexchange.com › questions › is-it.
4. en.wikipedia.org › wiki › Regular_expression
Study Session 3: Finite Automata (FA)
Introduction
Abstraction and modelling play a vital role in Computer Science. Computer programs are
designed to replace manual procedures. Hence, computer programs are models of these manual
procedures. Finite automata are good computational models for computer with an extremely
limited memory. One may wonder what such memories could do but they have been very useful
in the design of controller for the automatic door, the timer of a washing machine and the on and
off switches of bulbs among others. Automata are used to recognize the words/strings of a
language. An automaton processes a word and afterwards decides whether or not the word is in
the language.
It is probably the simplest and most widely used computer science model,. Intuitively, a FA is a
system that can enter a finite set of different states. As a consequence of some input, which may
range over a finite setoff possible values, the FA operates a transition from one state to another.
A graph provides an immediate representation of a FA. Nodes denote states and edges denote
transitions. For example, the graph shown in Figure 3.1 represents an automaton that models the
behavior of a light switch.
`3.1.1 Modelling the Behavior of a light switch with State Transitions
As earlier on said, nodes denote states and edges denote transitions.
Input Signals
States Turn knob left Turn knob right
OFF ON OFF
ON ON OFF
The controller has two states: "OPEN" or "CLOSED," representing the corresponding condition
of the door. As shown Figures 3.3 and 3.4 respectively, there are four possible input conditions:
"FRONT" (meaning that a person is standing on the pad in front of the doorway), "REAR"
(meaning that a person is standing on the pad to the rear of the doorway), "BOTH" (meaning that
people are standing on both pads), and "NEITHER" (meaning that no one is standing on either
pad).
The controller moves from one state to another, depending on the input it receives. When in the
CLOSED state and receiving input NEITHER or REAR, it remains in the CLOSED state. In
addition, if the input BOTH is received, it stays CLOSED because opening the door risks
knocking someone over on the rear pad. But if the input FRONT arrives, it moves to the OPEN
state. In the OPEN state, if input FRONT, REAR, or BOTH is received, it remains in OPEN. If
input NEITHER arrives, it returns to CLOSED.
The automatic door controller as a finite automaton suggests standard ways of representation.
This controller is a computer that has just a single bit of memory, capable of recording which of
the two states the controller is in. Other common devices have controllers with somewhat larger
memories. For instance, in an elevator controller a state may represent the floor the elevator is on
and the inputs might be the signals received from the buttons. This computer might need several
bits to keep track of this information. Controllers for various household appliances such as
dishwashers and electronic thermostats, as well as parts of digital watches and calculators, are
additional examples of computers with limited memories. The design of such devices requires
keeping the methodology and terminology of finite automata in mind.
We will now take a closer look at finite automata from a mathematical perspective. We will
develop a precise definition of a finite automaton, terminology for describing and manipulating
finite automata, and theoretical results that describe their power and limitations. Besides giving
you a clearer understanding of what finite automata are and what they can and cannot do, this
theoretical development will allow you to practice and become more comfortable with
mathematical definitions, theorems, and proofs in a relatively simple setting.
When this automaton receives an input string such as 1101, it processes that string and produces
an output. The output is either accept or reject. We will consider only this yes/no type of output
for now to keep things simple. The processing begins in T1's start state. The automaton receives
the symbols from the input string one by one from left to right. After reading each symbol, T1
moves from one state to another along the transition that has that symbol as its label. When it
reads the last symbol, T1 produces its output. The output is accept if T1 is now in an accept state
and reject if it is not. For example, when we feed the input string 1101 to the machine M1 in
Figure 1.4, the processing proceeds as follows.
1. Start in state ql.
2. Read 1, follow transition from qi to q2.
3. Read 1, follow transition from q2 to q2.
4. Read 0, follow transition from q2 to q3.
5. Read 1, follow transition from q3 to q2-
6. Accept because Ml is in an accept state q2 at the end of the input.
Experimenting with this machine on a variety of input strings reveals that it
accepts the strings 1, 01, 1 1, 101, and 0101010101 among others. In fact, Ml accepts any string
that ends with a 1, as it goes to its accept state q2 whenever it reads the symbol 1. In addition, it
accepts strings 100, 0100, 1100, 110000, and 0101000000, and any string that ends with an even
number of 0s following the last 1. It rejects other strings, such as 0, 10, 101000. With this
understanding in mind, the language of all strings that T1 accepts can be described. This will be
shown in the next section where we will give a formal definition of the automaton T1.
The formal definition says that a finite automaton is a list of five objects that are active in the
automaton: (i) set of states, (ii) input alphabet, (iii) rules for moving, (iv) start state, and (v)
accept states. In mathematical language a list of five elements is often called a 5-tuple. Hence we
define a finite automaton to be a 5-tuple consisting of these five parts. We use something called a
transition function, frequently denoted as δ, to define the rules for moving. If the finite
automaton has an arrow from a state x to a state y labeled with the input symbol 1, that means
that, if the automaton is in state x when it reads a 1, it then moves to state y. We can indicate the
same thing with the transition function by saying that δ(x, 1) = y. This notation is a kind of
mathematical shorthand. Putting it all together we arrive at the formal definition of finite
automata as follows:
A finite automaton is a 5-turple (Q, ∑, δ, q0, F) where
1. Q is a finite set called states
2. ∑ is a finite set called alphabet
3. δ: Q x ∑ → Q is the transition function
4. q0 ∈ Q is the start state and
5. F ⊆ Q is the set of accept states.
Now, we can use the above formal definition to define the finite automaton T1 shown in Figure
3.5which is also repeated below:
0 1
q1 q1 q2
q2 q3 q2
q3 q2 q2
4. q1 is the start state and
5. F = {q2}
If A is the set of all strings that machine T accepts, we say that A is the language of machine T
and it is written as L(T) = A. We say that T recognizes A or that T accepts A. Because the term
accept has different meanings when we refer to machines accepting strings and machines
accepting languages, we prefer the term recognize for languages in order to avoid confusion. A
machine may accept several strings, but it always recognizes only one language. If the machine
accepts no strings, it still recognizes one language namely, the empty language ∅. In our
example, let
A = {w| w contains at least one 1 and an even number of 0s follow the last 1}.
Then, L(T1) = A, or equivalently, T1 recognizes A.
0 1
q1 q1 q2
q2 q1 q2
4. q1 is the start state and
5. F = {q1}
Looking critically at T2, the start state is also the accept state. Hence, it can accept the empty
string ε. In addition to the empty string, the machine accepts any string that ends with 0. Hence,
its language can be described as follows:
L(T2) = {w | w is the empty string ε or ends with 0}.
3.2.2 Example 3.
Figure 3.7 shows a five-state finite automaton T3. Give the definition of the automaton and
describe its language.
a b
S q1 r1
q1 q1 q2
q2 q1 q2
r1 r2 r1
r2 r2 r1
4. S is the start state and
5. F = {q1, r1}
Machine T3 has two accept states, q1 and r1 , and operates over the alphabet = {a, b}. Some
experimentation shows that it accepts strings a, b, aa, bb, and bab, but not strings ab, ba, or bbba.
This machine begins in state S, and after it reads the first symbol in the input, it goes either left
into the q states or right into the r states. In both cases it can never return to the start state (in
contrast to the previous examples), as it has no way to get from any other state back to s. If the
first symbol in the input string is a, then it goes left and accepts when the string ends with an a.
Similarly, if the first symbol is a b, the machine goes right and accepts when the string ends in b.
So T3 accepts all strings that start and end with a or that start and end with b. In other words, T3
accepts strings that start and end with the same symbol.
Automaton F1.
2. Answer the following questions in relation to the finite automaton F2 shown below:
Automaton F2
a. What is the start state?
b. What is the set of accept states?
c. What sequences of states does the machine go through on input abb?
d. Does the machine accept aaabb?
e. Does the machine accept the string ε?
3. The formal description of a DFA, F3 is ({q1, q2, q3, q4, q5}, {c, d}, δ, q3, {q3}), where
δ is given by the following table. Give the state diagram of the machine.
c d
q1 q1 q2
q2 q1 q3
q3 q2 q4
q4 q3 q5
q5 q4 q5
References
1. Sipser M. (2006). Introduction to the Theory of Computation. Second Edition. Thomson
Course Technology. ISBN: 0-534-95097-3
2. Xavier S. P. U. (2005). Theory of Automata, Formal Languages and Computation.
Published by New Age International (P) Ltd., Publishers. ISBN (10): 81-224-2334-5.
ISBN (13): 978-81-224-2334-1
3. Mandrioli, D. and Ghezzi, C. (1987). Theoretical Foundations of Computer Science. John
Wiley and Sons, Inc.
4. Ayorinde I. T. (2014). Formal Languages and Automata Theory - Lecture Notes.
Study Session 4: Designing an Automaton
Expected Duration: 1 Week
Introduction
The design of an automaton is a creative process. Hence, it cannot be reduced to a simple recipe
or formula. However, you might find a particular approach helpful when designing various types
of automata. That is, put yourself in the place of the machine you are trying to design and then
see how you would go about performing the machine's task. Pretending that you are the machine
is a psychological trick that helps engage your whole mind in the design process.
References
1. Sipser M. (2006). Introduction to the Theory of Computation. Second Edition. Thomson
Course Technology. ISBN: 0-534-95097-3
2. Ayorinde I. T. (2014). Formal Languages and Automata Theory - Lecture Notes.
Study Session 5: Nondeterministic Finite Automata (NFA)
Expected Duration: 1 Week
Introduction
Unlike deterministic finite automata (DFA), where the next state is known or determined, the
next state is not totally known or determined in nondeterministic finite automata (NFA).
Nondeterminism is a generalization of determinism, so every deterministic finite automaton is
automatically a nondeterministic finite automaton.
Finally, if any one of these copies of the machine is in an accept state at the end of the input, the
NFA accepts the input string. If a state with an ε symbol on an exiting arrow is encountered,
something similar happens. Without reading any input, the machine splits into multiple copies,
one following each of the exiting ε-labeled arrows and one staying at the current state. Then the
machine proceeds nondeterministically as before.
Nondeterministic computation can be viewed is as a tree of possibilities. The root of the tree
corresponds to the start of the computation. Every branching point in the tree corresponds to a
point in the computation at which the machine has multiple choices. The machine accepts if at
least one of the computation branches ends in an accept state, as shown in Figure 5.2.
When the fifth symbol 1 is read, the fingers on q1 and q3 result in fingers on states q1, q2, q3,
and q4, as you saw with the fourth symbol. The finger on state q2 is removed. The finger that
was on q4 stays on q4. Now you have two fingers on q4, so remove one, because you only need
to remember that q4 is a possible state at this point, not that it is possible for multiple reasons.
When the sixth and final symbol 0 is read, keep the finger on qi in place, move the one on q2 to
q3, remove the one that was on q3, and leave the one on q4 in place. You are now at the end of
the string, and you accept if some finger is on an accept state. You have fingers on states q1, q3,
and q4, and as q4 is an accept state, R1 accepts this string.
What does R1 do on input 010? Start with a finger on q1. After reading the 0 you still have a
finger only on q1, but after the 1 there are fingers on q1, q2, and q3 (don't forget the ε arrow).
After the third symbol 0, remove the finger on q3, move the finger on q2 to q3, and leave the
finger on q1 where it is. At this point you are at the end of the input, and as no finger is on an
accept state, N, rejects this input. By continuing to experiment in this way, you will see that R1
accepts all strings that contain either 101 or 11 as a substring.
Nondeterministic finite automata are useful in several respects. As we will show, every NFA can
be converted into an equivalent DFA, and constructing NFAs is sometimes easier than directly
constructing DFAs. An NFA may be much smaller than its deterministic counterpart, or its
functioning may be easier to understand. Nondeterminism in finite automata is also a good
introduction to nondeterminism in more powerful computational models because finite automata
are especially easy to understand. Now we turn to several examples of NFAs.
Consider the state diagram of the NFA (R1 Repeated) in Figure 5.4 below. The formal definition
is given thus:
Figure 5.4:
The definition will now be:
R1 = (Q, ∑, δ, q1, F) where
1. Q = {q1, q2, q3, q4}
2. ∑ = {0,1}
3. δ is described as:
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
Using the above algorithm, we find its equivalent DFA. The state table of the DFA is shown in
below.
q δ(q,0) δ(q,1)
[d,e] [e] ∅
[e] ∅ ∅
[c, e] ∅ [b]
[c] ∅ [b]
DFA NFA
The transition from a state is to a single particular The transition from a state can be to multiple
next state for each input symbol. Hence it is next states for each input symbol. Hence it is
called deterministic. called non-deterministic.
Empty string transitions are not seen in DFA. NFA permits empty string transitions.
References
1. Sipser, M. (2006). Introduction to the Theory of Computation. Second Edition. Thomson
Course Technology. ISBN: 0-534-95097-3
2. Ayorinde, I. T. (2014). Formal Languages and Automata Theory - Lecture Notes.
3. www.quora.com › What-is-epsilon-NFA
4. https://www.javatpoint.com/automata-conversion-from-nfa-with-null-to-dfa
5.https://www.tutorialspoint.com/automata_theory/non_deterministic_finite_automaton.htm
Study Session 6: REGULAR EXPRESSIONS (REs) VERSUS FINITE AUTOMATA
(FA)
Expected Duration: 1 Week
Introduction
For any regular expression, one can always construct a corresponding automaton, and for
any finite automaton there is always a corresponding regular expression.
6.1 Definitions
1. A language is regular if and only if some regular expressions describe it.
2. A language is regular if we are able to construct one of the following: DFA, NFA or
ε-NFA.
3. A language is not regular if we can show that no FA ca be built for it. This can be shown
through the pumping lemma (discussed in session 6.3).
Figure 6.5: Building an NFA from the Regular Expression (ab ∪ a)*
Example 6.1
The language L = {0m1m | m ≥ 0} is not regular. Show this by the pumping lemma as follows:
Assume that L is regular. Let n be the "pumping lemma constant" assured by the pumping
lemma. Consider the word w = 0n1n. By the lemma, it can be split into w = xyz, with |xy| ≤ n, that
is, xy is made only from 0's. By the lemma, y is non-empty, say it is 0k, k > 0. Again by the
lemma, also 0n+k 1n ∈ L would follow, contradiction.
Example 6.2
Problem
Prove that L = {aibi | i ≥ 0} is not regular.
Solution
At first, we assume that L is regular and n is the number of states.
Let w = anbn. Thus |w| = 2n ≥ n.
By pumping lemma, let w = xyz, where |xy| ≤ n.
Let x = ap, y = aq, and z = arbn, where p + q + r = n, p ≠ 0, q ≠ 0, r ≠ 0. Thus |y| ≠ 0.
Let k = 2. Then xy2z = apa2qarbn.
Number of as = (p + 2q + r) = (p + q + r) + q = n + q
Hence, xy2z = an+q bn. Since q ≠ 0, xy2z is not of the form anbn.
Thus, xy2z is not in L. Hence L is not regular.
6.4 Closure Properties of Regular Languages (RLs)
If a set of regular languages are combined using an operator, then, the resulting language is also
regular. Note that this is different from Kleene closure. Hence, a regular language is closed under
the following operations:
1. Union, Intersection, Complement and Difference
2. Reversal
3. Kleene Closure
4. Concatenation
5. Homomorphism
6. Inverse Homomorphism
References
1. Sipser, M. (2006). Introduction to the Theory of Computation. Second Edition. Thomson
Course Technology. ISBN: 0-534-95097-3
2. Ayorinde, I. T. (2014). Formal Languages and Automata Theory - Lecture Notes.
3. www.quora.com › What-is-epsilon-NFA
4. Jaeger H. (2011). Formal Languages and Logics – Lecture Notes. Jacobs University
Bremen. Fall 2011
Study Session 7: DECISION PROPERTIES OF REGULAR LANGUAGES
Expected Duration: 2 Weeks
Introduction
Given two deterministic finite automata A and B, it is decidable whether they accept the same
language. As a consequence, using the closure properties, the following problems are also
decidable for arbitrarily given deterministic finite automata A and B, with accepted
languages LA and LB, respectively: Emptiness problem, Membership problem, finiteness problem
and equality problem.
Learning Outcomes for Study Session 7
At the end of this lecture, you should be able to:
1. Know whether or not a given DFA is finite, empty, a member of another language or
equal to another one.
2. Be able to determine any of these properties in a given DFA or two DFAs.
7.2.2 Minimization
Steps to minimize DFAs:
1. Remove all states unreachable from the start state
2. Identify and condense equivalent states into one.
DFA Minimization using Myphill-Nerode Theorem
Algorithm
Input − DFA
Output − Minimized DFA
Step 1 − Draw a table for all pairs of states (Qi, Qj) not necessarily connected directly [All are
unmarked initially]
Step 2 − Consider every state pair (Qi, Qj) in the DFA where Qi ∈ F and Qj ∉ F or vice versa
and mark them. [Here F is the set of final states]
Step 3 − Repeat this step until we cannot mark anymore states −
If there is an unmarked pair (Qi, Qj), mark it if the pair {δ (Qi, A), δ (Qi, A)} is marked for some
input alphabet.
Step 4 − Combine all the unmarked pair (Qi, Qj) and make them a single state in the reduced
DFA.
Example
Let us use Algorithm 2 to minimize the DFA shown below.
a b c d e f
a b c d e f
a
c ✔ ✔
d ✔ ✔
e ✔ ✔
f ✔ ✔ ✔
Step 3 − We will try to mark the state pairs, with green colored check mark, transitively. If we
input 1 to state ‘a’ and ‘f’, it will go to state ‘c’ and ‘f’ respectively. (c, f) is already marked,
hence we will mark pair (a, f). Now, we input 1 to state ‘b’ and ‘f’; it will go to state ‘d’ and ‘f’
respectively. (d, f) is already marked, hence we will mark pair (b, f).
a b c d e f
c ✔ ✔
d ✔ ✔
e ✔ ✔
f ✔ ✔ ✔ ✔ ✔
After step 3, we have got state combinations {a, b} {c, d} {c, e} {d, e} that are unmarked.
We can recombine {c, d} {c, e} {d, e} into {c, d, e}
Hence we got two combined states as − {a, b} and {c, d, e}
So the final minimized DFA will contain three states {f}, {a, b} and {c, d, e}
DFA Minimization using Equivalence Theorem
If X and Y are two states in a DFA, we can combine these two states into {X, Y} if they are not
distinguishable. Two states are distinguishable, if there is at least one string S, such that one of δ
(X, S) and δ (Y, S) is accepting and another is not accepting. Hence, a DFA is minimal if and
only if all the states are distinguishable.
Algorithm 3
Step 1 − All the states Q are divided in two partitions − final states and non-final states and
are denoted by P0. All the states in a partition are 0th equivalent. Take a counter k and initialize
it with 0.
Step 2 − Increment k by 1. For each partition in Pk, divide the states in Pk into two partitions if
they are k-distinguishable. Two states within this partition X and Y are k-distinguishable if
there is an input S such that δ(X, S) and δ(Y, S) are (k-1)-distinguishable.
Step 3 − If Pk ≠ Pk-1, repeat Step 2, otherwise go to Step 4.
Step 4 − Combine kth equivalent sets and make them the new states of the reduced DFA.
Example
Let us consider the following DFA −
q δ(q,0) δ(q,1)
a b c
b a d
c e f
d e f
e e f
f f f
References
1. Ruohonen K. (2009). Formal Languages.
2. Jaeger H. (2011). Formal Languages and Logics – Lecture Notes. Jacobs University
Bremen. Fall 2011
3. Ayorinde, I. T. (2014). Formal Languages and Automata Theory - Lecture Notes.
Study Session 8: THE CHOMSKY HIERARCHY
Expected Duration: 1 Week
Introduction
In this session, you will learn about the Chomsky hierarch, which describes a hierarchy of
languages, ranging from the very simple regular languages up through the very general
recursively enumerable languages. The hierarchy of languages can be described as a hierarchy of
languages (sets), a hierarchy of grammars or a hierarchy of machines.
Regular languages are a subclass of the context-free languages, which are a subclass of the
context-sensitive languages, which are included in the recursive languages, which are a subclass
of the recursively enumerable languages. Figure 8.1 shows this clearly.
References
1. Sipser M. (2006). Introduction to the Theory of Computation. Second Edition. Thomson
Course Technology. ISBN: 0-534-95097-3
2. Tinkham N. L. (2010). The Chomsky Hierarch – Section 12.7
3. https://www.javatpoint.com/automata-chomsky-hierarchy
Study Session 9: Context Free Languages (CFLs) and Context Free Grammars (CFGs)
Expected Duration: 3 Weeks
Introduction
Many formal languages of practical interest are not regular especially, programming languages.
Regular languages cannot "count" within words; hence, any (programming) language that needs
some counting/bookkeeping, e.g. for matching parentheses, cannot be regular. Regular languages
can be generalized such that they include a "counting" mechanism, arriving at the context-free
languages (CFLs). CFLs can be described by a generalization of DFAs, pushdown automata.
Context free languages support natural, recursive notation called “Context Free Grammars”.
9.1 Overview
Context-free grammars (CFGs) are used to describe context-free languages. A context-free
grammar is a set of recursive rules used to generate patterns of strings. A context-free grammar
can describe all regular languages and more, but they cannot describe all possible languages.
Context-free grammars are studied in fields of theoretical computer science, compiler design,
and linguistics. CFG’s are used to describe programming languages and parser programs
in compilers can be generated automatically from context-free grammars.
Context-free grammars can generate context-free languages. They do this by taking a set of
variables which are defined recursively, in terms of one another, by a set of production rules.
Context-free grammars are named as such because any of the production rules in the grammar
can be applied regardless of context—it does not depend on any other symbols that may or may
not be around a given symbol that is having a rule applied to it.
For comparison, a context-sensitive grammar can have production rules where both the left-
hand and right-hand sides may be surrounded by a context of terminal and nonterminal symbols.
Example 9.1
For example, grammar G1 generates the string 000#111. The sequence of substitutions to obtain
a string is called a derivation. A derivation of string 000#111 in grammar GI is:
A → 0A1 X4 → 00A11 X → 000A111 → 00B111 → 000#111
You may also represent the same information pictorially with a parse tree. An example of a
parse tree is shown in Figure 9.1
All strings generated in this way constitute the language of the grammar. We write L(G1) for the
language of grammar G1 . Some experimentation with the grammar G1 shows that the language
of grammar G1, represented as L(G1 ) is {On#1n | n ≥ 0}. Any language that can be generated
by some context-free grammar is called a context-free language (CFL). For convenience when
presenting a context-free grammar, we abbreviate several rules with the same left-hand variable,
such as A → 0A1 and A → B, into a single line A → 0A1 | B, using the symbol " | " as an "or."
Example 9.2
The following is a another example of a context-free grammar, called G2 , which describes a
fragment of the English language.
Grammar G2 has 10 variables (the capitalized grammatical terms written inside brackets); 27
terminals (the standard English alphabet plus a space character); and 18 rules. Strings in L(G2)
include
a boy sees
the boy sees a flower
a girl with a flower likes the boy
Each of these strings has a derivation in grammar G2 . The following is a derivation
of the first string on this list.
9.3 Formal Definition of a Context-Free Grammar
A Grammar G = (S, N, T, P) where
S - Is the Start symbol
N - Is a set of Non-terminal symbols
T - Is a set of Terminal symbols
P - Is a set of Productions
Note that an input string belongs to a CFL (i.e. is accepted) iff it can be generated by the CFG.
Example 9.3
Let w = 01110
And G3 is given as:
A → 0A0 | 1A1 | 0 | 1 | ε
G3 can be generated as follows:
A → 0A0
→ 01A10
→ 01110
Recursive Inference
A → 0A0
Derivation
→ 01A10
→ 01110
For rightmost derivation, always substitute rightmost variable as shown in example 9.5 below:
Example 9.5: Rightmost Derivation of the string: a*(ab+10)
E
E*E
E * (E)
E * (E + E)
E * (E + F)
E * (E + 1F)
E * (E + 10F)
E * (E + 10)
E * (F + 10)
E * (aF + 10)
E * (abF + 10)
E * (ab + 10)
F * (ab + 10)
aF * (ab + 10)
a * (ab + 10)
Note that for every leftmost derivation, there is a rightmost derivation. Every word generated by
a CFG has a leftmost and rightmost derivation. There can be words which have more than one
leftmost derivation (called ambiguity in CFG) or rightmost derivation, depending on the
grammar.
This grammar generates the string a+ax a ambiguously. The following figure
shows the two different parse trees.
Figure 9.5: The two parse trees for the string a+axa in grammar G
This grammar doesn't capture the usual precedence relations and so may group the + before the x
or vice versa. In contrast grammar G4 generates exactly the same language, but every generated
string has a unique parse tree. Hence G4 is unambiguous, whereas G5 is ambiguous. Now we
formalize the notion of ambiguity. When we say that a grammar generates a string ambiguously,
we mean that the string has two different parse trees, not two different derivations. Two
derivations may differ merely in the order in which they replace variables yet not in their overall
structure. To concentrate on structure we define a type of derivation that replaces variables in a
fixed order. A derivation of a string w in a grammar G is a leftmost derivation if at every step the
leftmost remaining variable is the one replaced.
References
1. Jaeger H. (2011). Formal Languages and Logics – Lecture Notes. Jacobs University
Bremen. Fall 2011
2. Moore K., Chumbley A. and Khim J. (). Ontext Free Grammars. Retrieved September 1,
2020 from https://brilliant.org/wiki/context-free-grammars/
3. Nelson, R. Context-Free Grammars. Retrieved June 11, 2016,
from https://www.cs.rochester.edu/~nelson/courses/csc_173/grammars/cfg.html
4. , J. Leftmostderivations. Retrieved June 14, 2016,
from https://en.wikipedia.org/wiki/File:Leftmostderivations_jaredwf.png
5. Sipser M. (2006). Introduction to the Theory of Computation. Second Edition. Thomson
Course Technology. ISBN: 0-534-95097-3
6. https://www.tutorialspoint.com/automata_theory/
Study Session 10: NORMAL FORMS
Expected Duration: 1 Week
Introduction
If all productions of the grammar could be expressed in the same form(s), then it becomes easier
to design algorithms that use the grammar. It also becomes easy to show proofs and properties.
Hence, expressing grammars in this form is called normal forms. Different kinds of normal
forms like Chomsky normal form and Greibach normal form will be treated in this session.
References
1. https://www.tutorialspoint.com/automata_theory/
Study Session 11: PUSHDOWN AUTOMATA (PDA)
Expected Duration: 1 Week
Introduction
Pushdown Automata is a finite automata with extra memory called stack which helps Pushdown
automata to recognize Context Free Languages. Pushdown automata are used in theories about
what can be computed by machines. They are more capable than finite-state machines but less
capable than Turing machines. Deterministic pushdown automata can recognize all deterministic
context-free languages while nondeterministic ones can recognize all context-free languages,
with the former often used in parser design. The term "pushdown" refers to the fact that
the stack can be regarded as being "pushed down" like a tray dispenser at a cafeteria, since the
operations never work on elements other than the top element
Example 11.1
Construct a PDA for language L = {0n1m2m3n | n>=1, m>=1}
Approach used in this PDA –
First 0’s are pushed into stack. Then 1’s are pushed into stack.
Then for every 2 as input a 1 is popped out of stack. If some 2’s are still left and top of stack is a 0
then string is not accepted by the PDA. Thereafter if 2’s are finished and top of stack is a 0 then for
every 3 as input equal number of 0’s are popped out of stack. If string is finished and stack is
empty then string is accepted by the PDA otherwise not accepted.
Step-1: On receiving 0 push it onto stack. On receiving 1, push it onto stack and goto next
state
Step-2: On receiving 1 push it onto stack. On receiving 2, pop 1 from stack and goto next
state
Step-3: On receiving 2 pop 1 from stack. If all the 1’s have been popped out of stack and
now receive 3 then pop a 0 from stack and goto next state
Step-4: On receiving 3 pop 0 from stack. If input is finished and stack is empty then goto
last state and string is accepted
Examples:
Input : 0 0 1 1 1 2 2 2 3 3
Result : ACCEPTED
Input : 0 0 0 1 1 2 2 2 3 3
Result : NOT ACCEPTED
Problem:
Construct a PDA for language L = {0n1m | n >= 1, m >= 1, m > n+2}
Approach used in this PDA –
First 0’s are pushed into stack.When 0’s are finished, two 1’s are ignored. Thereafter for every 1 as
input a 0 is popped out of stack. When stack is empty and still some 1’s are left then all of them are
ignored.
Step-1: On receiving 0 push it onto stack. On receiving 1, ignore it and goto next state
Step-2: On receiving 1, ignore it and goto next state
Step-3: On receiving 1, pop a 0 from top of stack and go to next state
Step-4: On receiving 1, pop a 0 from top of stack. If stack is empty, on receiving 1 ingore
it and goto next state
Step-5: On receiving 1 ignore it. If input is finished then goto last state
Examples:
Input : 0 0 0 1 1 1 1 1 1
Result : ACCEPTED
Input : 0 0 0 0 1 1 1 1
Result : NOT ACCEPTED
References
1. https://www.geeksforgeeks.org/construct-pushdown-automata-given-languages/
2. https://www.tutorialspoint.com/automata_theory/