0% found this document useful (0 votes)
928 views285 pages

Therotical Computer Science Book

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
928 views285 pages

Therotical Computer Science Book

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 285

A Book Of

T H E O R E T I C AL
C O M P U T E R SC I E N C E
For T.Y.B.Sc. Computer Science : Semester – V
[Course Code CS - 356 : Credits - 2]
CBCS Pattern
As Per New Syllabus, Effective from June 2021

Dr. Ms. Manisha Bharambe


M.Sc. (Comp. Sci.), M.Phil. Ph.D. (Comp. Sci.)
Vice Principal, Associate Professor, Department of Computer Science
MES's Abasaheb Garware College
Pune

Price ` 360.00

N5866
THEORETICAL COMPUTER SCIENCE ISBN 978-93-5451-193-6
Second Edition : September 2022
© : Author
The text of this publication, or any part thereof, should not be reproduced or transmitted in any form or stored in any
computer storage system or device for distribution including photocopy, recording, taping or information retrieval system or
reproduced on any disc, tape, perforated media or other information storage device etc., without the written permission of Author
with whom the rights are reserved. Breach of this condition is liable for legal action.
Every effort has been made to avoid errors or omissions in this publication. In spite of this, errors may have crept in. Any
mistake, error or discrepancy so noted and shall be brought to our notice shall be taken care of in the next edition. It is notified
that neither the publisher nor the author or seller shall be responsible for any damage or loss of action to any one, of any kind, in
any manner, there from. The reader must cross check all the facts and contents with original Government notification or
publications.

Published By : Polyplate Printed By :


NIRALI PRAKASHAN YOGIRAJ PRINTERS AND BINDERS
Abhyudaya Pragati, 1312, Shivaji Nagar, Survey No. 10/1A, Ghule Industrial Estate
Off J.M. Road, Pune – 411005 Nanded Gaon Road
Tel - (020) 25512336/37/39 Nanded, Pune - 411041
Email : [email protected]

DISTRIBUTION CENTRES
PUNE
Nirali Prakashan Nirali Prakashan
(For orders outside Pune) (For orders within Pune)
S. No. 28/27, Dhayari Narhe Road, Near Asian College 119, Budhwar Peth, Jogeshwari Mandir Lane
Pune 411041, Maharashtra Pune 411002, Maharashtra
Tel : (020) 24690204; Mobile : 9657703143 Tel : (020) 2445 2044; Mobile : 9657703145
Email : [email protected] Email : [email protected]
MUMBAI
Nirali Prakashan
Rasdhara Co-op. Hsg. Society Ltd., 'D' Wing Ground Floor, 385 S.V.P. Road
Girgaum, Mumbai 400004, Maharashtra
Mobile : 7045821020, Tel : (022) 2385 6339 / 2386 9976
Email : [email protected]

DISTRIBUTION BRANCHES
DELHI BENGALURU NAGPUR
Nirali Prakashan Nirali Prakashan Nirali Prakashan
Room No. 2 Ground Floor Maitri Ground Floor, Jaya Apartments, Above Maratha Mandir, Shop No. 3,
4575/15 Omkar Tower, Agarwal Road No. 99, 6th Cross, 6th Main, First Floor, Rani Jhanshi Square,
Darya Ganj, New Delhi 110002 Malleswaram, Bengaluru 560003 Sitabuldi Nagpur 440012 (MAH)
Mobile : 9555778814/9818561840 Karnataka; Mob : 9686821074 Tel : (0712) 254 7129
Email : [email protected] Email : [email protected] Email : [email protected]

KOLHAPUR JALGAON SOLAPUR


Nirali Prakashan Nirali Prakashan Nirali Prakashan
438/2, Bhosale Plaza, Ground Floor 34, V. V. Golani Market, Navi Peth, R-158/2, Avanti Nagar, Near Golden
Khasbag, Opp. Balgopal Talim Jalgaon 425001, Maharashtra Gate, Pune Naka Chowk
Kolhapur 416 012, Maharashtra Tel : (0257) 222 0395 Solapur 413001, Maharashtra
Mob : 9850046155 Mob : 94234 91860 Mobile 9890918687
Email : [email protected] Email : [email protected] Email : [email protected]

[email protected] | www.pragationline.com
Also find us on www.facebook.com/niralibooks
Preface …

I take an opportunity to present this Text Book on "Theoretical Computer Science" to


the students of Third Year B.Sc. (Computer Science) Semester-V as per the New Syllabus,
June 2021.

The book has its own unique features. It brings out the subject in a very simple and lucid
manner for easy and comprehensive understanding of the basic concepts. The book covers
theory of Finite Automaton, Regular Expressions and Languages, Context-free Grammars and
Languages, Pushdown Automata and Turing Machine.

A special word of thank to Shri. Dineshbhai Furia, and Mr. Jignesh Furia for
showing full faith in me to write this text book. I also thank to Mr. Amar Salunkhe and
Mr. Akbar Shaikh of M/s Nirali Prakashan for their excellent co-operation.

I also thank Ms. Chaitali Takle for Graphic Designing, Mr. Ravindra Walodare, Mr. Sachin
Shinde, Mr. Ashok Bodke, Mr. Moshin Sayyed and Mr. Nitin Thorat.

Although every care has been taken to check mistakes and misprints, any errors,
omission and suggestions from teachers and students for the improvement of this text book
shall be most welcome.

Author
Syllabus …
1. Finite Automation (10 Lectures)
• Introduction – Symbol, Alphabet, String, Prefix and Suffix of Strings, Formal Language,
Operations on Languages.
• Deterministic Finite Automaton – Definition, DFA as Language Recognizer, DFA as Pattern
Recognizer.
• Non-deterministic Finite Automaton – Definition and Examples.
• NFA To DFA (Myhill-Nerode Method)
• NFA with ∈ – Transitions Definition and Examples.
• NFA with ∈ – Transitions to DFA and Examples
• Finite Automaton with Output – Mealy and Moore Machine, Definition and Examples.
• Minimization of DFA, Algorithm and Problem using Table Method.
2. Regular Expressions and Languages (06 Lectures)
• Regular Expressions (RE) − Definition and Examples.
• Regular Expressions Identities.
• Regular Language − Definition and Examples.
• Conversion of RE to FA − Examples.
• Pumping Lemma for Regular Languages and Applications.
• Closure Properties of Regular Languages.
3. Context-Free Grammars and Languages (10 Lectures)
• Grammar − Definition and Examples.
• Derivation − Reduction − Definition and Examples.
• Chomsky Hierarchy.
• CFG − Definition and Examples. LMD, RMD, Parse Tree.
• Ambiguous Grammar – Concept and Examples.
• Simplification of CFG − Removing Useless Symbols, Unit Production, ∈-Production and
Nullable Symbol.
• Normal Forms – Greibach Normal Form (GNF) and Chomsky Normal Form (CNF).
• Regular Grammar – Definition.
• Left Linear and Right Linear Grammar − Definition and Example.
• Equivalence of FA and Regular Grammar.
• Construction of Regular Grammar Equivalent to a given DFA.
• Construction of a FA from the given Right Linear Grammar.
4. Pushdown Automata (05 Lectures)
• Definition of PDA and Examples.
• Construction of PDA using Empty Stack and Final State Method – Examples using Stack
Method.
• Definition DPDA and NPDA, their Correlation and Examples of NPDA.
• CFG (in GNF) to PDA – Method and Examples.
5. Turing Machine (05 Lectures)
• The Turing Machine Model, Definition and Design of TM.
• Problems on Language Recognizers.
• Language Accepted by TM.
• Types of Turing Machines (Multitrack TM, Two-way TM, Multitape TM, Non-deterministic
TM).
• Introduction to LBA (Basic Model) and CSG, (Without Problems).
Contents …

1. Finite Automation 1.1 – 1.102

2. Regular Expressions and Languages 2.1 – 2.36

3. Context-Free Grammars and Languages 3.1 – 3.74

4. Pushdown Automata 4.1 – 4.32

5. Turing Machine 5.1 – 5.32


CHAPTER
1

Finite Automaton
Objectives …
To study Basic Concepts in Theoretical Computer Science
To learn Finite Automata
To understand Deterministic Finite Automata
To learn Non-deterministic Finite Automaton

1.0 INTRODUCTION
• Theoretical computer science is a division of general computer science and mathematics. It
focuses on more mathematical and abstract aspects of computing.
• Theoretical computer science includes the theory of computation. In this chapter, we
introduce mathematical terms necessary for understanding the automata theory.

1.1 BASIC TERMS


• In this section we will study the basic terms used in theoretical computer science like
symbol, alphabet, string, prefix and suffix of strings, formal language, operations on
languages and so on.
Symbol:
• The symbol is the smallest building block in the theory of computation and can be any letter,
number or even pictograms.
• Symbol is an abstract or a user-defined entity. It cannot be formally defined (like "point" in
geometry).
• For example, digits (0, 1, 2….) and letters (a, b, c…).
• We can define many interesting things based on symbol. Using symbols, we can generate
strings.
Alphabet (ΣΣ):
• Alphabet is a finite set of symbols.
• Every element of an alphabet is called a symbol of alphabet Σ.
For examples: ΣD = {0, 1, 2, …, 9}
ΣR = {a, b, c, p, D, @}
Here, set R is containing heterogeneous (not to the same type) entities, though it is an
alphabet. The concept of alphabet is similar to the well-known alphabet set {a, b, c, …, z}
for the English language.
1.1
Theoretical Computer Science Finite Automaton

String: [April 16, Oct. 16, 17]


• A string is a finite ordered sequence of symbols from the alphabet.
• A string is defined as, a finite sequence of sequence of symbols from the alphabet Σ.
• For example, the a, b and c are symbols and abcb is a string.
• A string may have no symbols at all such string is called as empty string and is denoted
by ∈.
• The length of a string is number of symbols in the string. If string is ω; its length is denoted
as |ω| and |∈| = 0, (length of null string = 0).
Prefix and Suffix of Strings: [April 16, 18, 19, Oct. 16, 17, 18]
• A prefix of a string is any number of leading symbols of that string.
• For example, let x = abc the prefixes of x are ∈, a, ab, abc.
• The prefix other than the string itself is called as proper prefix. In the above example, ∈, a,
ab are proper prefixes of abc.
• A suffix of a string is any number of trailing symbols in it.
• For example, In above example, suffixes of x are ∈, c, bc, abc.
• The suffix other than the string itself is called a proper suffix. ∈, c, bc are proper suffixes of
abc.
• The concatenation of two strings is the string formed by writing the first followed by the
second with no intervening space.
• For example, if w and x are strings then 'ωx' is concatenation.
|ω x| = | ω | + |x|
• The length of the concatenated string is equal to the sum of the lengths of those strings. The
empty string is the identity for the concatenation operator. So,
∈ω=ω∈=ω
Formal Language: [April 19]
• Formal language is a set of strings of symbols from some alphabet.
• The empty set φ and the set consisting of empty string i.e. {∈} are also languages.
• Set of all strings over a fixed alphabet Σ is language denoted by Σ*.
For example,
1. Let Σ = {a} then
Σ* = {∈, a, aa, aaa, ……}
2. Let Σ = {0, 1} then
Σ* = {∈, 0, 1, 00, 01, 10, 11, 000, …}
3. Language over alphabet {a, b} having equal number of a's and b's.
Σ* = {∈, ab, ba, abab, baab, aabb, …}
• From the above language Σ*, if we exclude ∈, the empty word, the remaining set is a
language denoted by Σ+.
• For example, Σ+ = {0, 1, 00, 01, 10, 11, 000 …} for second example.
1.2
Theoretical Computer Science Finite Automaton

• The language we have defined above is the formal language. The word 'formal' refers to the
fact that all the rules for the language are explicitly stated in terms of what strings of symbols
can occur, and which are the valid sentences.
• Formal language will be considered as symbols on paper and not as expressions of ideas as in
Natural language like English. The rules are called as formal rules.
• The term formal used here emphasizes that it is the form of the string of symbols, we are
interested in, not the meaning.
Operations on Languages:
• In this point we will study various operations on languages.
Union:
• Union of languages L1 and L2 is the language (L) containing all strings of L1 and all strings
of L2.
L = L1 ∪ L2
Examples:
(i) If L1 = {a, b} and L2 = {c, d} then
L = L1 ∪ L2 = {a, b, c, d} (finite set)
(ii) If L1 = a* b and L2 = b* then
L = L1 ∪ L2
Here, L1 = {b, ab, aab, aaab …}
L2 = {∈, b, bb, bbb, … }
L = L1 ∪ L2 = {∈, b, ab, bb, aab, bbb, aaab …} (infinite set)
(iii) If L1 = {am bm | m ≥ 1} and L2 = {bm | m ≥ 0}
Here, L1 = {ab, aabb, aaabbb …}
and L2 = {∈, b, bb, bbb …} then
L = L1 ∪ L2 = containing strings having equal number of a's and b's and also
having strings of b*.
Intersection:
• Intersection of languages L1 and L2 is the language L containing common strings of L1 and
L2.
L = L1 ∩ L2
Examples:
(i) If L1 = {a, b} and L2 = {a, c} then
L = L1 ∩ L2 = {a}
(ii) If L1 = {aa, bb, aab} and L2 = {aa, aab} then
L = L1 ∩ L2
= {aa, aab}
(iii) If L1 = a* and L2 = b* then
L = L1 ∩ L2 = φ (No string common)
1.3
Theoretical Computer Science Finite Automaton

(iv) If L1 contains equal number of a's and b's over Σ = {a, b} and L2 = a* b* find
intersection of L1 and L2.
Here, L1 = EQUAL = {∈, ab, ba, aabb, abab abba, baab, baba, bbaa, aaabbb …}
and L2 = {∈, a, b, ab, aabb, aaabbb …}
∴ L1 ∩ L2 = {ab, aabb, aaabbb …}
L = L1 ∩ L2 = {an bn}
(v) If L1 containing all string starting with 'a' over {a, b} and L2 containing all strings ending
with 'a' over {a, b} then, L = L1 ∩ L2 = containing all string starting with 'a' and ending
with 'a' and also only 'a'.
∴ L = {a, aa, aba, abba, abbaa … }
Concatenation:
• The concatenation of languages L1 and L2 is the language L containing all strings of L1
followed by all strings of L2 without space.
L = L1 L2
Examples:
(i) L1 = {a}
L2 = {b}
L = L1 L2 = {ab}
(ii) L1 = {a, aa, aaa}
L2 = {bb, bbb}
Then, L = L1 L2 = {abb, abbb, aabb, aabbb, aaabb, aaabbb}
(iii) L1 = {a, bb, bab} and L2 = {a, ab}
L = L1 L2 = {aa, aab, bba, bbab, baba, babab}
(iv) L1 = {a, bb, bab} and L2 = {∈, bbbb}
L = L1 L2 = {a, bb, bab, abbbb, bbbbbb babbbbb}
(v) L1 = {∈, x} and L2 = y*
Here, L2 = {∈, y, yy, yyy, …}
Which, is infinite set.
L =L1 L2 = {∈, xy, xyy, xyyy, xyyyy, y, yy, yyy, …}

1.2 FINITE AUTOMATON [Oct. 17]


• Usually, in theoretical computer science, we consider the mathematical models of machines
which are the abstractions and simplifications of how certain actual machines do work.
• The word 'automaton’ (plural - 'automata') is derived from the Greek word 'automatos'
meaning 'self-acting'.
• Finite Automata or FA is considered as the mathematical model of finite state machine.
A Finite State Machine (FSM) is a mathematical model of computation.
1.4
Theoretical Computer Science Finite Automaton

• FA consists of a finite set of states and a set of transitions from state to state that occur on
input symbol chosen from an alphabet Σ.
• Computer itself can be viewed as a finite state system. The state of the central processor,
main memory and auxiliary storage at any time is one of a very large but fixed number of
states.
• Finite automaton is an abstract model (formally describe the behavior of the computer
system) of a digital computer which has three components namely, Input tape, Control unit
and Output.
• Using abstract model, the behavior of the actual system can be understood and build to
perform various activities.
• The pictorial/graphical presentation/block diagram of finite automata is shown in Fig. 1.1.
The FA contains:
o Input Tape: It is divided into number of cells/blocks/squares. Each input symbol is
placed in each cell. Each cell contains a single symbol from input alphabet Σ.
o Finite State Control: The finite automaton has some states one of which is the start
state designed as q0 and at least one final state. Apart from these it has some finite states
denoted by q1, q2, q3…qn. The tape reader reads the cells one by one from left to right,
and at a time only one input symbol is read. Based on the current input symbol, the state
can be change.
o Output: The output of FA may be accept or reject depending on the input. When end of
the input is encountered, the control unit may be in accept or reject state.
Input tape

Tape reader head reading


the input symbol

q0
q7 q1
Output
q6 q2 (accept/reject)
q5 q3
q4

Finite state control


Fig. 1.1: Pictorial Presentation of Finite Automaton (FA)
Working of Finite Automaton:
• Fig. 1.1 shows the working of the Finite Automation.
• The machine is assumed to be in start state q0.
• The input pointer points to the first cell of the tape pointing to the string to be processed.
• After scanning the current input symbol, the machine can enter into any of the states q0, q1,
q2…qn the input pointer automatically points to the next character by moving one cell towards
right.
• When the end of the string is encountered, the string is accepted if and only if the automation
will be in one of the final states. Otherwise, the string is rejected.
1.5
Theoretical Computer Science Finite Automaton

Example of Finite Automaton:


• Consider an electric switch which has only two states "OFF" and "ON". To start with, the
switch will be in OFF state. When we push the button, it goes to ON state. If we push once
again it goes to OFF state (See Fig. 1.2).
• Note that the circles represent the states and the labels associated with arcs represent the
input given to go to another state. The arrow with the label Start (not originating from any
state) is considered as the start state.
Push

Start
OFF ON

Push

Fig. 1.2: Example of Finite Automaton (FA)


• An automaton with a finite number of states is called a Finite Automaton (FA) or Finite State
Machine (FSM). Fig. 1.3 shows different forms/kinds of FSM.

Fig. 1.3
• Finite Automaton can be classified into two types namely, Deterministic Finite Automaton
(DFA) and Non-deterministic Finite Automaton (NFA).
• In DFA, for each input symbol, one can determine the state to which the machine will move.
Hence, it is called Deterministic Automaton.
• As it has a finite number of states, the machine is called Deterministic Finite
Machine or Deterministic Finite Automaton.
• In NFA, for a particular input symbol, the machine can move to any combination of the states
in the machine. In other words, the exact state to which the machine moves cannot be
determined. Hence, it is called Non-deterministic Automaton.
• As it has finite number of states, the machine is called Non-deterministic Finite
Machine or Non-deterministic Finite Automaton.
1.6
Theoretical Computer Science Finite Automaton

Symbols used in Finite Automaton:


• We use following notations/symbols for representing the FA:
Symbol Description
q0 A circle is used to represent a state of the machine (here, q0 is a state).

q0 A circle with an arrow which is not originating from any node


represents the start state of machine.
q0
Two circles are used to represent a final state of the machine (here, q0
is the final state).
An arrow with label 1 goes from state q0 to state q1 indicates there is a
q0
1 q1 transition (change of state) from state q0 on input symbol 1 to state q1.
This is represented as below:
δ(q0, 1) = q1
0 An arrow with label 0 starts and ends in q0 indicates the machine in
state q0 on reading a 0, remains in same state q0. This is represented as
q0 below:
δ(q0, 1) = q0
f The hang (or trap) state is represented by the symbol 'φ' within a
circle.
An arrow with label 0, 1 goes from state q0 to state q1 indicates that
the machine in state q0 on reading a 0 or a 1 enters into state q1. This
0,1
q0 q1 is represented as below:
δ(q0, 0) = q1
δ(q0, 1) = q1

1.3 DETERMINISTIC FINITE AUTOMATON [April 16, 18 Oct. 16, 17]


• A Deterministic Finite Automata (DFA) is a finite set of states and a set of transitions from
state to state that occur on input symbols chosen from an alphabet Σ. For each input symbol,
there is exactly one transition from each state.
• Definition of DFA: Deterministic Finite Automation (DFA) is a 5-tuple or quintuple
namely, Q, Σ, δ, q0, F), where
Q is a finite set of states.
Σ is a finite set of symbols called the alphabet.
δ is a transition function mapping Q × Σ to Q.
q0 is the initial state from where any input is processed (q0 ∈ Q).
F is a set of final state/states of Q (F ⊆ Q).
• The name DFA emerges from the following facts: [Oct. 18, April 19]
o D (Deterministic): There is exactly one transition for every input symbol from the state.
So, it is possible to determine exactly to which state the machine enters into after
consuming the input symbol. So, the machine is deterministic.
o F (Finite): Has finite number of states and arcs. So, it is deterministic and finite.
o A (Automation): Automation is a machine which may accept the string or reject the
string. So, it is deterministic finite automation.
1.7
Theoretical Computer Science Finite Automaton

• Following are the some common terms used in DFA:


1. Transitions:
• Transition is nothing but change of state after consuming an input symbol.
Example: Consider following DFA and observe the various transactions:
• From state q0 on input 0, the machine will stay
in q0.

0 0,1 • From state q0 on input 1, the machine will stay


in q1.
Start 1
q0 q1 accept • From state q1 on input 0, the machine will stay
in q1.
From state q1 on input 1, the machine will stay

in q1.
From the above diagram observe that, there is exactly one transition defined from a state on
an input symbol. Sometimes, there can be no transitions from a state as shown below:
• From state q0 on input 1, the machine enters
into a state q1.
Start 1
q0 q1
•But, from state q1, the transition is not defined
on any of the input symbol. We say there is a
zero transition from state q1.
Note that in a DFA there can be zero or one transition from a state on an input symbol and at
any point of time, the DFA will be one state.
2. Transition Equations: A finite automation can be represented with the help of transition
equations. For each transition there exists a transition equation. For example, consider a
finite automation M defined as follows:
M = ({q1, q2, q3}, {0, 1}, δ, {q3}) is a DFA, where δ is given by
δ(q1, 0) = q2 δ(q1, 1) = q1
δ(q2, 0) = q3 δ(q2, 1) = φ
δ(q3, 0) = q2 δ(q3, 1) = q3
Above finite automation is DFA as it follows the transition function Q × Σ → Q. This
represents that there are total three states in this DFA {q1, q2, q3} with q1 as initial or start
state and q3 as final state or accepting state, the option of input are {0, 1}.
o The transition equation δ(q1, 0) = q2, shows that there is a transition from state q1 to q2 on
input 0.
o The transition equation δ(q1, 1) = q1, shows that there is a transition from state q1 to q1 on
input 1.
o The transition equation δ(q2, 0) = q3, shows that there is a transition from state q2 to q3 on
input 0.
o The transition equation δ(q2, 1) = φ, shows that there is a transition from state q2 on
input 1.
1.8
Theoretical Computer Science Finite Automaton

o The transition equation δ(q3, 0) = q2, shows that there is a transition from state q3 to q2 on
input 0.
o The transition equation δ(q3, 1) = q3, shows that there is a transition from state q3 to q3 on
input 1.
3. Transition Table: A DFA can also be represented by a transition table. A transition table is
the tabular representation of the transition system of the automation. A transition table is also
known as transition function table or state table.
Example: The transition diagram and its equivalent transition table are shown below:
Columns
0 0,1 a d a
0 1
Start 1

Rows
q0 Start q0 q1
a
accept q1 accept qq00 q01 q1

Transition diagram **qq11 q1


q 1
q1

Transition diagram Transition table


Transition table
The transition diagram of DFA has
• • Represented using two rows q0 and q1.
two states q0 and q1.
• There are two input symbols 0 and • The two input symbols 0 and 1
1. correspond to two columns.
• Start state is represented using an • The start state is identified by putting
arrow mark and not originating an arrow with direction towards right.
from any state and labeled start.
• The final states are represented by • The final states are represented by
two circles. putting star (*) by the side of states.
4. Language of DFA:
• The language of DFA consist a set of all strings chosen from Σ* that all are accepted by
machine M.
5. State Transition Graph:
• A transition diagram or state transition graph or transition system is the graphical
representation of a DFA.
• A transition system or a transition graph is a finite labeled graph in which each vertex (or
node) represents a state and directed edge indicate the transition of a state and the edges are
labeled with Input/Output.
• Fig. 1.4 shows an example of transition diagram in DFA. In Fig. 1.4,
From initial state q0 and input 0, go to the next state q1.
From state q0 and input 1, go to the next state q0.
From state q1 and input 0, go to the next state q1.
From state q1 and input 1, go to the next state q2.
From state q2 and input 0, go to the next state q2.
From state q2 and input 1, go to the next state q2.
1.9
Theoretical Computer Science Finite Automaton

1 0 0,1
Start 0 1
q0 q1 q2

Fig. 1.4
6. Transition Function:
• A DFA may also represented by transition function δ. The transition function of the Fig. 1.4
is represented as follows:
δ(q0, 0) = q1
δ(q0, 1) = q0
δ(q1, 0) = q1
δ(q1, 1) = q2
δ(q2, 0) = q2
δ(q2, 1) = q2
• Here, the first argument of transition function δ represents the present state and second
argument represents the input letter and the right hand side represents the next states.
For example, in equation δ(q0, 0) = q1 where, q0 is a present state, 0 is an input alphabet and q1
is the next state
• The DFA accepts a string x if the sequence of transitions corresponding to the symbols of x
leads from start state to a final state. [Oct. 18]
• Finite control representation of DFA is shown in Fig. 1.5.

Fig. 1.5: Representation of DFA


• In DFA input tape is divided into squares (cells) containing input symbols from input
alphabet.
• A pointer or head points to the cell on the tape from which it is about to read the next
symbol, also the head is labeled by the state label in which currently it is residing.
• Input to the finite control is symbol '0' and state of the machine is q0. It can take following
actions:
1. Move reading head to the next input symbol.
2. Change the state of machine.
1.10
Theoretical Computer Science Finite Automaton

• Example, consider transition diagram in Fig 1.6.

Fig. 1.6: Transition Diagram of a DFA


• The FA accepts all strings containing even number of 1's and even number of 0's (say even-
even). We can formally define the FA as, M =({q0, q1, q2, q3}, {0, 1}, δ, q0, {q0}), where the
transition function δ is as shown in the Table 1.1.
• In Fig. 1.6, initial state q0 is shown by arrow labeled Table 1.1
start and final state q0 is shown by two concentric
circles.
• The δ is a transition function on a symbol i.e. δ (q, a) =
p means if input symbol is 'a', FA changes state from q
to p moving reading head to next input symbol to the
right. If the head has moved off the right end of the tape,
then it accepts the entire tape.
• We must extend the transition function δ to apply to a
state and a string rather than a state and a symbol.
^ ^
• We define a function δ from Q × Σ* to Q i.e. δ (q, w) = p means finite automata changes its
state from q to p after reading string w.
^
• We define δ as follows:
^
(i) δ (q, ∈) = q
^ ^
(ii) for all strings w and input symbol 'a', δ (q, wa) = δ (δ (q, w) , a).
• Thus, (i) states that without reading an input symbol, the FA cannot change state, and (ii)
tells us how to find the state after reading a non-empty input string wa.
^ ^ ^
• As δ (q, a) = δ (δ (q, ∈) , a) = δ (q, a), δ and δ give same resulting states. Consider example
1. FA always starts in initial state q0.
• Suppose 110101 is input to M. We note that δ (q0, 1) = q1 and δ (q1, 1) = q0. Thus,
δ (q0, 11) = δ (δ (q0, 1), 1) = δ (q1, 1) = q0
δ (q0, 110) = δ (δ (q0, 11), 0) = δ (q0, 0) = q2
δ (q0, 1101) = δ (δ (q0, 110), 1) = δ (q2, 1) = q3
δ (q0, 11010) = δ (δ (q0, 1101), 0) = δ (q3, 0) = q1
δ (q0, 110101) = δ (δ (q0, 11010), 1) = δ (q1, 1) = q0
1.11
Theoretical Computer Science Finite Automaton

• The entire sequence of states is


1 1 0 1 0 1
q0 q1 q0 q2 q3 q1 q0
• Thus 110101 is accepted by FA (... q0 ∈ F)
• A string x is said to be accepted by FA, M = (Q, Σ, δ, q0, F) if δ (q0, x) = p for some p in F.
• A language accepted by M, L(M), is the set {x | δ (q0, x) is in F}.
∴ 110101 ∈ L(M).
• A language is a regular set (or just regular) if it is the set accepted by some FA.
• In Fig. 1.6,
o If we change final state we get different languages. If final state is q3 we get language of
odd number of 1's and odd number of 0's (odd-odd).
o If final state is q0, we get language of even number of 0's and even number of 1's (even-
even).
o If final state is q2, we get language of odd number of 0's and even 1's (odd-even).
o If final state is q1, we get language of odd number of 1's and even 0's (even-odd).
DFA as Language Recognizer:
• A DFA can be viewed as a simple language recognition device. It called language recognizer
because it merely recognizes whether the input strings are in the language or not.
• Fig. 1.7 shows a schematic of a DFA as a recognizer. At any point in time, the DFA would
have recognized some sequence of source symbols at the start of the source string, which we
call the prefix of the source string, and it would be poised to recognize the source symbol
pointed to by the pointer next symbol.
• At the start of DFA operation the prefix would be an empty string. The DFA halts when all
symbols in the source string are recognized, or an error condition is encountered.
• The validity of a string is determined by giving it at the input of a DFA that is in its initial
state.
• The string is valid if and only if the DFA recognizes every symbol in the string and finds
itself in a final state at the end of the string.
Next symbol
Prefix

DFA
Source string
Fig. 1.7: DFA as a Recognizer
• Let us consider a DFA given in Fig. 1.8.
0 q1 1

q1 0 1 q2
1 0

1 q3 0

Fig. 1.8
1.12
Theoretical Computer Science Finite Automaton

• The Fig. 1.9 processes the strings 1, 00, 01, 010, 011, 1010 and 0100. The processing of two
strings 00 and 0110 is terminated to q0, which is the final state and rest strings are terminated
to a non-final state. Therefore, the strings 00 and 0101 are accepted and rest are not accepted.
0 1 0 1
q0 q1 q2 q3 q0 (Accepted)

1 0 0 1
q3 q0 q1 q2
(Accepted)

Fig. 1.9: Recognition of Strings 00 and 0101


DFA as Pattern Recognizer:
• DFA is the simplest machine to recognize patterns (strings). For the type of problems that
involve pattern recognition, the DFA can be constructed very easily.
• Now, let us see "what are the general steps to be followed while designing the DFA for
pattern recognition problems?".
• The steps are:
Step 1 : Identify the minimum string.
Step 2 : Identify the alphabets.
Step 3 : Construct the skeleton of DFA.
Step 4 : Identify other transitions not defined in Step 3.
Step 5 : Construct the DFA using transitions in Step 3 and Step 4.
Example: Drawing a DFA to accept string of a's having at least one a".
Solution: The DFA can be constructed as shown below:
Step 1 : Identifying the minimum string, 'a'.
Step 2 : Identify the alphabets, Σ = {a}.
Step 3 : Construct the skeleton DFA. The DFA for the string 'a' identified in Step 1 can be
constructed as shown in Fig. 1.10.
Start a End
Insert states at

Name states as q0 q1

Start a
q0 q1 accept
Skeleton
DFA accepting

Fig. 1.10
Step 4 : Identify the transitions not defined in Step 3:
Step (i) : δ(q1, a) = ? Move q0 to q1 on ‘a’, then think of transition from q1 on 'a'.
1.13
Theoretical Computer Science Finite Automaton
a a
q0 q1 ?

a Note: The sequence as should be accepted since


q1 q1 it is having at least one a. (go to final state q1)

d(q1, a) = q1

Fig. 1.11
Step 5 : Construct the DFA. The DFA can be obtained by skeleton DFA and transitions
obtained from previous step. The DFA is defined as:
M = (Q, Σ, δ, q0, F)
where,
o Q = (q0, q1)
o Σ = {a}
o q0 is the start state
o F = {q1}
o δ is shown in Fig. 1.12 using the transition diagram and table
a d a

Start a q0 q1
q0 q1 accept

* q1 q1

Transition diagram Transition table


Fig. 1.12
EXAMPLES [Oct. 17]
Example 1: Construct DFA for language containing strings starting with 'a' and ending with
'b' over alphabet {a, b}.
Solution:

Fig. 1.13: Transition Diagram


M = ({q0, q1, q2, q3}, {0, 1}, δ, q0, {q2})
Where, δ is as given in the Table 1.2.
1.14
Theoretical Computer Science Finite Automaton

Table 1.2: Transition table


δ a b
q0 q1 q3
q1 q1 q2
q2 q1 q2
q3 q3 q3 ← rejected state
Example 2: Construct DFA for language which contains all strings with exactly 2
consecutive zero's anywhere.
Solution:

Fig. 1.14: Transition Diagram


Table 1.3: Transition table
δ 0 1
q0 q1 q2
q1 q3 q2
q2 q1 q2
q3 q4 q3
q4 q4 q4
Let, M = (Q, Σ, δ, q0, F)
where, Q = {q0, q1, q2, q3, q4}
Σ = {0, 1}
q0 = q0
F = {q3}
and δ function is as shown in Table 1.3.
Example 3: Construct DFA for a language which contains all strings ending with "ab" or
"bc" over {a, b, c}.
Solution:

Fig. 1.15: Transition Diagram


1.15
Theoretical Computer Science Finite Automaton

Table 1.4: Transition table


δ a b c
q0 q1 q3 q0
q1 q1 q2 q0
q2 q1 q3 q4
q3 q1 q3 q4
q4 q1 q3 q0
Let, M = (Q, Σ, δ, q0, F)
where, Q = {q0, q1, q2, q3, q4}
Σ = {a, b, c}
q0 = q0
F = {q2, q4}
and δ function is as defined in Table 1.4.
Example 4: Construct DFA for accepting strings starting with 'a' and not having "abc" as a
substring over {a, b, c}.
Solution:

Fig. 1.16: Transition Diagram


Table 1.5: Transition table
δ a b c
q0 q1 q3 q3
q1 q1 q2 q4
q2 q1 q4 q3
q3 q3 q3 q3
q4 q1 q4 q4
Let, M = (Q, Σ, δ, q0, F)
where, Q = {q0, q1, q2, q3, q4}
Σ = {a, b, c}
1.16
Theoretical Computer Science Finite Automaton

δ function is as defined in Table 2.6.


q0 = q0
F = {q1, q2, q4}
Example 5: Construct a DFA for a language containing binary strings from LSB and string
is acceptable if it is divisible by 2.
Solution: When the binary string is given from a LSB and we are required to check the
divisibility by 2, it will be a easier task. For a binary number to be divisible by 2, the last bit must
be zero i.e. LSB = 0. We are given number from LSB, the first bit will indicate divisibility. In
other words, it is a string starting with zero.
e.g. 100 is written as 001.

Fig. 1.17: Transition diagram


Table 1.6: Transition table
δ 0 1
q0 q2 q1 ∴ M is Q = {q0, q1, q2}
q1 q1 q1 Σ = {0, 1}
q2 q2 q2 q0 = q0
F = {q2}
The δ function is as shown in the Table 1.6.
Example 6: Construct DFA for language containing string which contains 'a' at every even
position in the string.
Solution:

Fig. 1.18: Transition diagram


As the even positions are occupied by 'a', odd positions can be filled by 'a' or 'b'.
1.17
Theoretical Computer Science Finite Automaton

Table 1.7: Transition table


δ a b
q0 q1 q1
q1 q2 q3
q2 q1 q1
q3` q3 q3
∴ Finite automata M is,
Q = {q0, q1, q2, q3}
Σ = {a, b}
q0 = q0
F = {q1, q2}
δ function is as shown in Table 1.7.

1.4 NON-DETERMINISTIC FINITE AUTOMATON [April 16]


• In Non-Deterministic Finite Automata (NDFA/NFA) may be more than one move or no
move from a given state to the next state of any input symbol.
• NDFA is a simple machine that is used to recognize the pattern by consuming the string of
symbols and alphabets for each input symbol.
• Consider a transition diagram for a NFA for a language containing strings with either two
consecutive 0's or two consecutive 1's as shown in Fig. 1.19.

Fig. 1.19: Transition diagram for NFA


• Consider string 01001, the proliferation of states of an NFA can be shown in Fig. 1.20.

Fig. 1.20: Proliferation of states of an NFA


1.18
Theoretical Computer Science Finite Automaton

• The string is accepted by NFA, because it leads from initial state to final state (q4) i.e.
0 1 0 0 1
q0 → q0 → q0 → q3 → q4 → q4
Definition of NFA:
• NFA is denoted by 5-tuple (Q, Σ, δ, q0, F), where
o Q is a finite set of states.
o Σ is a finite set of symbols called the alphabets.
Q Q
o δ is the transition function where δ: Q × Σ → 2 , (here, the power set of Q (2 ) has been
taken because in case of NDFA, from a state, transition can occur to any combination of
Q states)
o q0 is the initial state from where any input is processed (q0 ∈ Q).
o F is a set of final state/states of Q (F ⊆ Q).
^
• The function δ can be extended to function δ mapping Q × Σ* to 2Q and is defined as
follows:
^
1. δ (q, ∈) = {q}
^ ^
2. δ (q, wa) = {p | for some state r in δ (q, w) , p is in δ (r, a)}. Means starting in state q
and reading input string w followed by input symbol, it can be in state p if one possible
state we can be in after reading w is r and from r we may go to p upon reading a. Note
^ ^
that δ (q, a) = δ (q, a). Thus we may replace δ to δ. It is also useful to extend δ to
arguments in 2Q × Σ* by, 3.δ (p, w) = Uq in p δ (q, w) for each set of states P ⊆ Q.

EXAMPLES
Example 1: Consider NFA of Fig. 1.10 whose transition function δ is as shown in Table 1.8.
Table 1.8
Inputs
δ 0 1
q0 {q0, q3} {q0, q1}
q1 φ {q2}
q2 {q2} {q2}
q3 {q4} φ
q4 {q4} {q4}
Let the input be 01001.
δ (q0, 0) = {q0, q3}
δ (q0, 01) = δ (δ (q0, 0), 1) = δ ({q0, q3}, 1) = 1
= δ (q0, 1) ∪ δ (q3, 1) = {q0, q1}
δ (q0, 010) = δ (δ (q0, 01), 0) = δ ({q0, q1}, 0)
= δ (q0, 0) ∪ δ (q1, 0)
= {q0, q3} ∪ φ
= {q0, q3}
1.19
Theoretical Computer Science Finite Automaton

δ (q0, 0100) = δ (δ (q0, 010), 0)


= δ ({q0, q3}, 0)
= δ (q0, 0) ∪ δ (q3, 0)
= {q0, q3, q4}
δ (q0, 01001) = δ (δ (q0, 0100), 1) = δ ({q0, q3, q4}, 1)
= δ (q0, 1) ∪ δ (q3, 1) ∪ δ (q4, 1)
= {q0, q1, q4}
Thus the string is accepted by the language of NFA, since {q4} ∈ F.
Example 2: Construct NFA for language which accepts a string starting with a, ending with
b or starting with b, ending with a.
Solution:

Fig. 1.21: Transition diagram


Table 1.9: Transition table
δ a b
q0 q1 q3
q1 q1 {q1, q2}
q2 φ φ
q3 {q3, q4} q3
q4 φ φ
Here, M = (Q, Σ, δ, q0, F)
where, Q = {q0, q1, q2, q3, q4}
Σ = {a, b}
δ function is as shown in Table 2.10.
q0 = q0
F = {q2, q4}

1.20
Theoretical Computer Science Finite Automaton

Differences between DFA and NFA/NDFA: [Oct. 16, April 19]


Sr.
DFA NFA/NDFA
No.
1. DFA stands for Deterministic Finite NFA stands for Non-deterministic Finite
Automaton. Automaton.
2. The transition from a state is to a single The transition from a state can be to
particular next state for each input multiple next states for each input
symbol. symbol.
3. DFA cannot use Empty String transition. NDFA permits empty string transitions.
4. DFA is more difficult to construct. NFA is easier to construct.
5. All DFA are NFA. Not all NFA are DFA.
6. Requires more space. Requires less space.

1.5 NFA TO DFA [April 17,19, Oct. 17, 18]


• Since, every DFA is an NFA, language accepted by DFA is also accepted by NFA.
Therefore, if given an NFA, it is possible to convert it to an equivalent DFA.
Theorem: Equivalence of NFA and DFA
• Let L be a set accepted by a NFA. Then there exists a DFA that accepts L.
Conversion Method:
• The major difference between NFA and DFA is because of δ, the transition function of NFA.
Since NFA is a 5-tuple M = (Q, Σ, δ, q0, F), where δ is Q × Σ → 2Q. Because of this, the
entries in the state table are sets instead of singular entry like DFA.
• While converting NFA into equivalent DFA, if we consider Q' = 2Q, as set of states for
resulting DFA, the transition function for DFA will become Q' × Σ → Q'.
Example 1: Construct DFA equivalent to NFA, M = ({q0, q1}, {0, 1}, δ, q0, {q1}), where δ
function is shown in Table 1.10 for NFA.
Table 1.10: NFA transition table
δ 0 1
q0 {q0, q1} {q1}
q1 φ {q0, q1}
Solution: For given NFA,
Q = {q0, q1}, Σ = {0, 1}, F = {q1}, q0 = q0 (initial state)
Let the resultant DFA is
M' = {Q', Σ, δ', q0, F')
where Σ = {0, 1}, starting state = q0.
The power set of Q is
2Q = {φ, {q0}, {q1}, {q0, q1}}
According to our method, Q' = 2Q = set of states of DFA

1.21
Theoretical Computer Science Finite Automaton

We use '[ ]' instead of '{ }'.


∴ Q' = {[q0], [q1], [q0, q1]}
We have excluded φ as it does not denotes any states of given NFA.
Now, we have to find δ' for every state in Q' on both '0' as well as '1'.
δ' ([q0], 0) = [q0, q1]
δ' ([q0], 1) = [q1]
δ' ([q1], 0) = φ from Table 1.11
δ' ([q1], 1) = [q0, q1]
We have to find δ' for one more state [q0, q1].
δ' ([q0, q1], 0) = δ (q0, 0) ≈ δ (q1, 0) = {q0, q1} ≈ φ = [q0, q1]
δ' ([q0, q1], 1) = δ (q0, 1) ≈ δ (q1, 1) = {q1} ≈ {q0, q1} = [q0, q1]
∴ the resultant DFA is as shown in Fig. 1.11.
Table 1.11 Transition table
δ 0 1
q0 [q0, q1] [q1]
q1 φ [q0, q1]
[q0, q1] [q0, q1] [q0, q1]

[q1]
1

Start 1
[q0]
0
0
[q0,q1]

1
Fig. 1.22: Equivalent DFA
The machine can have one initial state but more than one final state.
Note: Final states are those where at least one final state is included.
Example 2: Find a deterministic acceptor equivalent to
M = ({q0, q1, q2}, {a, b}, δ, q0, {q2})
Where, δ is as given by Table 1.12.
Table 1.12: State table

State/∑ a b
→ q0 q 0, q 1 q2
q1 q0 q1
q2 φ q 0, q 1

1.22
Theoretical Computer Science Finite Automaton

Solution: The deterministic automaton M1 equivalent to M is defined as follows:


M = (2Q, {a, b}, δ, [q0], F')
where F = {[q2], [q0, q2], [q1, q2], [q0, q1, q2]}
We start the construction by considering [q0] first. We get [q2] and [q0, q1]. Then we
construct δ for [q2] and [q0, q1]. [q1, q2] is a new state appearing under the input columns. After
constructing δ for [q1, q2], we do not get any new states and so we terminate the construction of
δ. The state table is given by Table 1.13.
Table 1.13: State table of M1
State/∑ ∑ a b
Start state [q0] [q0, q1] [q2]
*[q2] φ [q0, q1]
[q0, q1] [q0, q1] [q1, q2]
*[q1, q2] [q0] [q0, q1]
a
b

[q0,q1] b
a
[q1,q2]
Start a
[q0]
b
a b
[q2]

[q1]
b

Fig. 1.23
Example 3: Construct a deterministic finite automaton equivalent to
M = ({q0, q1, q2, q3}, {0, 1}}, δ, q0, {q3})
where δ is given by Table 1.14.
Table 1.14: State table for Example 2.8

State/∑ a b
→ q0 q 0, q 1 q0
q1 q2 q1
q2 q3 q3
q3 φ q2
Solution: Let Q = {q0, q1, q2, q3}. Then the deterministic automaton M1 equivalent to M is
given by M1 = (2Q, {a, b}, δ, [q0], F)
where F consists of:
[q3], [q0, q3], [q1, q3], [q2, q3], [q0, q1, q3], [q0, q2, q3], [q1, q2, q3]
and [q0, q1, q2, q3]
and where δ is defined by the state table given by Table 1.15.
1.23
Theoretical Computer Science Finite Automaton

Table 1.15: State table of M1


State/∑ ∑ A b
Start state [q0] [q0, q1] [q0]
[q0, q1] [q0, q1, q2] [q0, q1]
[q0, q1, q2] [q0, q1, q2, q3] [q0, q1, q3]
* [q0, q1, q3] [q0, q1, q2] [q0, q1, q2]
* [q0, q1, q2, q3] [q0, q1, q2, q3] [q0, q1, q2, q3]

1.6 NFA WITH ∈TRANSITIONS [April 16, 17, 18, 19, Oct. 18]
• It is an NFA including transitions on the empty input i.e. '∈' (epsilon). We can extent a NFA
by introducing 'ε-moves' that allow us to make a transition on the empty string.
• There would be an edge labeled ∈ between two states and this edge allows transition from
one state to another even without receiving an input symbol. This is another mechanism that
allows NFA to be in multiple states at once.
• Constructing such NFA is easy, but the NFA thus constructed is not that powerful. The NFA
with ∈-moves is given by M = (Q, Σ, δ, q0, F) where δ is defined as Q × Σ ∪ {∈} → 2Q.
Definition:
• NFA with ∈-moves is denoted by a 5-tuple (Q, Σ, δ, q0, F),
where Q : finite set of states
Σ : finite input alphabet
q0 : initial state contained in Q
F : set of final states ⊆ Q
and δ : transition function or state function mapping
Q × (Σ ∪ {∈}) to 2Q
i.e. δ : Q × (Σ ∪ {∈}) ∪ 2Q
Basically, it is NFA with few transitions on the empty input '∈'.
Note : DFA can never have ∈-moves.

Example:
• Let us consider the FA shown in Fig. 1.24. This FA accepts a language consisting any
number of 0's followed by any number of 1's followed by any number of 2's.
• For example, the strings ∈, 0, 00, 01, 1, 11, 2, 22, 12, 122, 012, 0012, etc. are all valid strings
of this language.

Fig. 1.24: NFA with ∈-Moves


1.24
Theoretical Computer Science Finite Automaton

Table 1.16: Transition table


δ 0 1 2 ∈
q0 {q0} φ φ {q1}
q1 φ {q1} φ {q2}
q2 φ φ {q2}
φ
• Consider a string "002" which is accepted by NFA of Fig. 1.24 by the path q0, q0, q1, q2,
q2 with arcs labeled 0, 0, ∈, ∈, 2.
• In real life, '∈' means 'nothing'. So we cannot say on reading 'nothing' machine has changed
its state. Therefore, we want the deterministic machine which tells what will be the output.
Hence, we can convert NFA with ∈-moves to NFA without ∈-moves.
Definition of ∈-closure of a State: [April 18, 19]
• The ∈-closure (q), pronounced as epsilon closure of 'q' is defined as the set of all states 'p'
such that there is a path from 'q' to 'p' labelled '∈' i.e. it is the set of states with distance zero
from state 'q'.
• Consider above NFA of Fig. 1.24.
∈-closure (q0) = {q0, q1, q2}
• State q0 is also added to the set because every state is at distance zero from itself and it is the
path from q0 to q0 with all arcs labelled ∈, path q0 to q1 on ∈ shows that q1 is in ∈-closure (q0)
and path q0 – q1 – q2 on ∈ shows that q2 is in ∈-closure(q0).
• Similarly, ∈-closure (q1) = {q1, q2}
∈-closure (q2) = {q2}
• Let NFA without ∈-moves is M = (Q, Σ, δ", q0, F'), where δ": Q × Σ → 2Q. We define new δ'
as follows:
δ' (q0, ∈) = ∈-closure (q0) = {q0, q1, q2}
δ" (q, a) = ∈-closure (δ (δ' (q, ∈), a))
where, δ' (q, ∈) = ∈-closure (q)
^ ^
• Same can be written as δ (q, a) = ∈-closure (δ (δ (q, ∈) , a)).
• Convert NFA with ∈-moves in Fig. 1.24 to equivalent NFA without ∈-moves accepting the
same language.
• From the definition of ∈-closure,
∈-closure (q0) = {q0, q1, q2}
∈-closure (q1) = {q1, q2}
∈-closure (q2) = {q2}
• We see that, q2 is a final state in Fig. 1.24, since q2 is in ∈-closure (q0) and ∈-closure (q1);
both q0, q1 are in a set of final state.
∴ F' = {q0, q1, q2}
1.25
Theoretical Computer Science Finite Automaton

^
• Now, find δ for resultant NFA.
^ ^
δ (q0, 0) = ∈-closure (δ (δ (q0, ∈) , 0))
= ∈-closure (δ ({q0, q1, q2}, 0))
= ∈-closure (δ (q0, 0) ∪ δ (q1, 0) ∪ δ (q2, 0))
= ∈-closure ({q0} ∪ φ ∪ φ)
= ∈-closure ({q0}) = {q0, q1, q2}
^ ^
δ (q0, 1) = ∈-closure (δ (δ (q0, ∈) , 1))
= ∈-closure (δ ({q0, q1, q2}, 1))
= ∈-closure (δ (q0, 1) ∪ δ (q1, 1) ∪ δ (q2, 1))
= ∈-closure (φ ∪ {q1} ∪ φ)
= ∈-closure (q1)
= {q1, q2}
^ ^
δ (q0, 2) = ∈-closure (δ (δ (q0, ∈) , 2))
= ∈-closure (δ ({q0, q1, q2}, 2))
= ∈-closure (δ (q0, 2) ∪ δ (q1, 2) ∪ δ (q2, 2))
= ∈-closure (φ ∪ φ ∪ {q2})
= ∈-closure (q2)
= {q2}
^ ^
δ (q1, 0) = ∈-closure (δ (δ (q1, ∈) , 0))
= ∈-closure (δ ({q1, q2}, 0))
= ∈-closure (δ (q1, 0) ∪ δ (q2, 0))
= ∈-closure (φ ∪ φ)
= ∈-closure (φ)
= φ
^ ^
δ (q1, 1) = ∈-closure (δ (δ (q1, ∈) , 1))
= ∈-closure (δ ({q1, q2}, 1))
= ∈-closure (δ (q1, 1) ∪ δ (q2, 1))
= ∈-closure ({q1} ∪ φ)
= ∈-closure (q1)
= {q1, q2}
• Similarly, we can obtain,
^
δ (q1, 2) = {q2}
^
δ (q2, 0) = φ
^
δ (q2, 1) = φ
^
δ (q3, 2) = {q2}
1.26
Theoretical Computer Science Finite Automaton

^
• Resultant δ is as shown in the transition Table 1.17.
Table 1.17
δ 0 1 2
q0 {q0, q1, q2} {q1, q2} {q2}
q1 φ {q1, q2} {q2}
q2 φ φ {q2}

Fig. 1.25: Resultant NFA without ∈-Moves


• There is a transition from q0 to q1 labeled 0 or 1 meaning there are two transitions from q0 to
q1 one on 0 and other on 1. Here they are combined for simplicity.
Example 1: Convert NFA with ∈-moves in Fig. 1.26 to equivalent NFA.

Fig. 1.26: Transition diagram


Solution:
Table 1.18: Transition table
δ a b ∈
q0 φ {q2} {q1}
q1 {q1} φ φ
q2 φ {q 2 } φ
Consider NFA with ∈-transition in Fig. 1.26.
∈-closure (q0) = {q0, q1}
∈-closure (q1) = {q1}
∈-closure (q2) = {q1, q2}
^ ^
δ (q0, a) = ∈-closure (δ (δ (q0, ∈) , a))
= ∈-closure (δ (q0, a) ∪ δ (q1, a))
= ∈-closure (φ ∪ q1)
= ∈-closure (q1)
= {q1}
1.27
Theoretical Computer Science Finite Automaton

^ ^
δ (q0, b) =
∈-closure (δ (δ (q0, ∈) , b))
=
∈-closure (δ (q0, b) ∪ δ (q1, b))
=
∈-closure (q2 ∪ φ)
=
∈-closure (q2)
=
{q1, q2}
^ ^
δ (q1, a) = ∈-closure (δ (δ (q1, ∈) , a))
= ∈-closure (δ (q1, a))
= ∈-closure (q1)
= {q1}
^ ^
δ (q1, b) = ∈-closure (δ (δ (q1, ∈) , b))
= ∈-closure (δ (q1, b))
= ∈-closure (φ)
= φ
^ ^
δ (q2, a) = ∈-closure (δ (δ (q2, ∈) , a))
= ∈-closure (δ (q1, a) ∪ δ (q2, a))
= ∈-closure (q1 ∪ φ)
= {q1}
^ ^
δ (q2, b) = ∈-closure (δ (δ (q2, ∈) , b))
= ∈-closure (δ (q1, b) ∪ δ (q2, b))
= ∈-closure (φ ∪ q2)
= {q1, q2}
Therefore equivalent NFA is as shown in the Fig. 1.27.
Table 1.19: Transition table
δ a b
q0 {q1} {q1, q2}
q1 {q1} φ
q2 {q1} {q1, q2}

Fig. 1.27: Equivalent NFA


The set of final state contains state q0 because ∈-closure of q0 contains q1, which is a final
state. State q2 is also included in the set of final state for the same reason.
1.28
Theoretical Computer Science Finite Automaton

1.7 NFA WITH ∈TRANSITIONS TO DFA


• Since, every DFA is an NFA, language accepted by DFA is also accepted by NFA.
Therefore, if given an NFA, it is possible to convert it to an equivalent DFA.
Constructing a DFA from NFA: (NFA to DFA Conversion):
Input: An NFA N.
Output: A DFA D accepting the same language.
Method:
1. Each state of D is a set of states which N could be in after reading some sequence of input
symbols. The initial state of DFA D is q0, which is initial state of NFA N together with all
states of N that can be reached from q0 by means of ∈-transitions only. The accepting states
of D are those sets of states that contain at least one accepting state of N. Here initial state is
∈-closure (q0).
2. While there is an unmarked state
x = {q1, q2, …, qn} of D do
begin
mark x;
for each input symbol a do
begin
let T be the set of states to which there is a transition on 'a' from some state
q1 in x;
y: = ∈-closure (T);
if y has not yet been added to the set of states of D then make y an
"unmarked" state of D;
add a transition from x to y labelled
'a' if not already present.
end
end.
Here, we consider ∈-closure (q0) be a state of D. This state is the start state of D. We assume
each state of D is initially "unmarked". Then repeat this procedure until no more states can
be added in D.
Example 1: Construct equivalent DFA for the NFA of Fig. 1.28 (a). [April 16]

Fig. 1.28: (a) NFA


The language of NFA is containing all strings ends with abb over {a, b}.
1.29
Theoretical Computer Science Finite Automaton

Solution: The initial state of the equivalent DFA is ∈-closure (q0) = {q0, q1, q2, q4, q7) = A
(say), since there are exactly the states reachable from state q0 via a path in which every edge is
labeled ∈ and state itself is in ∈-closure (q0).
Here we mark set A i.e. set x to A and compute T. The set of states of N having transitions
on 'a' from all members of A. So among the states q0, q1, q2, q4 and q7, only states q2 and q7 have
such transitions to q3 and q8, so
y = ∈-closure ({q3, q8}) = {q1, q2, q3, q4, q6, q7, q8}
= B (say)
Now on input symbol b, from all members of states A we get q5 is only reachable state.
∴ y = ∈-closure ({q5}) = {q1, q2, q4, q5, q6, q7}
= C (say)
Now, we continue this process with the unmarked sets B and C.
From B on input 'a' we get,
y = ∈-closure ([q3, q8] ) = B
From B on input 'b' we get,
y = ∈-closure ({q5, q9} )
= {q1, q2, q4, q5, q6, q7, q9}
= D (say)
From C on input 'a' we get,
y = ∈-closure ({q3, q8}) = B
From C on input 'b' we get,
y = ∈-closure ({q5}) = C
From D on input 'a' we get,
y = ∈-closure ({q3, q8}) = B
From D on input 'b' we get,
y = ∈-closure ({q5, q10})
= {q1, q2, q4, q5, q6, q7, q10}
= E (say)
From E on input 'a' we get,
y = ∈-closure ({q3, q8}) = B
From E on input 'b' we get,
y = ∈-closure ({q5}) = C
The transition table of the resulting DFA is shown in Table 1.20. This DFA will also accept
the same language as that of NFA and having less number of states.
1.30
Theoretical Computer Science Finite Automaton

Table 1.20: Transition table


δ a b
Start → A B C
B B D
C B C
D B E
Accept → E B C
Here set E contains final state q10 of NFA. Therefore E is final state of DFA.
Fig. 1.28 (b) shows the resultant DFA D.

Fig. 1.28: (b) Equivalent DFA


Example 2: Construct DFA equivalent to NFA of Fig. 1.29.
M = ({q0, q1}, {0, 1}, δ, q0, {q1})

Fig. 1.29: NFA


Solution: Here we are not having any ∈-transition. So ∈-closure of any state is that state
itself.
Let initial state is ∈-closure (q0) = {q0} = A.
From A on input '0' we get set,
{q0, q1} = B
From A, on input '1', we get set,
{q1} = C
From B on input '0' we get set = {q0, q1} = B.
From B on input '1' we get set {q0, q1} = B.
From C on input '0' we get set = {φ}.
From C on input '1' we get set = {q0, q1} = B.
1.31
Theoretical Computer Science Finite Automaton

Therefore resultant transition table is


Table 1.21: Transition table of DFA
→ δ 0 1
Start → A B C
B B B
C φ B
The resultant DFA is shown in Fig. 1.30.

Fig. 1.30: Equivalent DFA


Since the set B and C contains the state q1 which was final state of NFA, DFA contains B
and C has final states.
Here A = [{q0}], B = [{q0, q1}] and C = [{q1}].
Example 3: Construct equivalent DFA for NFA of Fig. 1.31.

Fig. 1.31: NFA


Solution: The initial state is q0.
∈-closure (q0) = {q0, q1, q2} = A (say)
From A on input 'a' we get the states q0 and q1. Hence, by taking ∈-closure of these states we
get, {q0, q1, q2}.
Since, ∈-closure (q0) = {q0, q1, q2)
and ∈-closure (q1) = {q0, q1, q2}
Hence, from all member of A on input 'a' we get
{q0, q1, q2} = A (again)
Similarly, from A on input 'b' we get the state q2 and ∈-closure (q2) = {q0, q1, q2}
∴ We get, {q0, q1, q2} = A (again)
We get the same set again and again as we proceed further. Therefore transition table we get
as follows:
Table 1.22: Transition table
δ a b
A A A

1.32
Theoretical Computer Science Finite Automaton

The equivalent DFA is shown in Fig. 1.32.

Fig. 1.32: Equivalent DFA


Example 4: Construct equivalent DFA for the NFA of Fig. 1.33.
M = ({q0, … q7}, {a, b}, δ, q0, {q7})

Fig. 1.33: NFA


Solution: Initial state is q0, we take:
∈-closure (q0) = [q0, q1] = A → initial state of DFA.
1. From A, on input 'a' we get only state q2. So
∈-closure (q2) = {q2, q3, q5} = B
From A, on input 'b' we get only state q4. So
∈-closure (q4) = {q4, q3, q5} = C
2. From B, on input 'a' we get only state q6
∈-closure (q6) = {q6) = D
From B, on input 'b' we get set φ.
3. From C, on input 'a' we get only state q6.
∴ ∈-closure (q6) = {q6) = D (again)
From C, on input 'b' we get set {φ}.
4. From D, on input 'a' we get set {φ}.
From D, on input 'b' we get only state q7.
∈-closure (q7) = {q7} = E
5. From E, on input 'a' we get set {φ}.
Also from E, on input 'b' we get set {φ}.
Therefore, equivalent DFA for above NFA is shown in Fig. 1.34 along with transition table.
1.33
Theoretical Computer Science Finite Automaton

Table 1.23: Transition table


δ a b
Start → A B C
B D φ
C D φ
D φ E
Accept → E φ φ

Fig. 1.34: Equivalent DFA


Here, set E contains final state q7. Therefore E is a final state.
Example 5: Construct DFA equivalent to NFA of Fig. 1.35.
M = ({p, q, r, s}, {0, 1}, δ, p, {q, s})
1 1

Start 0,1
P q

0 1 0,1
1
0,1

S r
0
Fig. 1.35: NFA
Solution: Initial state of DFA is P = A (say).
0
A → {q, r, s} = B
1
A → {p, q, r} = C
0
B → {r, s} = D (‡ q on 0 = r and r on 0 =s)
1
B → {p, q, r} = C
0
C → {q, r, s} = B
1
C → {p, q, r} = C
0
D → {s} = E
1
D → {p} = A
0
E → {φ}
1
E → {p} = A
1.34
Theoretical Computer Science Finite Automaton

Therefore equivalent DFA is shown in Fig. 1.36.


1

Start 0 0
A B D 1

0
1 0
1

C E

1
Fig. 1.36: Equivalent DFA

1.8 FINITE AUTOMATA WITH OUTPUT [Oct. 17, 18]


• As we know, FA is the mathematical model of FSM and is defined by a 5-tuple (Q, Σ, δ, q0,
F) which does not include the information about output.
• After reading a string if FA resides in final state, it says that, the string is "accepted" by FA
else it says that, the string is "rejected" by the FA.
• But if we need to produce some more precise information and not only 'accept' or 'reject';
then we shall consider the finite set of output symbols and the Machine Function (MAF) as
well; that we have considered while learning Finite State Machines (FSM).
• DFA may have outputs corresponding to each transition. There are two different types of
machines which can be formulated as FA with output namely, Moore machine and Mealy
machine.
Moore Machine: [April 16, 17, Oct. 17, 18]
• Moore machine is the machine with finite number of states and for which, the output symbol
at a given time depends only upon the present state of machine.
Definition: A Moore machine is a 6-tuple (Q, Σ, ∆, δ, λ, q0)
where, Q : Finite set of states.
Σ : Finite input alphabet.
∆ : An output alphabet.
δ : State function, δ : Q × Σ → Q.
λ : Machine function, λ : Q → ∆.
q0 : Initial state of the machine.
• Thus, for Moore machine, an output symbol is associated with each state. When the machine
is in a particular state, it produces the output, irrespective of what was the input on which the
transition is made.
1.35
Theoretical Computer Science Finite Automaton

Example 1: Construct a Moore machine to determine the residue (remainder) mod 3 for a
binary number.
Solution: (i) If 'i' is a binary number and if we write '0' after 'i' then its value becomes '2i'.
e.g. Consider binary number
i = '1' (value = 1)
If we write '0' after it,
'10' = 2 i.e. value = 2 × 1
As another example, if we consider
i = '100' (value = 4)
then i.0 = 1000 has value = 8
= 2×4
(ii) If we write '1' after 'i', where 'i' is any binary number, its value becomes '2i + 1'.
e.g. If i = '1' (value = 1)
i.1 = '11' = 3 i.e. value = 2 × 1 + 1 = 3
As another example, if
i = '100' (value 4)
i.1 = 1001 = 9
i.e. value = 2 × 4 + 1 = 9
As we are constructing a machine to determine remainder, when we divide any binary
number by 3, the different remainder values that we can have are 0, 1 and 2.
Table 1.24: Different remainder values
Remainder value (R) 0 1 2
When we write '0' after i + (R) and divide by 3, the remainder = 2R mode 3 0 2 1
When we write '1' after i + (R) and divide by 3, the remainder = (2R + 1) mod 3 1 0 2
In above Table 1.24, if say, the remainder of previous result is '2' i.e. '10' and if we write '0'
after it, it becomes '4' i.e. '100'. Now if we divide it by '3' the remainder will be '1'.
To construct a Moore machine, we can associate three different residue values with three
different states; i.e. '0' residue value with state 'q0', residue '1' with 'q1' and so on as described in
machine function stated in Table 1.25.
Table 1.25: Machine function for Moore machine λ: Q → ∆
State Q q0 q1 q2
Output ∆ 0 1 2
Now, Table 1.26 can be viewed as the state table or transition table for the required Moore
machine, given in Table 1.26 and resultant Moore machine is given in the Fig. 1.37.
Table 1.26: Transition table for required Moore machine δ: Q × Σ → Q
δ 0 1 Output λ
q0 q0 q1 0
q1 q2 q0 1
q2 q1 q2 2

1.36
Theoretical Computer Science Finite Automaton

Fig. 1.37: Moore Machine finding residue mode 3 for Binary Number
Example 2: Design a Moore machine for a language over {0, 1} which outputs '*' if string
contains '11' in it and outputs '#' otherwise.
Solution:

Fig. 1.38
Table 1.27: Transition table
δ Output
Q 0 1 λ
q0 q0 q1 #
q1 q0 q2 #
q2 q2 q2 *

Example 3: Design a Moore machine to get 1's complement of a given binary string.
Solution:

Fig. 1.39
Table 1.28: Transition table
δ Output
Q 0 1 λ
q0 q2 q1 0
q1 q2 q1 0
q2 q2 q1 1

1.37
Theoretical Computer Science Finite Automaton

Mealy Machine: [Oct. 16, 17, 18, April 17, 19]


• It is the machine with finite number of states and for which, the output symbol at a given
time is a function of (i.e. it depends on) the present input symbol, as well as the present state
of the machine.
Definition: A Mealy machine is denoted by a 6-tuple (Q, Σ, ∆, δ, λ, q0),
where, Q : Finite set of states.
Σ : Finite input alphabet
∆ : Finite output alphabet
δ : State function, δ": Q × Σ → Q
λ : Machine function λ: Q × Σ → ∆
q0 : Initial state of the machine.
• Thus, for this type of machine the output depends on both current state and the current input
symbol.
Example 1: Design a Mealy machine accepting the language consisting of strings from Σ*
where Σ = {0, 1} and ending with double zero's or double one's.
Solution: Let us assume that, the output alphabet
∆ = {y, n}
'y' i.e. 'yes' will be the output if string is accepted and 'n' i.e. 'no' will be the output if string is
not accepted i.e. rejected by the machine.
Let us assume that the initial state is 'q0'. From 'q0' there will be two transitions, one on '0' and
other on '1' as Σ = {0, 1}. On '0' we should make the transition to some other state, say 'P0', which
will look for second consecutive zero to get double zero's. Similarly, on '1', transition will go to
'P1' which will look for second consecutive one to get double one's [refer Fig. 1.40 (a)].

Fig. 1.40
In 'P0' we get '0', it should remain in the same state and should produce output 'y' as we got
double zero's. It should remain in the same state because the string may be of the form "0000".
Similarly, in 'P1' if it gets '1', it should go to itself with output 'y' indicating it has got double '1's.
This state is reflected in Fig. 1.40 (b).
Now, in 'P0' if we get '1', machine should make transition to 'P1' which will be checking for
second consecutive '1' and in 'P1' if we get '0', it should show the transition from 'P1' to 'P0' which
will be looking for second consecutive zero. Here ends the process at considering transitions on
both '0' and '1' from every state. Fig. 1.40 (c) shows the final Mealy machine with given
requirements.
1.38
Theoretical Computer Science Finite Automaton

Example 2: Design a Mealy machine to find out 2's complement of a given binary number.
Solution: Steps to be followed are:
(i) Read bit by bit from LSB.
(ii) Keep the bits unchanged till you get first '1'; do not replace this '1' by '0'. Keep that also
unchanged.
(iii) The remaining bits are to be changed from '0' to '1' and from '1' to '0'.
The design here requires again two states Q = {q0, q1} with q0 as initial state, 'q0' will read '0's
without replacing them till it gets first '1'. On getting '1', it makes a transition to 'q1' which now
replaces each coming '0' by '1' and '1' by '0'.
The Mealy machine can be drawn as in Fig. 1.41.

Fig. 1.41: Mealy Machine finding 2's complement of a binary number


(Reading number from LSB)
e.g. "1010" – gives number reading from right to left.
The output can be obtained by reading bottom to top as we are taking input in reverse order,
right to left. Output will be "0110".
Equivalence of Moore and Mealy Machines: [April 16]
• We will see that the Moore and Mealy machines are equivalent in their capability although
these machines differ in their output function. We will discuss how the construct equivalent
Mealy machine for a Moore machine and vice-versa.
Theorem: If M1 = (Q, Σ, ∆, δ, λ, q0) is a Moore machine, then there is a Mealy machine M2
equivalent to M1 and vice-versa.
• Moore to Mealy: To convert Moore machine to Mealy machine, state output symbols are
distributed to input symbol paths.
• If M1 = (Q, Σ, ∆, δ, λ, q0) is a Moore machine, then equivalent Mealy machine is,
M2 = (Q, Σ, ∆, δ, λ', q0)
where, λ' (q, a) = λ (δ (q, a))
∀ (for all) q ⊂ Q and ∀ a ⊂ Σ.
Example 1: Consider a Moore machine that we have already designed (refer Fig. 1.37) for
finding residue mod - 3 for any binary number, redrawn in Fig. 1.42 (a).

(a) Moore Machine

1.39
Theoretical Computer Science Finite Automaton

(b) Equivalent Mealy Machine


Fig. 1.42
Solution: The state function 'δ' for this Moore machine is as shown in the Table 1.29.
Table 1.29: Transition table, δ: Q × Σ → Q
Σ
0 1
Q
q0 q0 q1
q1 q2 q0
q2 q1 q2
Similarly, the machine function 'λ' is as shown in the following Table.
Machine function, λ: Q → ∆
Q q0 q1 q2
∆ 0 1 2
We have to convert this Moore machine to equivalent Mealy machine with δ (state function)
same as in the Table 1.29, but changed λ' i.e. machine function. According to the conversion
rule,
λ' (q, a) = λ (δ (q, a))
We can write, λ' (q0, 0) = λ (δ (q0, 0)) = λ (q0) = 0
λ' (q0, 1) = λ (δ (q0, 1)) = λ (q1) = 1
λ' (q1, 0) = λ (δ (q1, 0)) = λ (q2) = 2
λ' (q1, 1) = λ (δ (q1, 1)) = λ (q0) = 0
λ' (q2, 0) = λ (δ (q2, 0)) = λ (q1) = 1
λ' (q2, 1) = λ (δ (q2, 1)) = λ (q2) = 2
From this information, we can now create a table for MAF as shown in the Table 1.30.
Table 1.30: MAF, λ': Q × Σ → ∆ for Mealy machine
Σ
0 1
Q
q0 0 1
q1 2 0
q2 1 2
The transition diagram for the equivalent Mealy machine can be shown in Fig. 1.32 (b).
Here, the output is associated with the transition i.e. it depends on the state making the transition
as well as on what input the transition is.
1.40
Theoretical Computer Science Finite Automaton

Note: Consider both Moore and Mealy machines shown in Fig. 1.42, also consider the input
sequence "1010" of length = 4 i.e., length (input string) = n = 4.
(i) Output sequence for Moore machine is,

length (output sequence) = 5 = n + 1


(ii) Output sequence for Mealy machine is,

length (output sequence) = 4 = n.


• Thus, for Moore machine, the output sequence contains one more than the symbols in input
sequence while in Mealy machine, the output sequence contains exactly the same number of
symbols as in the input sequence.
• Mealy to Moore: To converting the Mealy machine to Moore machine, we will create a
separate state for every new output symbol and according to incoming and outgoing edges
are distributed.
• If given Mealy machine is,
M1 = (Q, Σ, ∆, δ, λ, q0) then,
an equivalent Moore machine will be,
M2 = ([QX∆], Σ, ∆, δ', λ', [q0, b0])
where, 'b0' is an arbitrarily selected member of ∆.
and δ' ([q, b], a) = [δ (q, a), λ (q, a)] and λ' ([q, b]) = b
Example 2: Consider the Mealy machine that we have designed accepting the strings ending
with '00' and '11' given in Fig. 1.40 and redrawn in Fig. 1.43 (a). The state tables for this machine
are as shown in the Table 1.31.

(a) Mealy Machine (b) Equivalent Moore Machine

1.41
Theoretical Computer Science Finite Automaton

(c) Moore Machine


Fig. 1.43
Table 1.31: Mealy machine state tables
(a) δ : Q × Σ → Q (b) λ: Q × Σ → ∆
Σ Σ
0 1 0 1
Q Q
q0 p0 p1 q0 n n
p0 p0 p1 p0 y n
p1 p0 p1 p1 n y
According to the rule of conversion, the resultant Moore machine consists of,
Q' = Q × ∆ = {[q0, n], [q0, y], [p0, n], [p0, y], [p1, n], [p1, y]}
Also δ' and λ' functions can be found out as,
(a) δ' ([q0, n], 0) = [δ (q0, 0), λ (q0, 0)]
= [p0, n]
δ' ([q0, n], 1) = [δ (q0, 1), λ (q0, 1)]
= [p1, n]
λ' ([q0, n]) = n
(b) δ' ([q0, y], 0) = [δ (q0, 0), λ (q0, 0)]
= [p0, n]
δ' ([q0, y], 1) = [δ (q0, 1), λ (q0, 1)]
= [p1, n]
λ' ([q0, y]) = y
(c) δ' ([p0, n], 0) = [δ (p0, 0), λ (p0, 0)]
= [p0, y]
δ' ([p0, n], 1) = [δ (p0, 1), λ (p0, 1)]
= [p1, n]
λ' [(p0, n)] = n
1.42
Theoretical Computer Science Finite Automaton

Similarly,
(d) δ' ([p0, y], 0) = [p0, y]
δ' ([p0, y], 1) = [p1, n]
λ' ([p0, y]) = y
(e) δ' ([p1, n], 0) = [δ (p1, 0), λ (p1, 0)]
= [p0, n]
δ' ([p1, n], 1) = [δ (p1, 1), λ (p1, 1)]
= [p1, y]
λ' ([p1, n]) = n
Similarly,
(f) δ' ([p1, y], 0) = [p0, n]
δ' ([p1, y], 1) = [p1, y]
λ' ([p1, y]) = y
• The transition graph for equivalent Moore machine is as shown in the Fig. 1.43 (b). We can
re-label the states if we want. Out of [q0, n] and [q0, y], we have to arbitrarily select the state
as initial state and here we have chosen [q0, n] as a initial state.
• We can remove state [q0, y] as there are no incoming edges to this state. After removing this
state and renaming the remaining states, we get the final Moore machine as shown in the
Fig. 1.43 (c).
Mealy Machine vs. Moore Machine: [April 17]
Sr.
Mealy Machine Moore Machine
No.
1. In Mealy machine output depends on In Moore machine output depends only
present state as well as present input. upon present state.
2. Generally, it has fewer states than Moore Generally, it has fewer states than Moore
machine. machine.
3. In Mealy machine, if input changes, In Moore machine If input changes,
output also changes. output does not change.
4. They react faster to inputs. They react slower to inputs (one clock
cycle later).
5. It is difficult to design. It is easy to design.
6. In Mealy machine the output is placed on In Moore machine the output is placed on
transitions. states.
1.43
Theoretical Computer Science Finite Automaton

1.9 MINIMIZATION OF DFA [April 16, 17, 18, 19, Oct. 17


• The number of states used in DFA directly depends upon the size of the automaton that we
have used. So, it is important to reduce the number of states.
• Minimization of DFA means reducing the number of states from given FA. Thus, we get the
FSM with redundant states after minimizing the FSM.
Myhill-Nerode Theorem:
• John Myhill and Anil Nerode of the University of Chicago proposed a theorem in 1958, used
to minimization of a DFA.
• Theorem: For any regular language L, there exists a DFA with total transition function,
which accepts L and has a number of states equal to the index of RL, the relation associated
with L.
• Any state in DFA constructed by the Myhill-Nerode theorem, which does not lie on a path
from the start state to a final state, can be omitted together with all arcs to or from that state.
The remaining DFA is called minimal DFA.
• Let M = (Q, Σ, δ, q0, F) be any DFA accepting L. We define relations D0, D1 … on Q as
follows:
1. A state 'q' distinguishable from state 'p' by a string of length 0 is written as qD0p, iff
either q ∈ F and p ∉ F or q ∉ F and p ∈ F.
2. For i > 0 we define q Di p i.e. q is distinguishable from p by a string of length < = i iff q
Di–1 p or there exists a ∈ Σ such that δ (q, a) Di–1 δ (p, a).
3. State q is distinguishable from p iff qDp.
Example 1: Construct minimal DFA for DFA given in the Fig. 1.44. [April 16]

Fig. 1.44
Solution:
δ a b
q0 q1 q2
q1 q1 q3
q2 q0 q1
q3 q1 q4
q4 q3 q1
Here q2 and q4 are final state. Therefore, mark X at entry (q2, q0) (q2, q1) (q4, q0) (q4, q1)
(q4 q3) as shown in Table 1.32.

1.44
Theoretical Computer Science Finite Automaton

Table 1.32

δ (q2, a) = q0 ∉F δ(q2, b) = q1 ∉ F
∴ q2, q0 are distinguishable
δ (q0, a) = q1 ∉ F δ (q0, b) = q2 ∈F
Once we mark entry X for final with nonfinal state i.e. (final, nonfinal); then
First we find D0. Table 1.33 shows D0. In this we mark entry (p, q) as X if pD0 q i.e. either q
∈ F and p ∉ F or q ∉ F and p ∈ F.
Check for those states which are not distinguishable using D1. First we copy distinguishable
states i.e. those marked as X from D0 for Table 1.33. Now q0 is not distinguishable from q1, so we
find q0 D1 q1. As δ (q0, b) ∈F and δ (q1, b) ∉ F, q0 is distinguishable from q1 with string if length
is 1 i.e. q0 D1 q1. Also, δ (q1, b) ∉ F and δ (q3, b) ∈ F, so q1 D1 q3 as shown in the Table 2.34.
Table 1.33

We do not get any other distinguishable pair with D1. We must repeat this till table
Di–1 = table Di. As D0 ∉ D1, we find D2 i.e. with string of length 2. As Σ = {a, b} possible strings
are aa, ab, bb and ba. Now, again copy distinguishable states from D1. The table D2 remains same
as D1. Hence, we get [q0 q3] and [q2 q4] as equivalent states. Thus minimal DFA is as shown in
the Fig. 1.35.

Fig. 1.45
1.45
Theoretical Computer Science Finite Automaton

Table 1.34: Transition table


δ a b
[q0 q3] q1 [q2 q4]
q1 q1 [q0 q3]
[q2 q4] [q0 q3] q1
Example 2: Construct minimal DFA for the DFA shown in the Fig. 1.46.

Fig. 1.46: DFA

Table 1.35: Transition table


δ 0 1
a b f
b g c
c a c
d c g
e h f
f c g
g g e
h g c
Solution: First we mark nonfinal Vs final entries as X. Here c is the final state. So all entries
with respect to c (row and column) are mark as X i.e. (c, a) (c, b) (c, d) (c, e) (c, f) and (c, g) and
(c, h) are mark as X as shown in Table 1.36.
Then we check for remaining entries in the table.
1. First check (b, a)
δ (b, 0) = g ∉ F δ (b, 1) = c ∈ F
∴ b and a are distinguishable
δ (a, 0) = b ∉ F δ (a, 1) = f ∉ F
1.46
Theoretical Computer Science Finite Automaton

2. Check for (d, a)


δ (d, 0) = c ∈ F
∴ d and a are distinguishable
δ (a, 0) = b ∉ F
3. Check for (e, a)
δ (e, 0) = h ∉ F δ (e, 1) = f
not distinguisable
δ (a, 0) = b ∉ F δ (a, 1) = f
4. Check for (b, e)
δ (b, 0) = g δ (b, 1) = c ∈ F
distinguisable
δ (e, 0) = h δ (e, 1) = f ∉ F
5. Check for (f, d)
δ (f, 0) = c δ (f, 1) = g
not distinguisable
δ (d, 0) = c δ (d, 1) = g
6. Check for (a, h)
δ (a, 0) = b δ (a, 1) = f ∉ F
distinguisable
δ (h, 0) = g δ (h, 1) = c ∈ F
We continue this for all entries in the table and mark X if they are distinguishable. Otherwise
mark as '='.
Table 1.36

We conclude that the equivalent states are a = e, b = h and d = f. Since (a, e), (b, h) and (d, f)
are not distinguishable. Therefore they are equivalent. Hence, the minimal finite automaton is as
shown in the Fig. 1.47.
1.47
Theoretical Computer Science Finite Automaton

Fig. 1.47: Minimal DFA


EXAMPLES [Oct. 17]
Example 1: Design DFA for language containing strings ending in '00' over alphabet {0, 1}.
Solution:

Fig. 1.48: DFA


Table 1.37: Transition table
δ 0 1
start → q0 q1 q0
q1 q2 q0
final → q2 q2 q0
Example 2: Construct DFA for language whose strings are binary representation of numbers,
multiples of 5 and starting with 1 to be accepted.
Solution: Example. → Strings are,
5 → 101, 10 → 1010, 15 → 1111,
20 → 10100, 25 → 11001, 30 → 11110

Fig. 1.49: DFA


1.48
Theoretical Computer Science Finite Automaton

Table 1.38: Transition table


δ 0 1
start → q0 q6 q1
q1 q2 q3
q2 q4 q5
q3 q1 q2
q4 q3 q6
final → q5 q5 q1
q6 q6 q6 ← rejected state
Example 3: Construct NFA for language containing all strings over {0, 1} such that some
two zero's are separated by a string, whose length is 4i, i ≥ 0.
Solution:

Fig. 1.50: NFA


Example 4: Construct NFA for language containing set of all strings over {0, 1} such that
10th symbol from right end is 1.
Solution:

Fig. 1.51: NFA


Example 5: Design Moore and Mealy machine for a binary input sequence such that if it has
a substring "101", the machine outputs A, if it has a substring "110" the machine outputs B,
otherwise it outputs C.
Solution: To design such machines, it should be checked for sequences 101 and 110. Both
these sequences are of length three and starting with the symbol 1, so start state has two paths,
one will be on substring 101 and other on 110.

Fig. 1.52 (a): Moore Machine


1.49
Theoretical Computer Science Finite Automaton

Fig. 1.52 (b): Mealy machine


Example 6: Design a Mealy machine which outputs 1's complement of input string treated as
a binary number.
Solution:

Fig. 1.53: Mealy Machine


Example 7: Construct a Moore machine equivalent to a Mealy machine in Fig. 1.54.

Fig. 1.54: Moore Machine


Solution:
Table 1.39: Transition table
δ' Output
a b λ
[q0, 0] [q2, 1] [q3, 0] 0
[q0, 1] [q2, 1] [q3, 0] 1
[q1, 1] [q0, 0] [q1, 1] 1
[q2, 0] [q1, 1] [q2, 0] 0
[q2, 1] [q1, 1] [q2, 0] 1
[q3, 0] [q2, 0] [q0, 1] 0
1.50
Theoretical Computer Science Finite Automaton

Fig. 1.55: Transition Diagram


Example 8: Draw a DFA for all strings starting with '10' and having substring 201 in it.
Solution:

Fig. 1.56: DFA


Table 1.40: Transition table
δ 0 1 2
Start → q 0 q 6 q 1 q6
q1 q2 q6 q6
q2 q2 q2 q3
q3 q4 q2 q3
q4 q2 q5 q3
Final → q5 q5 q5 q5
q6 q6 q6 q6
Example 9: Draw a DFA for L = L1 ∩ L2 over {a, b}
where L1 = All strings starting with 'a'
L2 = All strings not having 'ab' as substring.
Solution: L = all string starting with a and not having 'ab' as substring.

Fig. 1.57: DFA


1.51
Theoretical Computer Science Finite Automaton

Table 1.41: Transition table


δ a b
Start q0 q1 q3
q1 q1 q2
Final q2 q1 q2
q3 q3 q3 → rejected state
Example 10: Construct a DFA for a language over {0, 1, 2} which accepts all strings
containing atleast one occurrence of a double symbol.
L = {0022, 0011, 0000, 01211, 12122, 2100 …}
Solution:
0
q1
0 0 0, 1, 2
1 0
Start 1 1
q0 q2 q2

1 2
2 2
q3

2
Fig. 1.58: DFA
Table 1.42: Transition table
δ 0 1 2
q0 q1 q2 q3
q1 q4 q2 q3
q2 q1 q4 q3
q3 q1 q2 q4
q4 q4 q4 q4
Example 11: Construct a DFA over {0, 1} such that every even position is occupied by '0'
and odd by '1'.
Solution:

Fig. 1.59: DFA


1.52
Theoretical Computer Science Finite Automaton

Table 1.43: Transition table


δ 0 1
q0 q2 q1
q1 q0 q2
q2 q2 q3
Example 12: Construct a DFA to accept the set of all strings over {0, 1} such that every pair
of adjacent 0's appear before any pair of adjacent 1's.
Solution:

Fig. 1.60: DFA


Table 1.44: Transition table
δ 0 1
q0 q1 q0
q1 q2 q0
q2 q5 q3
q3 q5 q4
q4 q4 q4
q5 q5 q5
Example 13: Construct a DFA containing all strings starting with 01 and having 012 as
substring.
Solution:

Fig. 1.61: DFA

1.53
Theoretical Computer Science Finite Automaton

Table 1.45: Transition table


δ 0 1 2
start → q0 q1 q6 q6
q1 q6 q2 q6
q2 q4 q5 q3
final → q3 q 3 q3 q3
q4 q4 q2 q5
q5 q4 q5 q5
q6 q6 q6 q6 → rejected state
Example 14: Construct a DFA for
L = L1 ∩ L2 over {a, b}
where L1 = all strings starting with a and having even number of b's
L2 = all strings having odd number of b's
Solution: L = L1 ∩ L2 = φ.
L1 = {a, abab, abb, abbabab, …}
L2 = {b, ba, babb, abbb, ab, …}
No strings are common.
Example 15: Construct a DFA which accepts all strings ends with 1 over {0, 1, 2}.
Solution:

Fig. 1.62: DFA


Table 1.46: Transition table
δ 0 1 2
q0 q0 q1 q0
q1 q0 q1 q0
q1 is a final state.
Example 16: Construct a DFA which accepts all strings having substring ‘aba’ in it
over {a, b}.
Solution:

Fig. 1.63: DFA


1.54
Theoretical Computer Science Finite Automaton

Table 1.47: Transition table


δ a b
start → q 0 q 1 q 0
q1 q1 q2
q2 q3 q0
final → q3 q3 q3
Example 17: Construct DFA to accept all those strings starting with 00 but 00 does not
occur anywhere else in the string over {0, 1}.
Solution:

Fig. 1.64: DFA


Table 1.48: Transition table
δ 0 1
q0 q1 q4
q1 q2 q4
q2 q3 q2
q3 q4 q2
q4 q4 q4
Example 18: Construct a NFA to accept string which contains substring ‘abbc’ in it
over {a, b, c}.
Solution:

Fig. 1.65: NFA


Table 1.49: Transition table
δ a b c
q0 {q0, q1} {q0} {q0}
q1 φ {q2} φ
q2 φ {q3} φ
q3 φ φ {q 4}
q4 {q4} {q4} {q4}
q4 is final state.
1.55
Theoretical Computer Science Finite Automaton

Example 19: Design a DFA which checks whether a given decimal no. is even or not using
binary representation.
Solution: The given decimal number is even when its binary representation contains only ‘0’
at LSB. e.g. 100, 110, etc.
Now when number is entered from LSB, we can have DFA as shown below.
M = (Q, Σ, δ, S, F0) such that
Q = {q0, q1} , Σ = {0, 1}
S = {q0} , F0 = {q1}
L (M) = {∈, 0, 01, 0101, 0111, 01001, ……}

Fig. 1.66: DFA


Table 1.50: Transition table
δ 0 1
q0 q1 φ
q1 q1 q 1
For example, for number 14, we have binary representation as 1110, thus we can have it as
from LSB
δ (q0, 1110)
∴ δ (q0, 0) = q1
∴ δ (q0, 01) = δ (δ (q0, 0), 1) = δ (q1, 1) = q1
δ (q0, 011) = δ (δ (q0, 01), 1) = δ (q1, 1) = q1
δ (q0, 0111) = δ (δ (q0, 011), 1) = δ (q1, 1) = q1
As we get final state string is accepted.
Example 20: Design a DFA which checks whether given number is (decimal) divisible by 3.
Solution:

Fig. 1.67: DFA


1.56
Theoretical Computer Science Finite Automaton

M = (Q, Σ, δ, q0, F)
where Q = {q0, q1, q2},
Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
δ = Q×Σ→Q
q0 = q0
F = {q0}
Table 1.51: Transition table
δ 0 1 2 3 4 5 6 7 8 9
q0 q0 q1 q2 q0 q1 q2 q0 q1 q2 q0
q1 q1 q2 q0 q1 q2 q0 q1 q2 q0 q1
q2 q2 q0 q1 q2 q0 q1 q2 q0 q1 q2
^
δ (q, ∈) = q and δ (q, ωa) = δ (δ (q, w), a)
Consider 873,
^
δ (q0, 8) = q2
δ (q0, 87) = δ (δ (q0, 8), 7) = δ (q2, 7) = q0
^
δ (q0, 873) = δ (δ (q0, 87), 3) = δ (q0, 3) = q0
∴ q0 ∈ F ∴ String 873 is accepted by DFA.
Example 21: Design a DFA which checks whether a binary number is divisible by 5 or not
and also show the string 110011 is accepted.
Solution:

Fig. 1.68: DFA


M = (Q, Σ, δ, q0, F)
where Q = {q0, q1, q2, q3, q4}, Σ = {0, 1}
q0 = q0 , F = {q0}
and δ = Q × Σ → as
Table 1.52: Transition table
δ 0 1
q0 q0 q1
q1 q2 q3
q2 q4 q0
q3 q1 q2
q4 q3 q4
1.57
Theoretical Computer Science Finite Automaton

^
δ (q0, a) = q
and δ (q, wa) = δ (δ (q, w), a)
Consider string 1110011.
^
δ (q0, 1) = q1
^
δ (q0, 11) = δ (δ (q0, 1), 1) = δ (q1, 1) = q3
δ (q0, 111) = δ (δ (q0, 11), 1) = δ (q3, 1) = q2
δ (q0, 1110) = δ (δ (q0, 111), 0) = δ (q2, 0) = q4
δ (q0, 11100) = δ (δ (q0, 1110), 0) = δ (q4, 0) = q3
δ (q0, 111001) = δ (δ (q0, 11100), 1) = δ (q3, 1) = q2
δ (q0, 1110011) = δ (δ (q0, 111001),1) = δ (q2, 1) = q0
q0 is final state. ∴ String is accepted.
Example 22: Design a DFA which accepts odd number of 1’s and even number of 0’s over
{0, 1}.
Solution:

Fig. 1.69: DFA


M = (Q, Σ, δ, q0, F)
where Q = {q0, q1, q2, q3} , Σ = {0, 1}
q0 = q0
F = {q1}
δ = Q×Σ→Q
L (M) = {11, 101, 011, 00001, 0010101, ……}
Table 1.53: Transition table
δ 0 1
q0 q3 q1
final state → q1 q2 q0
q2 q1 q3
q3 q0 q2

1.58
Theoretical Computer Science Finite Automaton

^
We have, δ (q, ∈) = q and δ (q, a) = q'
δ (q, ωa) = δ (δ (q, ω), a)
Consider a string 0110100.
δ (q0, 0) = q3
δ (q0, 01) = δ (δ (q0, 0), 1) = δ (q3, 1) = q2
δ (q0, 011) = δ (δ (q0, 01), 1) = δ (q2, 1) = q3
δ (q0, 0110) = δ (δ (q0, 011), 0) = δ (q3, 0) = q0
δ (q0, 01101) = δ (δ (q0, 0110), 1) = δ (q0, 1) = q1
δ (q0, 011010) = δ (δ (q0, 01101), 0) = δ (q1, 0) = q2
δ (q0, 0110100) = δ (δ (q0, 011010), 0) = δ (q2, 0) = q1
Hence we get q1 ∈ F
∴ String is accepted by DFA.
Example 23: Design a DFA which accepts a language
L = {x | x has neither “aa” nor “bb” as a substring}
Solution:

Fig. 1.70: DFA


M = (Q, Σ, δ, q0, F)
where Q = {q0, q1, q2} , Σ = {a, b}
δ = Q×Σ→Q
q0 = q0
F = {q1, q2}
L (M) = {a, b, ab, bc, aba, bab, abab, ……}
Table 1.54: Transition table
δ a b
q0 q1 q2
q1 φ q2
q2 q1 φ

1.59
Theoretical Computer Science Finite Automaton

Consider string babab.


δ (qo, b) = q2
δ (q0, ba) = δ (δ (q0, b), a) = δ (q2, a) = q1
δ (q0, bab) = δ (δ (q0, ba), b) = δ (q1, b) = q2
δ (q0, baba) = δ (δ (q0, bab), a) = δ (q2, a) = q1
δ (q1, babab) = δ (δ (q0, baba), b) = δ (q1, b) = q2
which is a final state.
∴ String is accepted by DFA.
Example 24: Design a DFA in which every ‘b’ is preceded by ‘a’ over a, b, c.
Solution:

Fig. 1.71: DFA


M = (Q, Σ, δ, qo, F0)
where Q = {q0, q1, q2}
Σ = {a, b, c}
δ = Q×Σ→Q
q0 = q0
F0 = {q0, q1, q2}
Table 1.55: Transition table
δ a b c
q0 q1 q3 q0
q1 q1 q2 q0
q2 q1 q3 q2
q3 q3 q3 q3
L (M) = {a, c, ab, ac, cc, aa, abc, ababc, ……}
Consider a string abcab.
δ (q0, a) = q1
δ (q0, ab) = δ (δ (q0, a), b) = δ (q1, b) = q2
δ (q0, abc) = δ (δ (q0, ab), c) = δ (q2, c) = q2
δ (q0, abca) = δ (δ (q0, abc), a) = δ (q2, a) = q1
δ (q0, abcab) = δ (δ (q0, abca), b) = δ (q1, b) = q2 ∈ F
which is a final state.
∴ String abcab is accepted by this DFA.
1.60
Theoretical Computer Science Finite Automaton

Example 25: Design a DFA which accepts all strings over a, b, c such that if it starts with a,
then it should contain even number of ‘b’s. Else if it starts with ‘c’, then it should contain
substring ‘cba’ in it.
Solution:

Fig. 1.72: DFA


M = (Q, Σ, δ, q0, F)
Q = {q0, q1, q2, q3, q4, q5, q6}
Σ = {a, b, c}
δ = Q×Σ→Q
q0 = q0
F0 = {q1, q5}
L (M) = {ab, cba, abcb, acbb, cabcabc}
Table 1.56: Transition table
δ a b c
q0 q1 φ q 3
q1 q1 q2 q1
q2 q2 q1 q2
q3 q6 q4 q3
q4 q5 q6 q3
q5 q5 q5 q5
q6 q6 q6 q3
Consider string ‘cbbcba’
δ (q0, c) = q3
δ (q0, cb) = δ (δ (q0, c), b) = δ (q3, b) = q4
δ (q0, cbb) = δ (δ (q0, cb), b) = δ (q4, b) = q6
δ (q0, cbbc) = δ (δ (q0, cbb), c) = δ (q6, c) = q3
δ (q0, cbbcb) = δ (δ (q0, cbbc), b) = δ (q3, b) = q4
δ (q0, cbbcba) = δ (δ (q0, cbbcb), a) = δ (q4, a) = q5
q5 ∈ F
∴ String is accepted by DFA.
1.61
Theoretical Computer Science Finite Automaton

Example 26: Design a DFA which accepts all strings over {0, 1, 2} such that if it starts with
‘0’ then contains substring ‘01’ else it should contain substring ‘112’ in it.
Solution:

Fig. 1.73: DFA


M = (Q, Σ, δ, q0, F)
where Q = {q0, q1, q2, q3, q4, q5, q6, q7}
Σ = {0, 1, 2}
F = {q2, q5}
q0 = q0
Table 1.57: Transition table
δ 0 1 2
q0 q1 q3 q7
q1 q1 q2 q6
q2 q2 q2 q2
q3 q7 q4 q7
q4 q7 q3 q5
q5 q5 q5 q5
q6 q1 q6 q6
q7 q7 q3 q7
Consider the string 001101.
δ (q0, 0) = q1
δ (q0, 00) = δ (δ (q0, 0), 0) = δ (q1, 0) = q1
δ (q0, 001) = δ (δ (q0, 01), 1) = δ (q1, 1) = q2

1.62
Theoretical Computer Science Finite Automaton

δ (q0, 0011) = δ (δ (q0, 001), 1) = δ (q2, 1) = q2


δ (q0, 00110) = δ (δ (q0, 0011), 0) = δ (q2, 0) = q2
δ (q0, 001101) = δ (δ (q0, 00110), 1) = δ (q2, 1) = q2 ∈ F
∴ String is accepted.
Example 27: Design a DFA which accepts all strings not having abc as substring over
{a, b, c}.
Solution:

Fig. 1.74: DFA


M = (Q, Σ, δ, q0, F)
where Q = {q0, q1, q2, q3} , Σ = {a, b, c}
q0 = q0
F = {q0, q1, q2}
δ = Q×Σ→Q
Table 1.58: Transition table
δ a b c
q0 q1 q0 q0
q1 q1 q2 q0
q2 q1 q0 q3
q3 q3 q3 q3
Consider the string acabc.
δ (q0, a) = q1
δ (q0, ac) = δ (δ (q0, a), c) = δ (q1, c) = q0
δ (q0, aca) = δ (δ (q0, ac), a) = δ (q0, a) = q1
δ (q0, acab) = δ (δ (q0, aca), b) = δ (q1, b) = q2
δ (q0, acabc) = δ (δ (q0, acab), c) = δ (q2, c) = q3
As q3 ∉ F, ∴ String is not accepted.
Example 28: Design a FA which reads strings made up of letters in the word CHAPTER and
recognize those strings containing word CAP as substring.
Solution:

Fig. 1.75: FA
1.63
Theoretical Computer Science Finite Automaton

M = (Q, Σ, δ, q0, F)
where Q = {q0, q1, q2, q3}
Σ = {C, H, A, P, T, E, R}
δ = Q×Σ→Q
q0 = q0
F = {q3}
L (M) = {CAP, ACAP, ACAPTER, CAHTCPCACAPR}
Table 1.59: Transition table
δ C H A P T E R
q0 q1 q0 q0 q0 q0 q0 q0
q1 q1 q0 q2 q0 q0 q0 q0
q2 q1 q0 q0 q3 q0 q0 q0
q3 q3 q3 q3 q3 q3 q3 q3
Consider a string HCPCAPR.
δ (q0, H) = q0
δ (q0, HC) = δ (δ (q0, H), C) = δ (q0, C) = q1
δ (q0, HCP) = δ (δ (q0, HC), P) = δ (q1, P) = q0
δ (q0, HCPC) = δ (δ (q0, HCP), C) = δ (q0, C) = q1
δ (q0, HCPCA) = δ (δ (q0, HCPC), A) = δ (q1, A) = q2
δ (q0, HCACAP) = δ (δ (q0, HCPCA), P) = δ (q0, P) = q3
∴ δ (q0, HCPCAPR) = δ (δ (q0, HCPCAP), R) = δ (q3, R) = q3
As q3 ∈ F, ∴ String is accepted by DFA.
Example 29: Design a NFA which accepts all strings containing even number of ‘a’s over
{a, b}.
Solution:

Fig. 1.76: NFA


M = (Q, Σ, δ, q0, F)
Q = {q0, q1}
Σ = {a, b}
δ = Q × Σ → 2Q
q0 = q0
F = {q0}
L (M) = {∈, b, aa, baba, bbaba, aaaa, ……}
1.64
Theoretical Computer Science Finite Automaton

Table 1.60
δ a b
q0 q1 q0
q1 q0 q1
Consider string ‘babab’.
δ (q0, b) = q0
δ (q0, ba) = δ (δ (q0, b), a) = δ (q0, a) = q1
δ (q0, bab) = δ (δ (q0, ba), b) = δ (q1, b) = q1
δ (q0, baba) = δ (δ (q0, bab), b) = δ (q1, a) = q0
∴ δ (q0, babab) = δ (δ (q0, baba), b) = δ (q0, b) = q0
As q0 ∈ F, ∴ String is accepted by FA.
Example 30: Design a NFA which accepts the language with strings having even number of
‘a’s and odd number of ‘b’s over {a, b}.
Solution:

Fig. 1.77: NFA


M = (Q, Σ, δ, q0, F)
where Q = {q0, q1, q2, q3}
Σ = {a, b}
δ = Q × Σ → 2Q
q0 = q0
F = {q1}
L (M) = {b, aab, aba, baa, baabb, ……}
Table 1.61: Transition table
δ a b
q0 q3 q1
q1 q2 q0
q2 q1 q3
q3 q0 q2

1.65
Theoretical Computer Science Finite Automaton

Consider string ‘abab’.


δ (q0, a) = q3
δ (q0, ab) = δ (δ (q0, a), b) = δ (q3, b) = q2
δ (q0, aba) = δ (δ (q0, ab), a) = δ (q2, a) = q1
∴ δ (q0, abab) = δ (δ (q0, aba), b) = δ (q1, b) = q0
As q0 ∉ F, ∴ String is not accepted by FA.
Example 31: Design a NFA to accept string containing ‘aa’ but not 'bb' over {a, b}.
Solution:

Fig. 1.78: NFA


M = (Q, Σ, δ, q0, F)
Q = {q0, q1, q2, q3, q4}
Σ = {a, b}
δ = Q × Σ → 2Q
q0 = q0
F = {q2, q4}
L (M) = {aa, aab, aaba, babaa, ……}
Table 1.62: Transition table
δ a b
q0 q1 q3
q1 q2 q3
q2 q2 q4
q3 q1 φ
q4 q2 φ
Consider a string ‘abaab’.
δ (q0, a) = q1
δ (q0, ab) = δ (δ (q0, a), b) = δ (q1, b) = q3
δ (q0, aba) = δ (δ (q0, ab), a) = δ (q3, a) = q1
δ (q0, abaa) = δ (δ (q0, aba), a) = δ (q1, a) = q2
∴ δ (q0, abaab) = δ (δ (q0, abaa), b) = δ (q2, b) = q4
As q4 ∈ F, ∴ String is accepted by FA.
1.66
Theoretical Computer Science Finite Automaton

Example 32: Design a NFA with second last symbol is ‘a’ over {a, b}.
Solution:

Fig. 1.79: NFA


M = (Q, Σ, δ, q0, F)
Q = {q0, q1, q2}
Σ = {a, b}
δ = Q × Σ → 2Q
q0 = q0
F = {q2}
L (M) = {aa, ab, aab, abab, ……}
Table 1.63: Transition table
δ a b
q0 {q0, q1} {q0}
q1 {q2} {q2}
q2 φ φ
Consider string ‘abaa’.
δ (q0, a) = {q0, q1}
δ (q0, ab) = δ (δ (q0, a), b) = δ (q0, a) ∪ δ (q1, b)
= {q0, q2}
δ (q0, aba) = δ (δ (q0, ab), a) = δ (q0, a) ∪ δ (q2, a)
= {q0, q1}
δ (q0, abaa) = δ (δ (q0, aba), a) = δ (q0, a) ∪ δ (q1, a)
= {q0, q1, q2}
But q0, q1 ∉ F, q2 ∈ F
∴ String is accepted.
Example 33: Design NFA which accepts all strings containing every symbol in alphabetical
order over Σ = {p, q, a, b}.
Solution:

Fig. 1.80: NFA


1.67
Theoretical Computer Science Finite Automaton

M = (Q, Σ, δ, q0, F)
Q = {q0, q1, q2, q3}
Σ = {a, b, p, q}
q0 = q0
F = {q3}
L (M) = {a, b, p, q, abbpp, qq}
Table 1.64: Transition table
δ a b p q
q0 q0 φ φ φ
q1 φ q1 φ φ
q2 φ φ q2 φ
q3 φ φ φ q3
Example 34: Construct a DFA to accept the language
L = {ω | ω is of even length and begins with 01}
Solution:

Fig. 1.81: DFA


Example 35: Construct DFA for the languages which checks whether a given decimal
number is even.
Solution: A number is even means it is a multiple of 2. i.e. it should be divisible by 2.
∴ The strings accepted by this language are: 2, 4, 6, 8, 10, 12, 14, ……

Fig. 1.82: DFA


Table 1.65: Transition table
δ 0 1 2 3 4 5 6 7 8 9
q0 q0 q1 q0 q1 q0 q1 q0 q1 q0 q1
q1 q1 q0 q1 q0 q1 q0 q1 q0 q1 q0

1.68
Theoretical Computer Science Finite Automaton

Let us take decimal number which is even i.e. 14.


∴ 14 = 7 + 7
∴ δ (q0, 7) = q1
δ (q0, 14) = δ (δ (q0, 7), 7) = δ (q1, 7) = q0
Definition: M = {Q, Σ, δ, q0, F}
where Q = Finite set of states i.e. {q0, q1}
Σ = {0, 1, 2, ……, 9}
δ = Q×Σ→Q
q0 = Initial state
Final state (F) = {q0}
Example 36: Construct a DFA for a languages over alphabets {a, b, c} which accepts all
strings containing ‘a’ at every even position in the string.
Solution:

Fig. 1.83: DFA


Table 1.66: Transition table
δ a b c
q0 q2 q1 q1
q1 q0 q2 q2
q2 q2 q2 q2

EXAMPLES (MINIMIZATION OF DFA)


Example 1: Construct the minimize DFA for the following DFA.
δ 0 1
Start A B F
B G C
Final C A C
D C G
E H F
F C G
G G E
H G C
1.69
Theoretical Computer Science Finite Automaton

Solution:
Table 1.67

1. Draw grid.
2. Marked the entries with hyphen '-' as shown above.
3. From transition table we can directly find q Do p such that either q∈F and P∉F or q∉F
and P∈F. Mark the entries 'X'. This process is continue for length of string more than
(1 ≥ 1) i.e. D1, D2 ….
First we marked some entries with Do from transition table by checking non-final and
final states. The remaining entries we can proceed further with more length as follows:
(i) E D2 A
0 0 0 1 1 1
E → H → G → G GE → F → G → E
0 0 0 1 1 1
A → B → G → G GA → F → G → E
Both states shown with circle are non-final and same, so we can't distinguish A and E
for i = 2.
(ii) G D2 A
0 0 0 1 1 1
G → G → G → G G → E → F → G
0 0 0 1 1 1
A → B → G → G A → F → G → E
distinguishable
(iii) H D2 B
0 0 0 1 1 1
H → G → G → G H → C → C → C
0 0 0 1 1 1
B → G → G → G B → C → C → C
can't distinguish
1.70
Theoretical Computer Science Finite Automaton

(iv) F D2 D
0 0 0 1 1 1
F → C → A → B F → G → E → F
0 0 0 1 1 1
D → C → A → B D → G → E → F
can't distinguish
(v) G D2 E
0 0 0 1 1 1
G → G → G → G G → E → F → G
0 0 0 1 1 1
E → H → G → G E → F → G → E
distinguishable
Therefore we will combine the following states which are non-distinguishable.
(a) A and E
(b) B and H
(c) F and D
C is final state.
Transition table for minimized DFA is
δ 0 1
Start [A E] [BH] [FD]
[B H] [G] [C]
Final C [AE] [C]
[F D] [C] [G]
[G] [G] [AE]

Fig. 1.84
Example 2: Construct the minimize DFA for the following DFA.
Table 1.68
δ 0 1
Start a b a
b a c
c d b
Final *d d a
e d f
f g e
g f g
h g d
1.71
Theoretical Computer Science Finite Automaton

Solution:
Table 1.69

We direct proceed for D2 entries.


(i) b D2 a
0 0 0 1 1 1
b → a → b → a b → c → b → c
0 0 0 1 1 1
a → b → a → b a → a → a → a
(ii) f D2 a
0 0 0 1 1 1
f → g → f → g f → e → f → e
0 0 0 1 1 1
a → b → a → b a → a → a → a
(iii) g D2 a
0 0 0 1 1 1
g → f → g → f g → g → g → g
0 0 0 1 1 1
a → b → a → b a → a → a → a
(iv) f D2 b
0 0 0 1 1 1
f → g → f → g f → e → f → e
0 0 0 1 1 1
b → a → b → a b → c → b → c
(v) g D2 b
0 0 0 1 1 1
g → f → g → f g → g → g → g
0 0 0 1 1 1
b → a → b → a b → c → b → c
1.72
Theoretical Computer Science Finite Automaton

(vi) g D2 f
0 0 0 1 1 1
g → f → g → f g → g → g → g
0 0 0 1 1 1
f → g → f → g f → e → f → e
Similarly e D2 c and c D2 b are also distinguishable. Like this if we proceed for more length
we will not get here any non-distinguishable states. Therefore this DFA cannot be minimize.
Example 3: Construct minimal DFA for the following DFA.
δ 0 1
Start q0 q4 q0
Final q1 q1 q0
q2 q1 q3
q3 q7 q2
q4 q0 q5
q5 q1 q4
q6 q7 q1
q7 q3 q7
Solution: q0 Do q1
0
q0 → q4 ∉ F
0
q1 → q1 ∈ F distinguishable mark X.
q0 Do q2
0 1
q0 → q4 ∉ F q0 → q0
0 1
q2 → q1 ∈ F q2 → q3
Mark X.
Similarly make all Do entries then D1 entries
e.g. q3 D1 q0
0 0 1 1
q3 → q7 → q3 q3 → q2 → q3
0 0 1 1
q0 → q4 → q0 q0 → q0 → q0
Both reachable states are different. Hence, proceed with D2 entries.
(i) q3 D2 q0
0 0 0 1 1 1
q3 → q7 → q3 → q7 q3 → q2 → q3 → q2
0 0 0 1 1 1
q0 → q4 → q0 → q4 q0 → q0 → q0 → q0

1.73
Theoretical Computer Science Finite Automaton

Again distinguishable
(ii) q4 D2 q0
0 0 0 1 1 1
q4 → q0 → q4 → q0 q4 → q5 → q4 → q5
0 0 0 1 1 1
q0 → q4 → q0 → q4 q0 → q0 → q0 → q0
(iii) q7 D2 q0
0 0 0 1 1 1
q7 → q3 → q7 → q3 q7 → q7 → q7 → q7
0 0 0 1 1 1
q0 → q4 → q0 → q4 q0 → q0 → q0 → q0
Similarly,
q5 D2 q2
q3 D2 q4
q3 D2 q7
q4 D2 q7
All these remaining entries also we get as distinguishable as shown below.
Table 1.70

Since, all entries are distinguishable. We cannot minimize the DFA.

EXAMPLES (FA WITH OUTPUT)


Example 1: Design a Mealy machine which outputs even or odd according to number of 1's
encountered is even or odd over {0, 1}.
Solution:

Fig. 1.85
1.74
Theoretical Computer Science Finite Automaton

Example 2: Construct a Moore machine that outputs valid or invalid for a language
L = a (a + b)* b.
Solution:

Fig. 1.86
Example 3: Design a Mealy machine for input (a + b + c)* if input ends with 'bac', print A
else print B.
Solution:
a/B, c/B b/B b/B
b/B
Start q0 b/B q1 a/B q2 c/A q3

c/B
a/B

c/B, a/B
Fig. 1.87
Example 4: Design a Moore machine for binary input sequence such that if it has a substring
101, the machine output A, if it has substring 110 then outputs B else outputs C.
Solution:

Fig. 1.88
Example 5: Construct a Moore machine to convert each occurrence of substring 100 by 101.
0 1
Start 1 0 0
q0 q1 q2 q3

1 0 1

1
Fig. 1.89
1.75
Theoretical Computer Science Finite Automaton

Example 6: Design Mealy machine for a binary input sequence such that if it has a substring
101, the machine outputs A, if it has a substring 110, the machine outputs B otherwise it
outputs C.
Solution:

Fig. 1.90

EXAMPLES
Example 1: Construct NFA for language L, where L = {a (a + b)* b}.
Solution:

Fig. 1.91
Example 2: Construct DFA equivalent to NFA

Fig. 1.92
Solution: (1) To find initial state , find ∈-closure of (q0).
∈-closure (q0) = {q0, q1} = A (say)
From A on 0
∈-closure {q0, q1, q2} = {q0, q1, q2, q3} = B
A on input 1
∈-closure {q1} = {q1} = C
B on 0
∈-closure {q0, q1, q2} = B

1.76
Theoretical Computer Science Finite Automaton

B on 1
∈-closure {q1} = C
C on 0
∈-closure {q1, q2} = {q1, q2, q3} = D
C on 1
∈-closure {φ} = φ
D on 0
∈-closure {q1, q2} = D
D on 1
∈-closure {q3} = q3 = E
E on 0
∈-closure {φ} = φ
E on 1
∈-closure {q3} = E
Equivalent DFA is

Fig. 1.93
Example 3: Construct Mealy machine over {0, 1} which toggles its input.
Solution:

Fig. 1.94
Table 1.71
δ 0 1 λ (q0, 0) = 1
q0 1 0 λ (q0, 1) = 0
Example 4: Construct NFA without ∈ for language L, where L = (0 + 1)* 01.
Solution:

Fig. 1.95
1.77
Theoretical Computer Science Finite Automaton

Table 1.72
δ 0 1
q0 {q0, q1} q0
q1 φ q2
q2 q2 q2
Example 5: Construct DFA equivalent to given NFA.

Fig. 1.96
Solution: ∈-closure (q0) = q0 → Initial state of DFA (say A)
From A on input a i.e. q0 on a is
∈-closure {q1} = {q0, q1, q2} = B
From A on input b is
∈-closure {q0, q1} = {q0, q1, q2} = B
B on input a
∈-closure {q1, q2} = {q1, q2, q0} = B
B on input b is
∈-closure {q0, q1, q2} = {q0, q1, q2} = B
No more states are added.
∴ DFA is

Fig. 1.97
Example 6: Construct Moore machine to generate 1's complement of binary number.
Solution: M = (Q, Σ, ∆, δ, λ, q0)

Fig. 1.98

1.78
Theoretical Computer Science Finite Automaton

Example 7: Construct FA for following regular expression


ab (a + b)* + ba (a + b)*
Solution: Let r1 = ab (a + b)* and r2 = ba (a + b)*
FA for r1 is M1.

Fig. 1.99 (a): FA M1 for r1


FA M2 for r2 is

Fig. 1.99 (b)


∴ FA M is

Fig. 1.99 (c)


Example 8: Construct NFA for language L where L = 00 (0 + 1)* 1.
Solution:

Fig. 1.100
1.79
Theoretical Computer Science Finite Automaton

Example 9: Construct Moore machine which outputs even or odd according to number of a’s
encountered is even or odd.
Solution: M = (Q, Σ, ∆, δ, λ, q0)

Fig. 1.101
Table 1.73
δ a b λ
q0 q1 q0 Even
q1 q0 q1 Odd
Example 10: Construct DFA for accepting string over {a, b} such that it starts with a and not
having substring “bac” in it.
Solution:

Fig. 1.102
Example 11: Construct DFA equivalent to NFA

Fig. 1.103
Solution: ∈-closure (q0) = {q0, q1, q2} = Initial state = A
From A on input
∈-closure {q1, q1, q2} = {q0, q1, q2} = A
From A on input 1
∈-closure {q1, q2} = {q0, q1, q2) = A
No more states are added. Therefore, DFA is

Fig. 1.104
1.80
Theoretical Computer Science Finite Automaton

Example 12: Construct NFA for language L = 01* (01)* + 1.


Solution:

Fig. 1.105
Example 13: Construct Mealy machine to convert each occurrence of substring 101 by 100
over alphabet {0, 1}.
Solution:

Fig. 1.106
Table 1.74
δ 0 1
q0 q0 q1
q1 q2 q1
q2 q0 q0
λ (q0, 0) = 0
λ (q0, 1) = 1
λ (q1, 0) = 0
λ (q1, 1) = 1
λ (q2, 0) = 0
λ (q2, 1) = 0
Example 14: Construct NFA for ab* + ba*.
Solution:

Fig. 1.107
1.81
Theoretical Computer Science Finite Automaton

Example 15: Construct DFA for a language over {0, 1, 2} which starts with “00”, ends with
“22” and having substring “11” in it.
Solution:

Fig. 1.108
Example 16: Minimize the following DFA.

Fig. 1.109
Solution: Step 1: Table 1.75

F = {q2]
So put mark X at entries q2 Vs all non-final states.
1.82
Theoretical Computer Science Finite Automaton

Step 2: Find out distance D1 in between remaining states.


δ (q1, 0) = q6 δ (q1, 1) = q2 ∈ F
distinguishable
δ (q0, 0) = q1 δ (q0, 1) = q5 ∉ F
Put mark X at entry (q0, q1).
Similarly, we find out distance for all remaining states . We get states q0 and q4 equivalent, q1
and q7 are equivalent, q3 and q5 are equivalent.
∴ Minimal DFA is

Fig. 1.110
Example 17: Construct a Mealy machine equivalent to the following Moore machine.
Table 1.76
States 0 1 Output
q0 q1 q2 1
q1 q3 q2 0
q2 q2 q1 1
q3 q0 q3 1
Solution:

Fig. 1.111: Moore machine


The equivalent Moore machine is:
λ' (q0, 0) = λ (δ (q0, 0)) = λ (q1) = 0
λ' (q0, 1) = λ (δ (q0, 1)) = λ (q2) = 1
λ' (q1, 0) = λ (δ (q1, 0)) = λ (q3) = 1
λ' (q1, 1) = λ (δ (q1, 1)) = λ (q2) = 1
λ' (q2, 0) = λ (δ (q2, 0)) = λ (q2) = 1

1.83
Theoretical Computer Science Finite Automaton

λ' (q2, 1) = λ (δ (q2, 1)) = λ (q1) = 0


λ' (q3, 0) = λ (δ (q3, 0) = λ (q0) = 1
λ' (q3, 1) = λ (δ (q3, 1)) = (q3) = 1
∴ Equivalent Moore machine is:

Fig. 1.112
Example 18: Construct FA for L = {∈}.
Solution:
Start

Fig. 1.113
Example 19: Define Moore machine. Design a Moore machine to change all vowels to '$'
and rest of the 21 alphabets changes to '#'.
Solution:
b,c,d.....z
a,e,i,o,u
Start a,e,i,o,u

# b,c,.....z $

Fig. 1.114
Example 20: Construct a DFA which accepts odd number of 1's and even number of 0's over
{0, 1}.
Solution:
1
q0 q2 Final state
1
0 0 0 0
1
q1 1 q3

Fig. 1.115

1.84
Theoretical Computer Science Finite Automaton

Example 21: Construct FA for regular expression: (ab)* = (a + b)* a * b.


Solution:
'

' a ' b '

Start '
'

a
' '
' ' ' ' a ' b

' '
b '

'
Fig. 1.116
Example 22: Design Mealy machine to determine the residue (remainder) mod 3 for a
decimal number.
Solution:
0,3,6,9/0 0,3,6,9/1 0,3,6,9/2

Start 1,9,7/1 1,4,7/2


q0 q1 q2

2,5,8/0 2,5,8/1

1,4,7/0
2,5,8/2
Fig. 1.117
Example 23: Construct DFA for the following NFA with ∈-moves.
b a, b
a, b a, b
Start
q0 q1 q2
' '

'
Fig. 1.118
Solution:
a, b
a, b
A B

Fig. 1.119
1.85
Theoretical Computer Science Finite Automaton

PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which uses mathematical and logical methods to understand the nature of computation
and to solve fundamental problems arising through the everyday practical use of
computer systems?
(a) Computer Science (CS) (b) Theoretical Computer Science (TCS)
(c) Science Theory (ST) (d) Computer Science Theory (CST)
2. The word ‘automata’ (plural and the singular is ‘automaton’), a ______ provides the
simplest model of a computing device.
(a) Finite Automaton (FA) (b) Finite Grammar (FG)
(c) Finite Language (GL) (d) None of the mentioned
3. Which is a finite, non empty set of symbols?
(a) a language (b) a grammar
(c) an alphabet (d) All of the mentioned
4. Which is an entity or individual objects, which can be any letter, alphabet or any
picture like 1, x, y, #?
(a) symbols (b) alphabets
(c) strings (d) All of the mentioned
5. Which is a finite collection of symbols from the alphabet.
(a) symbol (b) alphabet
(c) string (d) language
6. Which is a subset of Σ* for some alphabet Σ?
(a) symbol (b) alphabet
(c) string (d) language
7. Which is a set of rules to define a valid sentence in any language?
(a) grammar (b) language
(c) symbol (d) string
8. Noam Chomsky classified (Chomsky hierarchy) the grammar into following types
depending on the production rules:
(a) Type 0: Type 0 grammar is a phase structure grammar without any restriction. All
grammars are type 0 grammar. For example, Turing machine.
(b) Type 1: Type 1 grammar is called context-sensitive grammar. For example, Linear
Bounded Automata (LBA).
(c) Type 2: Type 2 grammar is called context-free grammar. In the LHS of the
production, there will no left or right context. For example, Push down automata
(PDA).
(d) Type 3: Type 3 grammar is called regular grammar. For example, Finite Automata
(FA).
(e) All of the mentioned
1.86
Theoretical Computer Science Finite Automaton

9. Which is generated from the rules of a grammar?


(a) a language (b) a symbol
(c) a grammar (d) an string
10. Finite automata M is represented as, M = {Q, Σ, δ, q0, F}.
(a) M = (Q, Σ, Γ, δ, q0, F) (b) M = (Q, Σ, Γ, δ, q0, B, ⊄, $, F)
(c) M = {Q, Σ, δ, q0, F} (d) None of the mentioned
11. In which FA, there is one and only one move from a given state to the next state of any
input symbol.
(a) NFA (b) DFA
(c) MFA (d) None of the mentioned
12. In which FA, there may be more than one move or no move from a given state to the
next state of any input symbol.
(a) NFA (b) DFA
(c) MFA (d) None of the mentioned
13. The construction of finite automata with a minimum number of states, which is
equivalent to the given Finite Automata (FA) is called as,
(a) Maximization of FA (b) Context-freeing of FA
(c) Minimization of FA (d) None of the mentioned
14. The ______ machine is one type of finite automata where output depends on the
present state only, but the output is independent of the present input.
(a) Moore (b) Mealy
(c) transition machine (d) None of the mentioned
15. The ______ machine is one type of fi nite automata with output, where the output
depends on the present state and the present input.
(a) Moore (b) Mealy
(c) transition machine (d) None of the mentioned
16. A transition diagram also called as,
(a) transition graph (b) transition system
(c) Both (a) and (b) (d) None of the mentioned
17. Consider the regular expression (0 + 1)(0 + 1) . . . . . N times. The minimum state FA
that recognizes the language represented by this regular expression contains,
(a) (n + 1) states (b) (n + 2) states
(c) n states (d) None of the mentioned
18. Which is the string with zero occurrences of symbols?
(a) full (b) finite
(c) null (d) symbolic
Answers
1. (b) 2. (a) 3. (c) 4. (a) 5. (c) 6. (d) 7. (a) 8. (e) 9. (a) 10. (c)
11. (b) 12. (a) 13. (c) 14. (a) 15. (b) 16. (c) 17. (a) 18. (c)

1.87
Theoretical Computer Science Finite Automaton

Q.II Fill in the Blanks:


1. The term "Automata" is derived from the Greek word "automatos" which means
"______".
2. An ______ (Automata in plural) is an abstract self-propelled computing device which
follows a predetermined sequence of operations automatically.
3. The ______ is the smallest building block in the theory of computation and can be any
letter, number or even picture.
4. ______ (Σ) are set of symbols, which are always finite.
5. FA is called finite because it has a finite number of ______.
6. ______ is an automaton that computes a Boolean function is called an acceptor.
7. ______ is a finite sequence of symbols from some alphabet denoted by ω.
8. ______ of a string is the number of symbols present in a string.
9. A ______ is a set of strings, chosen from some Σ* or we can say, a language is a subset
of Σ*.
10. The finite automata are called ______ when there exist many paths for specific input
from the current state to the next state.
11. ______ string is the string with zero occurrence of symbols, represented as ε.
12. Non-terminal symbols are those symbols which can be replaced multiple times and
______ symbols are those symbols which cannot be replaced further.
13. An automaton with a finite number of states is called a ______ or Finite State
Machine (FSM).
14. If any FA contains ε transaction or move, the finite automata is called ______.
15. The production rules of a grammar consist of two parts namely, ______ [(LHS) of a
production rule mainly contains the non-terminal symbols to be replaced and may
contain terminal and non-terminal both for some cases but at least one non-terminal] and
______ [(RHS) of a production rule may contain terminal, non-terminal or any
combination of terminal and non-terminal even null].
16. The finite automata are called ______ finite automata if the machine is read an input
string one symbol at a time.
17. The ______ machine was proposed by George H. Mealy at the Bell Labs in 1960.
18. John Myhill and Anil Nerode of the University of Chicago proposed a theorem in 1958
which provides a necessary and sufficient condition for a language to be regular and
theorem can also be used to ______ a FA.
19. A ______ M is a 5-tuple, MDFA = (Q, Σ, δ, q0, F).
20. A transition diagram (also called transition graph or transition system) is a finite directed
labeled graph in which each ______ represents a state and directed edges indicate the
transition from one state to another.
21. A ______ diagram or state transition diagram is a directed graph for FA.
22. In a Mealy machine, the output depends on the ______ present state and the present
input.
1.88
Theoretical Computer Science Finite Automaton

23. The transition ______ is basically a tabular representation of the transition function
which takes two arguments (a state and a symbol) and returns a state (the "next state").
24. The set of rules for constructing a language is called the ______ for that language.
25. The Moore machine, the output depends only on the present ______.
26. A ______ of a string is formed by taking any number of symbols from the end of the
string.
27. A ______ language is a set of strings of symbols drawn from a finite alphabet.
Answers
1. self-acting 2. automaton 3. symbol 4. Alphabets
5. states 6. Recognizer 7. String 8. Length
9. language 10. NFA 11. Empty 12. Terminal
13. Finite 15. Left Hand Side 16. deterministic
Automaton and Right
(FA) 14. NFA with ∈ move Hand Side

17. Mealy 18. minimization 19. DFA 20. vertex


21. transition 22. present 23. table 24. grammar
25. state 26. suffix 27. formal
Q.III State True or False:
1. The word "Automata" is the plural form of the word "Automaton" means an object that
is capable of doing something on its own or itself.
2. An automaton is a system where materials, energy, or information are transformed and
transmitted for performing some operation without the direct participation of a human.
3. A language which can be formed over ‘Σ’ is any subset Σ* and can be Finite or Infinite.
4. Any non empty finite set is called as an alphabet (Σ).
5. In NFA, there is a finite set of states, a finite set of input symbols, and a finite set of
transitions from one state to another state that occur on input symbol chosen to form an
alphabet; there is exactly one transition out of each state.
6. NFA can be converted to an equivalent DFA.
7. In FA input tape (divided into squares/cells) contains a string of symbols, with one
symbol in each tape square. The finite, control is the main part of the machine whose
internal status can be specified as one of a finite number of distinct states. Using a
movable reading head, the finite control can sense the symbol written at any position on
the input tape.
8. A language is not complete without grammar.
9. A finite automaton has a finite set of states with which it accepts or rejects strings.
10. DFA can have any number of transitions to the next state from a given state on a given
input symbol.
1.89
Theoretical Computer Science Finite Automaton

11. The Moore machine was proposed by Edward F. Moore in IBM around 1960.
12. By minimizing, we can get a minimized DFA with minimum number of states and
transitions which produces that particular language.
13. A DFA with minimized states needs less time to manipulate a regular expression.
14. The Myhill–Nerode theorem is used to minimize finite automata.
15. A transition graph or a transition system is a finite directed labeled graph in which each
vertex (or node) represents a state and the directed edges indicate the transition of a state
and the edges are labeled with input/output.
16. An FA has a infinite number of states.
17. A transition diagram or state transition diagram is a directed graph for FA.
18. NFA with ε can be converted to NFA without ε, and this NFA without ε can be
converted to DFA.
19. Maximization of FA means reducing the number of states from given FA.
20. In NFA, when a specific input is given to the current state, the machine goes to multiple
states. It can have zero, one or more than one move on a given input symbol.
21. In DFA, when a specific input is given to the current state, the machine goes to only one
state. DFA has only one move on a given input symbol.
22. In Mealy Machine the output depends both on the current state and the current input and
in Moore Machine the output depends only on the current state.
23. A finite automaton recognizes regular language.
24. A prefix of a string is the string formed by taking any number of symbols of the string.
Answers
1. (T) 2. (T) 3. (T) 4. (T) 5. (F) 6. (T) 7. (T) 8. (T) 9. (T) 10. (T)
11. (F) 12. (T) 13. (T) 14. (T) 15. (T) 16. (F) 17. (T) 18. (T) 19. (F) 20. (T)
21. (T) 22. (T) 23. (T) 24. (T)
Q.IV Answer the following Questions:
(A) Short Answer Questions:
1. What is symbol?
2. Define alphabet.
3. Define FA.
4. What is prefix and suffix?
5. Define formal language.
6. List operations on languages.
7. Define grammar.
8. What are DFA and NFA?
9. Define minimization of FA.
10. Give the purpose of Mealy and Moore machines.
11. Compare DFA and NFA (any two points).
1.90
Theoretical Computer Science Finite Automaton

(B) Long Answer Questions:


1. What is FA? Enlist its types.
2. What is the necessity of converting an NFA to a DFA?
3. What is null string and length of string? Explain with example.
4. Write a short note on: NFA with ε-Transitions to DFA.
5. For the following NFA, find an equivalent DFA.

6. Describe DFA as pattern recognizer in detail.


7. With the help of example describe Myhill-Nerode method for NFA to DFA.
8. Describe NFA with ε- transitions wit hexample.
9. Construct a DFA to accept the set of all strings over {0, 1} such that every pair of
adjacent 0's appears before any pair of adjacent 1's.
10. Construct DFA for language over alphabet {a, b} which accepts all strings containing 'a'
at every even position in the string.
11. Construct NFA for language over alphabet {a, b} which accepts all strings starting with
'a' and not having substring 'bbb' in it.
12. Construct DFA for L = L1 ∩ L2, where
L1 = all strings starting with 'a' containing even number of b's over {a, b}.
L2 = all strings containing odd number of b's over {a, b}.
13. Construct DFA to accept following languages:
(i) Set of all strings which contain exactly two c's anywhere over {a, b, c}.
(ii) Set of strings over {0, 1} whose second and second last symbol is 1.
(iii) {x ∈ {0, 1}* | x has neither 00 nor 11 as substring}.
(iv) All strings over {0, 1} in which every even position in string is occupied by 0 and
odd position by 1.
(v) Which accepts all strings starting with 01 and having substring 012 in it.
(vi) All strings starting with '10' and having substring '201' in it.
(vii) All strings over {0, 1} such that if it starts with 0 then it contains even number of
0's and if it starts with 1 then it contains odd number of 1's.
(viii) Language over alphabet {0, 1, 2} which accepts all strings containing atleast one
occurrence of double symbol.
1.91
Theoretical Computer Science Finite Automaton

14. Construct DFA for the following NFA with ∈-transitions:


(i)

(ii)

(iii)

15. (i) Construct minimal DFA for the following:


M = ({A, B, C, D, E, F, G, H}, {0, 1}, δ, A, {C}), where δ is
δ 0 1
A B F
B G C
C A C
D C G
E H F
F C G
G G E
H G C
(ii)

1.92
Theoretical Computer Science Finite Automaton

(iii)

(iv)

Fig. 1.87
(v)

(vi)
Start e a
1 1 5

a a
a,b
b
3 4

16. Design a Mealy machine for the following: for input from (a + b + c)* if input ends in
"bac" then print A, else print B.
17. Design a Mealy machine to get 1's complement of a given binary string.
18. Design a Moore machine to get 1's complement of a given binary string.
1.93
Theoretical Computer Science Finite Automaton

19. Design a Moore machine for binary input sequence such that if it has a substring 101, the
machine outputs A, if it has a substring 110, the machine outputs B, otherwise it outputs
C.
20. Design a Mealy machine which outputs EVEN or ODD according to number of 1's
encountered is even or odd over {0, 1}.
21. Write a mapping of δ in case of NFA and DFA.
22. Differentiate between Moore and Mealy machine.
23. Convert the following NFA with ∈-moves to DFA.
Start q0 ' q1 a q3
b
' b a,b

q2 a, ' q4 a,b q5

a
a,b
24. Find minimum state FA equivalent to the following DFA, M = ({q0, …, q5}, {a, b}, δ, q0,
{q4, q5)}.
b
a
Start a b
q0 q1 q4
a
b b
a b a
q2 q3 b
a
q5

UNIVERSITY QUESTIONS AND ANSWERS


April 2016
1. What are the proper prefixes and proper suffixes of the string "India”? [1 M]
Ans. Refer to Page 1.2.
2. Compare 'λ' function of Melay and Moore machine. [1 M]
Ans. Refer to Section 1.8.
3. Construct a DFA to accept all decimal numbers divisible by 3. [5 M]
Ans. Refer to Section 1.3.
4. Construct DFA equivalent to the following NFA: [5 M]

Ans. Refer to Section 1.5.


1.94
Theoretical Computer Science Finite Automaton

5. Minimize the following DFA: [4 M]

Ans. Refer to Section 1.9.

October 2016

1. Define NFA. [1 M]
Ans. Refer to Section 1.4.
2. Define proper suffix with the help of an example. [1 M]
Ans. Refer to Page 1.2.
3. Write the mapping of λ function in Mealy Machine. [1 M]
Ans. Refer to Section 1.8.
4. Construct a DFA to accept the set of all strings over ∑ = {a, b, c} such that the string
starts with ‘ac’ and not having ‘cab’ as substring in it. [5 M]
Ans. L = {ac, aca, acb, acc . . . . . . .}
The TD is as follows.

M = (Q, ∑, δ, q0, F)
Q = {q0, q1, q2, q3, q4, q5}
∑ = {a, b, c}
q0 = {q0}
F = {q2, q3, q4}
1.95
Theoretical Computer Science Finite Automaton

δ Q\∑ a b c
q0 q1 q5 q5
q1 q5 q5 q2
q2 q2 q2 q3
q3 q4 q2 q3
q4 q2 q5 q3
q5 q5 q5 q5
5. Convert the following NFA with ∈ moves to DFA. [5 M]

Ans. Initial state is q0, we take


∈-chosure (q0) = {q0, q1} − A − initial state of DFA
1. From A, on input ‘a’, we get q2, so
∈-chosure (q2) = {q2, q3, q5} = B
From A, on input ‘b’ we get q4, so
∈-chosure (q4) = {q4, q3, q5} = C
2. From B, on input ‘a’ we get only state q6
∈-chosure (q6) = {q6} = D
From B, on input ‘b’, we get only set φ.
3. From C, on input ‘a’ we get state q6
∈-closure (q6) = {q6} = D
From c, on input ‘s’, we get set φ.
4. From D, on input ‘a’ we get only set {φ}
From D, on input ‘b’ we get only state q7
∈-chosure (q7) = {q7} = E
5. From E on input ‘ab’, we get set {φ}
Also from E on input ‘b’ we get set {φ}
Equivalent DFA

1.96
Theoretical Computer Science Finite Automaton

6. Construct a Mealy machine of a language L over ∑ = {0, 1} which outputs ‘$’ if string
ends with ‘aba’, outputs ‘#’ if string ends with ‘bab’, otherwise outputs ‘*’. [5 M]
Ans. Mealy Machine

δ φ

Q\∑ a b Q\∈
∈ a b
q0 q1 q4 q0 * *
q1 q1 q2 q1 * *
q2 q3 q4 q2 $ *
q3 q1 q6 q3 * #
q4 q5 q4 q4 * *
q5 q1 q6 q5 * #
q6 q3 q4 q6 $ *
7. Differentiate between DFA and NFA. [5M]
Ans. Refer to Section 1.21.
April 2017
1. Give the mapping of 'δ' function of NFA with ∈ moves. [1 M]
Ans. Refer to Section 1.6.
2. If A = {∈}. Find the value of |A|. [1 M]
Ans. Value of A = 1.
3. Differentiate between Moore and Mealy machine. [1 M]
Ans. Refer to Page 1.43.
4. Construct a DFA to accept the set of all strings over ∑ = {0, 1, 2} such that the string
ends with '012' or '20'. [5 M]
Ans. L = {012, 20, 0012, 020 …}

1.97
Theoretical Computer Science Finite Automaton

1
1

0 0

Start 0 1 2
q0 q1 q2 q3

0 2
2 0
1 2

0
q4 q5

2
2

5. Convert the following given NFA to DFA: [5 M]


a b b
Start b a
q0 q1 q2

b a,b
Ans. Refer to Section 1.5.
6. Construct a Moore machine for a language L over ∑ = {0, 1} which outputs '$' if string
ends with '100', outputs '#' if string ends with '001', otherwise outputs '*'. [5 M]
Ans. Refer to Section 1.8.
7. Minimize the following DFA:
M = ({q0, q1, q2, q3, q4, q5, q6, q7}, {0, 1}, δ, q0, {q1}) where, δ is given by: [5 M]
δ a b
→ q0 q4 q0
*q1 q1 q0
q2 q1 q3
q3 q7 q2
q4 q0 q5
q5 q1 q4
q6 q7 q1
q7 q3 q7
Ans. Refer to Section 1.9.
October 2017
1. Define suffix of a string. Give one example. [1 M]
Ans. Refer to Page 1.2.
2. "DFA cannot have more than one final states". Justify. [1 M]
Ans. Refer to 1.2.
1.98
Theoretical Computer Science Finite Automaton

3. Write output function λ of Moore and Mealy machines. [1 M]


Ans. Refer to Section 1.8.
4. Construct DFA to accept substrings having both aa and bb over ∑ = {a, b}. [5 M]
Ans. Refer to Section 1.3.
5. Convert the following NFA to DFA: [5 M]

Ans. Refer to Section 1.5.


6. Minimize the following DFA using Myhill-Nerode theorem: [5 M]

Ans. Refer to Section 1.9.


7. Construct Moore and Mealy machines which outputs valid for valid strings and invalid
for invalid strings for language L = a(a + b) * b. [4 M]
Ans. Refer to Section 1.8.
April 2018
1. What are the proper prefix and proper suffix of the string ‘‘India’’? [1 M]
Ans. Refer to Page 1.2.
2. Define DFA. [1 M]
Ans. Refer to Section 1.3.
3. Write down the ∈-closure of each state from the following FA. [1 M]
0 1
Start e
q0 q1

Ans. Refer to Section 1.6.


4. Construct DFA containing all string starting with 01 and having 012 as substring. [5 M]
Ans. Refer to Section 1.3.
1.99
Theoretical Computer Science Finite Automaton

5. Construct the following NFA with ∈- moves to DFA: [5 M]


q2
a
Start b a
q0 q1 q3 q5 q6 q7

b q4

Ans. Refer to Section 1.6.


6. Construct minimal DFA for the following: [2 M]
0

0 q3
q1
0
Start q0 1 0 0 1

1
q2 q4
1
1
Ans. Refer to Section 1.9.
7. Construct Mealy machine to convert each occurrence of substring 101 by 100 over
alphabet {0, 1}. [4 M]
Ans. Refer to Section 1.8.
October 2018
1. Define suffix of a string. Give one example. [1 M]
Ans. Refer to Page 1.2.
2. Compare ‘λ’ function of Melay and Moore machine. [1 M]
Ans. Refer to Section 1.8.
3. Write down the ‘∈-closure’ of each state from the following FA: [3 M]
e
q0 q1

Ans. Refer to Section 1.6.


4. Construct DFA for L = L1 ∩ L2 where
L1 = all strings starting with 'b' over {a, b}
L2 = all strings not having 'ba' as substring over {a, b}. [5 M]
Ans. Refer to Section 1.3.
5. Convert the following NFA to DFA: [5 M]
q2
a
Start b a
q0 q1 q3 q5 q6 q7

b q4

Ans. Refer to Section 1.5.


1.100
Theoretical Computer Science Finite Automaton

6. Construct the minimize DFA for the following DFA: [4 M]


M = ({q0, q1, q2, q3, q4}, {a, b}, δ, q0 {q2, q4})
δ a b
Start → q0 q1 q2
q1 q1 q3
Final ← q2 q0 q1
q3 q1 q4
Final ← q4 q3 q1
(Use Myhill-Nerode Theorem)
Ans. Refer to Section 1.9.
7. Construct Moore machines for binary input sequence such that if it has a substring 101,
the machine output ‘A’, if it has substring 110 then output ‘B’ else output ‘C’. [4 M]
Ans. Refer to Section 1.8.
8. DFA may have many final states. Comment. [1 M]
Ans. Refer to Section 1.3.
April 2019
1. What are the proper prefix and proper suffix of the string “Computer”? [1 M]
Ans. Refer to Page 1.2.
2. Write down the ∈-closure of each state from the following FA: [1 M]
Start 1 e
q0 q1 q2

Ans. Refer to Section 1.6.


3. Finite Automata has more than one Final states (True or False) Justify. [1 M]
Ans. Refer to Section 1.3.
4. State two differences between NFA and DFA. [1 M]
Ans. Refer to Page 1.21.
5. Construct a DFA for a language L = {x | x has neither “aa” nor “bb” as a substring} over
Σ = {a, b}. [5 M]
Ans. Refer to Section 1.2.

6. Convert the following NFA with moves to DFA. [5 M]
0 0,1

Start q0 0, e q1 1, e q2

0 0

e
Ans. Refer to Section 1.6.
1.101
Theoretical Computer Science Finite Automaton

7. Construct minimal DFA for the following: [5 M]


M = ({A, B, C, D, E}, {0, 1}, δ, A, {E})
when δ is given by,
δ 0 1
→ A B C
B B D
C B C
D B E
* E B C
Ans. Refer to Section 1.9.
8. Construct a mealy machine to convert each occurrence of substring 101 by 100 over
alphabet {0, 1}. [4 M]
Ans. Refer to Section 1.8.

1.102
CHAPTER
2
Regular Expressions
and Languages
Objectives …
To study Basic Concepts in Regular Expressions
To learn Regular Languages

2.0 INTRODUCTION
• The language accepted by finite automata can be easily described by simple expressions
called Regular Expressions. It is the most effective way to represent any language.
• The languages accepted by some regular expression are referred to as Regular languages. A
regular expression can also be described as a sequence of pattern that defines a string.
• Regular expressions are used to match character combinations in strings. String searching
algorithm used this pattern to find the operations on a string.

2.1 REGULAR EXPRESSION (RE) [April 16, 17, 18, 19, Oct. 17, 18]
• The languages accepted by finite automata are described or represented by simple
expressions called regular expressions.
• The languages that are associated with these regular expressions are called regular and are
also said to be defined by finite automata.
• Regular expressions are also referred as rational expressions. Regular expression is generally
a sequence of characters that is used to find a string in language.
Operations of Sets of Strings:
• Regular expressions represent simple language-denoting formulas, based on the operations of
concatenation, union and closure.
• Let, Σ be a finite set of symbols and let L1, L2 and L be sets of strings from Σ*. Then we can
define following operations on these sets.
1. Concatenation: Concatenation of L1 and L2 is denoted by,
L1 L2 = {xy | x is in L1 and y is in L2}
2.1
Theoretical Computer Science Regular Expressions and Languages

2. Kleene Closure: It is denoted by L* and is defined as, [April 17, 19]



L* = ∪ Li
i=0
We define L0 = {∈} and Li = LLi–1
3. Positive Closure: It is denoted by L+ and is defined as,

L+ = ∪ Li
i=1
Example: Let, L1 = {10, 1} and L2 = {011, 11} then,
L1 L2 = {10011, 1011, 111}
*
L1 = {∈, 10, 1, 1010, 101, 110 …}
+
L1 = {10, 1, 1010, 101, 110 …}
• Regular expressions play an important role in computer science applications, which involves:
1. It is used by many text editors and utilities to search block of text for certain pattern.
2. It is also used to design the compliers for programming languages.
2.1.1 Formal Definition of Regular Expression
• Let Σ be an alphabet. The regular expression over Σ and the sets they denote are defined
recursively as follows :
1. φ is a regular expression and denotes the empty set.
2. ∈ is a regular expression and denotes the set {∈}.
3. For each a in Σ, a is a regular expression and denotes the set {a}.
4. If r1 and r2 are regular expressions denoting languages R1 and R2 respectively then (r1 +
r2), (r1 r2) and r1* are regular expressions denoting the sets R1 ∪ R2, R1R2 and R1
*

respectively.
• If 'r' is a regular expression, then the language represented by 'r' is denoted by L (r).
1. If r = a+b
L (r) = {a, b}
2. If r = ab
L (r) = {ab}
3. If r = a*
L (r) = {∈, a, aa, aaa, aaaa, …}
Here, ∈ stands for zero occurrences of 'a'. Hence, 'a' denotes an infinite set of strings.
• If r = (ab)* = {∈, ab, abab, ababab, …}
• If r = a* b* = {∈, a, b, ab, aab, abb, aabb, …}
• If r = (a + b)* = {∈, a, b, ba, ab, baa, abb, …}
2.2
Theoretical Computer Science Regular Expressions and Languages

2.1.2 Examples
• In this section we will study some example of regular expressions.
Example 1: Define the language such that all words begin and end with 'a' and in between
any word using 'b', using regular expression.
Solution: Σ = {a, b}, start symbol and end symbol = 'a'. The regular expression is ab*a + a.
Example 2: Describe the language consisting of all strings over Σ = {0, 1} with atleast two
consecutive 0's, using regular expression.
Solution: Σ = {0, 1}
∴ Σ* = {∈, 0, 1, 00, 01, 10, 11, …}
Here, we want atleast one occurrence of "00" and any number of trailing 1's and 0's and any
number of leading 1's and 0's.
Therefore, the regular expression is,
r = (0 + 1)* 00 (0 + 1)*
Example 3: If L (r) = the set of all strings over {0, 1} ending with "011", find regular
expression r.
Solution: The regular expression is,
r = (0 + 1)* 011
Example 4: Define language over {0, 1} containing all possible combinations of
0's and 1's but not having two consecutive 0's.
Solution: (1 + 10)* is language containing the strings starting with 1 and not having two
consecutive 0's. Similarly, the language containing the strings starting with '0' and not having two
consecutive 0's is represented by 0 · (1 + 10)*.
If we combine both, we get the required regular expression as
(1 + 10)* + 0 (1 + 10)* i.e. (0 + ∈) (1 + 10)*.
Example 5: Define language containing all strings of a's and b's containing atleast one
combination of double letters using regular expression.
Solution: Regular expression, r = (a + b)* (aa + bb) (a + b)*.
Example 6: Define language containing all strings over {0, 1} having atmost one pair of 0's
or at most one pair of 1's.
Solution: Here we have four combinations
1. Consecutive 0's and 1's not present.
2. Only one pair of consecutive 0's present.
3. Only one pair of consecutive 1's present.
4. One pair of consecutive 0's and 1's present.
The regular expression is,
r = (01)* 00 (10)* + (10)* 11 (01)* + (01)* + (10)* + (10)* 0 (10)* + (01)* 1 (01)*
2.3
Theoretical Computer Science Regular Expressions and Languages

Example 7: If r = b*ab* ab*, describe L (r) in simple English.


Solution: L (r) is the language over Σ = {a, b} containing exactly two a's.
Example 8: Show that (a* b*)* = (a + b)*.
Solution: Let r1 = (a* b*)* and r2 = (a + b)*
L (r1) = ({∈, a, aa …} ∪ {∈, b, bb, …})
= {∈, a, b, aa, bb, ab, ba …} … (1)
L (r2) = {∈, a, b, aa, ab, bb, ba …} … (2)
From (1) and (2), we get,
(a* b*)* = (a + b)*
Example 9: Is a* b* is equal to (ab)*?
Solution: No. a* b* is not the same as (ab)* because a* b* does not contain substring "ba"
whereas (ab)* contains strings like abab, etc.
Example 10: Write a regular expression for a language containing set of all strings of a’s
and b’s ending in bb.
Solution: r = (a + b)* bb
Example 11: Write a regular expression for language containing string with any number of
a’s followed by any number of b’s followed by any number of c’s.
Solution: r = a * b* c *
Example 12: Write a regular expression to denote a language over Σ = {a, b} such that every
string begin and end with either aa or bb.
Solution: r = (aa + bb) (a + b)* (aa + bb) + aa + bb
Example 13: Write a regular expression for language contains atleast one a and atleast one
b.
Solution: r = [(a + b)* a (a + b)* b (a + b)*] + [(a + b)* b (a + b)* a (a + b)*]
Example 14: Write a regular expression to denote a language containing string in which the
4th character from the right end of string is always a over Σ = {a, b}.
Solution: r = (a + b)* a (a + b) (a + b) (a + b)
Example 15: Write a regular expression for language containing each string of even length
over {0}.
Solution: L = (∈, 00, 0000, ......}
r = (00)*
2.4
Theoretical Computer Science Regular Expressions and Languages

Example 16: Write a regular expression to denote a language having strings not containing
substring “01” over Σ = {0, 1}.
Solution: r = 1* 0*
L = {∈, 0, 1, 10, 100, 000, ......}
Example 17: Find regular expression for a language consists of strings over {a, b} which are
either all b’s or strings begin with a and followed with any number of b’s only or an empty
string.
Solution: L = {∈, b, bb, bbb, a, ab, abb, abbb, ......}
r = b* + ab*
= (∈ + a) b*
Example 18: Find regular expression for a language consists of string over {0, 1} whose 3rd
digit from right end is always 1.
Solution: r = (0 + 1)* 1 (0 + 1) (0 + 1)
Example 19: Write regular expression for a language consists of string having length
divisible by 3 over {a}.
Solution: r = (aaa)*
Example 20: Write regular expression for a languag containing strings do not have either
“aa” or “bb” as a substring in it over {a, b}.
Solution: L = {∈, a, b, ab, bab, aba, ......}
r = (b + ∈) (ab)* (a + ∈)
Example 21: Write regular expression for a language consisting of string such that total
number of b’s in each string is divisible by 3 over {a, b}.
Solution: L = {∈, a, aa, abbb, bbba, ababab, ......}
r = (a* b a* ba* ba*)* + a*
Example 22: Write a regular expression for a language of strings with total number of 0’s
are even over {0, 1}.
Solution: r = 1* + (1* 01* 01*)*
Example 23: Write regular expression to denote a language L over {a, b} such that all the
strings do not have a substring “ab”.
Solution: r = b* a* (L = {ε‚ a‚ b‚ bb‚ ba‚ baa‚ ……})
Example 24: Write regular expression for language containing strings without a substring
“abb” and “bba”.
Solution: r = b* + (b + ∈) (a + ab)*)*
∴ L = {∈, a, b, ab, ba, abab, aab, bb, ......}

2.5
Theoretical Computer Science Regular Expressions and Languages

Example 25: Write regular expression for language containing strings in which every block
of 4 consecutive symbols contain atleast two a’s over {a, b}.
Solution: For block of 4, atleast two a’s are required, among 4 positions any two can be a’s.
There are 4C2 possible ways = 6.
∴ R = r1 + r2 + r3 + r4 + r5 + r6
where r1 = (a + b) (a + b) (a) (a)
r2 = (a) (a) (a + b) (a + b)
r3 = (a) (a + b) (a) (a + b)
r4 = (a + b) (a) (a) (a + b)
r5 = (a + b) (a) (a + b) (a)
r6 = (a) (a + b) (a + b) (a)
∴ R = [(a + b) (a + b) aa] + [aa (a + b) (a + b)] + [a (a + b) a (a + b)]
+ [(a + b) aa (a + b)] + [(a + b) a (a + b) a] + [a (a + b) (a + b) a]
Example 26: Write regular expression for language containing strings without consecutive
0’s or 1’s or without consecutive 0’s followed by without consecutive 1’s over {0, 1}.
Solution: r = (1 + 01)* + (0 + 01)*
Example 27: Define the language of the following regular expression :
(a + b)* a (∈ + bbb)
Solution: It defines the language made up of zero or more occurrences of a or b ending with
“a” or “abbb”.
Example 28: Define the language of following regular expression:
((0 + 1) 0)*
Solution: It defines the language containing set of strings of even length in which “0” is at
even position.
Example 29: Find the regular expression for the set of strings not having "101" as a
substring.
Solution: In this, we should take care that if 1 is followed by 0 (i.e. 10) then it should be
followed by another 0 or nothing to avoid (101). Thus the regular expression is 0* (1* (00+)*)*.
Example 30: Find the regular expression for the set of all strings such that every block of
four consecutive symbols contains atleast two zeros.
Solution: This means that always we have to search four symbols and see that atleast two
zeros are there i.e. if A, B, C, D are four symbols then any of these two symbols should be zero.
i.e. AB, or BC, or CD, or AC or AD or BD.
2.6
Theoretical Computer Science Regular Expressions and Languages

Thus the regular expression is,


[00 (0 + 1) (0 + 1) | 0 (0 + 1) 0 (0 + 1) | 0 (0 + 1) (0 + 1) 0 |
(0 + 1) 00 (0 + 1) | (0 + 1) 0 (0 + 1) 0 | (0 + 1) (0 + 1) 00]*
Example 31: Find the regular expression for the set of all strings such that the fifth symbol
from the right end is 1.
Solution: The regular expression is (0 + 1)* 1 (0 + 1) (0 + 1) (0 + 1) (0 + 1).

2.1.3 Regular Expression Identities [Oct. 16]


• Two regular expressions P and Q are equivalent and written as P = Q if P and Q represent the
same set of strings.
• Following are the identities for regular expressions which are useful for simplification of
regular expression :
1. φ+R=R

2. φR = Rφ = R

3. ∈R = R∈ = R

4. ∈* = ∈ and φ* = ∈

5. R+R=R

6. R* R* = R*

7. RR* = R* R = R+

8. (R*)* = R*

9. ∈ + RR* = R* = ∈ + R* R

10. (PQ)* P = P (QP)*

11. (P + Q)* = (P* Q*)* = (P* + Q*)

12. (P + Q) R = PR + QR

13. R (P + Q) = RP + RQ
Arden’s Theorem:
• The Arden’s theorem can be applied to find the regular expression. It states that, if P and Q
are two regular expressions over Σ. If P does not contain ∈, then R=Q + RP has a unique
solution given by, R = QP*.
2.7
Theoretical Computer Science Regular Expressions and Languages

Example 1: Example show that (a · b)* ≠ a* b*


Solution: Let L1 = (a · b)* and L2 = a* b*

L1 = {∈, ab, abab, ......}

L2 = {∈, a, aa, b, bb, ab, aabb, ......}


The strings accepted by both languages are not same.
∴ (ab)* ≠ a* b*

Example 2: Show that (1 + 011)* = ∈ + 1* (011)* (1* (011)*)*


Solution: (1 + 011)* = ∈ + 1* (011)* + (1* (011)*)*
= ∈ + R · R* where R = 1* (011)*
= ∈ + R+ (using identity of regular expression)
= R*
= (1 + 011)*
Hence L.H.S. = R.H.S.

Example 3: Justify true or false : (01)* 0 = 0 (10)*


Solution: True, since (PQ)* P = P (QP)* (identity of regular expression)

Example 4: Show that regular expressions given below are equivalent.

(b + aa* b) + (b + aa* b) (a + bab)* δ (a + ba* b) = a* b (a + ba* b)*

Solution: L.H.S. = (b + aa* b) + (b + aa* b) (a + ba* b)* (a + ba* b)

Let b + aa* b = P

and a + ba* b = Q
∴ L.H.S. = P + PQ* Q
= P + PQ* Q
= P (∈ + Q* Q) = PQ* (∈ + RR* = R*)
= (b + aa* b) (a + ba* b)*
= (∈ + aa*) b (a + ba* b)*
= a* b (a + ba* b)*
= R.H.S.
2.8
Theoretical Computer Science Regular Expressions and Languages

Example 5: Show that ∈0* + (0* 1) 1* = 0* 1*

Solution: L.H.S. = ∈0* + (0* 1) 1*

= 0* + (0* (11)*) (identity property)

= 0* + (∈ + 11*)

= 0* (1*) = 0*1*

= R.H.S.

2.2 REGULAR LANGUAGES [April 17, 19, Oct. 17]


• A language is regular if it can be expressed in terms of regular expression.
2.2.1 Definition
• The regular languages are those languages that can be constructed from the three set
operations viz., Union, Concatenation and Kleene closure.
• A regular language is defined as , let ∑ be an alphabet. The class of "regular languages" over
∑ is defined inductively as follows:
1. φ is a regular language.
2. For each a ∈ E, {a} is a regular language.
3. For any natural number n ≥ 2 if L1, L2, …, Ln are regular languages, then L1 ∪ L2 ∪ …
Ln is regular language.
4. For any natural number n ≥ 2, if L1, L2, …, Ln are regular languages, then L1 ⋅ L2 ⋅ … Ln
is regular language.
5. If L is a regular language, then L* is regular language.
2.2.2 Examples
• If L = φ, φ is a regular language by rule (a).
• L = {a, ab} is a language over ∑ = {a, b} because, both {a} and {b} are regular languages by
rule (b). By rule (d) it follows that {a} o {b} = {ab} is a regular language. Using rule (c), we
see that {a} ∪ {ab} = L is a regular language.
• The language over the alphabet {0, 1} where strings contain an even number of 0's can be
constructed by 1*((01)* (01)*)* or simply 1*(01* 01*)*.

EQUIVALENCE OF FINITE AUTOMATA (FA)


2.3
AND REGULAR EXPRESSION (RE) [April 16, 17, 18, 19, Oct. 17, 18]
• In this section we will study how to transform any FA to an equivalent RE and conversion of
any RE to an equivalent FA. Thereby, establish the equivalence between REs and FAs.
2.9
Theoretical Computer Science Regular Expressions and Languages

• Theorem: Let r be a regular expression. There exists an NFA with ∈-transition that accepts
L (r).
• Proof: The languages accepted by FA are precisely the languages denoted by regular
expression.
2.3.1 Conversion from RE to FA
• In this section we will study how to convert the RE to FA. Consider the pictorial
representation in Fig. 2.1 of RE to FA.
Case 1:

Fig. 2.1
Case 2: If r = r1 + r2 (for union)
there are NFAs, M1 = (Q1, Σ1, δ1, q1, {f1})
and M2 = (Q2, Σ2, δ2, q2, {f2}) with
L (M1) = L (r1) and L (M2) = L (r2)
Construct, M = (Q1 ∪ Q2 ∪ {q0, f0}, Σ1 ∪ Σ2, δ, q0, {f0})
q0 = new initial state, f0 = new final state.
L (M) = L (M1) ∪ L (M2)

Fig. 2.2
Case 3: If r = r1 r2 (for concatenation)
M1 and M2 are same as above,
L (M) = L (M1) L (M2)

Fig. 2.3
2.10
Theoretical Computer Science Regular Expressions and Languages

*
Case 4: r1 = r1 (for closure)
Let M1 = (Q1, Σ1, δ1, q1, {f1}) and L (M1) = r1
Construct, M = (Q1 ∪ {q0, f0}, Σ1, δ, q0, {f0})

Fig. 2.4
• Any path from q0 to f0 is either path from q0 to f0 on ∈ or a path from q0 to q1 on ∈ followed
by some number of paths from q1 to f1, then back to q1 on ∈, each labeled by a string in L
(M1) followed by path q1 to f1 on string in L (M1) then to f0 on ∈.
Example 1: Construct NFA for regular expression 01* + 1.
Solution: 01* + 1 r + r
1 2

where, r1 = 01* and r2 = 1.


NFA for r2 is,

(a)
NFA for r1 = r3 r4 , where r3 = 0 and r4 = 1*.
NFA for r3 is,

(b)
NFA for r4 is,

(c)
NFA for r1 = 01* is,

(d)
2.11
Theoretical Computer Science Regular Expressions and Languages

NFA for 01* + 1 is r1 + r2 as follows:

(e)
Fig. 2.5
Example 2: Construct a FA equivalent to regular expression (1* + 0)*.
Solution:

Fig. 2.6
Example 3: Construct a FA equivalent to regular repression (01 + 10)* + 11.
Solution: (01 + 10)* + 11 = r1 + r2, where r1 = (01 + 10)* and r2 = 11.
NFA for r1 is M1 as shown below.

Fig. 2.7 (a)


NFA for r2 is M2 as shown in Fig. 3.7 (b).

Fig. 2.7 (b)


2.12
Theoretical Computer Science Regular Expressions and Languages

Therefore, equivalent NFA is as shown in the Fig. 3.7 (c).

Fig. 2.7 (c)


Example 4: (Direct method) For DFA shown in the Fig. 2.8, find the regular expression.

Fig. 2.8
Solution: If the start symbol is 'a', we can find different expressions to reach to final state q3
from q0 via q1 and are as follows :
a (ba)* a (a + b)* and a (ba)* bb (a + b)* .
Similarly, if the start symbol is 'b' then we have,
b (ab)* b (a + b)* and b (ab)* aa (a + b)*
Therefore, the required regular expression is obtained by "ORing all above expressions,
i.e. a (ba)* a + a (ba)* bb (a + b)* + (ab)* b (a + b)* + b (ab)* aa (a + b)* a (a + b)*.
Example 5: For the DFA shown in the Fig. 3.9, find the regular expression.

Fig. 2.9
Solution : The regular expression is 1* 00 (0 + 1)* + (1* 01)* 00 (0 + 1)*.
Example 6: For the DFA shown in the Fig. 3.10, find the regular expression.

Fig. 2.10
Solution: The required regular expression is (0 + 1) (0 + 1)*.
2.13
Theoretical Computer Science Regular Expressions and Languages

Example 7: Language L1 consists of strings beginning with 'a' and language L2 consists of
strings ending with 'a'.
(a) What is L1 intersection L2 ?
(b) Write regular expression for L1 and L2 and L1 ∩ L2.
(c) Draw FA.
Solution:
(a) L1 intersection L2 is all words starting with 'a' and ending with 'a'.
(b) L1 : r1 = a (a + b)*
L2 : r2 = (a + b)* a
L1 ∩ L2 : r3 = a (a + b)* a + a.
(c)

Fig. 2.11
Example 8: Give regular expression of L1 ∩ L2 if
L1 : all strings of even length
L2 : starting with 'b'.
Solution: L1 intersection L2 is all strings of even length and starting with 'b'.
L1 = ((a + b) (a + b))*
L2 = b (a + b)*
Now, for intersection the string should start with 'b' and length is to be even. Hence, 'b'
should be concatenated by a string of odd length.
((a + b) (a + b))* is even length, adding an extra symbol we get the odd length.
Hence, ((a + b) (a + b))* (a + b)
Therefore, the required regular expression is
b ((a + b) (a + b))* (a + b).

PUMPING LEMMA FOR REGULAR LANGUAGES AND


2.4
APPLICATIONS [April 16, 17, Oct. 17]

• By using FA and RE, we have been able to define many languages, but language which
cannot be defined by a regular expression is called a non-regular set.
• All languages are regular or non-regular. Pumping lemma, which is a powerful tool for
proving certain languages, is non-regular.
2.14
Theoretical Computer Science Regular Expressions and Languages

• The term Pumping Lemma is made up of two words:


1. Pumping: The word pumping refers to generate many input strings by pushing a symbol
in an input string again and again.
2. Lemma: The word Lemma refers to intermediate theorem in a proof.
• Theorem: Let L be a regular set. Then there is a constant n such that if z is any word in L
and |z| ≥ n, we may write z = uvw such that |uv| ≤ n, |v| ≥ 1 and for all i ≥ 0, uviw∈L.
Furthermore, n is a number greater than the number of states of the smallest FA accepting L.
• Proof: Let DFA M = (Q, Σ, δ, q0, F) with some particular number of states, say n. Consider
an input of n or more symbols a1, a2 … am, m ≥ n and for i = 1, 2, …, m, let δ (q0, a1, a2 … ai)
= qi .
• It is not possible for each of the n + 1 states q0, q1, …, qn to be distinct, since there are only n
different states. Thus there are two integers j and k, 0 ≤ j < k ≤ n, such that qj = qk.
• The path labelled a1, a2 … am in the transition diagram of M is as shown in the Fig. 2.12.
• Since, j < k, the string aj+1 … ak is of length atleast 1 and since k ≤ n, its length is no more
than n.

Fig. 2.12: Path in transition diagram of DFA M


• If qm is in F, i.e. a1, a2, …, am ∈ L(M).
δ (q0, a1, a2 … aj ak+1 … am) = δ (δ (q0, a1 … aj), ak+1 … am)
= δ (qj, ak+1 … am)
= δ (qk, ak+1 … am)
= qm
• Similarly, we could go around the loop of Fig. 3.12 more than once. Thus
a1 … aj (aj+1 … ak)i ak+1 … am is in L (M) for any i ≥ 0.

2.4.1 Applications
• We will use above theorem to prove that given language is not regular.
1. Select the language L which we wish to prove non-regular.
2. Assume L is regular and let n be number of states in the corresponding FA.
3. Select a string z such that |z| ≥ n. Use pumping lemma to write z = uvw with
|uv| ≤ n and |v| ≥ 1.
4. Find a suitable integer i such that uviw ∉ L.
• Above contradicts our assumption and hence L is non-regular.
2.15
Theoretical Computer Science Regular Expressions and Languages

Example 1: Show that the set L = {0i2 | i is an integer, i ≥ 1} which consists of all strings of
0's whose length is a perfect square, is not regular.
Solution:
Case 1:
1. Assume L is regular set and let n be the integer in the pumping lemma. Let z = 0n2 .
2. By the pumping lemma, 0n2 may be written as uvw where 1 ≤ |v| ≤ n and uviw is in L
for all i.
3. Let i = 2 (say).
The length of uv2w is |uvvw| > n2 always. Since, v is at least one and maximum n, therefore,
the maximum length of string is n2 + n.
Hence, n2 < |uv2w| ≤ n2 + n < (n + 1)2 i.e. the length of uv2w lies properly between n2 and
(n + 1)2 and is thus not a perfect square. Thus uv2w is not in L, a contradiction.
Therefore, L is not regular.
Case 2: L = {0, 0000, 000000000, ......}
1. Assume L is regular set.
2. Let z = 0000 (any word from language)
3. Split z into uvw such that |uv| ≤ |z| and |v| ≥ 1.
Suppose u = 0, v = 00, w = 0.
4. Let i = 2.
uvi w = uv2 w
= 0 (00)2 0
= 000000 ∉ L
∴ i
uv w ∉ L
Hence, L is non-regular.
Example 2: Show that L = {ap | p is prime} is non-regular.
Solution:
Case 1:
1. Let L be regular and n be number of states in FA accepting L.
2. Let p is a prime number and p ≥ n.
u = am, v = a and w = ap – (m + 1), 1 ≤ n.
So, |uv| ≤ n and |v| = 1.
By pumping lemma, uviw is in L.
Let, i = p + 1
∴ am (a)p+1 ap–m–1 ∈ L
am+p+1+p–m–1 = a2p ∈ L
but 2p is not a prime number.
So, uvp+1 w ∈ L is a contradiction. Hence, L is not regular.
2.16
Theoretical Computer Science Regular Expressions and Languages

Case 2: L = {a, aa, aaa, aaaaa, ......}


1. Assume L be a regular set.
2. Let z = aaaaa be a word such that
z = uvw ∈ L (By pumping lemma)
Let u = aa, v = a, w = aa
Let i = 2
uvi w = uv2 w
= aa (a)2 aa
= aaaaaa ∉ L
∴ i
uv w ∉ L
Hence L is non-regular.
Example 3: Show that L = {ww | w ∈ (a, b)*} is not regular.
Solution:
1. Let L be regular. Let n be the number of states in FAM accepting L.
2. Let, ww = am b am b in L
Let, |ww| = 2 (m + 1) = n
We can have two different ways.
Case 1: v does not contain any b's, so k > 1 and k ≠ m, u = ak, v = am–k, w = bamb.
∴ |uv| < n, |v| ≥ 1
By pumping lemma, uw ∈ L.
But uw = ak b am b ∈ L
This is a contradiction. Hence, L is not regular.
Case 2: v contains only one b.
u = ak, v = b, w = amb
∴ |uv| < n and |v| ≥ 1
By pumping lemma |uw| ∈ L.
But, uw = ak am b ∈ L.
This is a contradiction. Hence L is not regular.
Example 4: Determine whether following language is regular ? Justify.
L = L1 ∩ L2
L1 = {an bm | n ≥ m and n, n > 0}
L2 = {bm an | m > 0, n > 1}
Solution: L1 contains all strings starting with 'a' and L2 contains all strings starting with 'b'.
∴ L = L1 ∩ L2 = φ
2.17
Theoretical Computer Science Regular Expressions and Languages

We can construct FA for L as shown in the Fig. 2.13.

Fig. 2.13
Hence, L is a regular language.
Example 5: Show that L = {0n 1n | n ≥ 1} is not regular.
Solution:
1. Let L is regular.
2. Let z = 0n 1n, then |z| = 2n > n
By pumping lemma, split z = uvw such that |uv| ≤ n and |v| ≥ 1.
3. Let z = 000111 and u = 00, v = 011, w = 1
|uv| ≤ 6 satisfies the condition.
4. Let i = 2.
uvi w = uv2 w
= 00 (011)2 1
= 000110111 ∉ L
∴ L is not regular.
Example 6: Prove that L = {an bn+1 | n > 0} is non-regular.
Solution: Let, n = 1
an bn+1 = ab2
|ab2| = 1 + 2 = 3
Let n = 2 |a2 b3| = 5
All strings of this language are of odd length.
1. Assume L be regular.
2. z = a 3 b 4. |z| = 7
Split z into uvw such that |uv| ≤ n, |v| ≥ 1.
Let u = aa, v = ab, w = bbb.
z = aa ab bbb
uvw
3. Let i = 2 uvi w = uv2 w
= (aa) (ab)2 bbb
= aa ab ab bbb ∉ L
∴ Language L is not regular.
2.18
Theoretical Computer Science Regular Expressions and Languages

CLOSURE PROPERTIES OF REGULAR LANGUAGES


2.5
[April 16, 17, Oct. 17]
• In this section, we discuss the closure properties of regular sets under union, concatenation,
Kleen closure, intersection, complementation, etc.
• These operations when we apply on regular sets result in regular set and if class of languages
is closed under particular operation we call that fact a closure property of the class of
languages.
• Theorem 1: The regular sets are closed under union, concatenation and kleen closure. i.e. if
*
L1 and L2 are regular sets, then L1 ∪ L2, L1 L2 and L1 are also regular sets.
• Proof: L1 + L2 means the language of all words in either L1 or L2, say regular expressions for
L1 and L2 are r1 and r2 respectively. Then we define,
r1 + r2 is regular expression for L1 ∪ L2,
* *
r1 r2 is regular expression for L1 L2 and r1 is regular expression for L1 .
• Therefore, all three of these sets of words are definable by regular expressions and so are
themselves regular sets.
Example: Let, L1 = Strings starts with and ends with same letter over {a, b}
L2 = Strings containing “ab” as substring over {a, b}
Regular expression for language L1 is
[a (a + b)* a + b (a + b)* b]
Regular expression for language L2 is
(a + b)* ab (a + b)*
1. The language represented by regular expression L1 + L2 is
[a (a + b)* a + b (a + b)* b] + (a + b)* ab (a + b)* … (1)
Therefore, language L1 ∪ L2 is regular because it is represented by regular expression
(1).
2. The language L1L2 is represented by regular expression
[a (a + b)* a + b (a + b)* b] · [(a + b)* ab (a + b)*]
Therefore, it is regular.
*
3. The language L1 is represented by regular expression
(a (a + b)* a + b (a + b)* b)*
Therefore, it is regular.
• Theorem 2: The class of regular sets is closed under complementation.
• Definition: If L is language over alphabet Σ, we define its complement L' to be the language

of all strings of letters from Σ that are not words in L (or L' or L ) i.e. L is a regular set and L
⊆ Σ* then Σ* – L is a regular set.
2.19
Theoretical Computer Science Regular Expressions and Languages

• Proof: Let L be L (M). Some of states of this FA, M are final states and some are not. Let us
reverse the status of each state i.e. if it was a final state, make it non-final and if it was non-
final state, make it final state. If the input ends formerly with non-final state, it now ends in
final state and vice-versa. This new FA accepts all strings that were not accepted by the
original FA (L'). Therefore, the machine accepts the language L'. Therefore L' is a regular
set.

Example: Construct DFA for a language over {a, b} which contains all strings not having
"bba" as a substring.
Solution: First construct DFA which accepts substring "bba".

Fig. 2.14

Now, complement the final state. We get the required DFA as shown in the Fig. 3.15.

Fig. 2.15
• Theorem: The regular sets are closed under intersection (i.e. if L1 and L2 are regular sets,
then L1 ∩ L2 is also a regular set).
• Proof: By DeMorgan's law for sets of any kind
'
L1 ∩ L2 = (L' + L' )
1 2

• This means the language L1 ∩ L2 consists of all words that are not in either L 1' or L 2' . Since
L and L are regular sets, then L' and L' are regular sets proved by theorem 3.4.2. By
1 2 1 2

theorem 3.4.1, since L 1' and L '2 are regular then L '1 + L 2' is also a regular set.
'
Again by theorem 3.4.2, L1' + L'2 is a regular set.
( )
Therefore, L1 ∩ L2 is a regular set.
Example: Construct DFA for L = L1 ∩ L2 over {0, 1}, where
L1 = starting with 0 and ending with 11
L2 = containing substring "010" in it.
2.20
Theoretical Computer Science Regular Expressions and Languages

Solution: L = L1 ∩ L2 = All strings with 0 ending with 11 and having substring 010 in it.
DFA of L is as shown in the Fig. 2.16.

Fig. 2.16

2.6 OPERATIONS ON REGULAR LANGUAGES


• In this section we will study operations on regular languages.
• Union: Union of languages L1 and L2 is the language (L) containing all strings of L1 and all
strings of L2.
L = L1 ∪ L2
• Example:
If L1 = a*, L2 = b*
then L = L1 ∪ L2 = a* b* + b* a*
• Concatenation: The concatenation of languages L1 and L2 is the language L containing all
strings of L1 followed by all strings of L2 without space.
L = L1 L2
• Example:
L1 = {a, bb, bab} and L2 = {a, ab}
L = L1 L2 = {aa, aab, bba, bbab, baba, babab}
• Intersection: Intersection of languages L1 and L2 is the language L containing common
strings of L1 and L2.
L = L1 ∩ L2
• Example:
If, L1 = a* and L2 = b* then
L = L1 ∩ L2 = φ (No string common)
2.21
Theoretical Computer Science Regular Expressions and Languages

• Kleene Closure: Given an alphabet Σ, we wish to define a language in which any string of
letters from Σ is a word, even the null string. This language we shall call the closure of the
alphabet. It is denoted by writing a star (asterisk) after the name of the alphabet as a
superscript.
• The Σ* is a notation called Kleene star or Kleene closure.
• Example:
1. If Σ = {x} then
Σ* = {∈, x, xx, xxx, …}
2. If Σ = {0, 1} then
Σ* = {∈, 0, 1, 00, 01, 10, 11, 000, 011 …}
3. If Σ = {a, b, c} then
Σ* = {∈, a, b, c, aa, bb, cc, ac, ab, ba, bc, ca, cb, aaa …}
4. If S = {aa, b} then
S* = {∈, aa, b, aab, baa, bbb, aaaa, aabb, …}
The string aabaaab is not in S* since it has a's of length 3.
5. If S = {a, ab} then
S* = {∈, a, ab, aa, aab, aba, aaaa, aaab, aaba, abab, aaaab, abaaa …}
Here, for each word in S* every b must have an 'a' immediately to its left.
6. If S = {10, 1} then
S* = {∈, 10, 1, 1010, 11, 101 110 …}
Therefore, Kleene closure of language is denoted by L* and is defined as

L* = ∪ Li
i=0

• Positive Closure: If S is a set of strings not including ∈, then S+ is the language S* without
the word ∈. If S is a language that does contain ∈, then S+ = S*.
• This plus operation is called positive closure. Therefore, positive closure of language is L+
and is defined as

L+ = ∪ Li
i=0

• Example:
1. If S = {10, 1} then
S+ = {10, 1, 1010, 11, 101, 110 …}
2.22
Theoretical Computer Science Regular Expressions and Languages

2. If S = {aa, b} then
S+ = {aa, b, aab, baa, bbb, aaaa … }
• Closure operator can be applied to infinite set or finite set.

EXAMPLES
Example 1: Find the regular expression for the set of strings not having "101" as a substring.
Solution: In this, we should take care that if 1 is followed by 0 (i.e. 10) then it should be
followed by another 0 or nothing to avoid (101). Thus the regular expression is 0* (1* (00+)*)*.
Example 2: Find the regular expression for the set of all strings such that every block of four
consecutive symbols contains atleast two zeros.
Solution: This means that always we have to search four symbols and see that atleast two
zeros are there i.e. if A, B, C, D are four symbols then any of these two symbols should be zero.
i.e. AB, or BC, or CD, or AC or AD or BD.
Thus the regular expression is
[00 (0 + 1) (0 + 1) | 0 (0 + 1) 0 (0 + 1) | 0 (0 + 1) (0 + 1) 0 |
(0 + 1) 00 (0 + 1) | (0 + 1) 0 (0 + 1) 0 | (0 + 1) (0 + 1) 00]*
Example 3: Find the regular expression for the set of all strings such that the fifth symbol
from the right end is 1.
Solution: The regular expression is (0 + 1)* 1 (0 + 1) (0 + 1) (0 + 1) (0 + 1).
Example 4: Prove L = {an bn an} is not regular.
Solution: By pumping lemma, w = an bn an split into xyz (say)
w ∈ L ∴ xyz ∈ L.
Case 1: x = ∈
y = a
z = bn a n
∴ w = a i bn a n such that xyiz ∈ L
Thus we can show that this is not regular.
Case 2: x = an
y = b
z = an
∴ w = an bi an, again for this i ≠ n, w generates a word which is
not in L.
Case 3: x = a n bn
y = a
z = ∈
∴ w = a n bn a i
Thus, for any value of i, w will not generate valid words. Thus it is not regular.
2.23
Theoretical Computer Science Regular Expressions and Languages

Case 4: x = ∈
y = aba
z = ∈
∴ w = (aba)i
For i = 1, w = aba ∈ L
For i = 2, w = abaaba is not in L.
Thus, the language is not regular from case 1 through case 4.
∴ L = {an bn an} is not regular.
Example 5: Prove L = {an | bn} valid words in L = {a, b, … } is regular using pumping
lemma.
Solution: Here again y can be either a or b.
Case 1: x = ∈
y = a
z = ∈
i
Thus, w = a and for all i, w will generate a word which is in L. Thus, the language is regular.
Case 2: x = ∈
y = b
z = ∈
∴ w = bi
Again for this, w generates words which are in L.
Thus the language is regular. Thus L = {an | bn} is regular.
Example 6: Draw a FA for a language that will not accept strings "aba" and "abb" over
alphabet Σ = {a, b}.
Solution: The FA that accepts only the strings "aba" and "abb" is shown below.

Fig. 2.17
An FA that accepts all strings other than "aba" and "abb" is complement of above language
as shown below.

Fig. 2.18
Here, we have to reverse the final / non-final status of the states.
2.24
Theoretical Computer Science Regular Expressions and Languages

Example 7: Construct a NFA for the following regular expression :


(ab + ba)* aa (ab + ba)*
Solution: Let r1 = (ab + ba)*
r2 = aa
R = r1 r2 r1
NFA for r1 is

Fig. 2.19 (a)


NFA for r2 is

(b)
Fig. 2.19 (b)
∴ NFA for R is

Fig. 2.19 (c)

2.25
Theoretical Computer Science Regular Expressions and Languages

Example 8: Construct NFA for the following regular expression:


(0 (11)* 0 + 01* 0)*
Solution:

Fig. 2.20

EXAMPLES
Example 1: Describe the language L = {an bn | n ≥ 1}.
Solution: ∈ = {a, b}
L = {ab, aabb, aaabb, ……}
The language contain equal number of a's followed by equal no. of b's.

Example 2: If L1 = (a*, b* + b* a*) and L2 = (a + b)* find L1 ∩ L2


Solution: L1 = {∈, ab, ba, abb, aabb, baa, bbaa, ……}
L2 = {∈, a, b, ab ba, aab, baa, ……}
L1 ∩ L2 = L1

Example 3: Find the language of the following : (a* b*)* a.


Solution: Language L containing a string starts with a followed by a's followed by b's and
ends with a L = {a, aba, aabba, aaabbba ……}

Example 4: What is the suffix and prefix of string x = abcd.


Solution: Suffix = ∈, d, cd, bcd, abcd
Prefix = ∈, a, ab, abc, abcd
2.26
Theoretical Computer Science Regular Expressions and Languages

Example 5: Find the language of the following : (a + b)*


Solution: L = {∈, a, b, aa, bb, ab, ba, aab ……}
Example 6: In (a* + b*)* = (a + b)* true ? Justify.
Solution: LHS = {∈, a, b, aa, bb, ab, ba, bba, aab ……}
RHS = {∈, a, b, aa, bb, ab, ba, bba, ……}
LHS = RHS
Example 7: Write smallest possible string accepted by given RE (0 + 1*) 01*.
Solution: 00
Example 8: Write smallest possible strings accepted by the following regular expression
10 + (0 + 11) 0* 1
Solution: {10, 01}
Example 9: Construct FA for the following regular expression :
a (a + b)* b + b (a + b)* a
Solution:

Fig. 2.21
Example 10: Construct NFA without ∈ for language L = (0 + 1)* 01
Solution:

Fig. 2.22
2.27
Theoretical Computer Science Regular Expressions and Languages

Example 11: Write smallest possible string accepted by the following regular expression :
(ab + ba*)* b
Solution: L = (ab + ba*)* b
Smallest string is b.
Example 12: Construct FA for the following regular expression :
ab (a + b)* + ba (a + b)*
Solution:

Fig. 2.23

Example 13: If ∈ is a regular expression, then it denotes set {∈}. Justify.


Solution: True, since ∈ is a regular expression.
Example 14: Construct NFA without ∈ for language
L = 00 (0 + 1)* 1
Solution:

Fig. 2.24
Example 15: Write smallest possible string accepted by the regular expression :
a (a + b)* ab
Solution: aab is the smallest string.
2.28
Theoretical Computer Science Regular Expressions and Languages
*
Example 16: Construct FA for regular expression ((a + b)* + abb)
Solution:

Fig. 2.25
Example 17: .Describe the language L = {an bn | n ≥ 1}.
Solution: L = {ab, aabb, aaabbb, ......}
Language contains equal number of a’s and b’s.
Example 18: Construct NFA for 01* (10)* + 1
Solution: L = {0, 1, 01, 010, 0110, 011010, ......}

Fig. 2.26
Example 19: Write the smallest possible string accepted by regular expression :
01 + (0 + 1) 01*
Solution: Smallest string = 01, 00, 10.
Example 20:.If L1 = a* b* + b* a* and L2 = (a + b)*, find L1 ∩ L2
Solution: L1 = {∈, a, b, ab, ba, aab, bba, ......}
L2 = {∈, a, b, ab, ba, aab, bba, ......}
L1 ∩ L2 = φ
Example 21: Find the language L of the following expression : (a* b*) a.
Solution: L = {a, aa, ba, aba, aaba, bba, ......}
Language containing string ends with a.
2.29
Theoretical Computer Science Regular Expressions and Languages

Example 22: Construct NFA with ∈-transition for : ab* + ba*


Solution:

Fig. 2.27
Example 23: Write smallest possible string generated by regular expression :
a (a + b) b*
Solution: Smallest string = aa or ab
Example 24: Draw FA equivalent to regular expression : a (a + b)* + b (b + a)*
Solution:

Fig. 2.28

Example 25: Give two kinds of operations that can be carried out on regular language.
Solution: (i) Union, (2) Intersection. (Refer section 3.6.2 for examples)
Example 26: Write the smallest string generated by regular expression.
b (a * b + ab*) c
Solution: bac or bbc.
2.30
Theoretical Computer Science Regular Expressions and Languages

PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which is a set of strings of symbols over an alphabet?
(a) Expression (b) Grammar
(c) Language (d) None of the mentioned
2. A language is generated from the rules of a,
(a) expression (b) grammar
(c) language (d) None of the mentioned
3. Which can be defined as a language or string accepted by an FA.
(a) Regular Expression (RE) (b) Regular Grammar (RG)
(c) Regular Language (RL) (d) None of the mentioned
4. The machine format of regular expression is,
(a) Push down automata (b) Turing machine
(c) Finite Automata (FA) (d) None of the mentioned
5. The language of all words with at least 2 a’s can be described by the regular
expression,
(a) (a + b)*ab*a(a + b)* (b) b*ab*a(a + b)*
(c) (ab)*a (d) None of the mentioned
6. The pumping lemma for regular expression is used to prove that,
(a) Certain sets are not regular (b) Regular grammar don’t produce RE
(c) Certain sets are regular (d) Certain regular grammar produce RE
7. Which of the languages is accepted by the following FA?
(a) a(a + bb*a*)*b* d) (b) b(a + bba*)*a* b)
(c) a(a + bba*)*b* (d) None of the mentioned
8. Which of the strings do not belong to the regular expression (ba + baa)*aaba,
(a) babaabaaaba (b) baaaba
(c) baaaaba (d) babababa
9. A finite automata recognizes,
(a) Context sensitive language (b) Context-free language
(c) Regular language (d) None of the mentioned
10. Which of the following regular expressions describe the language over {0, 1}
consisting of strings that contain exactly two 1’s?
(a) 0*10*10* (b) (0 + 1)*1(0 + 1)*1(0 + 1)*
(c) 0*110* (d) (0 + 1)*11(0 + 1)*
2.31
Theoretical Computer Science Regular Expressions and Languages

11. Given the language L = {ab, aa, baa}, which of the following strings are in L*?
(a) baaaaabaa (b) baaaaabaaaab
(c) aaaabaaaa (d) abaabaaabaa
12. Which one of the following regular expressions is not equivalent to the regular
expression (a + b + c)*?
(a) (a*b*c*)* (b) (a*b* + c*)
(c) (a* + b* + c*)* (d) ((ab)* + c*)*
13. The set of all strings over {a, b} of even length is represented by the regular
expression,
(a) (ab + aa + bb + ba)* (b) (a + b)* (a* + b)*
(c) (aa + bb)* (d) (ab + ba)*
14. The set of strings over {a, b} having exactly 3a's is represented by the regular
expression,
(a) b*aaa (b) b*ab*ab*a
(c) ab*ab*a (d) b*ab*ab*ab*
15. {a2n | n ≥ 1} is represented by regular expression
(a) (aa)* (b) a*
(c) aa*a (d) a*a*
16. (0* 1*) is equivalent to,
(a) (0 + 1)* (b) (01)*
(c) (10)* (d) None of the mentioned.
Answers
1. (c) 2. (b) 3. (a) 4. (c) 5. (a) 6. (c) 7. (b) 8. (d) 9. (c) 10. (a)
11. (b) 12. (d) 13. (d) 14. (d) 15. (a) 16. (d)
Q.II Fill in the Blanks:
1. A regular ______ is a formal language that can be expressed using a regular
expression.
2. ______ is accepted by the machine called Finite Automata (FA).
3. ______ is used to prove that given language is not regular.
4. The language specified by a ______ referred as regular language.
5. ______ are those languages that are described by regular expressions and can be
accepted by FA.
6. ______ of regular expressions used for simplifying RE.
7. Pumping lemma, which is a powerful tool for proving certain languages is ______.
2.32
Theoretical Computer Science Regular Expressions and Languages

8. The property which describes when we combine any two elements of the set and the
result is also included in the set is called as______.
Answers
1. language 2. RE 3. Pumping Lemma 4. regular expression
5. Regular languages 6. Identities 7. non-regular 8. ∈-closure
Q.III State True or False:
1. The languages accepted by Finite Automata (FA) are called as regular expressions.
2. The regular languages are those languages that can be constructed from the three set
operations viz., Union (∪), Concatenation (⋅) and Kleene closure (*)
3. Pumping Lemma should never be used to show a language is regular.
4. A regular expression is written as RE or regex or regexp.
5. To prove a given language is regular we use Pumping Lemma.
6. The language that is accepted by FA is known as Regular language.
7. A regular expression can be defined as, a language or string accepted (recognisable) by
FA.
8. Every FA can have regular expression.
9. FA can accept only non-regular sets.
10. (R*)* = R* is true or false.
11. Regular sets are closed under union.
12. Every finite subset of ∑* is a regular language.
13. Every regular language over ∑ is finite.
14. The regular expression (0 + 1)* 2* = 0* (1 + 2)*.
15. aa* + bb* = (a + b)*.
16. The Language {0,1}* - {0101} is regular.
Answers
1. (F) 2. (T) 3. (T) 4. (T) 5. (F) 6. (T) 7. (T) 8. (T) 9. (F) 10. (T)
11. (T) 12. (F) 13. (F) 14. (T) 15. (F) 16. (T)
Q.IV Answer the following Questions:
(A) Short Answer Questions:
1. Define regular language.
2. Define regular expression.
3. List any four identities of regular expression.
4. What is purpose of Pumping Lemma?
5. Develop the finite automata for (a*ab + ba)*a regular expressions.
6. Give closure properties of regular languages.
2.33
Theoretical Computer Science Regular Expressions and Languages

(B) Long Answer Questions:


1. What is regular language? Explain with example.
2. What is regular expression? Explain with example.
3. Describe in English the sets denoted by the following regular expressions :
(i) (11 + 0)* (00 + 1)*.
(ii) (1 + 01 + 001)* (∈ + 0 + 00)
(iii) [(00 + 11) + (01 + 10) (00 + 11)* (01 + 10)]*
4. Construct finite automata equivalent to the following regular expressions :
(i) 10 + (0 + 11) 0* 1
(ii) ((a + bb)* a a)*
(iii) (ab + ba)* a (ab*)*
(iv) 01 [((10)* + 111)* + 0]* 1
(v) 0* (1 + 0)* 10 + 11
(vi) (ab + ba)* a (ab*)*
(vii) a* b (ab* + ba) c*
5. Check whether the following languages are regular?
(i) {an bm | n ≥ m}
(ii) {a1 a3 a5 … a2n–1 | a1 a2 a3 … a2n is in L}
(iii) L = {0n | n is a prime}
(iv) {ai b2i+1 | i ≥ 1}
(v) {0p 1q 0p+q | p ≥ 1, q ≥ 1}
(vi) {xxRw | x, w in (0 + 1)+}
(vii) {0m 1 0m+n | m ≥ 1 and n ≥ 1}
(viii) {0a 1b | gcd (a, b) = 1}
(ix) L = {0n 1m | m ≥ n, n > 0}
(x) L = L1 ∩ L2 where
L1 = all strings over {a, b} starting with a and ending with b.
L2 = all strings over {a, b, c} having 'abb' as a substring in it.
6. Find L = L1 ∩ L2 where
L1 = all strings starting with 'a' containing even number of b's over {a, b}
L2 = all strings containing odd number of b's over {a, b}.
7. Find L = L1 ∩ L2 where
L1 = All strings starting with 'a'.
L2 = All strings not having "ab" as substring over {a, b}.

2.34
Theoretical Computer Science Regular Expressions and Languages

8. Find L = L1 L2 where
L1 = a* and L2 = b*.
9. Find L = L1 ∪ L2 where
L1 = {an bn | n ≥ 1}
L2 = {an bn ci | n ≥ 1, i ≥ 1}
UNIVERSITY QUESTIONS AND ANSWERS
April 2016
1. Write the regular expression for the following FA: [1 M]

Ans. Refer to Section 2.1.


2. State pumping lemma of regular set. [1 M]
Ans. Refer to Section 2.4.
3. Construct FA for the following regular expression:
(010 + 00)* (10)*. [5 M]
Ans. Refer to Section 2.3.
October 2016
1. Give the regular expression for the following DFA: [1 M]

Ans. r = (1 + 0)*.0.
2. Give any two identities of regular expression. [1 M]
Ans. Refer to Section 2.1.3.
April 2017
1. Define regular expression. [1 M]
Ans. Refer to Section 2.1.
2. Which tool is used to prove that the language is not regular? [1 M]
Ans. Refer to Section 2.2.
3. Define Kleene Closure. [1 M]
Ans. Refer to Page 2.2, Point (2).

2.35
Theoretical Computer Science Regular Expressions and Languages

4. Construct a FA for the given RE = (a*b + b*a).ab + ba.b*. [5 M]


Ans. Refer to Section 2.3.
October 2017
1. Write regular expression for the set of all strings of a's and b's ending with ab over
Σ = {a, b}. [1 M]
Ans. Refer to Section 2.2.
2. Construct NFA for regular expression 1.0* + 0*. [3 M]
Ans. Refer to Section 2.3.
3. How to apply pumping lemma to prove certain language are non-regular? [3 M]
Ans. Refer to Section 2.4.
April 2018
1. Write smallest possible string accepted by the following regular expression: [1 M]
(01 + 10*)* 1
Ans. Refer to Section 2.1.
2. Construct FA for regular expression: [5 M]
(01)* + (0 + 1)* 0* 1
Ans. Refer to Section 2.3.
October 2018
1. Write the smallest possible string accepted by regular expression: (0 + 1)* 01*. [1 M]
Ans. Refer to Section 2.1.
2. Construct FA for the following regular expression: (010 + 00)* (10)*. [4 M]
Ans. Refer to Section 2.3.
April 2019
1. Write smallest possible string accepted by the following regular expression.
(11)* (00)* + (10+01)*.1 [1 M]
Ans. Refer to Section 2.1.
2. Construct FA for regular expression: [2 M]
(0 + 1)* 01 + (1 + 0)* 11
Ans. Refer to Section 2.3.
3. Define the term: Kleene closure. [1 M]
Ans. Refer to Page 2.2, Point (2).
4. Show that CFL’s are closed under concatenation. [2 M]
Ans. Refer to Section 2.2.

2.36
CHAPTER
3

Context-Free Grammars and


Languages
Objectives …
To study Basic Concept of Grammar
To learn Context Free Grammar
To understand Normal Forms
To learn Regular Grammar

3.0 INTRODUCTION
• A language consists of a finite or infinite set of sentences. Infinite language is specified by a
grammar.
• A grammar consists of a finite non-empty set of rules or productions, which specify the
syntax of language.
• Number of grammar may generate the same language but consists of different structures on
the sentences of that language.
• Even though human language's have rules of grammar. For example, English language has
grammar and grammar has rules.
• Another method for language specification is to have a machine, called an acceptor,
determine whether a given sentence belongs to the language.
• In 1959, Chomsky, cataloged the hierarchy of grammars according to the structure of their
productions, which we will discuss further.
• Context-Free Grammar (CFG) is a formal grammar which is used to generate all possible
patterns of strings in a given formal language.
• The context-free languages are the languages which can be represented by CFG.

3.1 GRAMMAR
• Grammar can be defined as, a set of formal rules for generating syntactically correct sentence
from a particular language for which it is written.
3.1
Theoretical Computer Science Context-Free Grammars and Languages

• For any type of language [Formal (like 'C') or Natural (like English)] is required to have a
grammar, which can be defined in syntactically correct statement formats or in turn we can
say that it is the syntactic definition of the language, or in other words, we can also say that,
grammar defines syntax of a language.
• For example: If we want to generate an English statement "Dog runs", we have to use the
following rules:
< sentence > → < noun > < verb >
< noun > → Dog
< verb > → runs
• These rules describe how the sentence can be generated as 'noun' followed by 'verb' and so
on. There are many such rules which can be defined for English language and collectively
can be called as grammar for the language.
• Grammar normally consists of two types of basic elements, namely Terminal symbols and
Non-terminal symbols or variables.
1. Terminal symbols are those which are the constituents of the generated sentence, which
we have generated using a grammar. For example, in the above example, 'Dog' and 'runs'
are terminal symbols.
2. Non-terminal symbols are those which take part in the formation or generation of the
statement, but are not the part of generated statement like terminal symbols. For
example, 'sentence', 'noun' and 'verb' are non-terminals in the above example, which are
not in the generated statement as,
gives
< sentence > → < noun > < verb >
gives
→ Dog runs.
• The rules of the grammar are called as productions or production rules or syntactical rules.
Example:
Sentence = Omkar ate an apple.
We use the following rules for sentence.
<sentence> → <subject> <predicate>
<predicate> → <verb> <article> <noun>
<subject> → <noun>
<noun> → Omkar | apple
<verb> → ate
<article> → an
3.2
Theoretical Computer Science Context-Free Grammars and Languages

Using these rules we can derive the sentence


<sentence> 
⇒ <subject> <predicate>
⇒Omkar <predicate>
⇒Omkar <verb> <article> <noun>
⇒Omkar ate <article> <noun>
⇒Omkar ate an <noun>
 Omkar ate an apple.
• So using the production rules we derive a sentence. We also say that a sentence belongs to
the language of that grammar.
3.1.1 Definition of the Grammar
• The two aspects of the language which is to be considered are:
1. Generative capacity.
2. Grammatical constituents.
• The first aspect indicates that the grammar should generate all and only those sentences
which belong to the language. The second aspect deals with primitive structures, like
terminals and non-terminals.
• A grammar based on the constituent structure as described above is called as phrase structure
grammar.
• Formal Definition: A phrase structure grammar or simply a grammar is quadruple denoted
as, G = (V, T, P, S)
where V : finite set of non-terminals (variables)
T : finite set of terminals
S : S is a non-terminal symbol called as start symbol
and P : finite set of productions of the form
α → β where α, β ∈ (V ∪ T)*
and α has atleast one symbol from V.
• The α and β consist of any number of terminals and non-terminals and they are called as
sentential forms.
3.1.2 Examples of the Grammar
• Following is the example of the grammar:
S-> ABC | d
A-> aA | a
B -> bB | b
C-> cC | c
3.3
Theoretical Computer Science Context-Free Grammars and Languages

Here, Set of Non terminals V = {S, A, B, C}


Set of terminals T = {a, b, c, d}
Set of production rules,
P = {S → ABC, S → d, A → aA, A → a, B → bB, B → b, C → cC, C → c}
And starting symbol of the grammar is S.

DERIVATIONS AND LANGUAGE GENERATED BY A


3.2
GRAMMAR [April 16]
• The sequence of application of production rules that produces the finished string of terminals
from the start symbol is called a derivation.
• If A → β is a production of p and α, γ ∈ (V ∪ T)*, then αAγ ⇒ αβγ. We say that αAγ
directly derives αβγ in grammar G. i.e. production A → β is applied on αAγ to get αβγ.
• There are two different derivations possible, namely
1. Leftmost derivation.
2. Rightmost derivation.
• * w}. Thus string is in
The language generated by G denoted by L(G) is {w | w is in T* and S ⇒
L(G) iff
1. String consists of only terminals.
2. The string can be derived from S.
• A language generated by a CFG is called as context-free language, abbreviated as CFL.
We say G1 and G2 are equivalent if L(G1) = L(G2).

3.2.1 Derivation Trees or Parse Tree [April 16, 19]

• It is the graphical representation of derivation. The derivation in the CFG be represented


using a tree is called as derivation tree.
• Definition: Let G = (V, T, P, S) be a CFG. A tree is derivation tree for G (parse tree) if
1. Every vertex has a label which is a symbol V ∪ T ∪ {∈}.
2. The root node is always start symbol, denoted by S.
3. A vertex is interior and has label A, then A must be in V.
4. If the string w of the language generated by grammar has a length n, then there are n
terminal nodes arranged from left to right.
5. Vertex n has label ∈, then n is a leaf and is the only son of its father.

Example 1: Consider grammar G = ({S, A, B}, {a, b}, P, S) where


P is {S → AB, A → a, B → b}. We can derive the string "ab" as S ⇒ AB ⇒ aB ⇒ ab.

3.4
Theoretical Computer Science Context-Free Grammars and Languages

Solution: Derivation tree is shown in the Fig. 3.1.

|ab| = 2
There are two terminal nodes arranged
from left to right.

Fig. 3.1
Example 2: Draw the derivation tree for a substring "001100" using the following grammar
G:
G = ({S, A}, {0, 1}, {S → 0AS | 0, A → S1A | SS | 10}, S)
Solution: Since S is the start symbol, string is derived from S.
S ⇒ 0AS ⇒ 0S1AS ⇒ 001AS ⇒ 00110S ⇒ 001100
The derivation tree is shown in the Fig. 3.2.

Fig. 3.2

3.2.2 Left and Right Derivations


• If at each step in a derivation, a production is applied to the leftmost variable (non-terminal),
then the derivation is called as LeftMost Derivation (LMD).
• If at each step in a derivation, a production is applied to the rightmost variable
(non-terminal) then the derivation is called as RightMost Derivation (RMD).
Example 1: Consider the following grammar G = ({S, A, B}, {a, b}, P, S)
where, P is S → aB | bA
A → a | aS | bAA
B → b | bS | aBB
Find leftmost and rightmost derivations for the string "bbaaba".
Solution: 1. Leftmost derivation:


S ⇒ bA


⇒ bbAA


⇒ bbaSA


⇒ bbaaBA
⇒ bbaabA

⇒ bbaaba
3.5
Theoretical Computer Science Context-Free Grammars and Languages

2. Rightmost derivation:

S ⇒ bA
⇒bbAA
⇒bbAa
⇒bbaSa
⇒bbaaBa
⇒bbaaba
Example 2: Consider the following grammar: G = ({S, A}, {a, b}, P, S)
Where, P is S → AbaaA | aA
A → Aa | Ab | ∈
Find the leftmost and rightmost derivation for the string “abaabb”.
Solution: 1. Leftmost derivation:

S ⇒ AbaaA
⇒AabaaA
⇒abaaA
⇒abaaAb
⇒abaaAbb
⇒abaabb
2. Rightmost derivation:

A ⇒ AbaaA
⇒AbaaAb
⇒AbaaAbb
⇒Abaabb
⇒Aabaabb
⇒abaabb

3.2.3 Reduction
• Reduction means derivation in reverse. The process starts from a sentence. It finds the string
which matches with RHS of the production rule.
• When the match is found also called handle, then it replace with LHS of same production
rule. This is called reduction. The process repeats until we get starting non-terminal symbol.
This process is called handle running.
• Reduction is used in bottom-up parsing.
3.6
Theoretical Computer Science Context-Free Grammars and Languages

Example: Consider the grammar of expression,


E → E + E | E * E | id
Find out whether the string id + id * id is generated by this grammar.
id + id * id (starting string or sentence)
 E + id * id reduce by E → id
 E + E * id reduce by E → id (id is handle)
 E+E*E reduce by E → id
 E*E reduce by E → E + E (handle is E + E)
 E (handle is E * E)
Since E is the start symbol, the string id + id * id is generated from the grammar or it is yield
by grammar of expression.

3.3 CHOMSKY HIERARCHY [April 16, 17, Oct. 16, 17, 18]
• Linguist Noam Chomsky defined a hierarchy of languages, in terms of complexity. This four-
level hierarchy, called the Chomsky hierarchy, corresponds to four classes of machines.
• Each higher level in the hierarchy incorporates the lower levels i.e., anything that can be
computed by a machine at the lowest level can be computed by a machine at the next highest
level.
• The Chomsky hierarchy classifies grammars according to the form of their productions into
the following levels:
1. Type 0 (unrestricted grammar),
2. Type 1 (context-sensitive grammar),
3. Type 2 (context-free grammar), and
4. Type 3 (regular grammar).
• Let us see above grammars in detail.
1. Type 0 (Unrestricted Grammar ):
• There are no restrictions on the productions of grammar of this type. This type of grammar
permits productions of the form α → β with α ≠ ∈, where 'α' and 'β' are sentential forms i.e.
any combinations of any number of terminals and non-terminals i.e. α, β ∈ (V ∪ T)* but α
≠ ∈. Such grammar is called as unrestricted grammar.
• Unrestricted grammar generates the recursively enumerable languages or every type 0
language forms a recursively enumerable set. i.e. we can construct Turing Machine (TM) to
recognize the sentences generated by this type of grammar.
• For example: Grammar, G = (V, T, P, S)
where V = {S, B, C}, T = {a, b, c}
and P = S → SB
SB → BC
B→a
3.7
Theoretical Computer Science Context-Free Grammars and Languages

2. Type 1 (Context-Sensitive Grammar): [April 19]


• The restrictions on this type of grammar are:
(i) For each production of the form α → β, length of 'β' is at least as much as the α except S
→ ∈.
(ii) Also, the start symbol 'S' does not appear on the right-hand side of any production.
• The productions are of the form: α1 A α2 → α1 β α2 (β ≠ ∈)
where, the replacement of a non-terminal 'A' is allowed by 'β' only in the context α,
preceding 'A' and 'α2' succeeds 'A'.
• For example: Grammar, G = ({A, B, C}, {a, b}, P, A)
where, P is A → AB
AB → AC
C → ab
Above grammar is context-sensitive grammar.
3. Type 2 (Context Free Grammar): [Oct. 16]
• In this grammar, the only allowed type of production is A → α, where 'A' is a non-terminal
and 'α' is a sentential form i.e. α ∈ (V ∪ T)* (here, α can be ∈).
• The left-hand side of the production should contain only one non-terminal.
• For example: Grammar, G = ({S}, {a, b}, P, S)
where, P is S → aSa | bSb | a | b
4. Type 3 (Regular Grammar):
• In this type of production, there are following restrictions on the type of production:
(i) Left-hand side of each product should contain only one non-terminal.
(ii) Right-hand side can contain at the most one non-terminal symbol which is allowed to
appear as the rightmost symbol or leftmost symbol.
• The language generated using this grammar i.e. regular language is too primitive and can be
generated using an finite state machine.
• Depending on the position of a non-terminal, whether leftmost or rightmost, regular grammar
is further classified as:
(i) Left-linear grammar.
For example: G = ({A, B}, {a, b}, P, A)
P: A → Ba | a
B→b
(ii) Right-linear grammar.
For example: P: A → aB | a B → b
3.8
Theoretical Computer Science Context-Free Grammars and Languages

• The Table 3.1 shows the Chomsky hierarchy of grammars and machine which are acceptors
of the grammar.
Table 3.1: The Chomsky hierarchy of grammars and validating machines for languages
Name of Production restriction Example of
Type languages Acceptor
X→Y application
generated
0 Phrase-structure = X = any string with non- TM Computers
recursively terminals
enumerated Y = any string
1 Context sensitive X = any string with non- TM with Computers
terminals bounded (not
Y = any string as long as infinite) TAPE,
or longer than X called linear
bounded
automata LBA.
2 Context-free X = one non-terminal PDA Programming
(CFL) Y = any string languages,
statements,
compilers.
3 Regular X = one non-terminal FA Text editors
Y = tN or Y = t,
where t is terminal and N
is non-terminal
• From the above table we would know that regular languages are accepted by machine or
mathematical model called Finite Automata (FA), Non-regular Languages (CFL's) are
accepted by Pushdown Automata (PDA) and context sensitive, enumerated languages are
accepted by Turing Machine (TM).

3.4 CONTEXT-FREE GRAMMAR (CFG) [April 16, Oct. 16]


• A CFG is notation that can be used to specify language, and the language specified by a CFG
is called as context-free language.
3.4.1 Definition of CFG
• The context free grammar G = (V, T, P, S) where V is finite set of non-terminals.
T = finite set of terminals
P = finite set of production rules of the form
Non-terminal → terminal + non-terminal
3.9
Theoretical Computer Science Context-Free Grammars and Languages

• This form is called Backus Naur Form (BNF) since left hand side of the production rule
contains only one non-terminal.
S = start symbol of a grammar
• For example: Grammar of expression,
1. E → E + E | E * E | (E) | id
Here, V = {E}
T = {+, *, (, ), id}
There are 4 production rules and E is start symbol.
2. S → AB
A → a
B → b
Here, V = {S, A, B}
T = {a, b}
There are three production rules and S is start symbol.
Conventions Regarding Grammars (used in this Chapter):
1. The capital letters A, B, C, D, E and S denote variables, S is the start symbol.
2. Lower case letters a, b, c, d, e digits are terminals.
3. The capital letters X, Y and Z denote symbols that may be either terminal or variable.
4. The lower case letters u, v, w, x, y and z denote string of terminals.
5. The lower case Greek letters α, β and γ denote string of variables and terminals i.e. (V
∪ T)*.
6. "→" symbol stands for production, For Example: S → a.
7. "⇒" symbol stands for process of derivation.
*
"⇒ " stands for deriving in any number of steps.
8. If A → α1, A → α2 … A → αk are productions for variable A of some grammar then we
can express it using notation A → α1 | α2 | α3 | … |αk.

3.4.2 Examples
• Consider the grammar:
S → AB.
A → a
B → b
Here, S->AB , A→a, B→b are production rules.
S=>AB=>aB=> ab (The string ab is derived from the S, called derivation).
And, S ⇒* ab is the derivation from S using number of steps.
3.10
Theoretical Computer Science Context-Free Grammars and Languages

3.5 AMBIGUOUS GRAMMAR [April 16, 17, Oct. 16, 18]


• A grammar is said to be ambiguous if, for a string, there exists more than one parse trees.
• An ambiguous grammar may derive ambiguous strings, which may be undesirable words of a
language. Thus, if a grammar is ambiguous, then it has to be modified by making necessary
changes in the production rules.
• A CFG is called ambiguous if for at least one word in the language that it generates, there are
two possible derivations of the word that correspond to different parse trees or syntax trees.
3.5.1 Concept and Examples
• A CFG is called ambiguous if for at least one word in the language that it generates, there are
two possible derivations of the word that correspond to different parse trees or syntax trees.
Example 1: Let consider CFG,
S → AB prod. 1
A → a prod. 2
B → b prod. 3
There are two different derivations for the word "ab”:
1. S ⇒ AB ⇒ aB ⇒ ab (leftmost derivation)
2. S ⇒ AB ⇒ Ab ⇒ ab (rightmost derivation)

(a) Parse Tree for Leftmost Derivation (b) Parse Tree for Rightmost Derivation
Fig. 3.3: Parse Tree
Since the word "ab" represents only one parse tree, therefore, grammar is unambiguous,
(only one leftmost derivation is possible).
Hence, we can say that if some string has more than one leftmost or rightmost derivation then
it is an ambiguous CFG.
Example 2: Consider the following CFG:
S → aB | aA
A → aAB | a | b
B → Abb | b
The leftmost derivation for the string "aaabbbbb"
S ⇒ aA ⇒ aaAB ⇒ aaaABB ⇒ aaabBB ⇒ aaabAbbB ⇒ aaabbbbB ⇒ aaabbbbb
3.11
Theoretical Computer Science Context-Free Grammars and Languages

The rightmost derivation for the string "aaabbbbb"


S ⇒ aA ⇒ aaAB ⇒ aaAAbb ⇒ aaAbbb ⇒ aaaABbbb ⇒ aaaAbbbb ⇒ aaabbbbb
The corresponding parse tree is shown in the Fig. 3.4.

Fig. 3.4: Parse Tree


We can derive the string "aaabbbbb" using leftmost derivation in other way as
S ⇒ aB ⇒ aAbb ⇒ aaABbb ⇒ aaaABBbb
⇒ aaabBBbb

⇒ aaabbBbb ⇒ aaabbbbb
As there are two leftmost derivations for string "aaabbbbb" which give two different parse
trees, grammar is ambiguous grammar.

3.5.2 Removal of Ambiguity of the CFG's


• Ambiguous grammar is not suitable for compilation. The control structures like if, for, while,
etc. are generated by context-free grammar. So we need unambiguous grammar as input to
the compiler.
• To remove the ambiguity we need two concepts.
1. Associativity: Check operator is left associative or right associative.
2. Precedence: Consider the grammar
E → E + E | E * E | E – E | E/E | id
Consider a string id + id * id and draw parse tree.

Fig. 3.5
3.12
Theoretical Computer Science Context-Free Grammars and Languages

Clearly the grammar is ambiguous. So find the precedence and associativity of each operator.
Operator Precedence Associativity
+, – 2 left
*, / 4 left
↑ (exponent) 6 right
• First level of production rule contain lowest precedence operator. Lower priority
symbols are closer to the start symbols.
• Add new non-terminal symbols T and F in the above CFG.
• If operator is left associative, the production rule of that grammar is left recursive.
• If operator is right associative, then the production rule of that grammar is right
recursive.
Consider +, – which are having lower priority and left associativity, so production rule
should be left recursive.
E → E+T|E–T
E → T
Then for operator *, /, productions are added.
T → T * F | T/F
T → F
id has no associativity.
F → id
So unambiguous grammar becomes,
G = (V, T, P, S)
where V = {E, T, F} T = {+, –, *, /, id}
P is E → E+T|E–T|T
T → T * F | T/F | T
F → id
Now consider string id + id + id, we get the following derivation.
E ⇒ E+T 
⇒ T+T 
⇒ F+T 

⇒ id + T

⇒ id + T * F
⇒id + F * F
⇒id + id * F
⇒id + id * id
3.13
Theoretical Computer Science Context-Free Grammars and Languages

We get only one parse tree.

Fig. 3.6
So the above grammar is unambiguous.

EXAMPLES
Example 1: Consider CFG, G = ({S}, {a}, {S → aS, S → ∈}, S). Find the language L(G).
Solution: Firstly consider production S → ∈.
So S ⇒ ∈. Thus ∈ is in L(G).
Now for all n ≥ 1,
1. S ⇒ aS ⇒ a.
2. S ⇒ aS ⇒ aaS ⇒ aa
3. S ⇒ aS ⇒ aaS ⇒ aaaS ⇒ aaa
4. S ⇒ aS ⇒ aaS ⇒ aaaS ⇒ aaaaS ⇒ aaaa and so on.
The string an comes from n application of production 1 followed by one application of
production 2.
The language generated by this CFG is a*.
Thus, L(G) = {∈, a, aa, …}.
Example 2: Find CFL associated with CFG given below:
G = ({S}, {0}, {S → SS}, S).
Solution: In this case, L(G) = φ. This is because only production in G is S → SS and in
production there is no terminal symbol.
Example 3: Find CFL associated with CFG
G = ({S}, {a, b}, {S → aSb | ab}, S)
Solution: 1. S → ab production 2

2. S ⇒ aSb ⇒ aabb

3. S ⇒ aSb ⇒ aaSbb ⇒ aaabbb and so on.
Thus language L(G) = {ab, aabb, aaabbb, …}
i.e. L(G) = {an bn | n ≥ 1}
3.14
Theoretical Computer Science Context-Free Grammars and Languages

Example 4: Find CFL associated with CFG


G = ({S}, {a, b}, P, S), where P is
S → aB | bA
A → a | aS | bAA
B → b | bS | aBB
Solution:
1. S ⇒ aB ⇒ ab
2. S ⇒ bA ⇒ ba
3. S ⇒ aB ⇒ abS ⇒ abaB ⇒ abab
4. S ⇒ aB ⇒ abS ⇒ abbA ⇒ abba
5. S ⇒ aB ⇒ aaBB ⇒ aabb
6. S ⇒ bA ⇒ baS ⇒ babA ⇒ baba
7. S ⇒ bA ⇒ bbAA ⇒ bbaa.
Thus CFL, L(G) consists of all strings having equal number of a's and b's.
∴ L(G) = {x | x containing equal number of a's and b's}
Example 5: Construct a context-free grammar which accepts set of palindromes over {a, b}*.
Solution: (i) ∈ is a palindrome.
(ii) a and b are palindromes.
(iii) If x is a palindrome then axa and bxb are also palindromes.
To construct grammar G, we use one production for each of above.
So grammar will be, G = ({S}, {a, b}, P, S)
where P is S → ∈
S →a
S → b
S → aSa
S → aSb
or S → aSa | aSb | a | b | ∈
Example 6: Construct a context-free grammar generating strings with no consecutive b's but
may or may not with consecutive a's.
Solution: The grammar G will be as follows:
G = ({S, A}, {a, b}, P, S)
where P is {S → aS | bA | a | b | ∈
A → aS | a | ∈}
3.15
Theoretical Computer Science Context-Free Grammars and Languages

Example 7: Construct CFG generating L = {wcwR | w ∈ (a + b)*}.


Solution: Here, w ∈ {a, b}* and wR ∈ {a, b}*.
wR → is the string reverse of w.
The grammar G will be as follows:
G = ({S}, {a, b, c}, P, S)
where P is {S → aSa | bSb | c}
Example 8: Construct CFG for language L, where
L = {an bm cn | n > 1, m > 0}
Solution: Grammar G will be as follows:
G = ({S, A} {a, b, c}, P, S)
where P is {S → aSc | aAc | ac
A → bA | b}
Example 9: Construct CFG for L = L1 ∪ L2 where
L1 = {an bm | n ≥ m, m > 0}
L2 = All strings not having 01 as a substring over {0, 1}.
Solution: Language L accepts all strings of L1 or L2 since L = L1 ∪ L2.
Let grammar G1 for L1 = (V1, T1, P1, S1) where
V1 = {S1, A}, T1 = {a, b}, P1 = {S1 → aS1b | ab | aAb A → aA | a}
Let grammar G2 for L2 = (V2, T2, P2, S2) where
V2 = {S2, A1}, T2 = {0, 1} and P2 is
{S2 → 0A1 | 1S2 | 0 | 1
A1 → 0A1 | 0}
∴ Resultant grammar G for L = L1 ∪ L2 is (V, T, P, S)
where V = V1 ∪ V2 ∪ {S}, T = T1 ∪ T2 = {a, b, 0, 1}
and P = P1 ∪ P2 ∪ {S → S1 | S2}
Therefore P is, S → S1 | S2
S1 → aS1b | ab | aAb
A → aA | a
S2 → 0A1 | 1S2 | 0 | 1
A1 → 0A1 | 0
3.16
Theoretical Computer Science Context-Free Grammars and Languages

Example 10: Construct a CFG for each of the language defined by the following regular
expressions: (i) ab*, (ii) a* b*, (iii) (baa + abb)*.
Solution: 1. The required CFG for ab* is
S → aB
B → bB | ∈
2. The required CFG for a* b* is
S → AB
A → aA | ∈
B → bB | ∈
3. The required CFG for (baa + abb)* is
S → AS | BS | ∈
A → baa
B → abb
where 'S' is start symbol and T = {a, b}.
Example 11: Construct a grammar for L = {an bn cm | n ≥ 1, m ≥ 0}.
Solution: The required grammar will be
G = ({S, A}, {a, b, c}, P, S)
where P = {S → A | Sc
A → ab | aAb }
Example 12: Construct a CFG for language L = L1 ∩ L2
where L1 = {an b an | n ≥ 1}
L2 = All strings having odd length over {a, b}.
Solution: Since we observe that length of L1 is odd, therefore, L1 ∩ L2 = L1.
Required CFG for L is ({S}, {a, b}, {S → aSa | aba}, S)
Note: All examples we discussed in this chapter are called CFG. The property of CFG is that
all productions are of the form: one non-terminal → string of T and V. This form of production
is called as Backus Nour Form (BNF).
Example 13: Let G = ({S, A}, {a, b}, P, S),
where P is S → aAa
A → aAa | b construct the language.
Solution: 
S ⇒ aAa ⇒ aba

S ⇒ aAa ⇒ aaAaa ⇒ aabaa

S ⇒ aAa ⇒ aaAaa ⇒ aaaAaaa
⇒ aaabaaa
:
:
L (G) = {an b an | n ≥ 1}
3.17
Theoretical Computer Science Context-Free Grammars and Languages

Example 14: Construct a CFG for a language in which all strings with no consecutive b's but
may or may not with consecutive a's.
Solution: Here, L = {∈, a, b, aa, ba, ab, ......}
The grammar G will be
G = ({S, A) {a, b}, P, S)
where P is S → aS | bA | a | b | ∈
A → aS | a | ∈
Example 15: Construct the CFG for language containing the string with atleast “aaa” in it
over {a, b}.
Solution: G = ({S, A} {a, b}, P, S)
where P is S → AaaaA
A → aA | bA | ∈
Example 16: Find the CFL generated by the following grammar.
S → AB
A → aA | bB | a
B → Ba | Bb | a
Solution: 1. 
S ⇒ AB ⇒ aa
2. S ⇒AB
⇒aAB ⇒ aaa
3. S ⇒AB
⇒bBB
⇒bBaB
⇒baaa
So, L = {aa, aaa, baaa, baab, ……}
L = {w | w containing atleast one occurrence of two consecutive a's}
Example 17: Write CFG for a language containing string having atleast one occurrence of
“00” over {0, 1}. [April 16]
Solution: The regular expression for language is
(0 + 1)* 00 (0 + 1)*
So CFG is S → ABA
A → 0A | 1A | ∈
B → 00
OR S → A00A
A → 0A | 1A | ∈
3.18
Theoretical Computer Science Context-Free Grammars and Languages

Example 18: Write CFG for generating IF loop of ‘C' language.


Solution: S → if COND ST else ST
COND → id RELOP id | id RELOP num
ST → id | id = id | id = num
RELOP → < | < = | > | > = | < > | =
id → LX
X → LX|DX|∈
L → A | B | C | ...... | Z | a | b ...... | Z
D → 0 | 1 | 2 | ...... | 9
num → D Num | D
Here, statement is “if” loop i.e. if condition statement else statement.
where condition is
identifier relational operator identifier | num
Regular expression for identifier is
(l) (l/d)*
where l = A to Z or a - z
d = 0 to 9
CFG for identifier is →
ID → L X
X → LX|DX|∈
L → A | B | ...... | Z | a | ...... | Z
D → 0 | ...... | 9
CFG for number is,
Num → D Num | D
i.e. atleast one digit = (d+) is regular expression.
Example 19: Construct a grammar for regular expression (a + b)* over {a, b}.
Solution: L = {∈, a, b, ab, ba, abb, aab, aa ……}
CFG is S → aA | bA | ∈
A → bS | aS
Example 20: Construct a grammar which accept a language (a + b + c)*.
Solution: L = {∈, a, b, c, ab, bc, ac, aa, bb, cc, abc, ......}
CFG is G = {V, T, P, S)
where V = {S, A} T = {a, b, c}
P is S → aA | bA | cA | ∈
S → aS | bS | cS
3.19
Theoretical Computer Science Context-Free Grammars and Languages

Example 21: Construct CFG for language defined by regular expression


(abb + baa)*
Solution: L = {∈, abb, baa, abbbaa, ......}
Grammar is S → AS | BS | ∈
A → abb
B → baa
where S is start symbol.
V = {A, B}
T = {a, b}
Example 22: Construct CFG for L = {0i, 1n, 2n | n ≥ 1, i ≥ 0} [April 16]
Solution: L = {12, 012, 0012, 0112, 0122, ......}
G = (V, T, P, S)
where P is S → S|A
A → 1A2 | 12
where V = {S, A} T = {0, 1, 2} S = Start symbol
Example 23: Construct CFG for Do fragment of C.
Solution: S → DO ST while COND
ST → id = id | num
COND → id relop id | num
relop → < | < = | > | > = | < > | =
id → L X
X → LX|DX|∈
L → A | B | ...... | Z | a | ...... | z
D → 0 | 1 | ...... | 9
num → Dnum | D
Example 24: Draw the derivation tree for substring 001100 using following grammar
G = ({S, A} {0, 1} {S → 0AS | 0, A → S | A| SS | 10}, S)
Solution: 
S ⇒ 0AS
⇒0S1AS
⇒001AS
⇒00110S
⇒001100
3.20
Theoretical Computer Science Context-Free Grammars and Languages

Derivation tree or Parse tree is,

Fig. 3.7
∴ 001100 is the yield of above language.

3.6 SIMPLIFICATION OF CFG [Oct. 17]


• In CFG, sometimes all the productions rules and symbols are not needed for the derivation to
solve.
• Some productions rules are never used during derivation of any string. Elimination of these
types of productions or symbols is called Simplification of Context Free Grammar.
• There are certain standard ways of writing CFG. They impose certain restrictions on the
productions in the CFG, so that it is easier to analyze the grammar. One such form is
Chomsky Normal Form (CNF).
• Another aspect of grammar is that not always the grammars are optimized i.e. they consist of
some extra symbols. Such symbols can be removed from the grammar without changing the
language represented by the grammar.
• In some methods, the grammar which provides a reduced form of grammar is called as
reduced grammar.
• There are three steps for simplification of CFG:
1. Elimination of useless symbol.
2. Elimination of unit production.
3. Elimination of ∈-production.
3.6.1 Elimination of Useless Symbols [Oct. 17]

• The productions in context free grammar that contains useless symbols are called Useless
productions. The grammar that we obtain after deleting useless production rules are
called reduced Context Free Grammar.
• Let G = (V, T, P, S) be a grammar. A symbol X is useful if there is a derivation
* αXβ ⇒ w for some α, β and w, where w ∈T*, otherwise X is useless.
S⇒
3.21
Theoretical Computer Science Context-Free Grammars and Languages

Symbol X is useful if
1. Some string must be derivable from X.
2. 'X' must appear in the derivation of atleast one string derivable from 'S' (start symbol).
3. It should not occur in any sentential form that contains a variable from which no
terminal string can be derived.
Example 1: Consider the grammar,
S → AB | a
A → a
Solution: Here, variable 'B' is not deriving any string of terminals. Thus, it is an useless
symbol. For removing 'B' to get reduced grammar, we should delete all productions for which 'B'
is appearing on the right hand side. Hence, we drop only S → AB to get simplified grammar
without useless symbol as
S → a
A → a
Here to derive a string of terminal through 'A', symbol 'A' cannot occur in any derivation of
some string derivable from 'S'. Therefore A is also a useless symbol.
Hence, the simplified grammar without useless symbol is
G = ({S}, {a}, {S → a}, S)
Example 2: Consider a grammar,
S → AB | BC
A → aAa | aAb
B → bB | b
D → dD | d
Solution: Consider production S → AB. As 'A' cannot derive any terminal string, it is a
useless symbol. So drop production S → AB. Similarly, for S → BC, symbol 'C' does not derive
any terminal string, drop S → BC.
Hence start symbol itself does not derive any string. Grammar is useless.
Example 3: Construct a grammar without useless symbols for the grammar
S → AB | CA
B → BC | AB
A → a
C → aB | b
D → SS | d
Solution: Consider S → AB, here B is useless because it cannot derive any terminal string.
Hence remove all productions which contain 'B' in RHS of production.
3.22
Theoretical Computer Science Context-Free Grammars and Languages

We rewrite the grammar as,


S → CA
A → a
C → b
D → SS | d
Since, D is not in the derivation of string which derives from the starting symbol S, hence, 'D'
is useless.
Thus, resulting grammar without useless symbol is
G = ({S, A, C}, {a, b}, P, S)
where P is {S → CA, C → b, A → a}

3.6.2 Elimination of Unit Productions [Oct. 17]

• A production of the form A → B where A and B both are non-terminals are called unit
productions. All other productions including ∈-productions are non-unit productions.


• Elimination Rule: If A → B is a unit production or if there is a chain of unit productions
leading from A to B such as A ⇒ X1 ⇒ X2 ⇒ … B where all Xi's are non-terminals, then
introduce new production(s) according to the following rule.
If the non-unit productions for 'B' are
B → α1 | α2 | …
where α1, α2 … are all sentential forms (not containing only one non-terminal)
then, create the productions for 'A' as
A → α1 | α2 | …
Example 1: Consider the grammar,
S → A | bb
A → B|b
B → S|a
Solution: As we can see there is a chain of unit productions S → A → B → S.
As A → B and B → S | a, we can add new productions to A as
A → S|a
Hence the grammar becomes,
S → A | bb
A → S|a|b
Still there is a unit production S → A, removing this we get S → S | a | b | bb.
3.23
Theoretical Computer Science Context-Free Grammars and Languages

There is one more unit production S → S. We can directly remove as both side symbols are
same.
Therefore, the equivalent grammar without unit production will be,
S → a | b | bb
Example 2: Consider the grammar G as,
A → B
B → a|b
Find the equivalent grammar without unit production.
Solution: By rule, B → α1 | α2 where α1 = a and α2 = b
∴ Reduced grammar without unit production is A → a | b.
Example 3: Consider the grammar G as,
S → Saab | A
A → Sbba | B
B → aS | bS | a | b
Solution: First substitute all B-productions in A
S → Saab | A
A → Sbba | aS | bS | a | b
Now substitute all A-productions in S.
S → Saab | Sbba | aS | bS | a | b
This is the grammar without unit production.

3.6.3 Elimination of ∈-Production


• Production of the form A → ∈ where A is non-terminal is called as ∈-production.
• Note that if ∈ is in L(G) for grammar G, we cannot eliminate all ∈-productions from G but
'∈' is not in L(G), then we can.
• Theorem 1: Let G = (V, T, P, S) be a CFG then we can find CFG G' having no null
production such that L(G) = L(G') – {∈}.
Construction:
Step 1: Delete all ∈-productions from grammar.
Step 2: * ∈ then N is
If CFG G1 there is non-terminal N and production N → ∈ or N ⇒
called nullable non-terminal.
Identify nullable non-terminal.
Step 3: If there is a production of the form 'A → α' where α is any sentential form
containing atleast one nullable non-terminal, then add new productions having
right hand side formed by deleting all possible subsets of nullable non-terminals
from α.
Step 4: If using step 3, we get production A → ∈ then do not add that to final grammar.
3.24
Theoretical Computer Science Context-Free Grammars and Languages

Example 1: Eliminate ∈-productions from G, where G is S → aSa | bSb | ∈.


Solution: 1. As step 1, deleting ∈-production we get,
S → aSa | bSb
2. As S → ∈ is production, hence S is nullable.
3. There are two productions containing nullable non-terminal on the RHS, these
are S → aSa and S → bSb.
Deleting S from RHS from both productions, we get,
S → aa and S → bb, which we add to the grammar.
Thus, the final grammar without ∈-productions is
S → aSa | bSb | aa | bb.
Example 2: Eliminate ∈-productions from grammar G as,
S → ABA
A → aA | ∈
B → bB | ∈
Solution: 1. Deleting ∈-productions, A → ∈ and B → ∈.
2. A and B both are nullable.
S ⇒ ABA ⇒ BA ⇒ A ⇒ ∈ is also nullable.
3. Find production consisting of nullable symbol on RHS and delete nullable
non-terminals from RHS. From S → ABA after deleting all possible subsets of nullable
non-terminals, we get,
S → AB
S → BA
S → AA
S → A|B|∈
We can add all other productions except S → ∈ to final set.
From A → aA, we get A → a
From B → bB, we get B → b
Therefore, the final grammar G' is,
S → ABA | AB | BA | AA | A | B
A → aA | a
B → bB | b
Here again we have unit production so remove unit production, we get
S → ABA | AB | BA | AA | aA | a | bB | b
A → aA | a
B → bB | b
or Substitute A - productions and B - productions in S for all A's and B's.
3.25
Theoretical Computer Science Context-Free Grammars and Languages

Example 3: Eliminate ∈-productions from G as S → AB, A → SA | BB | bB, B → b | aA| ∈.


Solution: Set of nullable variables = {B, A, S}.
The final grammar is,
S → AB | A | B
A → bB | b
A → B
A → SA | S | A
B → aA | a
B → b
Example 4: Eliminate ∈-productions for grammar,
S → aS | bA | a | b | ∈
A → aS | a | ∈
Solution: 1. Eliminate ∈-productions we get
S → aS | bA | a | b
A → aS | a
2. S and A are nullable symbols.
3. Find productions consisting of nullable symbols on RHS and delete nullable non-
terminals.
S = aS | a | bA | b
A → aS | a
This is grammar without ∈-productions.

3.7 NORMAL FORMS


• A grammar is said to be in a normal form when every production of grammar has specific
form.
• In context free grammar, LHS is only one non-terminal, but RHS is any string or non-
terminal and terminals. When the productions in G satisfy certain constraints, then G is said
to be in “normal form”.
• In this section we will discuss two normal forms:
1. Chomsky Normal Form (CNF),
2. Greibach Normal Form (GNF).

3.7.1 Chomsky Normal Form (CNF) [April 16, 17, 18, 19, Oct. 17, 18]

• In CNF, there are restrictions on the length of right-hand side, and type of symbols is used in
right hand side of production rules.
3.26
Theoretical Computer Science Context-Free Grammars and Languages

• Any CFL without ∈ is generated by a grammar in which all productions are of the form A →
BC or A → a, where A, B and C are variables and 'a' is a terminal. This type of grammar is
said to be in CFG.
• Note that any CFL that does not contain ∈ as a word has a CFG in CNF that generates
exactly as it's.
• However, if the CFL contains ∈, then when we convert the CFG into CNF, the ∈-word drops
out of the language while all other words stay the same.
Example 1: Convert the following CFG into CNF:
S → aSa | bSb | a | b | aa | bb
Solution: In CNF, we have only two types of productions A → BC or A → a.
We add two productions A → a and B → b and get,
S → ASA | BSB | a | b | AA | BB
A → a
B → b
Consider S → ASA which is not in CNF
S → D1A
D1 → AS
Similarly, S → BSB which is not in CNF
S → D2B
D2 → BS
Thus the equivalent grammar in CNF is
S → D1A | D2B | a | b | AA | BB
A → a
B → b
D1 → AS
D2 → BS
Example 2: Convert the following grammar into CNF:
S → aAab | Aba
A → aS | bB
B → ASb | a
Solution: Thus the required grammar in CNF is
S → CaD1 | AD3
D1 → AD2
D2 → Ca Cb
3.27
Theoretical Computer Science Context-Free Grammars and Languages

D3 → Cb Ca
A → Ca S | Cb S
B → A D4 | a
D4 → S Cb
Ca → a
Cb → b
Example 3: Convert the following grammar into CNF,
S → ABA
A → aA | ∈
B → bB | ∈
Solution: First remove ∈-productions to get new grammar L(G) – {∈}. After removing ∈-
productions as per rule, we get new grammar as
S → ABA | AB | BA | AA | A | B
A → aA | a
B → bB | b
Then by eliminating unit-productions S → A and S → B, we get grammar as,
S → ABA | AB | BA | AA | aA | a | bB | b
A → aA | a
B → bB | b
Now, convert the grammar into CNF.
So final grammar in CNF will be as follows:
S → D1 A | AB | BA | AA | Ca A | a | Cb B | b
A → Ca A | a
B → Cb B | b
D1 → AB
Ca → a
Cb → b
Example 4: Convert the following grammar into CNF,
S → bA | aB
A → bAA | aS | a
B → aBB | bS | b
3.28
Theoretical Computer Science Context-Free Grammars and Languages

Solution: Substitute X for a and Y for b.


S → YA | XB
A → YAA | XS | a
B → XBB | YS | b
X → a
Y → b
1. All s-productions are in CNF.
2. Consider A-production
A → YAA
Replace AA by P
A → YP
P → AA
3. Consider B → XBB
Replace BB by R
B → XR
R → BB
∴ Equivalent CNF grammar is
S → YA | XB
A → YP | XS | a
B → XR | YS | b
X → a
Y → b
P → AA
R → BB
Example 5: Convert the following CFG into CNF.
S → ABA
A → aA | ∈
B → bB | ∈
Solution: The grammar contains ∈-production, we have to eliminate ∈-production. Since
CNF is always represent language without ∈.
After eliminating ∈-productions, we get
S → ABA | AB | BA | AA | A | B
A → aA | a
B → bB | b
3.29
Theoretical Computer Science Context-Free Grammars and Languages

This grammar contains unit productions, so remove unit productions, we get


S → ABA | AB | BA | AA | aA | a | bB | b
A → aA | a
B → bB | b
Now grammar is simplified grammar without ∈-productions and unit productions, so convert
into equivalent CNF.
S → ABA
is S → AX
X → BA
Equivalent CNF grammar is
S → AX | AB | BA | AA | D1 A | a | D2 B | b
A → D1 A | a
B → D2 B | b
X → BA
D1 → a
D2 → b

3.7.2 Greibach Normal Form (GNF) [Oct. 16, 17, 18, April 17, 19]

• In GNF, there is restriction on the position, in which, terminals and variables can appear on
right-hand side of production rules.
• In GNF, every production must start with a single terminal followed by any number of
variables.
• In every CFL, L without ∈-production can be generated by a grammar for which every
production is of the form A → aα, where 'A' is a variable, 'a' is a terminal and α is a string of
only variables. This type of grammar is said to be GNF.
• Lemma 1: The production with variable A on left is called as A-production.
Let G = (V, T, P, S) be a CFG. Let A → α1βα2 be a production in P and B → β 1 | β 2 | … | β r
be the set of all B-productions. Let G1 = (V, T, P1, S) be obtained by deleting production
A → α1βα2 from P and adding productions A → α1β 1α2 | α1β 2α2 | … | α1 β r α2.
Then L(G) = L(G1).
• Lemma 2: Let G = (V, T, P, S) be a CFG. Let A → Aα1 | Aα2 | … | Aαr be the set of A-
productions for which A is the leftmost symbol of the RHS. Let A → β 1 | β 2 | … | β s be the
remaining A-productions. Let G1 = (V ∪ {B}, T, P1, S) be the CFG formed by adding the
variable B to V and replacing all the A-productions by the productions:
1. A → βi
1≤i≤S
A → βiB
3.30
Theoretical Computer Science Context-Free Grammars and Languages

2. B → αi
1≤i≤r
B → αiB
Then, L(G) = L(G1)
Note : Grammar should be in CNF before converting into GNF.
Example 1: Construct a grammar in GNF equivalent to grammar
S → AA | a
A → SS | b
Solution: Observe that the CFG is in CNF. If we rename S as A1 and A as A2 respectively,
then productions will be
A1 → A2 A2 | a
A2 → A1 A1 | b
We leave A2 → b as it is in the required form.
Now, consider A2 → A1 A1. To convert this we will use lemma 4.1, and get,
A2 → A2 A2 A1 By replacing the first A1 on
A2 → aA1 RHS of A2 → A1 A1 by definition of A1
(Note : Here we have been considering production A2 → A1 A1 because production is in the
form Ai → Aj α, j < i).
Now the production A2 → aA1 is in the required form.
But we need lemma for A2 → A2 A2 A1 as it is in the form A → Aα.
Applying lemma to productions of A2, A2 productions are,
A2 → A2 A2 A1 | aA1 | b
Here, β 1 = aA1, β 2 = b, α = A2 A1
∴ Adding new non-terminal B2, we get,
A2 → aA1 | b
A2 → aA1B2 | bB2
B2 → A2 A1
B2 → A2 A1 B2
Now, all A2 productions are in the required form.
Now we will have to consider A1 production,
A1 → A2 A2 | a
Applying lemma 4.2 i.e. replacing all A2 productions on RHS, we get,
A1 → aA1A2 | bA2 | aA1B2A2 | bB2A2 | b

3.31
Theoretical Computer Science Context-Free Grammars and Languages

Now, modify B2 production by replacing A2 production on RHS.


Consider B2 → A2 A1 | A2 A1 B2.
Both RHS A2 are replaced by A2 productions.
∴ B2 → aA1A1 | bA1 | aA1B2A1 | bB2A1
B2 → aA1A1B2 | bA1B2 | aA1B2A1B2 | bA1B2
So the grammar in GNF will be
G' = ({A1, A2, B2}, {a, b}, P', A1)
where P is {A1 → a | aA1A2 | bA2 | aA1B2A2 | bB2A2
A2 → aA1 | b | aA1B2 | bB2
B2 → aA1A1 | bA1 | aA1B2A1 | bB2A1
B2 → aA1A1B2 | bA1B2 | aA1B2A1B2 | bB2A1B2}
Example 2: Convert the following CFG into GNF:
S → AB | B
A → BS
B → A1 | 1
Solution: Above grammar contains unit production. So first eliminate it, and we get,
S → AB | A1 | 1
A → BS
B → A1 | 1
This grammar is not in CNF. Converting into CNF, we get,
S → AB | AC1 | 1
A → BS
B → AC1 | 1
C1 → 1
Replacing variable S by A1, A by A2, B by A3 and C1 by A4, we get,
A1 → A2 A3 | A2 A4 | 1
A2 → A3 A1
A3 → A2 A4 | 1
A4 → 1
Only A3 → A2 A4 is not in the required form Ai → Aj α, j > i.
∴ Apply lemma i.e. substitute all A2 productions in A3 productions.
3.32
Theoretical Computer Science Context-Free Grammars and Languages

We get, A3 → A3 A1 A4 | 1
Now, applying lemma 4.2, we get,
A3 → 1 | 1 B1
B1 → A1 A4 | A1 A4 B1
Now convert all productions in GNF.
A3 and A4 productions are already in GNF.
Applying lemma 4.1 on A2 productions we get,
A2 → 1 A1 | 1 BA1
Applying lemma 4.2 on A1 productions, we get,
A1 → 1 A1 A3 | 1 B1 A1 A3 | 1 A1 A4 | 1 B1 A1 A4 | 1
We get B1 productions as
B → 1 A1 A3 A4 | 1 B1 A1 A3 A4 | 1 A1 A4 A4 | 1 B1 A1 A4 A4 | 1 A4 |
1 A1 A4 A4 B1 | 1 B1 A1 A4 A4 B1 | 1 A1 A3 A4 B1 | 1 B1 A1 A3 A4 B1|
1 A4 B1
Thus the required grammar in GNF is
A1 → 1 A1 A3 | 1 B1 A1 A3 | 1 A1 A4 | 1 B1 A1 A4 | 1
A2 → 1 A1 |1 BA1
A3 → 1 | 1 B
A4 → 1
B1 → 1 A4 A3 A4 | 1 B1 A1 A3 A4 | 1 A1 A4 A4 | 1 B1 A1 A3 A4 B1 |
1 A4 | 1 A1 A4 A4 B1 | 1 B1 A1 A4 A4 B1 | 1 A1 A3 A4 B1 |
1 B1 A1 A3 A4 B1 | 1 A4 B1
Example 3: Convert the following grammar G to GNF:
A1 → A2 A3
A2 → A3 A1 | b
A3 → A1 A2 | a
Solution: Given grammar is in CNF.
Only the production A3 → A1 A2 is not in the required form.
Since j < i (i.e. 1 < 3).
Applying lemma 4.1 on A3 production, we get,
A1 → A2 A3
A2 → A3 A1 | b
A3 → A2 A3 A2 | a
3.33
Theoretical Computer Science Context-Free Grammars and Languages

Again substitute 'A2' on RHS of A3 with A2 productions, and we get,


A3 → A3 A1 A3 A2 | b A3 A2 | a
Now, applying lemma 4.2, we get,
A3 → b A3 A2 | a
A3 → b A3 A2 B3 | a B3
B3 → A1 A3 A2 | A1 A3 A2 B3
All A3 productions are in GNF form.
Therefore, substitute A3 productions in A2 productions, and we get,
A2 → b A3 A2 A1 | b A3 A2 B3 A1 | a B3 A1 | a A1 | b
Now, substitute A2 productions in A1 productions, and we get,
A1 → b A3 A2 B3 A1 A3 | a B3 A1 A3 | b A3 A2 A1 A3 | a A1 A3 | b A3
Now, substitute all A1 productions in B3 productions.
An equivalent grammar in GNF can be written as,
A1 → b A3 A2 B3 A1 A3 | a B3 A1 A3 | b A3 A2 A1 A3 | a A1 A3 | b A3
A2 → b A3 A2 B3 A1 | a B3 A1 | b A3 A2 A1 | a A1 | b
A3 → b A3 A2 B3 | a B3 | b A3 A2 | a
B3 → b A3 A2 B3 A1 A3 A3 A2 | b A3 A2 B3 A1 A3 A3 A2 B3 | a B3 A1 A3 A3 A2 |
a B3 A1 A3 A3 A2 B3 | b A3 A2 A1 A3 A2 | b A3 A2 A1 A3 A3 A2 B3|
a A1 A3 A3 A2 | a A1 A3 A3 A2 B3 | b A3 A3 A2 | b A3 A3 A2 B3
Example 4: Convert the following grammar into GNF.
S → ABA | AB | BA | AA | A | B
A → aA | a
B → bB | b
Solution: In previous example we have already simplified this grammar as
S → ABA | AB | BA | AA | aA | a | bB | b
A → aA | a
B → bB | b
Substitute A and B-productions, we get
S → aABA | aBA | aAB | aB | bBA | bA| aAA | aA | a | bB | b
A → aA | a
B → bB | b
3.34
Theoretical Computer Science Context-Free Grammars and Languages

3.8 REGULAR GRAMMAR [April 16, Oct. 18]


• Regular grammar is a formal grammar, in which every production is restricted to have one of
the following forms:
1. A → aB
2. A → a, where A and B are the non-terminals, a is a terminal symbol.
With ∈-productions permitted as special case L(G) contain ∈.
• It is called a regular grammar, because if the format of every production in CFG is restricted
to A → aB or A → a, then the grammar can specify only regular sets and hence there exists a
finite automata accepting L(G), if G is regular.
• Definition: A regular grammar is a formal grammar (N, Σ, P, S) where the rules of P are of
the following forms:
o A → a, where A is a non-terminal in N and a is terminal in Σ.
o A → ∈, where A is N and ∈ is the empty string and either.
o A → Ba, where A and B are in N and a is in Σ. In this case, the grammar is a left regular
grammar of the form.
o A → aB, where A and B are in N and a is in Σ. In this case, the grammar is a right
regular grammar.
Example:
S → aB | ∈
B → Ba | ∈

3.9 LEFT LINEAR AND RIGHT LINEAR GRAMMARS [April 16, 18]
• Regular grammar is classified as Left-linear grammar and Right-linear grammar.
o Left-linear grammar (Definition): If all productions of a CFG are of the form A →
Bw, or A → w or A → ∈ where A, B are variables and w is a string of terminals then we
say that grammar is a left-linear grammar.
o Right linear grammar (Definition): If all productions of a CFG are of the form A →
wB or A → w or A → ∈ where A, B and w are same as above then grammar is called a
right-linear grammar.
• A right and left linear grammar is called a regular grammar. A grammar is called linear
grammar in which at most one non-terminal can occur on the right side of any production
rule.
Example 1: The language 0 (10)* is generated by the right-linear grammar S → 0 A,
A → 10 A | ∈ and left-linear grammar S → S 10 | 0.
Example 2:
A → a, A → aB, A → ∈
where, A and B are non-terminals and a is terminal.
3.35
Theoretical Computer Science Context-Free Grammars and Languages

Example 3:
S → 00A | 11S
A → 0A | 1A | 0 | 1
where, S and A are non-terminals and 0 and 1 are terminals.
Left-linear grammar examples:
A → a, A → Ba, A → ∈
where,
A and B are non-terminals, a is terminal and ∈ is empty string.
S → A00 | S11
A → A0 | A1 | 0 | 1
where, S and A are non-terminals and 0 and 1 are terminals.

3.10 EQUIVALENCE OF REGULAR GRAMMAR AND FA


• The equivalence exists between regular grammar and finite automata in accepting languages.
3.10.1 Construction of Regular Grammar from the DFA
• If L is a regular set then L is generated by regular grammar.
• Proof: Let L = L(M) for DFA M = (Q, ∑, δ, q0, F). Let q0 ∉ F then L = L(G) for right linear
grammar. G = (Q, ∑, p, q0) where p is set of productions, is constructed as follows :
1. If δ (qi, a) = qj and qj∉ F then add production qi → a qj in p.
2. If δ (qi, a) = qj and qj ∈ F then add productions qi → a qj | a in p.
*
So δ (qi, w) = qj iff qi ⇒ wqj.
3. Here we had assumed that q0 ∉ F.
If q0 ∈ F, then ∈ is accepted by L (M) then we add production S → q0 | ∈ in p.
Example 1: Construct regular grammar for DFA shown in the Fig. 3.8.

Fig. 3.8
3.36
Theoretical Computer Science Context-Free Grammars and Languages

Solution: δ (q0, a) = q1 and q 1∉ F gives q0 → a q1


δ (q0, b) = q3 and q3 ∉ F gives q0 → b q3
δ (q1, a) = q1 and q1 ∉ F gives q1 → a q1
δ (q1, b) = q2 and q2 ∈ F gives q1 → b q2 | b
δ (q2, a) = q2 and q2 ∈ F gives q2 → a q2 | a
δ (q2, b) = q2 and q2 ∈F gives q2 → b q2 | b
δ (q3, a) = q3 and q3 ∉ F gives q3 → a q3
δ (q3, b) = q4 and q4 ∈ F gives q3 → b q4 | b
δ (q4, a) = q4 and q4 ∈ F gives q4 → a q4 | a
δ (q4, b) = q3 and q3 ∉ F gives q4 → b q3
∴ Equivalent regular grammar is,
G = ({q0, q1, q2, q3, q4}, {a, b}, p, q0)
where p is {q0 → a q1 | b q3
q1 → a q1 | b q2 | b
q2 → a q2 | b q2 | a | b
q3 → a q3 | b q4 | b
q4 → a q4 | b q3 | a}
Example 2: Construct regular grammar for DFA shown in the Fig. 3.9.

Fig. 3.9
Solution: Equivalent regular grammar is
G = ({q0, q1, q2}, {a, b}, p, q0)
where p is {q0 → b q0 | a q1
q1 → b q1 | a q2 | a
q2 → a q1 | b q2 | b}
Example 3: Construct regular grammar for the DFA shown in the Fig. 3.10.

Fig. 3.10
3.37
Theoretical Computer Science Context-Free Grammars and Languages

Solution: Equivalent regular grammar is


G = ({S, q0, q1, q2, q3}, {a, b, c}, P, S)
where P is {S → q0 | ∈
q0 → a q1 | b q1 | c q2
q1 → a q1 | b q1 | c q2
q2 → a q3 | b q1 | c q2 | a
q3 → a q3 | b q3 | c q3 | a | b | c}

3.10.2 Construction of FA from the given Right Linear Grammar


• Let G = (V, T, P, S) be a regular grammar. We will construct a FA (NFA) with ∈-transitions
M = (Q, T, δ, [S], {[∈]}), where
1. Q consists of the symbol [α] such that α is a variable or a suffix of some RHS of a
production in 'P' and
2. 'δ' is defined as,
(i) If A is variable then δ ([A], ∈) = {[α] | A → α is a production}
(ii) If a ∈T and α in (T* ∪ T* V) then
δ ([aα], a) = {[α]}

Example 1: Construct FA for following regular grammar:


S → aA | bB
A → aS | a
B → bS | b
Solution: First we start with [S]. As there are two productions from S we get,
δ ([S], ∈) = {[aA], [bB]}
δ ([aA], a) = {[A]} and
δ ([bB], b) = {[B]}
δ ([A], ∈) = {[aS], [a]}
δ ([B], ∈) = {[bS], [b]}
δ ([aS], a) = {[S]}
δ ([a], a) = {[∈]}
δ ([bS], b) = {[S]}
δ ([b], b) = {[∈]}
3.38
Theoretical Computer Science Context-Free Grammars and Languages

Thus FA is shown in the Fig. 3.11.

Fig. 3.11

Example 2: Construct FA for following regular grammar:


S → 0A | 1B
A → 0C | 1A | 0
B → 1B | 1A | 1
C → 0 | 0A
Solution: FA is shown in the Fig. 3.12.

Fig. 3.12

Example 3: Construct NFA for following regular grammar:


S → bB
b → bC | aB | b
C → a
3.39
Theoretical Computer Science Context-Free Grammars and Languages

Solution: Equivalent NFA with ∈-transition is shown in the Fig. 3.13.

Fig. 3.13

EXAMPLES
Example 1: Construct regular grammar for a language over {a, b} consisting of all strings
starting with a and having ba as a substring in it.

Fig. 3.14
Solution:
(i) δ (q0, a) = q1 q1 ∉ F q0 → aq1
δ (q0, b) = φ
(ii) δ (q1, a) = q1 q1 ∉ F q1 → aq1
δ (q1, b) = q2 q2 ∉ F q1 → bq2
(iii) δ (q2, a) = q3 q3 ∈ F q2 → aq3 | a
δ (q2, b) = q2 q2 ∉ F q2 → bq2
(iv) δ (q3, a) = q3 q3 ∈ F q3 → aq3 | a
δ (q3, b) = q3 q3 ∈ F q3 → bq3 | b
∴ G = ({q0, q1, q2, q3}, {a, b}, p, q0}
P is:
q0 → aq1
q1 → aq1 | bq2
q2 → aq3 | bq2 | a
q3 → aq3 | bq3 | a | b
3.40
Theoretical Computer Science Context-Free Grammars and Languages

Example 2: Construct regular grammar for a language over {0, 1}. That starts with 00 and
ends with 1 having a substring 10 in it.

Fig. 3.15
Solution:
(i) δ (q0, 0) = q1 q1 ∉ F q0 → 0q1
δ (q0, 1) = φ
(ii) δ (q1, 0) = q2 q2 ∉ F q1 → 0q2
δ (q1, 1) = φ
(iii) δ (q2, 0) = q2 q2 ∉ F q2 → 0q2
δ (q2, 1) = q3 q3 ∉ F q2 → 1q3
(iv) δ (q3, 0) = q4 q4 ∉ F q3 → 0q4
δ (q3, 1) = q3 q3 ∉ F q3 → 1q3
(v) δ (q4, 0) = q4 q4 ∉ F q4 → 0q4
δ (q4, 1) = q5 q5 ∈ F q4 → 1q5 | 1
(vi) δ (q5, 0) = q4 q4 ∉ F q5 → 0q4
δ (q5, 1) = q5 q5 ∈ F q5 → q5 | 1
∴ G = ({q0, q1, q2, q3, q4, q5}, {0, 1}, P, q0}
∴ P is:
q0 → 0q1
q1 → 0q2
q2 → 0q2 | 1q3
q3 → 0q4 | 1q3
q4 → 0q4 | 1q5 | 1
q5 → 0q4 | 1q5 | 1
Example 3: Construct regular grammar for L = L1 ∩L2.
L1 = All strings over {a, b, c} having equal no. of a's and c's
L2 = {an b cn | n ≥ 0 and n ≤ 5}
3.41
Theoretical Computer Science Context-Free Grammars and Languages

Solution:

Fig. 3.16
(i) δ (q0, a) = q1 q1 ∉ F q0 → aq1
δ (q0, b) = φ
δ (q0, c) = φ
(ii) δ (q1, a) = q4 q4 ∉ F q1 → aq4
δ (q1, b) = q2 q2 ∉ F q1 → bq2
δ (q1, c) = φ
(iii) δ (q2, a) = φ
δ (q2, b) = φ
δ (q2, c) = q3 q3 ∈ F q2 → cq3 | c
(iv) δ (q3, a) = φ
δ (q3, b) = φ
δ (q3, c) = φ
(v) δ (q4, a) = q7 q7 ∉ F q4 → aq7
δ (q4, b) = q5 q5 ∉ F q4 → bq5
δ (q4, c) = φ
(vi) δ (q5, a) = φ
δ (q5, b) = φ
δ (q5, c) = q6 q6 ∉ F q5 → cq6
3.42
Theoretical Computer Science Context-Free Grammars and Languages

(vii) δ (q6, a) = φ
δ (q6, b) = φ
δ (q6, c) = q3 q3 ∈ F q6 → cq3
(viii) δ (q7, a) = q10 q10 ∉ F q7 → aq10
δ (q7, b) = q8 q8 ∉ F q7 → bq8
δ (q7, c) = φ
(ix) δ (q8, a) = φ
δ (q8, b) = φ
δ (q8, c) = q9 q9 ∉ F q8 → cq9
(x) δ (q9, a) = φ
δ (q9, b) = φ
δ (q9, c) = q6 q6 ∉ F q9 → cq6
Similarly, we get,
q10 → aq13 | bq11
q11 → cq12
q12 → cq9
q13 → bq14
q14 → cq15
q15 → cq12
∴ G = ({q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15}, {a, b, c}, P, q0}
P is:
q0 → aq1
q1 → aq4 | bq2
q2 → cq3 | c
q4 → aq7 | bq5
q5 → cq6
q6 → cq3
q7 → aq10 | bq8
q8 → cq9
q9 → cq6
q10 → aq13 | bq11
3.43
Theoretical Computer Science Context-Free Grammars and Languages

q11 → cq12
q12 → cq9
q13 → bq14
q14 → cq15
q15 → cq12
Example 4: Construct a regular grammar for a language over {a, b, c} starting with a and
having odd no. of b's.

Fig. 3.17
(i) δ (q0, a) = q1 q1 ∉ F q0 → aq1
δ (q0, b) = φ
δ (q0, c) = φ
(ii) δ (q1, a) = q1 q1 ∉ F q1 → aq1
δ (q1, b) = q2 q2 ∈ F q1 → bq2 | b
δ (q1, c) = q1 q1 ∉ F q1 → cq1
(iii) δ (q2, a) = q2 q2 ∉ F q2 → aq2 | a
δ (q2, b) = q1 q1 ∉ F q2 → bq1
δ (q2, c) = q2 q2 ∈ F q2 → cq2 | c
The grammar is: G = ({q0, q1, q2}, {a, b, c}, P, q0}
P is:
q0 → aq1
q1 → aq1 | bq2 | cq1 | b
q2 → aq2 | bq2 | cq2 | a | c
Example 5: Construct regular grammar for the language {a2n | n ≥ 1}.
Solution: Explanation.
When n = 1
a2n → a2 × 1 = a2 = aa
When n = 2
a2 × 2 = a4 = a4 = aaaa
3.44
Theoretical Computer Science Context-Free Grammars and Languages

When n = 3
a2 × 3 = a6 = a6 = aaaaaa
∴ We draw DFA for which accepts only even number of a'S.

Fig. 3.18
∴ Regular grammar is
q0 → aq1
q1 → aq2 | a
q2 → aq1

EXAMPLES
Example 1: Define nullable symbol.
Solution: If N is any non-terminal in CFG and N →* ∈ or N ∈, then N is nullable.
Example 2: What is the yield of following derivation tree?

Fig. 3.19
Solution: The yield of derivation tree is abba.
Example 3: Convert the following grammar into GNF.
S → AB
A → BS | b
B → SA | a
Solution: Substituting S-productions in B, we get,
B → ABA | a
Substitute A-productions in B.
B → BSBA | bBA | a
Grammer becomes
S → AB
A → BS | b
B → BSBA | bBA | a
3.45
Theoretical Computer Science Context-Free Grammars and Languages

Apply lemma (2) for B-productions


B → BSBA | bBA | a
α β1 β2
we get B → bBAB' | aB' | bBA | a
B' → SBAB' | SBA
Now all B-productions are in GNF. Substitute all B-productions in A-production.
A → BS | b
∴ A → bBAB's | aB's | bBAS | aS | b
Now substitute all A-productions in S.
S → AB
S → bBAB' SB | aB' SB | bBASB | aSB | bB
Now substitute all S-productions in B'.
B' → bBAB' SBBAB'| aB' SBBAB' | bBASBBAB'| aSBBAB'| bBBAB'
B' → bBAB'SBBA | aB'SBBA | bBASBBA | aSBBA | bBBA
∴ Equivalent grammar in GNF is
S → bBAB'SB | aB'SB | bBASB | aSB | bB
A → bBAB'S | aB'S | bBAS | aS | b
B → bBAB' | aB' | bBA | a
B' → bBAB'SBBAB' | aB'SBAB'| bBASBAB'| aSBAB'|
|bBAB'| bBAB'SBBA| aB'SBBA | bBASBBA | aSBBA | bBBA
Example 4: Convert following into CNF.
S → aAbB | BbS
B → aAbA | aAB | b
A → aB | aBb | a
Solution:
1. S → Ca A Cb B 2. S → BCb S
S → XY S → PS
X → Ca A P → BCb
Y → Cb B
Ca → a
Cb → b
3.46
Theoretical Computer Science Context-Free Grammars and Languages

3. B → aA bA 4. B → aAB
B → Ca A Cb A B → Ca A B
B → XQ B → XB
X → Ca A (already added)
Q → Cb A
5. A → CaB 6. A → Ca B Cb
A → RCb
R → CaB
∴ Equivalent grammar in CNF is
S → XY | PS
X → Ca A
Y → Cb B
P → BCb
B → XQ | XB | b
Q → Cb A
A → Ca B | RCb | a
R → Ca B
Ca → a
Cb → b
Example 5: Define useless symbols.
Solution: Let G = (V, T, P, S) be a grammar. A symbol X is useful if there is a derivation.
S ⇒ αXβ ⇒ w, where α, β ∈ (VUT)*
Otherwise X is useless.
Example 6: What is the yield of following derivation tree?

Fig. 3.20
Solution: The yield is aaba.
3.47
Theoretical Computer Science Context-Free Grammars and Languages

Example 7: Construct CFG for a language over {a, b} which accepts equal number of a's and
b's.
Solution: G = (V, T, P, S)
where V = {S} T = {a, b}
P is S → aB | bA
A → a | aS | bAA
B → b | bS | aBB
Example 8: Construct the following grammar into GNF.
S → AB
A → SB | a
B → AB | b
Solution: Replace all S-productions in A-productions.
We get S → AB
A → ABB | a By lemma 1
B → AB | b
Now apply lemma 2 for A-productions.
[A → Aα | β , then A → Bi | Bi A' and A' → αi | αi A']
A → ABB | a
αβ
A → aA' | a
A' → BBA' | BB
Now all A-productions are in GNF, replace all A-productions in S-production, we get
S → aA'B | aB
A → aA' | a
A' → BBA' | BB
B → AB | b
Now replace all A-productions in B-productions
S → aA'B | aB
A → aA'| a
A' → BBA'| BB
B → aA'B | aB | b
Now replace all B-productions in A'-productions we get
S → aA'B | aB
A → aA' | a
A' → aA'BBA' | aA'BB | aBBA' | aBB | bBA' | bB
B → aA'B | aB | b
The grammar is in GNF with total productions = 13.
3.48
Theoretical Computer Science Context-Free Grammars and Languages

Example 9: Convert the following into CNF.


S → aSa | bSb
S → a | b | aa | bb
Solution: CNF means the productions are in the form
A → BC or A → a where A, B, C ∈ V
S → ASA | BSB
S → a | b | AA | BB Replacing a by A and b by B
A → a
B → b
Then convert S → ASA and S → BSB in CNF by introducing new variable C and D.
S → AC | BD | a | b | AA | BB
C → SA
D → SB
A → a
B → b
This is an equivalent CNF.
Example 10: Define unit production with an example.
Solution: A production of the form A → B, where A, B ∈ V.
Example 11: Construct CFG which accepts set of palindromes over {0, 1}.
Solution: S → 0S0 | 1S1 | 0 | 1 | ∈
Example 12: Convert following grammar into CNF.
S → AaB | a
A → SBb | bA
B → Ba | b
Solution: S → ACa B | a
A → SBCb | Cb A
B → BCa | b
Convert S → ACa B, we get S → AD1
D1 → Ca B
Convert A → SBCb, we get A → SD2
D2 → BCb
3.49
Theoretical Computer Science Context-Free Grammars and Languages

∴ Grammar in CNF is,


S → AD1 | a
A → SD2 | Cb A
B → BCa | b
D1 → Ca B
D2 → BCb
Ca → a
Cb → b
Example 13: Rewrite the grammar after removing ∈-productions.
S → aS | AB
A → a|∈
B → b|∈
D → b
Solution: After removing ∈-productions, we get,
S → aS | AB
A → a
B → b
D → b
A and B are nullable symbols.
S → aS | AB | A | B | a
A → a
B → b
Symbol D is useless.
Example 14: Every CFL is regular language. State true or false.
Solution: False. CFL are regular or non-regular.
Example 15: Define ambiguous grammar.
Solution: CFG is ambiguous if for atleast one word in the language that generates two
possible derivations that corresponds to different parse trees or syntax trees.
Example 16: What is yield of tree given below:

Fig. 3.21
Solution: bbaaba is the yield of tree.
3.50
Theoretical Computer Science Context-Free Grammars and Languages

Example 17: Convert the following grammar into GNF.


S → ABC
A → a|b
B → Bb | aa
C → aC | cC | ba
Solution: First convert the grammar into CNF.
S → AD
D → BC
A → a|b
B → BCb | aCa
C → Ca C | Cc C | Cb Ca
Cb → b and Ca → a
Replace A-productions in S, we get
S → aD | bD
A → a|b
B → BCb | aCa Apply lemma 2
αβ
B → aCa | aCa B'
B' → Cb | Cb X
Replace Cb by b and Ca by a, we get
S → aD | bD
A → a|b
B → aCa | aCa B'
B' → b | bX
C → aC | cC | bCa
Now consider D productions, replace B-productions in it we get,
D → aCaC | aCaB'C
∴ The equivalent GNF grammar is
S → aD | bD
A → a|b
B → aCa | aCaB'
B' → b | bX
C → aC | cC | bCa
D → aCaC | aCaB'C
3.51
Theoretical Computer Science Context-Free Grammars and Languages

Example 18: Remove unit productions from the following grammar.


S → A | bb
A → B|b
B → S|a
Solution: S → A | bb
A → B|b
B → A | bb | a Removing unit production from B
After, remove unit production in A by replacing B-production.
A → A | bb | a | b
A → bb | a | b (Since A → A, A is useless)
Substitute A-production in S, we get the grammar
S → bb | a | b
Example 19: State any one lemma used in the procedure for constructing GNF.
Solution: A → αβγ
B → β 1 | β 2 | β 3 ...... | β m
then A → αβ 1γ | αβ 2 γ | ...... | αβ m γ
Example 20: Define right linear grammar.
Solution: If all productions of CFG are of the form A → ωB or A → ω or A → ∈, where A,
B are variables and ω is string of terminal, then grammar is called right linear grammar.
Example 21: Construct CFG for L = {0x 1y 0z | y > x + z}
Solution: G = (V, T, P, S) V = {S, A, B, C, D} T = {0, 1}
where P is { S → 0A00 | 00B0 | 0C0
A → 1A11 | 11D1
D → 1 | D1
B → 1B11 | 11D1
C → 1C1 | 1D1
}
S is start symbol.
Example 22: Convert the following grammar into GNF.
S → aAS | a
A → SbA | SS | bA
Solution: Substitute S-production in A-productions, we get
S → aAS | a
A → aAsbA | aASS | aSbA | aS | bA
3.52
Theoretical Computer Science Context-Free Grammars and Languages

Substitute b by B, we get
S → aAS | a
A → aASBA | aASS | aSBA | aS | bA
B → b
The grammar in GNF.
Example 23: Remove unit production from the following grammar.
S → A | bb
A → B|b
B → S|a
Solution: S → A | bb
A → B|b
B → A | bb | a
Remove unit production from B-production.
S → A | bb
A → A | bb | a | b (A is useless in A → A)
Substitute A-production in S.
S → A | bb
A → bb | a | b
After substituting we get
S → bb | a | b
Example 24: What are the types of grammar in the Chomsky hierarchy?
Solution: Type 0(unrestricted)
Type 1 (context-sensitive)
Type 2 (context-free)
Type 3 (regular)
Example 25:.Define inherently ambiguous context free languages.
Solution: If small some word of a language has more than one leftmost derivation or more
than one rightmost derivation then it is ambiguous. A CFL for which every CFG is ambiguous is
said to be an inherently ambiguous CFL.
Example 26: Construct CFG for each of the following languages:
(i) ab* a , (ii) 0*1*2*, (iii) {an bn cm | n ≥ 1, m ≥ 0}.
Solution: (i) ab*a: Grammar is
S → aBa
B → bB | ∈
3.53
Theoretical Computer Science Context-Free Grammars and Languages

(ii) 0*1*2*: Grammar is


S → ABC
A → 0A | ∈
B → 1B | ∈
C → 2C | ∈
(iii) {an bn cm | n ≥ 1, m ≥ 0}
CFG is S → AS | Sc
A → ab | aAb
Example 27: State lemma 2 for converting a CFG to GNF.
Solution: If A → Aα1 | Aα2 ...... | Aαi| β 1 β 2 ...... | β i
then, A → β 1 | β 2 | ...... | β i| β 1β| β 2 β ...... | β i β
B → α1 | α2 ...... | αi| α1β|α2 β| ...... |αi β
Example 28: Construct CFG for the following sets.
(i) {an bn ci | n ≥ 1, i ≥ 0}
(ii) {aj bn cn | n ≥ 1, j > 0}
(iii) language in which every string has triple 1's.
Solution: (i) S → AS | Sc
A → ab | aAb
(ii) S → Sa | SA
A → bc | bAc
(iii) S → A111B
A → aA | ∈
B → bB | ∈
Example 29: Define context-sensitive grammar.
Solution: For each production α → β length of β is atleast as much as the α except S → ∈.
Example, A → AB
AB → AC
C → ab
Example 30: Construct the CFG accepting the sets
(i) L = {0n 1m | n, m ≥ 0}
(ii) L = L1 ∪ L2, where L1 = {an b | n ≥ 0} and L2 = All strings not having 01 as a substring
over {0, 1}.
3.54
Theoretical Computer Science Context-Free Grammars and Languages

Solution: (i) S → 0S1 | 01 | 0A1


A → 0A | 0
(ii) L1 is S1 → Ab
A → aA | ∈
L2 is S2 → 0X | 1S2 | 0 | 1
X → 0X | 0
For L1 ∪ L2 the CFG is
S → S1 | S2
S1 → Ab
A → aA | ∈
S2 → 0X | 1S2 | 0 | 1
X → 0X | 0
Example 31: Write the steps for eliminating ∈-production in CFG.
Solution: (i) Eliminate ∈-productions.
(ii) Find nullable non-terminals N where N ⇒ ∈ or N → ∈
(iii) Replace nullable non-terminals as ∈ in RHS for all productions where it is present. Find
all subsets by replacing ∈.
Example 32: Class of CFG and PDA is same. Justify true or false.
Solution: True. PDA is the acceptor of CFG.
Example 33: What is yield from the following Parse tree?

Fig. 3.22
Solution: abba.
Example 34: Construct CFG for language L = {an bn cm dm | m, n ≥ 1}.
Solution: S → AB
A → aAb | ab


B → cBd | cd
S ⇒ AB

(i)
⇒ abcd
(ii) S ⇒ AB 
⇒aAbcBd
⇒aabbccdd
3.55
Theoretical Computer Science Context-Free Grammars and Languages

Example 35: Construct CFG for language L = {ai bj ck | i = j or j ≤ k}


Solution: S → AC
A → aAb | ab | E
C → cC | c
Example 36: Convert grammar into GNF.
S → a | AA | BA
A → a | AB | b
B → a
Solution: A → a | AB | b
A → AB | a | b
αβ
∴ A → aA' | bA'
A' → BA' | B
Now substitute A and B productions in S we get
S → a | aA'A | bA'A | aA
A → aA' | bA'
A' → aA' | a
B → a
Now all productions in GNF form.
Example 37: Convert the grammar into CNF.
S → A|B|C
A → aAa | B
B → bB | bb
C → aCaa | D
D → baD | abD | aa
Solution: S → aAa | B | bB | bb | aCaa | D
S → A'AA' | B'B | B'B | BB | A'CA'A' | D
A' → a
B' → b
S → A'D
S → B'B
S → BD
S → B'B | BB
3.56
Theoretical Computer Science Context-Free Grammars and Languages

S → D2D3
D → AA'
D2 → A'C
D3 → A'A'
D → B'A'D | A'B'D | A'A'
D → B'D4 | A'D5 | A'A'
D4 → A'D
D5 → B'D
Example 38: Construct CFG for L = L1 ∪ L2 where L1 = {anb | n ≥ 1} and L2 = {0n | nn n ≥
1}
Solution: L1 ∪ L2 = all strings of L1 and L2
CFG of L1 is S1 → Ab
A → aA | b
CFG of L2 is S2 → AB
A → OA | O
B → |B|1
CFG of L1 ∪ L2 is S → S1 | S2
Example 39: Rewrite the following CFG after eliminating unit productions.
S → aAb | A
A → B|b
B → ∈
D → F
F → 01 | B
Solution: S → aAb | b | ∈
A → b|∈
D → 01 | ∈
Example 40: What is the field of following derivation tree.
S

a A b

A b b

'
Fig. 3.23
Solution: abbb.
3.57
Theoretical Computer Science Context-Free Grammars and Languages

Example 41: Find nullable symbols of following CFG.


S → AB | aBb
A → aA | ∈
B → AD | aAb
D → bD | ∈
Solution: S, A, B, D all are nullable.
Example 42: Find CFG for the language, L = {anbmcn | m, n ≥ 1}.
Solution: S → aSc | aAc | abc
A → aAc | ∈
Example 43: If L1 = {ambm | m ≥ 1} and L2 = {bm | m ≥ 0}
Find L1 ∪ L2 and L1 ∩ L2.
Solution: {∈, ab, b, bb, aabb, bbb, aaabbb …}
Example 44: Define useless symbol.
Solution: Useless symbol: If S ⇒ α X β ⇒ α ω β, X is useful if it is derivable from S and it
derives the sentence (input string) otherwise X is useless.
Example 45: Find CFG for the language represent by the following regular expression:
(0 + 1)* 01 (0 + 1)*
Solution: S → ABA
A → 0A | 1A | ∈
B → 01
Example 46: Construct PDA for a language: L = {W ⊂ WR | W ∈ (0 + 1)*}.
Solution: δ (q1, 0, R) = (q, BR)
δ (q1, 0, B) = (q1, BB)
δ (q1, 0, G) = (q1, BG)
δ (q, 1, G) = (q1, GG)
δ (q1, 1, B) = (q1, GB)
δ (q1, C, R) = (q2, R)
δ (q1, C, B) = (q2, B)
δ (q1, C, G) = (q2, G)
δ (q2, 0, B) = (q2, ∈)
δ (q2, 1, G) = (q2, ∈)
δ (q2, ∈, R) = (q2, ∈)
3.58
Theoretical Computer Science Context-Free Grammars and Languages

Example 47: Construct regular grammar for the following DFA.


a
a, b
Start a b
q0 q1 q2

b
b
a a
q3 q4
b
Fig. 3.24
Solution: q0 → aq1 | bq3
q1 → aq1 | bq2 | b
q2 → aq2 | bq2 | a | b
q3 → aq3 | bq4 | b
q4 → aq4 | bq3 | a
Example 48: Define left linear and right linear grammar.
Solution: Left linear → A → Bw → w or A → w or A ∈.
Right linear → A → wB or A → w or A → ∈

Example 49: Determine whether the following language is regular? Justify: L = L1 ∩ L2


where:
L1 = {an bm | n ≥ m and n > 0}
L2 = {bm an | m > 0, n > 1}
Solution: L = L1 ∩ L2 = φ It is regular.

q0 q1

Example 50: Construct CFG for L = L1L2


where L1 = {anb | n > = 1} and
L2 = {an bn+2 | n > = 0}
Solution: S1 → aAb | ab and S2 → aAbbb | bb
A → aA | ∈ and A → aAb | ∈
L = L1 L2
∴ S → S1S2
3.59
Theoretical Computer Science Context-Free Grammars and Languages

PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which grammars define the context free languages?
(a) Context Free Grammars (CFG)
(b) Regular Grammars (RG)
(c) Context Sensitive Grammars (CSG)
(d) Unrestricted Grammars (URG)
2. The language generated by the CFG is called as, context-free language (CFL).
(a) Context-Automata Language (CAL)
(b) Context-Free Language (CFL)
(c) Context-Regular Language (CRL)
(d) None of the mentioned
3. The productions (rules of a grammar) are applied iteratively to generate a string or
language this process is called as,
(a) language (b) Alphabet
(c) derivation (d) expression
4. Which in grammar contains less number of non-terminals and productions, so the time
complexity for the language generating process becomes less from the reduced
grammar?
(a) derivation (b) expression
(c) automata (d) reduction
5. Which grammars generate the regular languages? Such a grammar restricts its rules to
a single non-terminal on the left hand side. The right hand side consists of either a
single terminal or a string of terminals with a single non-terminal on the left or the
right end.?
(a) Context Free Grammars (CFG)
(b) Regular Grammars (RG)
(c) Context Sensitive Grammars (CSG)
(d) Unrestricted Grammars (URG)
6. Which is the tree representation of deriving a CFL from a given context grammar?
(a) parse tree (b) derivation tree
(c) Both (a) and (b) (d) None of the mentioned
7. If we replace only the rightmost non-terminal by some production rule at each step of
the generating process of the language, then the derivation is called as a,
(a) leftmost derivation (b) rightmost derivation
(c) linear derivation (d) None of the mentioned
3.60
Theoretical Computer Science Context-Free Grammars and Languages

8. A grammar is said to be _____ if any Non-terminal generates a single Non-terminal on


the RHS.
(a) linear (b) non-linear
(c) right-linear (d) left-linear
9. The _____ symbols are those which are the constituents of the sentence generated
using a grammar.
(a) non-terminal (b) terminal
(c) linear-terminal (d) None of the mentioned
10. A language is context free if it can be generated by,
(a) CFG (b) FA
(c) RE (d) RL
11. A grammar is said to be in Greibach Normal Form (GNF) if every production of the
grammar is of the form,
(a) Non-terminal single terminal)(string of non-terminals)
(b) Non-terminal single terminal (in one line, it can be said that all the productions
will be in the form)
(c) Non-terminal (single terminal)(non-terminal)
(d) All of the mentioned
12. Useless symbols in CFG are,
(a) Non-terminal symbols (b) Null alphabets and null string
(c) Both (a) and (b) (d) Non-generating and non-reachable symbols
13. Parsing a string from a given grammar means,
(a) Finding a derivation tree (b) Finding a leftmost derivation
(c) Finding a derivation (d) Finding a rightmost derivation
14. Simplification of CFG includes,
(a) Removal of useless symbols (b) Removal of unit productions
(c) Removal of null productions (d) All of the mentioned
15. A grammar is called ambiguous if,
(a) It generates more than one parse tree for a given string
(b) It generates both leftmost and rightmost derivation for a given string
(c) It generates more than one string
(d) All of the mentioned
16. The intersection of CFL and regular set is,
(a) regular (b) context free
(c) non-context free (d) None of the mentioned
Answers
1. (a) 2. (b) 3. (c) 4. (d) 5. (b) 6. (c) 7. (b) 8. (a) 9. (b) 10. (a)
11. (d) 12. (d) 13. (c) 14. (d) 15. (a) 16. (b)
3.61
Theoretical Computer Science Context-Free Grammars and Languages

Q.II Fill in the Blanks:


1. A regular (or finite) ______ describes a regular language and is equivalent to a finite
automaton.
2. In a ______ grammar, all production rules have a single non-terminal on the left-hand
side, and either a single terminal or a combination of a single non-terminal and a single
terminal on the right-hand side.
3. Regular grammar is a ______ of Context-free grammar (CFG).
4. A ______ is called a leftmost derivation if we replace only the leftmost non-terminal
by some production rule at each step of the generating process of the language from the
grammar.
5. A grammar is said to be ______ if the productions contain a single one non-terminal on
LHS of production which occur at the Left most of the string.
6. A ______ defines a formal language i.e. a set of all sentences that can be derived by
the grammar.
7. A grammar of a language is called ______ if any of the cases for generating a
particular string, more than one parse tree can be generated.
8. A CFG is said to be in Chomsky Normal Form (CNF) if all the ______ of the grammar
are in the following form:
o Non-terminal String of exactly two non-terminals
o Non-terminal Single terminal
9. A grammar is said to be in ______ when every production of the grammar has some
specific form.
10. The ______ for CFL is used to prove that certain sets are not context free.
11. A grammar is called ______ grammar if it is context free, and the RHS of all
productions have at most one non-terminal.
12. A grammar is said to be '______ if the productions contain a single non-terminal on
R.H.S. of production which occur at the Right most of the string.
13. A parse tree (sometimes called as derivation trees) is the tree representation of deriving
a CFL from a given ______ grammar.
14. ______ a string is finding a derivation for that string from a given grammar.
15. A ______ generated by a Context-Free Grammar (CFG) is called as Contect-Free
Language or CFL.
Answers
1. grammar 2. regular 3. subset 4. derivation
5. left-linear 6. CFG 7. ambiguous 8. productions
9. normal form 10. Pumping Lemma 11. linear 12. right-linear
13. context 14. Parsing 15. language
3.62
Theoretical Computer Science Context-Free Grammars and Languages

Q.III State True or False:


1. Converting any context-free grammar to Chomsky normal form will ensure that it is
unambiguous.
2. Non-context free languages are always defined by regular expression.
3. A regular grammar cannot describe a context free language.
4. Regular Languages are always Context-Free Languages (CFL).
5. A linear grammar can be converted to regular grammar.
6. Any FA can be converted into regular grammar.
7. A regular grammar is the least powerful type of grammar in the Noam Chomsky
hierarchy (a context free grammar describes a context free language, a context sensitive
grammar describes a context sensitive language, and an unrestricted grammar describes a
recursively enumerable language).
8. All context free languages are infinite set.
9. In a CFG, when the productions in a grammar satisfy certain restrictions, then that
grammar is said to be in its normal form.
10. A linear grammar is called left linear if the RHS non-terminal in each productions are at
the left end.
11. A linear grammar is called right linear if the RHS non-terminal in each productions are at
the right end.
12. String consists of only non-terminal symbols.
13. A regular grammar is a subset of CFG thus, for every regular language, there exists a
CFG.
14. A context-free grammar with more than one parse tree for some expression is called
ambiguous.
15. Context Free Languages are always Regular Languages.
16. Pumping Lemma is used to show that language is not context free.
17. Different types of derivation can be generated for deriving a particular string from a
given grammar and for each of the derivations, a parse tree is generated.
18. In simplification of CFG unnecessary productions of a grammar should be eliminated.
19. The derivations in a CFG can be represented using trees called as derivation trees.
20. A parse tree is an ordered tree in which the LHS of a production represents a parent node
and the RHS of a production represents a children node.
Answers
1. (T) 2. (F) 3. (T) 4. (T) 5. (T) 6. (T) 7. (T) 8. (F) 9. (T) 10. (T)
11. (T) 12. (F) 13. (T) 14. (T) 15. (F) 16. (T) 17. (T) 18. (T) 19. (T) 20. (T)
3.63
Theoretical Computer Science Context-Free Grammars and Languages

Q.IV Answer the following Questions:


(A) Short Answer Questions:
1. Define grammar.
2. Define CFG.
3. What is CPL.
4. Define regular language.
5. Define normal form.
6. Define linear grammar.
7. Define RG.
8. Compare CNF and GNF (any two points).
9. Define derivation.
10. Define ambiguous grammar.
11. Define left and right linear grammar.
12. What is meant by RMD and LMD?
(B) Long Answer Questions
1. What is grammar? Explain with example.
2. With the help of example describe derivation.
3. What is reduction? Define it. Also explain with example.
4. Construct the CFG accepting each of the following sets:
(i) {an bm am bn | m, n ≥ 1}
(ii) {an b2n | n ≥ 1}
(iii) L = L1 ∪ L2
L1 = {0m 1n 2m–n | m, n ≥ 1}
L2 = {0i 22i – 2 | i ≥ 0}
(iv) Set of balanced parenthesis over {(, ), [, ], {, }}.
(v) The set of all strings over {a, b} consisting of equal number of a's and b's.
(vi) Set of all strings over {a, b} starting with "a" and ending with "bb".
(vii) L = L1 ∪ L2 where
L1: {an bm | n ≥ m, n > 0}
L2: all strings not having "ab" as substring over {0, 1}.
5. Consider the following grammar:
S → aB | bA
A → aS | bAA | a
B → bS | aBB | b
3.64
Theoretical Computer Science Context-Free Grammars and Languages

For the string "aaabbabbba", find


(i) Leftmost derivation
(ii) Rightmost derivation
(iii) Parse tree.
6. Show that the following CFGs are ambiguous:
(i) S → a | abSb | aAb
A → bS | aAAb
(ii) S → aB | ab
A → aAB | a
B → ABb | b
(iii) S → S0S0S | 1
(iv) S → S + S | S * S | (S) | a
7. Find CFGs with no useless symbols equivalent to CFG given below:
(i) S → AB | CA
A→a
B → BC | AB
C → aB | b
(ii) S → AB | BC
A → aAa | aAb
B → bB | b
D→dD|d
(iii) S → aAB | BC | aB
A → bA | aC
B → bBB | aS | b
C → CA | BC
8. Design equivalent CFG without unit productions for following CFGs:
(i) S → 0A | 1B
A→S
B → 1B | 1
(ii) S → 0A1 | 1B0
A → 0A | B10
B → 1A | S | 1
3.65
Theoretical Computer Science Context-Free Grammars and Languages

(iii) S → AB
A→B
B → 0B | 1 | ∈
9. Convert following CFG into CNF:
(i) S → aSd | aAd
A → bAc | bc
(ii) S → 01S1 | 0 | 0A1
A → 1S | 0AA1
(iii) S → abAB | bAda
A → baB | a
B → CAb | Bb
(iv) S → aAbB | BbS
B → aAbA | aAB | b
A → aB | aBb | a
(v) S → aAab | Aba
A → aS | bB
B → ASB | a
10. Convert following CFG into GNF:
(i) S → 0AB | A | B
A → S0B | 1B | 1
B → A1 | 0
(ii) S → AB
A → BSB | BB | b
B → aAb | a
(iii) S → aSd | aAd
A → bAc | bc
(iv) S → aA | b
A → SA | a
(v) S → AB
A → BS | b
B → SA | a
3.66
Theoretical Computer Science Context-Free Grammars and Languages

(vi) S → AaB | a
A → SBb | bA
B → Ba | b
(vii) S → 0A0 | 1B1 | BB
A→C
B→S|A
C→S|∈
(viii) S → AAA | B
A → aA | B
B→∈
11. Consider following grammar:
S → A *B | *A
A→#B|B#
B → *A | #
For the string "# * # # * #", find
(i) Leftmost derivation
(ii) Rightmost derivation
(iii) Parse tree.
12. Write a short note on: Simplification of CFG.
13. Describe Chomsky hierarchy with four types of grammars.
14. What is ambiguous grammar? Explain its concept with example.
15. Explain equivalence of FA and regular grammar with example.
16. How to construct of regular grammar equivalent to a given DFA?
17. What is normal form? Explain Greibach Normal Form (GNF) and Chomsky Normal
Form (CNF) with example. Also compare them.
18. Construct CFG accepting following sets:
(i) L = {0n 1m | n, m ≥ 0, n is not equal to m}
(ii) L = {am bn cm–n | m ≥ 1, m > n}
(iii) L = {am bn cm | n ≥ 1, m ≥ 0}
(iv) The set of all strings with exactly twice as many b's as a's.
(v) L = L1 ∪ L2 where
L1 = {an b | n ≥ 0}
L2 = {0m 1n 2n+1 | m, n ≥ 1}
3.67
Theoretical Computer Science Context-Free Grammars and Languages

19. Construct leftmost and rightmost derivations for ababa and draw parse tree.
S → AS | a
A → SA | b
20. Show that the grammar
S → a | abSb | aAb
A → bS | aAAb is ambiguous.
21. For the following grammar, find an equivalent grammar with no unit production:
S → AB
A→a
B→C|b
C→D
D→E
E→a
22. Eliminate ∈-productions from the following grammar:
S → AB | ∈
A → aASb | a
B → bS
23. Construct regular grammar for the following languages:
(i) Set of all strings over {a, b} such that if it starts with 'a' then it ends with "ab" and if
it starts with 'b' then it contains even number of a's in it.
(ii) (a + b)* aba (a + b)* bb
(iii) {a2n | n ≥ 1}
(iv) The set of all strings over {0, 1} beginning with "0".
(v) {al bm cn | l, m, n ≥ 1}
(vi) 01* ((01) (10))* + 1 (10)*.
(vii) L = L1 ∩ L2, where
L1 = All strings over {a, b, c} having equal number of a's and c's.
L2 = {an b cn | n ≥ 0 and n ≤ 5}
24. Construct regular grammar for DFA in Fig. 4.19 to 4.21
(i)

3.68
Theoretical Computer Science Context-Free Grammars and Languages

(ii)

(iii)

25. S → ABC | BaB


A → aA | BaC | aaa
B → bBb | a | D
C → CA | AC
D→∈
(i) Eliminate ∈-productions.
(ii) Eliminate any unit productions.
(iii) Eliminate any useless symbols.
(iv) Convert into CNF.
26. Show that following languages are not context free.
(a) {am bn ck | m < n < k}
(b) {an | n is a prime}
2
(c) {am bm | m ≥ 1}
(d) {an bn cm | n ≤ m ≤ 2n}
(e) {an bn an bn | n ≥ 1}
(f) {an bn cn dn | n ≥ 1}
27. Show that the CFL's are closed under the following properties:
(a) union,
(b) concatenation,
(c) closure.
3.69
Theoretical Computer Science Context-Free Grammars and Languages

28. Prove that CFL's are not closed under intersection.


29. Is the language L = {an b3n an | n ≥ 1} context free?
If so, find a CFG. If not, prove it.
30. L1 and L2 are two regular languages?
L1 – L2 = {x | x is in L but not in L2}
Is (L1 – L2) is regular language?

(Hint: L1 – L2 = L1 ∩ L2 )
31. L1 and L2 are two non-regular languages. Then their union is also non-regular, state true
or false and explain.
32. With the help of example describe concept of left linear and right linear grammar.
33. How to construction of a FA from the given right linear grammar? Explain with example.
34. What is reduction? Describe with example.
UNIVERSITY QUESTIONS AND ANSWERS
April 2016
1. Define left linear and right linear grammar. [1 M]
Ans. Refer to Section 3.9.
2. State the machines used for context free grammar and context-sensitive grammar. [1 M]
Ans. Refer to Section 3.2.
3. Construct CFG for the L = {ax by cx + y | x, y ≥ 1}. [1 M]
Ans. Refer to Section 3.4.
4. Define the terms:
(i) Ambiguous grammar (ii) Parse tree. [2 M]
Ans. Refer to Sections 3.5 and 3.2.1.

October 2016
1. State the machines used for CFL and CFG. [1 M]
Ans. Refer to Sections 3.1 and 3.2.
2. Define ambiguous grammar. [1 M]
Ans. Refer to Section 3.5.
3. Construct CFG for language L = {an bn cm dr | m, n, r ≥ 1}. [4 M]
Ans. Refer to Section .
4. Define type-2 grammar. [1 M]
Ans. Refer to Section 3.3, Point (3).
3.70
Theoretical Computer Science Context-Free Grammars and Languages

5. Convert the following grammar to GNF:


S → AA | a
A → SS | b [5 M]
Ans. The given CFG is in CNF
By replacing the production of A in S we get
S → SSA | bA | a
After removing the left recursion, we get
S → bA | a | bAS’ | as’
S → SA | SAS’
The productions of S are in GNF.
By replacing the productions of S in A we get
A → bAS | as | bAs’s | as's | b
Now the productions of A are in GNF
By replacing the productions of s in s’ we get
S’ → bAA | aA | bAs’A | as’A
S’ → bAAS’ | aAS’ | bAS’AS’| as’AS’.
So the grammar in GNF is G = ((S, AS’), (a, b), P, S)
Where P = S → bA | a | bAS’ | as’
= S’ → bAA | aA | bAs’A | as’A
= S’ → bAAS’ | aAs’ | bAS’AS’ | as’AS’
= A → bAS | as | bAS’S | as’S | b
April 2017
1. Write a language for CFG: S → aSa | bSb | a | b | ∈. [1 M]
Ans. Refer to Examples on Pages 3.14 to 3.15.
2. Construct CFG for language L = {anbmcmdn | n ≥ 1, m ≥ 1}. [3 M]
Ans. Refer to Examples on Pages 3.14 to 3.15.
3. Construct CFG for language L which accepts set of all palindromes over ∑ = {a, b}.
[2 M]
Ans. Refer to Examples on Pages 3.14 to 3.15.
4. Convert the following grammar to GNF:
S → AB | B
A → BS
B → A1 | 1 [5 M]
Ans. Refer to Section 3.7.2.
3.71
Theoretical Computer Science Context-Free Grammars and Languages

October 2017
1. Define ambiguous grammar. [1 M]
Ans. Refer to Section 3.5.
2. Consider the following grammar:
S → ADa
A→ a
D→ d
The grammar is in CNF. Justify. [1 M]
Ans. Refer to Section 3.7.1.
3. Convert the following grammar in GNF:
S → aAS | a
A → SbA SS bA [5 M]
Ans. Refer to Section 3.7.2.
4. Construct CFG for the language L = L1L2
where, L1 = {an b | n ≥ 0}
L2 = {bm c | m ≥ 0}. [4 M]
Ans. Refer to Examples on Pages 3.14 to 3.15.
5. Write a short note on Chomsky's hierarchy. [4 M]
Ans. Refer to Section 3.3.
6. Consider the following grammar:
S → AB | aD | a
A→ a
D → aD | aDD
Remove useless symbols and rewrite the grammar. [2 M]
Ans. Refer to Section 3.6.1.

April 2018
1. Define right linear grammar. [1 M]
Ans. Refer to Section 3.9.
2. State lemma 1 for converting a CFG to GNF. [1 M]
Ans. Refer to Examples on 3.31 to 3.33.
3. Construct the following CFG into Chomsky Normal Form (CNF):
S → aSa|bSb|a|b|aa|bb [5 M]
Ans. Refer to Section 3.7.1.
3.72
Theoretical Computer Science Context-Free Grammars and Languages

4. Construct CFG for the following: [5 M]


n n+2
(i) L = {a b | n > = 0}
(ii) A language containing string having at least one occurrence of 11 over {0, 1}.[5 M]
Ans. Refer to Examples on Pages 3.14 to 3.15.

October 2018
1. Define ambiguous grammar. [1 M]
Ans. Refer to Section 3.5.
2. What are the types of grammar in Chomsky hierarchy? [1 M]
Ans. Refer to Section 3.3.
3. Convert the following grammar in CNF: [5 M]
S → ABA
A → aA/∈ (epsilon)
B → bB/∈ (epsilon)
Ans. Refer to Section 3.7.1.
4. Convert the following grammar in GNF: [5 M]
S → AB | A
A → BS
B → AI | I
Ans. Refer to Section 3.7.2.
5. Explain types of regular grammar. [2 M]
Ans. Refer to Section 3.8.
6. Construct CFG for: [4 M]
n m
(a) {a b | n, m ≥ 0}
(b) {an bnci | n > i, i ≥ 0}
Ans. Refer to Examples on Pages 3.14 to 3.15.

April 2019
1. Define context sensitive grammar. [1 M]
Ans. Refer to Section 3.3, Point (2).
2. State lemma 2 for converting a CFG to GNF. [1 M]
Ans. Refer to Section 3.7 and Examples on 3.31 to 3.33.
3. Construct CFG for the following: [5 M]
(i) L1 = {0n 1n 2m | n > 1, m > 0}
(ii) L2 = {0n 1m | n, m > 0}
Ans. Refer to Examples on Pages 3.14 to 3.15.
3.73
Theoretical Computer Science Context-Free Grammars and Languages

4. Construct the following CFG into Chomsky Normal Form (CNF): [5 M]


S→ AaB|a
A → S B b | bA
B→ Ba|b
Ans. Refer to Section 3.7.1.
5. Define the term: Derivation tree. [1 M]
Ans. Refer to Section 3.2.1.

3.74
CHAPTER
4

Pushdown Automata
Objectives …
To study Basic Concepts in Pushdown Automata
To learn Construction of Pushdown Automata
To understand Deterministic and Non-deterministic Pushdown Automata

4.0 INTRODUCTION
• We have seen finite automata (abstract machine) with the following dual property:
1. For each regular language, there is at least one machine that runs successfully only on
the input string from that language.
2. For each machine in the class the set of words it accepts is a regular set.
• We are now considering a different class of languages but we want to answer the same
questions, so we would again like to find a machine formulation.
• We are looking for a mathematical model of some class of machines that corresponds to
CFLs i.e., there should be at least one machine that accepts each CFL and the language
accepted by each machine is context-free. We want CFL-acceptor just as FA's are regular
language recognizers and acceptors.
• To build these new machines, we start with our old FA's and add stack and input tape to
make it more powerful. This FA with stack (LIFO) is called as Pushdown Automata (PDA).
• A pushdown automaton is a way to implement a context-free grammar in a similar way we
design DFA for a regular grammar.
• The pushdown automaton is a finite automaton with an additional tape, which behaves like a
stack. The PushDown Automata (PDA) is the machine format of the context-free language.
• Input tape is infinitely long in one direction to hold any possible input. The tape has a first
location for the first letter of the input, then a second location and so on.
• The locations into which we put the input letters are called cells as shown in the Fig. 4.1.

Fig. 4.1
4.1
Theoretical Computer Science Pushdown Automata

• As we process this tape on the machine we read one letter at a time and eliminate each as it is
used.
• When we reach the first blank cell we stop, i.e. rest of tape is also blank. We read from left to
right and never go back to a cell that was read before.
• As a part of our new pictorial representation for FAs, let us introduce the symbols shown
below.

Fig. 4.2
• An accept state is a final state and reject state is not a final. Read state is shown by diamond
shaped boxes as shown below.

Fig. 4.3
(Note: Symbol ∆ stands for blank).
• The finite automata that accepts all words ending in the letter a is as shown in the Fig. 4.4.

Fig. 4.4
• The FA in the new symbolism is shown in the Fig. 4.5.

Fig. 4.5
4.2
Theoretical Computer Science Pushdown Automata

• Our machine is still an FA. Here we chosen this representation because we now want to add
additional component called pushdown stack (last-in-first-out) to our machine. The only
stack operations allowed to us are push and pop.
• Popping an empty stack, like reading an empty tape, gives us the blank character ∆. We
include the states as:

Fig. 4.6
• The edges coming out of a pop state are labeled in the same way as the edges from a read
state.
Basic Concept of Pushdown Automata:
• Pushdown Automata is a finite automaton with extra memory called stack which helps
Pushdown automata to recognize Context Free Languages.
• 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.
• Fig. 4.7 shows diagram for PDA. The components of it are described below:
1. The input tape contains the input symbols. The tape is divided into a number of squares.
Each square/block contains a single input character. The string placed in the input tape is
traversed from left to right. The two end sides of the input string contain an infi nite
number of blank symbols.
2. The reading head scans each square in the input tape and reads the input from the tape.
The head moves from left to right. The input scanned by the reading head is sent to the
finite control of the PDA.
3. The finite control can be considered as a control unit of a PDA. An automaton always
resides in a state. The reading head scans the input from the input tape and sends it to the
finite control.
4. A stack is a temporary storage of stack symbols. Every move of the PDA indicates one
of the following to the stack
o One stack symbol may be added to the stack (push)
o One stack symbol may be deleted from the top of the stack (pop)
The stack is the component of the PDA which differentiates it from the finite automata.
In the stack, there is always a symbol z0 which denotes the bottom of the stack.
4.3
Theoretical Computer Science Pushdown Automata

I/P Tape

Reading Head

Finite
Control Stack
z0

Fig. 4.7
Example: Consider the following PDA shown in the Fig. 4.8.

Fig. 4.8
Solution: The question is what language is represented by the above PDA? To find this, we
will analyze the PDA. Let us consider input string aabb. We assume that this string has been put
on the tape.
We must begin at start and then read the symbol 'a' from the input tape. Push a state tells us
to push an a onto the stack as shown in Fig. 4.9 (b). We now read another a and proceed as
before. See Fig. 4.9 (c).
After the second push a, we return back to same read state again. Here we read the letter b,
we take the b edge out of this state down to left pop. The state pop takes the top element off the
stack as shown in Fig. 4.9 (d). Now, next read symbol is b, it returns to pop state again and pop
the top element off the stack as shown in Fig. 4.9 (e), leaving the stack empty. Next read is
symbol ∆; which will be the accept state. We observe that the language of words accepted by this
machine is exactly {an bn, n = 0, 1, 2, …}.

(a) Initial configuration


4.4
Theoretical Computer Science Pushdown Automata

(b) After reading first 'a' (c) After reading second 'a'

(d) After reading first 'b' (e) After reading second 'b'
Fig. 4.9
This language is non-regular language, generated by CFG. We constructed machine (PDA)
which accepts this language. So PDAs are more powerful than FA.
FA could not keep the track of how many times 'n' occurs {an bn}. PDA has a primitive
memory unit. It can keep track of how many a's are read at the beginning.
Difference between PDA and FA: [April 16, 18, Oct. 16, 18]
1. The length of the path formed by a given input: If a string of seven letters is fed into
an FA, it follows a path exactly seven edges long. In a PDA, the path could be longer or
shorter.
For example, following PDA accepts only the language of all words beginning with an a.

Fig. 4.10
No matter how long the input string, the path is only one or two edges long.
2. PDA accepts regular or non-regular language. FA accepts regular language.
3. PDA is more powerful than FA because it has unlimited memory units (stack); so it can
remember arbitrarily long strings.
4. A DFA can remember a finite amount of information, but a PDA can remember an
infinite amount of information.

4.1 DEFINITION OF PUSHDOWN AUTOMATA [April 17, 19 Oct. 17]


• A pushdown automata M is a system (Q, Σ, Γ, δ, q0, Z0, f),
where,
Q is finite set of states.
Σ is an alphabet called the input alphabet.
Γ is an alphabet called stack alphabet.
4.5
Theoretical Computer Science Pushdown Automata

q0 is initial state and q0 ∈ Q.


Z0: Z0 ⊂ Γ is a particular stack symbol called start symbol.
F: set of final states F ∏ Q.
δ: is a mapping from Q × (Σ ∪ {∈}) × Γ to finite subsets of Q × Γ*.
Moves:
1. The interpretation of δ (q, a, z) = {(p1, γ1), (p2, γ2) … (pm, γm)}, where q and pi ∀ 1 ≤ i
≤ m are states, a is in Σ, Z is stack symbol and γi is in Γ* where 1 ≤ i ≤ m i.e. a PDA in
state q with input symbol a and Z is top of stack (TOS) ∀ i enter state pi, replace the
symbol Z by string γi and advance input head one symbol.
2. The interpretation of δ (q, ∈, Z) = {(p1, γ1), (p2, γ2), … (pm, γm)} is that the PDA in
state q, independent of the input symbol being scanned and with Z the TOS, can enter
the state pi and replace Z by γi ∀ i where 1 ≤ i ≤ m. Here the input head is not advanced.
Instantaneous Description (ID): [April 17]
• It is defined as the configuration of PDA at a given instant.
• It is denoted as (q, w, γ) where q is state, w is input string and γ is a string of stack symbols.
• If M = (Q, Σ, Γ, δ, q0, Z0, F) is a PDA we say (q, aw, Zα) |– (p, w, βα) if δ (q, a, Z) contains
(p, β). We use |– for one or more than one moves.
Accepted Language: [Oct. 17]
• There are two methods for defining languages accepted by PDA.
1. Acceptance by empty stack: It is a set of all strings for which some sequence of moves
causes PDA to empty its stack. We define, N (M) = {w | (q0, w, Z0) |– (p, ∈, ∈) for
some p ∈Q}.
2. Acceptance by final state: It is a set of all inputs for which some sequence of moves
causes PDA to enter a final state. We define:
L (M) = {w | (q0, w, z0) |– (p, ∈, γ) where p ∈F and γ ∈Γ*}
Example 1: Construct PDA for L = {an bn | n ≥ 1}.
Solution: We have already seen in example 5.1 that how it works. Let us put this using δ
mapping and construct PDA M.
1. Initially PDA is in state q0 and stack is empty.
2. We push 'A' on stack for a's and PDA remains in state q0.
3. When we get first 'b', we change the state into q1 and pop the 'A' from stack.
4. If the input is b and state is q1, then pop the symbol A. Do not change the state.
5. If input string is ∈ and stack is empty, PDA accepts the string by empty stack.
4.6
Theoretical Computer Science Pushdown Automata

∴ PDA M = ({q0, q1}, {a, b}, {A}, δ, q0, ∈, φ), where δ is defined as
δ (q0, a, ∈) = (q0, A)
δ (q0, a, A) = (q0, AA)
δ (q0, b, A) = (q1, ∈)
δ (q1, b, A) = (q1, ∈)
δ (q1, ∈, ∈) = (q1, ∈)

Fig. 4.11: PDA


Simulation for string ‘aabb’:
Consider the input string aabb. Instantaneous description is as follows:
(q0, aabb, ∈) |– (q0, abb, A)
|– (q0, bb, AA)
|– (q1, b, A)
|– (q1, ∈, ∈) = q1 is accept state
Since the language is accepted by empty stack, aabb ∈ L (M). The PDA is shown in
Fig. 5.10.
Example 2: Construct PDA for L = {w cwR | w in (0 + 1)*}.
Solution: To accept L we consider two states q1 and q2 and a stack on which you place blue,
green and red plates. The device will operate by following rules:
1. The start state is q1 and R (red) plate on the stack, so Z0 = R.
2. If the input is 0 and the state is q1, then push blue plate on the stack. Do not change the
state.
3. If the input is 1 and the state is q1, then push green plate on the stack. Do not change the
state.
4. If the input is c and the state is q1, then change the state to q2, no plates are added or
removed.
5. If the input is 0 and the state is q2 with blue plate on the top of stack then pop the blue
plate. If the input is 1 and the state is q2 with green plate on the top of stack then pop the
green plate. State remains in q2.
4.7
Theoretical Computer Science Pushdown Automata

6. If the device is in state q2 and a red plate is on the top of stack, then plate is popped and
stack is empty.
We write PDA as follows:
PDA M = ({q1, q2}, {0, 1, c}, {R, B, G}, δ, q1, R, φ)
where δ is δ (q1, 0, R) = {(q1, BR)}
δ (q1, 0, B) = {(q1, BB)}
δ (q1, 0, G) = {(q1, BG)}
δ (q1, 1, G) = {(q1, GG)}
δ (q1, 1, B) = {(q1, GB)}
δ (q1, c, R) = {(q2, R)}
δ (q1, c, B) = {(q2, B)}
δ (q1, c, G) = {(q2, G)}
δ (q2, 0, B) = {(q2, ∈)}
δ (q2, 1, G) = {(q2, ∈)}
δ (q2, ∈, R) = {(q2, ∈)}
Consider the string "01c10".
The sequence of moves is as follows:
(q1, 01c10,R) |– (q1, 1 c 10, BR) as δ (q1, 0, R) = {q1, BR}
|– (q1, c 10, GBR) as δ (q1, c, G) = {(q2, G)}
|– (q2, 10, GBR) as δ (q2, 1, G) = (q2, ∈)
|– (q2, 0, BR) as δ (q2, 0, B) = (q2, ∈)
|– (q2, ∈, R) as δ (q2, ∈, R) = (q2, ∈)
|– (q2, ∈, ∈)
The pictorial representation of PDA is shown in the Fig. 4.12.

Fig. 4.12
4.8
Theoretical Computer Science Pushdown Automata

4.2 CONSTRUCTION OF PDA [April 16, Oct. 16]


• In this section we will study construction of PDA.
4.2.1 Acceptance by Final State
• Let P = (Q, Σ, Γ, δ, q0, z0 F) be a PDA. Then L(P), the language accepted by P by final state
is {w | (q0, w, zo) |–* (q, ∈, α)}, where q is some state in F and stack is not empty (say α).
Example: L = {wwR | w ∈ (0 + 1)*}
Solution: We have to show the accepting computation of P.
If string S = wwR then
(qo, wwR, z0) |–* (q0, wR, wR z0)
|– (q1, wR, wR z0) |–* (q1, ∈, z0) |–* (q2, ∈, z0)
Here one option is to read from its inputs and store it on its stack, in reverse. After reading of
w, (in reverse), state is changed from q0 and q1. At state q1, it matches wR on the input with the
same string on its stack and finally enter in state q2.
So P accepts by final state.

4.2.2 Acceptance by Empty Stack


• Let P = {Q, Σ, Γ, δ, q0, z0, F) we define N(P) = {w | (q0, w, z0) |–* (q, ∈, ∈)}
Where, q is any state and N(P) is the set of inputs w that P can consume and at the same time
empty its stack.
Example: L = {wwR | w ∈ (0 + 1)*}
P = (Q, Σ, Γ, δ, q0, z0, φ) stack is empty
We define P = ( {q0, q1, q2}, {0, 1}, {0, 1, z0}, δ, q0, z0, φ)
where δ is
1. δ (q0, 0, z0) = {(q0, 0, z0)}
and δ (q0, 1, z0) = {(q0, 1, z0)}
we read first input symbol and push into the stack.
2. δ (q0, 0, 0) = δ (q0, 00)
δ (q0, 0, 1) = δ (q0, 01)
δ (q0, 1, 0) = δ (q0, 10) use for pushing each onto the top of the stack
δ (q0, 1, 1) = δ (q0, 11)
3. δ (q0, ∈, z0) = {(q1, z0)}
δ (q0, ∈, 0) = {(q1, 0)}
δ (q0, ∈, 1) = {(q1, 1)}
This three rules allow P to go from state q0 to state q1 simultaneously (on ∈ input),
leaving intact whatever symbol is at the top of the stack.
4.9
Theoretical Computer Science Pushdown Automata

4. δ (q1, 0, 0) = (q1, ∈)
δ (q1, 1, 1) = (q1, ∈)
In state q1, match input symbols against the top symbols on the stack. Then pop the
symbol.
5. Pop all symbols until z0 marker we get. Then pop z0 and stack is empty.

DETERMINISTIC AND NON-DETERMINISTIC PDA


4.3
(DPDA AND NPDA) [April 17, Oct. 17]
• We have seen that in case of FA, DFA or NFA are equivalent. Similarly, we have
deterministic and non-deterministic PDA (DPDA and NPDA) but they are not equivalent.
• A language accepted by a NPDA may not be accepted by a DPDA. Thus, for every NPDA
there may not exist an equivalent DPDA.
• A PDA is said to be a Deterministic PushDown Automata (DPDA) if all derivations in the
design give only a single move.
• A pushdown automata is called Non-deterministic PushDown Automata (NPDA) if one of
the derivations generates more than one move.
• If a PDA being in a state with a single input and a single stack symbol gives a single move,
then the PDA is called DPDA. If a PDA being in a state with a single input and a single stack
symbol gives more than one move for any of its transitional functions, then the PDA is called
NPDA.
• The Venn diagram shown in the Fig. 4.13 gives the relative powers of the machines DFA,
NFA, DPDA and NPDA.

Fig. 4.13: Venn Diagram


• Thus though DFA and NFA have equal powers, NPDA and DPDA have different
capabilities. NPDA can accept any CFL but DPDA is a special case of CFL, NPDA accepts.
• Thus, the PDAs that are equivalent to CFGs is the class of NPDAs.
So, M = (Q, Σ, Γ, δ, q0, Z0, F) is deterministic if
1. for each q in Q and Z in Γ, whenever δ (q, ∈, Z) is non-empty then δ (q, a, Z) is empty
for all a ∈Σ.
2. for no q in Q, Z in Γ and a in Σ ∪ {∈} does δ (q, a, Z) contains more than one element.
• If any of the above conditions is not satisfied then PDA is a Non-deterministic PDA
(NPDA).
4.10
Theoretical Computer Science Pushdown Automata

Example 1: Consider PDA for language L = {wwR | w ∈ (a + b)*}.


Solution: To construct NPDA, we define
M = ({q1, q2} {0, 1}, {R, B, G}, δ, q1, R, φ)
where, δ is δ (q1, 0, R) = {(q1, BR)}
δ (q1, 1, R) = {(q1, GR)}
δ (q1, 0, B) = {(q1, BB) (q2, ∈)}
δ (q1, 0, G) = {(q1, BG)}
δ (q1, 1, B) = {(q1, GB)}
δ (q1, 1, G) = {(q1, GG), (q2, ∈)}
δ (q2, 0, B) = {(q2, ∈)}
δ (q2, 1, G) = {(q2, ∈)}
δ (q1, ∈, R) = {(q2, ∈)}
δ (q2, ∈, R) = {(q2, ∈)}
This is NPDA since moves for string 001100 are as follows:
(q1, 001100, R) → (q2, 001100, ∈)

(q1, 01100, BR)
↓ (q2, 1100, R) → (q2, 1100, ∈)
(q1, 1100, BBR)

(q1, 100, GBBR)
↓ (q2, 00, BBR)
(q1, 00, GGBBR)
↓ (q2, 0, BR)
(q1, 0, BGGBBR)
↓ (q2, ∈, R)
(q1, ∈, BBGGBBR) (q2, ∈, GGBBR)
(q2, ∈, ∈)
reject ↓
Accept
Input string 001100 is accepted by NPDA, since there is one sequence of moves which
empties the stack.
4.11
Theoretical Computer Science Pushdown Automata

EXAMPLES USING STACK METHODS


Example 1: Construct PDA to accept a string of properly balanced parenthesis over the set
of alphabet {{, (, [, }, ), ] }.
Solution: We push A, B, C for {, (, [ respectively and consider red (R) at bottom of the
stack.
Let, M = ({q0, q1}, { {, (, [, }, ), ] }, {A, B, C, R}, δ, q0, R, φ)
where δ is defined as
δ (q0, {, R) = (q0, AR)
δ (q0, (, R) = (q0, BR)
δ (q0, [, R) = (q0, CR)
δ (q0, {, A) = (q0, AA)
δ (q0, {, B) = (q0, AB)
δ (q0, {, C) = (q0, AC)
δ (q0, (, A) = (q0, BA)
δ (q0, (, B) = (q0, BB)
δ (q0, (, C) = (q0, BC)
δ (q0, [, A) = (q0, CA)
δ (q0, [, B) = (q0, CB)
δ (q0, [, C) = (q0, CC)
δ (q0, }, A) = (q0, ∈)
δ (q0, ), B) = (q0, ∈)
δ (q0, ], C) = (q0, ∈)
δ (q0, ∈, R) = (q1, ∈)

Simulation of string “{([ ])}":


(q0, {([ ])}, R) |– (q0, ([ ])}, AR)
|– (q0, [ ])}, BAR)
|– (q0, ])}, (BAR)
|– (q0, )}, BAR)
|– (q0, }, AR)
|– (q0, ∈, R)
|– (q1, ∈, ∈) = accept
4.12
Theoretical Computer Science Pushdown Automata

Example 2: Construct PDA for language L = {0m 1n 2k | m, n, k ≥ 1, m = n + k}.


Solution: We push B for each 0 and when we get first 1 start popping plates and change the
state. For first occurrence of 2, we have to change the state because order is important.
PDA M = ({q0, q1, q2}, {0, 1, 2}, {R, B}, δ, q0, R, φ), where δ is defined as follows:
δ (q0, 0, R) = (q0, BR)
δ (q0, 0, B) = (q0, BB)
δ (q0, 1, B) = (q1, ∈)
δ (q1, 1, B) = (q1, ∈)
δ (q1, 2, B) = (q2, ∈)
δ (q2, 2, B) = (q2, ∈)
δ (q2, ∈, R) = (q2, ∈)
Simulation of string “0012”:
(q0, 0012, R) |– (q0, 012, BR)
|– (q0, 12, BB)
|– (q1, 2, B)
|– (q2, ∈) = accept
Example 3: Construct a PDA to check the well-formedness of parenthesis.
Solution: The PDA will scan the input symbols and everytime it finds an opening bracket '(',
it will push A onto the stack. Whenever a closing bracket ')' is found, the PDA will pop A from
the stack.

Fig. 4.14
δ (q0, (, R) = (q0, AR)
δ (q0, (, A) = (q0, AA)
δ (q0, ), A) = (q1, ∈)
δ (q1, ), A) = (q1, ∈)
δ (q1, ∈, R) = (q1, ∈)
4.13
Theoretical Computer Science Pushdown Automata

Example 4: Design a PDA which accepts a language L = {an b2n+1 | n ≥ 1}.


Solution: Each string in the language will be of the form an b2n b.
PDA reads and push all a's. When it reads the first b, it recognises that all a's are over. So
hereafter for every two b's in the string it should pop one a from the stack. At the end, it should
find only one b and accept the strings. PDA is as shown in the Fig. 4.15.

Fig. 4.15
We construct PDA M = ({q0, q1, q2, q3}, {a, b}, (R, A), δ, q0, R, φ), where δ is defined as
follows:
δ (q0, a, R) = (q0, AR)
δ (q0, a, A) = (q0, AA)
δ (q0, b, A) = (q1, A)
δ (q1, b, A) = (q2, ∈)
loop
δ (q2, b, A) = (q1, A)
δ (q2, b, R) = (q3, ∈)
δ (q2, ∈, R) = (q3, ∈)
Simulation of string “aabbbbb”:
(q0, aabbbbb, R) |– (q0, abbbbb, AR)
|– (q0, bbbbb, AAR)
|– (q1, bbbb, AAAR)
|– (q1, bbb, AAR)
|– (q2, bb, AR)
|– (q2, b, R)
|– (q3, ∈) = accept
4.14
Theoretical Computer Science Pushdown Automata

Example 5: Construct a PDA that accepts the language generated by CFG,


S → S + S | S *S | 2 [April 16]

Solution:

Fig. 4.16

Example 6: Construct PDA for L = {0m 1n 2n+2 3m | n, m ≥ 1}


Solution: L = {0m 1n 2n+2 3m | n, m ≥ 1}
is written as, {0m 1n 2n 2 2 3m | n, m ≥ 1}
In this language total number of 0’s and total number of 3’s are same.
Also total number of 1’s and total number of 2’s are same and two 2’s are extra.
Steps:
1. All 0’s are push into the stack at state q0.
2. All 1’s are push into the stack at state q1.
3. Read extra two 2’s without changing the stack.
4. Read one 2 and pop one 1 from the stack.
5. Pop will continue until we encounter 3.
6. When 3 is read, then for each 3 read, pop 0 from stack.
7. When string is over and stack is empty, then the string is accepted.
4.15
Theoretical Computer Science Pushdown Automata

The PDA is
M = (Q, Σ, Γ, δ, q0, z0, φ)
where Q = {q0, q1, q2, q3, q4}
Σ = {0, 1, 2, 3}
Γ = {0, 1, R}
and δ mapping is
δ (q0, 0, R) = (q0, 0R) → Push 1st 0
δ (q0, 0, 0) = (q0, 00) → Push all 0’s
δ (q0, 1, 0) = (q1, 10) → Push first 1
δ (q1, 1, 1) = (q1, 11) → Push all 1’s
δ (q1, 2, 1) = (q2, 1) → Change state without push or pop
δ (q2, 2, 1) = (q3, 1) → Change state without push or pop
δ (q3, 2, 1) = (q3, ∈)
δ (q3, 3, 0) = (q4, ∈)
δ (q4, 3, 0) = (q4, ∈)
δ (q4, ∈, R) = (q4, ∈) = accept
Simulation of string 012223:
(q0, 012223, R) |– (q0, 12223, 0R)
|– (q1, 2223, 10R)
|– (q2, 223, 10R)
|– (q3, 23, 10R)
|– (q3, 3, 0R)
|– (q4, ∈, R)
|– (q4, ∈) = accept

Example 7: Construct PDA to check whether given string is correct prefix expression or
not?
Solution: Logic:
• All operators are push into the stack.
• When operands are read, pop operator from stack.
4.16
Theoretical Computer Science Pushdown Automata

Fig. 4.17
PDA is M = (Q, Σ, Γ, δ, q0, z0, φ)
Q = {q0, q1, q2,q3}
Σ = {operator, operand}
Γ = operator, z0
δ is δ (q0, operator, z0) = (q0, operator, z0)
δ (q0, operator, operator) = (q0, operator, operator)
δ (q0, operand, operator) = (q1, operator)
(By reading 1st operand do not change the stack)
δ (q1, operand, operator) = (q2, ∈)
δ (q2, operand, operator) = (q3, ∈)
δ (q3, ∈, z0) = (q3, ∈)
Simulation of string “+ * a b c”:
δ (q0, + * a b c, z0) |– (q0, * a b c, + z0)
|– (q0, a b c, * + z0)
|– (q1, b c, * + z0)
|– (q2, c, + z0)
|– (q3, ∈, z0)
|– (q3, ∈) string is accepted
4.17
Theoretical Computer Science Pushdown Automata

Example 8: Construct PDA to check whether given equation is in postfix form or not.
Solution: Logic:
• All operands are push into the stack.
• When operator is read, two operands are popped from the stack.

Fig. 4.18
PDA is M = (Q, Σ, Γ, δ, q0, z0, φ)
where Q = {q0, q1, q2, q3}
Σ = {operator, operand}
Γ = {operand, z0}
δ is δ (q0, operand, z0) = (q1, z0)
δ (q1, operand, z0) = (q1, operand, z0)
δ (q1, operand, operand) = (q1, operand, operand)
δ (q1, operator, operand) = (q1, ∈)
δ (q1, ∈, operand) = (q2, ∈)
δ (q2, ∈, z0) = (q2, ∈) = accept
Simulation of string "a b * c +": The string is operand operand operator operand operator.
δ (q0, a b * c +, z0) |– (q1, a b * c +, z0)
|– (q1, b * c +, a z0)
|– (q2, * c +, b a z0)
|– (q1, c +, a z0)
|– (q1, +, c a z0)
|– (q1, ∈, az0)
|– (q2, ∈, z0)
|– (q2, ∈) = accept
4.18
Theoretical Computer Science Pushdown Automata

Example 9: Construct PDA to accept all those strings containing equal number of a’s and
b’s.
Solution: L = {ab, abab, abba, aabb, bbaa, baba, ……}
Logic:
• When a is read at state q0, push a and change the state to q1. If b is read, then push b and
change the state to q2.
• At state q1, a is read, then push a. At state q2, b is read, then push b.
• When the PDA is at q1 state means a is already read and current input symbol is b then
pop a.
• When the PDA is at state q2 and it reads a then pop b.
• When string is over and stack is empty, then language is accepted.
PDA is, δ (q0, a, z0) = (q1, az0)
δ (q1, a, a) = (q1, aa)
δ (q1, b, a) = (q1, ∈)
δ (q1, b, z0) = (q2, bz0)
δ (q0, b, z0) = (q2, bz0)
δ (q2, b, b) = (q2, bb)
δ (q2, a, b) = (q2, ∈)
δ (q2, a, z0) = (q1, az0)
δ (q2, ∈, z0) = (q3, ∈)
δ (q3, ∈, ∈) = (q3, ∈) = accept
Simulation of string “abba”:
δ (q0, abba, z0) |– (q1, bba, az0)
|– (q1, ba, z0)
|– (q2, a, bz0)
|– (q2, ∈, z0)
|– (q3, ∈, ∈) = accept
Example 10: Show that {an bn | n ≥ 1} ∪ {am b2m | m ≥ 1} cannot be accepted by a
deterministic PDA. OR It is NPDA.
Solution: L = {ab, aabb, abb, aabbbb, ……}
The union of a language containing strings of equal number of a’s followed by equal number
of b’s or double b’s for single a.
4.19
Theoretical Computer Science Pushdown Automata

Logic:
• PDA push all a’s.
• When it reads the first b, it recognize that a’s are over. Then we either push 2nd b, or we
pop ‘a’ to check equal a’s and b’s.
PDA is, δ (q0, a, z0) = (q0, Az0)
δ (q0, a, A) = (q0, AA)
δ (q0, b, A) = {(q1, A) (q2, ∈)}
δ (q1, b, A) = (q2, ∈)
δ (q2, b, A) = (q1, A)
δ (q2, b, z0) = {(q3, ∈) (q2, ∈)}
δ (q2, ∈, z0) = (q3, ∈)
Simulation of “abb”:
δ (q0 abb, z0) |– (q0, bb, Az0)
|– (q1, b, Az0)
|– (q2, ∈, z0)
|– (q3, ∈) = accept
Simulation of “ab”:
δ (q0, ab, z0) |– (q0, b, Az0)

(q1, ∈, AAz0) (q2, ∈, z0)

rejected (q3, ∈, ∈)
accepted
The given PDA is NPDA.
Example 11: Construct PDA for language L = {0n 1m 2n + m | n, m ≥ 1}
Solution: Logic:
• Read all 0’s at state q0 and push A onto stack.
• Read all 1’s at state q1 and push B onto stack.
• When 2 is read at state q1, POP A and B from the stack by changing the state q3.
• At state q4, language is accepted.
δ (q0, 0, z0) = (q0, Az0)
δ (q0, 0, A) = (q0, AA)
δ (q0, 1, A) = (q1, BA)
δ (q1, 1, B) = (q1, BB)
4.20
Theoretical Computer Science Pushdown Automata

δ (q1, 2, B) = (q2, ∈)
δ (q2, 2, A) = (q3, ∈)
δ (q3, 2, A) = (q3, ∈)
δ (q3, ∈, ∈) = (q4, ∈)
Example 12: Design a PDA for checking the acceptance of string an bn cn.
Solution: This is two stack problem. We require two stack for counting a and b, then b and c.
So it is double check problem. If we are using a single stack, then we cannot solve such a
problem because we can push all a’s in the stack which will get poped on b. By the time we reach
c, the stack will be empty and we do not have anything to compare. We only check equal number
of a’s and b’s.
Using two stack all a’s can be push into one stack and all b’s can be push into other stack.
When we read c, we can pop one symbol from each stack. When we finish reading input, both
the stacks should be empty and then the string can be accepted.

Fig. 4.19
Two stack PDA for L = {an bn cn
| n ≥ 0}
PDA with two stack is more powerful than PDA with one stack. But some languages
(context-sensitive) are not accepted by PDA. In the above figure (1) represents stack 1 and (2)
represents stack (2).
4.21
Theoretical Computer Science Pushdown Automata

4.4 CREATING FILES CFG (IN GNF) TO PDA [April 16]

• PDA is the machine accepting a context-free language. A context-free language is generated


from context-free grammar. This section describes the process of generating a PDA from the
CFG.
• Theorem 1: Given a language L generated by a particular CFG, there is a PDA that accepts
exactly L.
• Theorem 2: Given a language L that is accepted by a certain PDA, there exists a CFG that
generates exactly L.
Construction of PDA from the Given CFG:
• We will assume that the given CFG does not consist of any productions of the form S → ∈.
Step 1: Convert the grammar into GNF.
Step 2: Say, if S → aSB | aB then
δ (q0, a, S) = {(q0, SB) (q0, B)}
↓ ↓ ↓
Input LHS
State symbol non-terminal
Say, if S → a then δ (q0, a, S) = (q0, ∈).

EXAMPLES
Example 1: Construct a PDA for CFG
S → aAA
A → aS | bS | a
Solution: We construct PDA M = ({q0}, {a, b}, {S, A}, δ, q0, S, φ), where δ is defined as
follows:
δ (q0, a, S) = {(q0, AA)}
δ (q0, a, A) = {(q0, S) (q0, ∈)}
δ (q0, b, A) = {(q0, S)}
Consider a string derived from CFG, "abaaaa" and find whether it is accepted by our
constructed PDA by ID.
(q0, abaaaa, S) |– (q0, baaaa, AA)
|– (q0, aaaa, SA) |– (q0, aaa, AAA)
|– (q0, aa, AA) |– (q0, a, A) |– (q0, ∈, ∈)
Therefore, the string which is accepted by CFG is also accepted by PDA.
4.22
Theoretical Computer Science Pushdown Automata

Example 2: Construct a PDA equivalent to CFG, S → aSa | bSb | a | b.


Solution: Converting CFG into GNF, we get,
S → aSA
S → bSB
S → a
S → b
A → a
B → b
We construct the PDA.
M = ({q0}, {a, b}, {S, AB}, δ, q0, S, φ), where δ is defined as follows:
δ (q0, a, S) = {(q0, SA) (q0, ∈)}
δ (q0, b, S) = {(q0, SB) (q0, ∈)}
δ (q0, a, A) = {(q0, ∈)}
δ (q0, b, B) = {(q0, ∈)}
Consider a string aababaa and find whether it is accepted by above PDA.
(q0, aababaa, S) |– (q0, ababaa, SA)
|– (q0, babaa, SAA) |– (q0, abaa, SBAA)
|– (q0, baa, BAA) |– (q0, aa, AA)
|– (q0, a, A) |– (q0, ∈, ∈)
Example 3: Construct a PDA that accepts language as S → aS | aSbS | a.
Solution: Convert CFG into GNF.
S → aS
S → aSBS
S → a
B → b
We construct PDA.
M = ({q0}, {a, b}, {S, B}, δ, q0, S, φ), where δ is defined as follows:
δ (q0, a, S) = {(q0, S) (q0, SBS) (q0, ∈)}
δ (q0, b, B) = {(q0, ∈)}
Consider a string aaaba derived from CFG. We will see whether this string is accepted by
above PDA or not.
(q0, aaaba, S) |– (q0, aaba, S) |– (q0, aba, SBS)
|– (q0, ba, BS) |– (q0, a, S) |– (q0, ∈, ∈)
String "aaaba" is accepted.
4.23
Theoretical Computer Science Pushdown Automata

Example 4: Show with example that if the language is accepted by CFG then the same
language is accepted by PDA.
Solution: Consider CFG in CNF.
S → SB
S → AB
A → CC
B → b
C → a
Let, Γ = {S, A, B, C}
Σ = {a, b}
The stack contains the symbol S and always contains non-terminals. There are two
possibilities (NPDA): either we replace the removed non-terminal by two non-terminals or we do
not replace non-terminal at all but we go to a read state to read the terminal from the tape.
Consider string "aab" generated by leftmost derivation.
S ⇒ AB ⇒ CCB ⇒ aCB ⇒ aaB ⇒ aab.
Now, we shall trace how the word "aab" can be accepted by PDA.
Leftmost Derivation State Stack Tape
Start ∈ aab
S PUSH S S aab
POP (S) ∈ aab
PUSH B B aab
S ⇒ AB PUSH A AB (... TOS = A) aab
POP (A) B aab
PUSH C CB
aab
A ⇒ CC PUSH C CCB
aab
POP (C) CB
aab
Read a CB
aab
POP (C) B
aab
Read a aab
B
POP (B) aab

Read b aab

ACCEPT ∈

Both stack and tape are empty. Thus language is accepted by empty stack or PDA, N (M).
Thus, we construct a PDA as shown in the Fig. 4.20.
4.24
Theoretical Computer Science Pushdown Automata

Fig. 4.20
Example 5: Construct a PDA to check whether the given expression is a valid postfix
expression. Assume all operators to be binary.
Solution: If all operators are assumed to be binary, the postfix expression is → operand 1,
operand 2, operator e.g. ab * c +, ab +.

Fig. 4.21
Step 1 : Read the first symbol (operand).
Step 2 : Read next symbol and push it (operand).
Step 3 : Read next symbol, if it is operator then pop the symbol from the stack. If the
symbol popped is operand then go back to step 2, else if symbol popped is ∆ then
go to Reject. If the next input symbol read is ∆, pop the symbol from the stack. If
it is ∆, go to Accept, otherwise go to reject. The PDA is shown in the Fig. 4.21.
4.25
Theoretical Computer Science Pushdown Automata

Example 6: Construct a PDA for a language in which all the strings have at least one
occurrence of aa.
Solution:

Fig. 4.22
Here we have a language. Start with a or b, when we read b, the next symbol is either a or b.
But when we read a, then check for the next symbol. If it is 'b', then read again the next symbol.
If it is 'a' (here we get two consecutive a's) then go for accept state, since the language is
{aa, baa, baab, baaba, …}
PDA is δ (q0, a, z0) = (q1, Az0)
δ (q1, a, A) = (q2, ∈)
δ (q1, b, A) = (q1, BA)
δ (q1, a, B) = (q1, AB)
δ (q1, b, B) = (q1, BB)
Note: q2 is the final state language accepted by final state.

EXAMPLES
Example 1: Construct PDA for L = {ax by zz | x = 2y + z, y, z ≥ 1}
Solution: δ (q0, a, z0) = (q0, az0)
δ (q0, a, a) = (q0, aa)
δ (q0, b, a) = (q1, ∈)
δ (q1, b, a) = (q1, ∈)
δ (q1, c, a) = (q2, ∈)
δ (q0, ∈, z0) = (q2, ∈, ∈) = accept.
4.26
Theoretical Computer Science Pushdown Automata

Example 2: Construct PDA for L = {an b2n ck | n ≥ 1, k ≥ 0}.


Solution: δ (q0, a, z0) =
(q0, az0)
δ (q0, a, a) =
(q0, aa)
δ (q0, b, a) =
(q1, a)
δ (q1, b, a) =
(q2, ∈)
δ (q2, c, ∈) =
(q3, ∈)

q3 is accept state.
This PDA is the language accepted by final state.
We check only a and double b's when c is read, language is accepted even though the stack is
not empty.

PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which is the machine format of the context-free language?
(a) Pushdown Automata (PDA) (b) Finite Automaton (FA)
(c) Push Automata (PA) (d) None of the mentioned
2. If a PDA being in a state with a single input and a single stack symbol gives a single
move, then the PDA is called as deterministic pushdown automata?
(a) Deterministic Finite Automaton (DFA)
(b) Deterministic Pushdown Automata (DPDA)
(c) Non-Deterministic Pushdown Automata (NPDA)
(d) None of the mentioned
3. The difference between FA and PDA is in,
(a) Finite control (b) Reading head
(c) Stack (d) Input tape
4. Which of the following is not possible algorithmically?
(a) NPDA to DPDA (b) NFA to DFA
(c) RE to CFG (d) CFG to PDA
5. A PDA can be formulated using how many tuples,
(a) 7 (b) 5
(c) 6 (d) 9
6. The Instantaneous Description (ID) of PDA is,
(a) the identity of that PDA
(b) describes the identity of that PDA always nonzero
(c) describe the configuration of PDA at a given instant (answer) always zero
(d) All of the mentioned
7. Which is the machine accepting a context-free language?
(a) PDA (b) FA
(c) DFA (d) NFA
4.27
Theoretical Computer Science Pushdown Automata

8. In PDA which consists of a finite list of symbols.


(a) input tape (b) stack
(c) finite control (d) head
9. Which of the following languages is accepted by a NPDA but not by a deterministic
PDA?
(a) Always a context-free language (b) Always regular
(c) Never regular (d) None of the mentioned
10. To declare a string accepted by a pushdown automata languages uses,
(a) accepted by the empty stack (b) accepted by the final state
(c) Both (a) and (b) (d) None of the mentioned
Answers
1. (a) 2. (b) 3. (c) 4. (a) 5. (a) 6. (c) 7. (a) 8. (b) 9. (c) 10. (c)
Q.II Fill in the Blanks:
1. A ______ consists of a 7-tuple M is a system (Q,  ,  ,  , q0, Z0, f).
2. A DFA can remember a finite amount of information, but a PDA can remember an
______ amount of information.
3. PDA is the machine format of ______.
4. The ______ control can be considered as a control unit of a PDA.
5. If a PDA being in a state with a single input and a single stack symbol gives more than
one move for any of its transitional functions, then the PDA is called ______ pushdown
automata.
6. Instantaneous Description (ID) describes the configuration of the PDA at a given
______.
7. A ______ is a temporary storage of stack symbols.
8. A context-free language is called non-deterministic context-free language if it is accepted
by a ______.
9. PDA has a stack called the ______pushdown store (abbreviated PDS) which is a read-
write pushdown store as we add elements to PDS or remove elements from PDS.
10. A context-free language is called deterministic context-free language if it is accepted by
a ______.
11. A pushdown automaton reads a given input string from ______left to right and in each
step, it chooses a transition by indexing a table by input symbol, current state, and the
symbol at the top of the stack.
Answers
1. Pushdown Automata (PDA) 2. infinite 3. context-free language 4. finite
5. nondeterministic 6. instance 7. stack 8. NPDA
9. pushdown store (PDS) 10. DPDA 11. left to right
4.28
Theoretical Computer Science Pushdown Automata

Q.III State True or False:


1. A pushdown automaton (PDA) is a way to implement a context-free grammar in a
similar way we design DFA for a regular grammar.
2. Every PDA accepts only CFL's.
3. PDA is the same as finite automata with the attachment of an auxiliary amount of storage
as stack.
4. PDA accepts only non-regular sets.
5. ID remembers the information of the state and the stack content at a given instance of
time.
6. A PDA is said to be a DPDA if all derivations in the design give only a single move.
7. In a PDA adding a symbol at the top of the stack is referred to as pushing a symbol onto
the stack, and removing a symbol is referred to as popping an item from the stack.
8. PDA is the machine accepting a context-free language.
9. PDA which accepts the language that is produced by the context-free grammar G.
10. A pushdown automaton is called NDPA if one of the derivations generates more than
one move.
11. A pushdown automaton can also manipulate the stack, as part of performing a transition.
The manipulation can be to push a particular symbol to the top of the stack, or to pop off
the top of the stack.
12. The PDA can be directly generated from the CFG.
13. A DPDA which accepts by empty stack cannot accept all Regular Languages.
14. Instantaneous Description (ID) is an informal notation of how a PDA computes an input
string and makes a decision that string is accepted or rejected.
15. A language can be accepted by Pushdown automata using two approaches namely
Acceptance by Empty Stack (on reading the input string from the initial configuration for
some PDA, the stack of PDA gets empty) and Acceptance by Final State (to accept its
input by the final state if it enters any final state in zero or more moves after reading the
entire input).
16. For directly constructing a PDA from a given grammar, we fi rst need to convert the
grammar into Greibach Normal Form (GNF).
Answers
1. (T) 2. (T) 3. (T) 4. (T) 5. (F) 6. (T) 7. (T) 8. (T) 9. (T) 10. (T)
11. (T) 12. (T) 13. (T) 14. (T) 15. (T) 16. (T)
Q.IV Answer the following Questions:
(A) Short Answer Questions:
1. Define is PDA.
2. Construct an equivalent PDA for S aB/bA context-free grammars.
3. Define DPDA.
4. What is NPDA?
5. Compare FA and PDA (any two points).
6. Which component used in PDA for temporary storage of symbols.
4.29
Theoretical Computer Science Pushdown Automata

(B) Long Answer Questions:


1. What is PDA? Explain with the help of diagram.
2. How to construct a PDA using empty stack and final state methods?
3. Give an equivalent PDA for the following CFG:
(i) S → AB
A → BB|a
B → AB|a|b
(ii) S → aAbb|AB
A → bbB|b|ab
B → BbA|b|aB
(iii) S → AB
A → aAb|ab
B → cBd|cd
(iv) S → 01AA|A10
A → 0B|0
B → |B00|0B|1
(v) S → aB|bA
A → a|aS|bAA
B → b|bS|aBB
4. Construct a PDA for the following languages:
(i) {an b2n | n ≥ 1}
(ii) {an bm | n ≥ 1} ∪ {am b2n | m ≥ 1}
(iii) Set of all strings over {0, 1} starting with "00" and containing even number of 0's
in it.
(iv) L = L1 ∪ L2
L1 = Set of all strings over {a, b} having equal number of a's and b's
L2 = {bn cm | n > m, m ≥ 0}
(v) L = {0n 12n+1 | n ≥ 1}
(vi) L = {am bn cn+2 dm | n, m ≥ 1}
(vii) All strings starting with 0 and having even number of 1's in it over {0, 1}*.
5. Show that the DPDA and NPDA are not equivalent with an appropriate example.
6. Prove with an example that if the language is accepted by a CFG, then the same language
is accepted by a PDA.
7. Write formal definition of DPDA and NDPA.
UNIVERSITY QUESTIONS AND ANSWERS
April 2016
1. Different between PDA and FA. [1 M]
Ans. Refer to Page 4.5.
4.30
Theoretical Computer Science Pushdown Automata

2. Define PDA and construct PDA for L = {an bm an | m, n ≥ 1}. [5 M]


Ans. Refer to Sections 4.1 and 4.2.
3. Convert the following CFG into PDA: [4 M]
S → 1S | 1S0S | 1
Also show that string "11101" is accepted by the PDA.
Ans. Refer to Section 4.4.
October 2016
1. Why is PDA more powerful than FA? [1 M]
Ans. Refer to Page 4.5.
2. Construct a PDA for L = {ambncn + 1dm | n ≥ 1, m ≥ 1}. [1 M]
Ans. δ (q0, a, 20) = (q0, A 20)
δ (q0, a, A) = (q0, AA)
δ (q0, b, A) = (q1, BA)
δ (q1, b, B) = (q1, BB)
δ (q1, c, B) = (q2, ∈)
δ (q2, C, B) = (q2, ∈)
δ (q2, c, A) = (q3, A)
δ (q3, d, A) = (q4, ∈)
δ (q4, d, A) = (q4, ∈)
δ (q4, 20) = (q5, ∈)
3. Construct PDA equivalent to the given CFG: [5 M]
S → AA
A → 0A0 | A1 | 1
Ans. The given CFG is not CNF
Set B → 0 and C → 1
By replacing B and C in A, we get
A → OAB | AC | 1
After removing the left recursion in A, we get
A → OAB | 1 | OABA’ | 1A’
A’ → C | CA’
The production of A is in GNF
By replacing the production of C in A’, we get
A’ → 1 | 1A’
Now the production of A' are also in GNF.
By replacing the production of A in S we get
S → OABA | 1A | OABA’A | 1A’A.
The grammar is
S → OABA | 1A | OABA’A | 1A’A
A → OAB | 1 | OABA’ | 1A’
A’ → 1 | 1A’
B→0
C→1

4.31
Theoretical Computer Science Pushdown Automata

April 2017
1. Define ID for PDA. [1 M]
Ans. Refer to Page 4.6.
2. Construct a PDA for L = {0m 1n 2n 0m | m ≥ 1, n ≥ 1}. [5M]
Ans. Refer to Section 4.1, Examples.
October 2017
1. Name the type of languages accepted by Pushdown Automata. [1 M]
Ans. Refer to Page 4.6.
2. Construct PDA for language: L = {ambm | m > n ≥ 1} [5 M]
Ans. Refer to Section 4.1, Examples.
April 2018
1. State two differences between PDA and FA. [1 M]
Ans. Refer to Page 4.5.
2. Construct PDA that accept langauge as S → aS | aSbS | a. [4 M]
Ans. Refer to Section 4.1, Examples.
October 2018
1. Write a mapping of δ in PDA. [5 M]
Ans. Refer to Section 4.1.
2. Construct PDA for language: [1 M]
L = {anb2n+1 | n > 1}.
Ans. Refer to Section 4.1, Examples.
3. Differentiate between FA and PDA. [2 M]
Ans. Refer to Page 4.5.
April 2019
n 2n+1
1. Construct PDA for L = {a b | n > 1} [4 M]
Ans. Refer to Section 4.1, Examples.
2. Construct PDA equivalent to the given CFG: [1 M]
S→bAB|aB
A→aAB|a
B→aBB|b
Ans. Refer to Section 4.4, Examples.

4.32
CHAPTER
5

Turing Machine
Objectives …
To study Basic Concepts in Turing Machine
To learn Design of Turing Machine
To understand Linear Bounded Automation (LBA)

5.0 INTRODUCTION
• We have seen various machines such as FA and PDA. To remove the limitations, we
required a more powerful machine, called Turing machine.
• Turing machine is a simple mathematical model of a modern digital computer. This machine
was introduced by Alan Turing in 1936.
• We can compare different machines which are shown in the Table 5.1.
Table 5.1
Language Defined Acceptor or Non-determinism Example of
By Recognizer = Determinism ? Application
1. Regular Finite automata Yes Text editors
expression
2. Context-free Pushdown automata No Programming
grammar language, statements,
compilers
3. Type 0 grammar Turing machine Yes Computers
• Turing machine can be constructed to accept a given language or to carry out some
algorithm. Machine may take its own output as input for further operation, hence no
distinction between input and output set.
• The machine can choose the current location and also decides whether to move left or right
in the memory.
• Today, the turning machine has become the accepted formalization of an effective procedure.
Turing machine is equivalent in computing power to the digital computer as we know it
today and also to all the most general mathematical notion of computation.
5.1
Theoretical Computer Science Turing Machine

5.1 BASIC CONCEPTS IN TURING MACHINE [Oct. 16]


• In this section we will study basic concepts in Turing Machine (TM) like definition, TM
model and design of TM.
5.1.1 Turing Machine Model
• Fig. 5.1 shows model of Turing machine. The Turing machine consists of
1. The head which can read or write a symbol and move left or right or stay in position,
corresponding to the square cell read.
2. An infinite tape, which is divided into cells, on which the symbols from an alphabet set
can be written (refer Fig. 5.1). Tape is infinite to right.
3. The tape contains the input alphabets with an infinite number of blanks at the left and
the right hand side of the input symbols.
Tape a1 a2 a3 am B B

Finite head
control

Fig. 5.1: Turing Machine Model


• Finite set of states, in one of which the machine can reside. In one move, TM
o changes the state.
o prints a symbol on the tape cell scanned, replacing the previous symbol.
o moves the head to left (L) or right (R).
Working of Turing Machine:
• A Turing Machine (TM) is a mathematical model which consists of an infinite length tape
divided into cells on which input is given. It consists of a head which reads the input tape.
• After reading an input symbol, it is replaced with another symbol, its internal state is
changed, and it moves from one cell to the right or left. If the TM reaches the final state, the
input string is accepted, otherwise rejected.
5.1.2 Definition of Turing Machine [April 17. Oct. 18]
• A TM can be formally defined as, M is a 7-tuple
M = (Q, Σ, Γ, δ, q0, B, F)
where,
Q is a finite set of states.
Γ is a finite set of tape symbols.
B ∈ Γ is a blank symbol.
Σ is a set of input symbols and Σ ⊆ Γ – {B} (i.e. not including B).
5.2
Theoretical Computer Science Turing Machine

δ is the next move function, a mapping from Q ∞ Γ to Q ∞ Γ ∞ {L, R}.


q0 in Q is the start state.
F ⊆ Q is set of final states.
Instantaneous Description (ID):
• An ID of TM is defined in terms of the entire input string and the current state. We denote ID
of the TM, M by αqβ. Here q is the current state of M, is in Q.
• The αβ is the string in Γ* i.e. the contents of the tape upto the rightmost non-blank symbol or
the symbol to the left of the head, whichever is rightmost.
• Consider the TM shown in the Fig. 5.2. Obtain its ID.

Fig. 5.2
• The present symbol under head is a5 and the present state is q3. So a5 is written to the right of
q3. The non-blank symbols to the left of a5 is a string a1 a2 a3 a4 which is written to the left of
q 3.
• The sequence of non-blank symbols to the right of a5 is a6 a7. Thus, ID is a1 a2 a3 a4 q3 a5 a6 a7
then α = a1 a2 a3 a4 and β = a5 a6 a7 where head is scanning a5.
TM Moves:
• The δ (q, xi) induces a change in ID of the TM. Such a change in ID is called a move.
• Let x1, x2, x3 … xn is an input string to be processed and present symbol under tape head is xi.
1. Let δ (q, xi) = (p, y, L)
So the ID before processing the symbol xi will be
x1 x2 … xi–1 q xi xi+1 … xn.
So after processing xi, the ID will be
x1 x2 … xi–2 p xi–1 y xi+1 … xn
This is represented as
x1 x2 … xi–1 q xi … xn |– x1 x2 … xi–2 p xi–1 y xi+1 … xn.
2. If δ (q, xi) = (p, y, R) then the change in ID is represented by
x1 x2 … xi–1 q xi … xn |– x1 x2 … xi–1 y p xi+1 … xn.
Note: The symbol |–* denotes the reflexive transitive closure of relation |–.
5.3
Theoretical Computer Science Turing Machine

Language Acceptance by TM:


• The language accepted by M is,
L (M) = {w | w ∈ Σ* and q0 w |– αpβ, for some p in F and αβ in Γ*}.
• We assume that TM halts whenever the input is accepted. So TM has no next move when it
enters in the final state. But if string is not accepted, it may go in infinite loop.
5.1.3 Design of TM [Oct. 16, April 17, 18]

• Turing machine is a simple mathematical model which computes two types of problems.
1. Problems on language recognition (Class of languages).
2. Problems on Arithmetical operation (Class of integer functions).
• Examples on Language Recognition:
1. These examples check whether the string or word is accepted by language or not.
2. If language is accepted by Finite Automata, then it is also accepted by TM.
3. If language is accepted by pushdown automata, then it is also accepted by TM.
4. All regular sets and context free languages are accepted by TM.
5. Languages which are not regular set as well as not context free are also accepted
by TM.
• All the following examples show that TM accept all types of regular or non-regular
languages.
Example 1: Construct the TM which accept regular language a* b*.
Solution: L = {∈, a, b, aa, bb, ab, aab, ……}
Logic:
1. Read all a’s using state q0.
2. If string is over, then accept the string, otherwise scan all b’s using state q1. State q1 will
not accept any ‘a’.
We design TM = (Q, Σ, Γ, δ, q0, B, F)
where Q = {q0, q1}
Σ = {a, b}
Γ = {a, b, B} and δ is
Table 5.2
State a b B
q0 (q0, a, R) (q1, b, R) (q2, B, R)
q1 (q1, b, R) (q2, B, R)
q2 → Final state

5.4
Theoretical Computer Science Turing Machine

Fig. 5.3
The instantaneous description for “aabbb” is
q0aabbbB |– aq0abbbB |– aaq0bbbB
|– aabq1bbB |– aabbq1bB
|– aabbbq1B |– aabbbq2 → Final state
Example 2: Construct the TM which will accept the language starts with 0 and ends with 1.
Solution: L = {01, 001, 011, 01011, ……}
It is regular language since it is represented with regular expression
0 (0 + 1)* 1
Logic:
• Read first symbol 0 at state q0 and change the state q1 when only 0 is read. (1 is not the
first symbol).
• At state q1 read 0 without changing the state move to right.
• At state q1, when 1 is read, change the state to q2 and move to right.
• At state q2, when 0 is read, change the state to q1 and when 1 is read, state will not
change, move to right till end of the string occurs.

Fig. 5.4
TM is as follows:
M = ({q0, q1, q2, q3} {0, 1}, {0, 1, B}, δ, q0, B, {q3})
δ is
Table 5.3
State 0 1 B
q0 (q1, 0, R) – –
q1 (q1, 0, R) (q2, 1, R) –
q2 (q1, 0, R) (q2, 1, R) (q3, B, R)
q3 Final state

5.5
Theoretical Computer Science Turing Machine

Instantaneous description for string “0011” is


q0 0011 B |– 0q1 011 B
|– 00 q1 11 B |– 001q2 1B
|– 0011q2 B |– 0011q3 = accept
Example 3: Design a TM, M to accept the context-free language L = {0n 1n | n ≥ 1}.
Solution: Repeatedly, M replace the leftmost 0 by X, moves right to the leftmost 1, replacing
it by Y, moves left to find the rightmost X, moves one cell right to leftmost 0 and repeats the
cycle. When searching for a 1, M finds a blank instead, then M halts without accepting.
Let, Q = {q0, q1, q2, q3, q4} and Σ = {0, 1}
T = {0, 1, X, Y, B} and F = {q4}
1. State q0 is entered initially and used when each replacement of a leftmost 0 by X and
change the state to q1.
2. State q1 is used to search right, skipping over 0's and Y's until it finds the leftmost 1. If M
finds a 1, it changes it to Y entering state q2.
3. State q2 searches left for X and enters state q0 upon finding it, moving right, to the
leftmost 0, as it changes the state.
4. As M searches right in state q1 and if a B or X is encountered before a 1, then input is
rejected, either there are too many 0's or input ∉ 0* 1*.
From q0, scanning Y, state q3 is entered to scan over Y's and check that no 1's remains. If the
Y's are followed by a B, state q4 is entered and acceptance occurs, otherwise rejected.
Let input string = 0011
δ (q0, 0) = (q1, X, R) → first move
δ (q3, B) = (q4, B, R) → last move.
Therefore δ function is as follows shown in the Table 5.4.
Table 5.4
State 0 1 X Y B
q0 (q1, X, R) – – (q3, Y, R) –
q1 (q1, 0, R) (q2, Y, L) – (q1, Y, R) –
q2 (q2, 0, L) – (q0, X, R) (q2, Y, L) –
q3 – – – (q3, Y, R) (q4, B, R)
q4 final state – – – –
A computation of TM, M for a string "0011" is as follows:
q0 0011 |– X q1 011 |– X 0 q1 11 |– X q2 0 Y1
|– q2 X 0 Y1 |– X q0 0 Y1 |– X X q1 Y1
5.6
Theoretical Computer Science Turing Machine

|– XX Y q1 1 |– XX q2 YY |– X q2 X YY
|– XX q0 YY |– XX Y q3 Y |– XX YY q3
|– XX YY B q4
As TM enters in q4, the string '0011' is accepted.
Example 4: Design a TM to recognize all strings consisting of even number of 1's. Assume
that the string is made up of only 1's.
Solution: We will construct TM as follows:
1. Let q0 be the initial state. The machine M will enter q1 after reading a '1' and it will
replace 1 by B.
2. In state q1, on reading a 1, the machine goes back to q0 after replacing that 1 with B.
3. q0 will be the final state.
Thus, if number of 1's are even in input string, then after scanning all the symbols in the
input, the machine will be in q0 which is the final state and the string is accepted, otherwise
reject.
Hence, TM M = ({q0, q1}, {1}, {1, B}, δ, q0, B, {q0}), where δ function is as shown in the
Table 5.5.
Table 5.5: Transition table for example 5.4
State 1 B
q0 (q1, B, R) Accept
q1 (q0, B, R) Reject
The ID for the word "1111" will be
q0 1111 B |– B q1 111 B
|– BB q0 11 B
|– BBB q1 1 B
|– BBBB q0 B accept.
The ID for the word "111" will be
q0 111 B |– B q1 11 B
|– BB q0 1 B
|– BBB q1 B.
Here, q1 is not a final state, therefore the word "111" is rejected.
Example 5: Construct a TM for language L = {am bn | n ≥ m and m ≥ 1}.
Solution: The steps involved can be written as:
1. TM starts in state q0.
2. If the symbol a is read, then state q0 changes to q1 and replace symbol by X and move
towards right then check for 1st occurrence of b; skipping a and Y.
5.7
Theoretical Computer Science Turing Machine

3. Replace b by Y and change the state to q2 and move towards left till we get first X then
move towards right.
4. If symbol is 'a' then repeat the cycle from step 2, else if symbol is Y then move towards
right until current symbol is not equal to b.
5. If current symbol is 'b' then it means that there is atleast one extra b then check whether
remaining string consists of only b's followed by a blank symbol. Also if current symbol
is 'B' then string is accepted as m = n.
We construct TM as follows:
M = ({q0, q1, q2, q3, q4}, {a, b}, {a, b, X, Y, B}, δ, q0, B, {q5}), where δ function is as
shown in the Table 5.6.
Table 5.6
State a b X Y B
q0 (q1, X, R) (q3, Y, R)
q1 (q1, a, R) (q2, Y, L) (q2, Y, R)
q2 (q2, a, L) (q2, Y, L) (q0, X, R)
q3 (q4, b, R) (q3, Y, R) (q5, b, R)
q4 (q4, b, R) (q5, b, R)
q5 final state
The TM process for string aabbb is as follows:
q0 aabbb |– X q0 abbb |– X a q1 bbb |– X q2 a Y bb
|– q2 X a Y bb |– X q0 a Y bb |– XX q1 Y bb
|– XX Y q2 bb |– XX q2 YY b |– X q2 X YY b
|– XX q0 YY b |– XX Y q3 Y b |– XX YY q3 b
|– XX YY b q4 B |– XX YY b B q5
As TM enters in q5, the string is accepted.

Example 6: Construct a TM for language L = {an bn an | n ≥ 1}. [Oct. 17]


Solution: The TM, M works as follows:
1. TM starts in state q0.
2. If symbol a is read, then state q0 changes to q1 and a changes to X. TM moves towards
right to check first occurrence of b, skipping a's and Y's.
3. If symbol b is read, state q1 changes to q2 and replace b to Y, and moves towards right to
check first occurrence of a, skipping b's and Z's.
4. If symbol a is read, state q2 changes to q3 and a is replaced with Z and moves towards
left to check leftmost X.
5.8
Theoretical Computer Science Turing Machine

5. If leftmost X is read at state q3, state changes to q0 and X remains as it is and the cycle
repeats from step 1 again.
6. At q0 when symbol Y is read the state q4 enters and in q4 when B is read then string is
accepted. So we construct TM as follows:
M = ({q0, q1, q2, q3, q4, q5}, {a, b}, {a, b, X, Y, Z, B}, δ, q0, B, {q5}), where δ function is
shown in the Table 5.7.
Table 5.7
State a b X Y Z B
q0 (q1, X, R) – – (q4, Y, R) –
q1 (q1, a, R) (q2, Y, R) – (q1, Y, R) –
q2 (q3, Z, L) (q2, b, R) – – (q2, Z, R)
q3 (q3, a, L) (q3, b, L) (q0, X, R) (q3, Y, L) (q3, Z, L)
q4 – – – (q4, Y, R) (q4, Z, R) (q5, B, R)
q5 final state
Computation for string "aabbaa" is as follows:
q0 aabbaa |– X q1 abbaa |– X a q1 bbaa |– X a Y q2 baa |– X a Y b q2 aa
|– X a Y q3 b Z a |– X a q3 Y b Z a |– X q3 a Y b Z a |– q3 X a Y b Z a
|– X q0 a Y b Z a |– XX q1 Y b Z a |– XXYY q2 Z a
|– XX YY Z q2 a |– XX YY q3 ZZ |– XX Y q3 Y ZZ
|– XX q3 YY ZZ |– X q3 X YY ZZ |– XX q0 YY ZZ
|– XX Y q4 Y ZZ |– XX YY q4 ZZ |– XX YY Z q4 ZB
|– XX YY ZZ q4B |– XX YY ZZ B q5
String is accepted.
Example 7: Design a TM to recognize well-formedness of parenthesis.
Solution: We construct TM, M as follows:
M = ({q0, q1, q2, q3}, { (, )}, { (, ), X, Y, B}, δ, q0, B, {q3}), where δ function is shown in the
Table 5.8.
Table 5.8
State ( ) X Y B
q0 (q0, (, R) (q1, X, L) (q0, X, R) (q0, Y, R) (q2, B, L)
q1 (q0, Y, R) – (q1, X, L) (q1, Y, L) –
q2 – – (q2, X, L) (q2, Y, L) (q3, B, R)
q3 final state – – – –

5.9
Theoretical Computer Science Turing Machine

Computation of string ( ( ) ) is as follows:


q0 ( ( ) ) B |– (q0 ( )) B |– ((q0)) B |– (q1 (X) B
|– (Y q0 X) B |– (Y X q0) B |– (Y q1 XX B |– (q1Y XX B
|– q1 (Y XX B |– Y q0 Y XX B |– YY q0 XX B |– YY X q0 X B
|– YY XX q2 B |– YY XX B q3 → accept.
The string is accepted.
Example 8: Design a TM to accept L = {wcwR | w is in (0 + 1)*}.
Solution: If w is 100 then word is 100 c 001.
1. Let q0 be the start state, checking for c moving to the rightmost direction, skipping 0's
and 1's and then changing the state to q1.
2. At q1, if input 0 is read, state q2 goes left to find 0, if it gets 0 then q3 will move right to
get 0 or 1. If 1 is found then state changes to q4 and q4 moves left to find 1 and the
process is repeated till in q3 machine gets blank.
3. If in q5 machine gets blank then accept the string, otherwise reject.
We construct the TM as follows:
TM, M = ({q0, q1, q2, q3, q4, q5, q6}, {0, 1, c}, {0, 1, c, X, B}, δ, q0, B, {q6}), where
δ function is as shown in the Table 5.9.
Table 5.9
State 0 1 c X B
q0 (q0, 0, R) (q0, 1, R) (q1, c, R)
q1 (q2, X, L) (q4, X, L)
q2 (q3, X, R) (q2, c, L) (q2, X, L)
q3 (q2, X, L) (q4, X, L) (q3, c, R) (q3, X, R) (q5, B, L)
q4 – (q3, X, R) (q4, c, L) (q4, X, L)
q5 – (q5, c, L) (q5, X, L) (q6, B, R)
q6 final state
Computation for string "100c001" is as follows:
q0 100c001 |– 100 q0 c 001 B |– 100 c q1 001 B |– 100 q2 c X 01 B
|– 10 q2 0 c X 01 B |– 10 X q3 c X 01 B |– 10 X c X q3 01 B
|– 10 X c q2 XX 1 B |– 1 q2 0 X c XX 1 B |– 1 X q3 X c XX 1 B
|– 1 XX c XX q3 1 B |– 1 XX c X q4 XX B |– q4 1 XX c XXX B
|– X q3 XX c XXX B |– XXX c XXX q3 B |– XXX c XX q5 X B
|–* q5 XXX c XXX B → accept.
5.10
Theoretical Computer Science Turing Machine

Example 9: Design a TM which will replace every occurrence of a substring ‘11’ by ‘10’
over {0, 1}.
Solution: We will construct TM as follows:
1. Let q0 be the initial state. At q0 when we read 1, change the state to q2 without replacing
the symbol. At q0 when we read 0, state will not change.
2. At state q1, when we read 1 (substring 11 is found), replace 1 by 0 and again state
changes to q0. When we read B go to state q2 which is the final state.
TM is M = ({q0, q1}, {0, 1}, {0, 1, B}, δ, q0, B, {q0, q1})
where, δ is shown in Table 5.10.
Table 5.10
State 0 1 B
q0 (q0, 0, R) (q1, 1, R) (q2, B, R)
q1 (q1, 0, R) (q0, 0, R) (q2, B, R)
q2 final state
The ID for the word “011011” is
q0 011011B |– 0q0 11011B |– 01q1 1011B
|– 010q0 011B |– 0100q0 11B
|– 01001q1 1B |– 010010q0 B
|– 010010 q2 = accept

Example 10: Design a TM to recognize words in the language {an bn cn | n ≥ 1}


Solution: L = {abc, aabbcc, ......}
Logic:
1. At state q0, if we read a replace it by X and change the state to q1, move right.
2. At q1, skip all a’s and y’s, searching for b when the b is read it is replaced by Y and
change the state to q2.
3. At q2, skip all b’s and z’s, when c is read it is replaced by z, state changes to q3 and
move to left.
4. At q3, move left by skipping all a’s, b’s, y’s, z’s. When X is read, then state changes to
q0 and move to right. The cycle is repeated.
5.11
Theoretical Computer Science Turing Machine

Fig. 5.5: Steps in simulating string


The TM is as follows:
M = ({q0, q1, q2, q3, q4, q5} {a, b, c} {a, b, c, X, Y, Z, B}, δ, q0, B, {q5})
where δ is:
Table 5.11
State a b c X Y Z B
q0 (q1, X, R) – – – (q4, Y, R) –
q1 (q1, a, R) (q2, Y, R) – – (q1, Y, R) –
q2 – (q2, b, R) (q3, Z, L) – – (q2, Z, R)
q3 (q3, a, L) (q3, b, L) – (q0, X, R) (q3, Y, L) (q3, Z, L)
q4 – – – – (q4, Y, R) (q4, Z, R) (q5, B, R)
q5 final state
Here when all a’s are over, then machine enters in state q4 to check that there are no more
symbols remaining on the tape. So at q4 skipping all Y’s and Z’s, when B is read machine enters
into final state q5.
The TM process for string “aabbcc” is as follows:
q0 aabbccB |– Xq1abbccB
|– Xaq1bbccB |– XaYq2 bccB
|– XaYbq2 cc |– XaYbq3 ZcB
|– XaYq3 bZcB |– Xaq3 YbZcB
|– Xq3 aYbZcB |– q3 XaYbZcB
5.12
Theoretical Computer Science Turing Machine

|– Xq0 aYbZcB
|– XXq1 YbZcB |– XXYq1 bZcB
|– XXYYq2 ZcB
|– XXYYZq2 cB |– XXYYZq3 ZB
|– XXYYq3 ZZB
|– XXYYq3 ZZB
|– XXq3 YYZZB |– Xq3 XYYZZB
|– XXq0 YYZZB
|– XXYq4 YZZB
|– XXYYq4 ZZB
|– XXYYZZq4 B |– XXYYZZq5 = accept
Example 11: Design a TM for language {wwR | w ∈ (0 + 1)*}
Solution: Logic:
If we read first 0, then read last 0 and If we read first 1 then read last 1.

Fig. 5.6
The TM is
M = ({q0, q1, q2, q3, q4, q5, q6} {0, 1} {0, 1, X, B} {δ, q0, B, F}) where δ is as:
Table 5.12
State 0 1 X B
q0 (q1, X, R) (q4, X, R) (q6, X, R) –
q1 (q1, 0, R) (q1, 1, R) (q2, X, L) (q2, B, L)
q2 (q3, X, L) – –
q3 (q3, 0, L) (q3, 1, L) (q0, X, R) –
q4 (q4, 0, R) (q4, 1, R) (q5, X, L) (q5, B, L)
q5 – (q3, X, L) – –
q6 accept state (q6, X, R) (q6, B, R)
Consider string “0110”.
q0 0110B |– Xq1110B
|– X1q110B |– X11q10B
|– X110q1B | – X11q2 0B
|– X1q3 1XB |– Xq3 11XB
5.13
Theoretical Computer Science Turing Machine

|– q3 X11XB |– Xq0 11XB


|– XXq4 1XB |– XX1q4 XB
|– XXq5 1XB |– Xq3 XXXB
|– XXq0 XXB |– XXXq6 XB
| XXXXq6 B |– XXXXq6 = accept
Example 12: Design a TM which recognize well formednerss of parenthesis,
over {(, [, {, }, ], )}. [April 18]
Solution: L = {{ }, [ ], ( ), [( )], {( )}, ......}
Logic:
1. At state q0, find the first symbol. The symbol is {, [, or (, skip all opening parenthesis till
machine reads first leftmost closing parenthesis.
2. When machine reads ) then change it to Y and change the state to q1 and move to left.
3. If machine reads ] then change to Y and state is q2, move to left.
4. If machine reads } then change to Y and state is q3 and move to left.
5. At q1, check for ( bracket.
If machine reads ( parenthesis then change to X and state changes to q0.
6. At q2 when [ is read then make X and change state to q0.
7. At q3 when { is read then make X and change state to q0.
8. The TM is
M = ({q0, q1, q2, q3, q4} {(, [, {, }, ], )}, {(, [, {, }, ], ), X, Y, B}, δ, q0, B, {q4})
where δ is
Table 5.13
State ( [ { } ] ) X Y B
q0 (q0, (, R) (q0, [, R) (q0, {, R) (q3, Y, L) (q2, Y, L) (q1, Y, L) (q0, X, R) (q0, Y, R) (q4, B, L)

q1 (q0, X, R) (q1, X, L) (q1, Y, L)

q2 (q0, X, R) (q2, X, L) (q2, Y, L)

q3 (q0, X, R) (q3, X, L) (q3, Y, L)

q4 Final state

Consider string “[{( )}]”


q0 [{( )}] B |– [q0 {( )}] B |– [{q0 ( )}] B
|– [{(q0)}] B |– [{q1 (Y}] B
|– [{Xq0Y}] B |– [{XYq0}] B
|– [{XYq3 Y] B |– [{Xq3 YY] B
5.14
Theoretical Computer Science Turing Machine

|– [{q3 XYY] B |– [q3 {XYY] B


|– [XXq0 YY] B |– [XXYYq0] B
|– [XXYq2 YYB
|– [XXq2 YYYB |– [Xq2 XYYYB
|– q2 [XXYYYB |– Xq0 XXYYY |– XXXYYYq0 B
|– XXXYYYq4 = accept
Example 13: Design TM for language L = {am bn cm | m, n > = 0}
Solution: L = {∈, b, ac, abc, abbc, aabbcc, ......}
Logic:
• Let start state is q0. At q0, read leftmost a and make X move towards right. Skipping all
b’s, search for last ‘c’.
• When at state q1 ‘c’ is read make it Y and move to left.
• Repeat this process till all a’s and c’s are over. We need to check only equal number of
a’s and c’s. The string consists of any number of b’s in between. The TM is as follows:
Table 5.14
State a b c X Y B
q0 (q1, X, R) (q6, b, R)
q1 (q1, a, R) (q2, b, R) (q3, c, R)
q2 (q2, b, R) (q3, c, R)
q3 (q3, c, R) (q4, Y, L) (q4, B, L)
q4 (q5, Y, L)
q5 (q5, a, L) (q5, b, L) (q5, c, L) (q0, X, R)
q6 (q6, b, R) (q6, Y, R) accept
Consider the string “aabcc”
q0 aabccB |– Xq1abccB |– Xaq1bccB
|– Xabq2 ccB |– Xabcq3 cB
|– Xabccq3 B |– Xabcq4 cB
|– Xabq5 cYB |– Xq5 abcYB
|– q5 XabcYB |– Xq0 abcYB
|– XXq1bcYB |– XXbq2YB |– XXbcq3YB |– XXbq4 cYB
|– XXq5 bYYB |– Xq5 XbYYB |– XXq0 bYYB
|– XXbq6 YYB |– XXYYq6 B |– XXYYq6 = accept

5.15
Theoretical Computer Science Turing Machine

5.2 TYPES OF TM [April 16]


• The different types of Turing machine are Multi-track TM, Two-way TM, Multi-tape TM,
Non-deterministic TM.
5.2.1 Multi-track TM
• So far, we have discussed the single tap TM. Assume, single tape TM is divided into several
tracks. Tape alphabet is consists of k-tuples of tape symbols.
• Single tape and multi-tape TM differs only with the set of tape symbols. Consider the tape
symbols of multi-track TM is, Γ*.
• First, a TM program for the subroutine is written, which will have an initial state and return
state. After reaching the return state, there is a temporary halt.
• A new state is introduced by using a subroutine. When there is a need for calling the
subroutine, moves are effected to enter the initial state for the subroutine and to return to the
main program of TM, when return state is reached.
• The following examples illustrate the concept of multiple track TM using computation of
unary numbers.
Use of TM for Computation of Unary Numbers:
• A language that is accepted by a TM is said to be recursively enumerable. In addition to
being a language acceptor, the TM may be viewed as a computer of functions from integer to
integer.
• The integers are represented in unary, the integer i ≥ 0 is represented by a string 0i.
• If the function has k argument i1, i2, …, ik, then these integers are initially placed on the tape
i i i
separated by 1's, as 0 1 1 0 2 1 … 1 0 k . If the TM halts with a tape consisting of 0m for
some M, then we say that f(i1, i2 …ik) = m, where f is the function of k arguments computed
by this TM.
Example 1: Design a TM to subtract two unary numbers.
f(m, n) = m – n (proper subtraction)
i.e. f(m – n) = m – n if m ≥ n
= 0 if m < n
Solution: Unary system represents integers as shown in the Fig. 5.7.

Fig. 5.7: Unary system represents integers


The TM, M = ({q0, q1, q2 … q6}, {0, 1}, {0, 1, B}, δ, q0, B, {q6}) defined below, started with
0m 1 0n .
5.16
Theoretical Computer Science Turing Machine

M repeatedly replaces its leading 0 by blank, then searches right for 1 followed by 0 and
changes the 0 to 1. Next M moves left until it encounters a blank and then repeats the cycle. The
repetitions end if
1. Rightmost 0's changes to 1 and paired with leftmost 0's, changes to B and leaving m – n
0's on tape.
2. If n ≥ m, then m – n = 0, replace all remaining 1's and 0's by B.
The function δ is as described below in the Table 5.15.
Table 5.15
State 0 1 B
q0 (q1, B, R) (q5, B, R) –
q1 (q1, 0, R) (q2, 1, R) –
q2 (q3, 1, L) (q2, 1, R) (q4, B, L)
q3 (q3, 0, L) (q3, 1, L) (q0, B, R)
q4 (q4, 0, L) (q4, B, L) (q6, 0, R)
q5 (q5, B, R) (q5, B, R) (q6, B, R)
q6 final state
Here, if in state q0 a 1 is encountered instead of 0. In this, M enters in q5 to erase the rest of
the tape, then enters in q6 and M halts. State q4 is used for changing all 1's and B's until
encountering a B. B changes back to 0, state q6 is entered and M halts.
Computation for input 0010 (m – n = 2 – 1 = 1) is shown below.
q0 0010 |– B q1 010 |– B 0 q1 10 |– B 01 q2 0 |– B 0 q3 11 |– B q 3011 |– q3 B 011
|– B q0 011 |– BB q1 11 |– BB 1 q2 1 |– BB 11 q2 |– BB 1 q4 1 |– BB q4 1 |– B q4
|– B 0 q6 → accept.
If the input is 0100 then TM moves is shown below.
q0 0100 |– B q1 100 |– B 1 q2 00 |– B q3 110
|– q3 B 110 |– B q0 110 |– BB q5 10 |– BBB q5 0
|– BBBB q5 |– BBBBB q6
Here tape is blank i.e. output = 0.
Example 2: Design a TM to multiply two unary numbers.
Solution:
B 1 1 1 * 1 1 = B
5.17
Theoretical Computer Science Turing Machine

Let this is the initial configuration. The machine is as follows:


Table 5.16
State 1 * = X Y B
q0 (q1, X, R) (q6, *, R) – – – –
q1 (q1, 1, R) (q2, *, R) – – – –
q2 (q3, Y, R) – (q5, =, L) – – –
q3 (q3, 1, R) – (q3, =, R) – – (q4, 1, L)
q4 (q4, 1, L) – (q4, =, L) – (q2, Y, R) –
q5 (q5, 1, L) (q3, *, L) – (q0, X, L) (q5, 1, L) –
q6 (q6, 1, R) – (q7, =, R) – – –
q7 final state
Let q0 is the initial state. At q1, second number has been read and states q2, q3, q4 copy
multiplier at the end. Once copying is over, state q5 again retains the multiplier by replacing Y by
1 and the process continues till in q0 machine gets * i.e. machine goes upto = and stays in the final
state q7.
For input 11 * 11 = the computation is as follows:
B q0 11 * 11 = B |– B X q1 1 ** 11 = B |– B X 1 q1 * 11 = B
|– B X 1 * q2 11 = B |– B X 1 * Y q3 1 = B |–* B X 1 * Y 1 = q3 B
|– B X1 * Y1 q4 = 1 B |–*B X1 * q4 Y1 = 1 B |– B X 1 * Yq2 1 = 1 B
|– B X1 * YY q3 = 1 B |–* BX1 * YY = 1 q3 B |– B X 1 * YY = q4 11 B
|– B X1 * Y q4 Y = 11 B |– B X1 * YY q2 = 11 B
|– B X1 * Y q5 Y = 11 B |– B q5 X 1 * 11 = 11 B
|– B X q0 1 * 11 = 11 B |– B XX q1 * 11 = 11 B
|– B XX * q2 11 = 11 B |– B XX * Y q4 = 1111 B
|– B XX * Y q5 Y = 1111 B |– B X q5 X * 11 = 1111 B
|– B XX q0 * 11 = 1111 B |–* B XX * 11 q7 = 1111 B

5.2.2 Multi-tape TM [Oct. 16]

• A multi-tape TM has finite set of states Q, initial state q0, a set of P of tape symbols, set of
final states F, F ⊆ Q and blank symbol b ∉ ∑.
• Consider K-tapes, each divided into cells. The Fig. 5.8 represent multi-tape TM.
5.18
Theoretical Computer Science Turing Machine

Finite
control

Fig. 5.8: Multi-tape TM


• In a move:
o M entries a new state.
o On each tape, a new symbol is written in the cell under the head.
o Each tape head moves to the left or right or remains stationary. The head move perform
independently. Some move to the left, some to the right and the remaining heads do not
move.
5.2.3 Two-way TM
• Two-way infinite tape Turing machine having infinite tape is unbounded in both directions
left and right.
• Two-way infinite tape Turing machine can be simulated by one-way infinite Turing machine.
• TM can move indefinitely in either direction.
• Example:

Fig. 5.9

5.2.4 Non-Deterministic TM [Oct. 17]

• A Non-deterministic Turing Machine (NTM) differs from the deterministic TM by transition


function δ such that each state q and tape symbol X, δ (q, X) is the set of triples:
{(q1, X1, D1), (q2, X2, D2), ......, (qn, Xn, Dn)}
where, n is any finite integer.
• All the examples which we have discussed, the transition function δ has a unique ID on each
input symbol. So all are deterministic TM.
5.19
Theoretical Computer Science Turing Machine

• Non-deterministic TM represents ID with tree structure.


ID1
ID2 ID3
• A string ω is said to be accepted by NTM if there exists a sequence of moves starting from
the initial ID to an accepting condition.
o If MN is a non-deterministic TM, then there is a deterministic TM (DTM), MD such that
L (MN) = L (MD)
o So language accepted by NTM and DTM are same.
Examples of NTM:
• The language of all strings of 0’s and 1’s that have some string of length 100 that repeats, not
necessarily consecutively. Formally this language is the set of strings of 0’s and 1’s of the
form ωxyxz, where |x| = 100, ω, y, z are of any length.

5.3 LINEAR BOUNDED AUTOMATION (LBA) [April 16, 19, Oct. 16]
• The machine to accept context-sensitive language is Linear Bounded Automation (LBA).
• An LBA is a special type of Turing machine with restricted tape space. The name ‘linear
bounded’ suggests that the machine is linearly bounded.
Difference between TM and LBA: [Oct. 16, 18]
1. In TM the storage is not restricted on tape. Tape head can move to both directions at
infinite storage. In LBA, the input string tape space is the only tape space allowed to use.
So storage is restricted in size.
2. In LBA, linear function is used to restrict (to bound) the length of the tape.
3. In LBA, the computation is done between end markers. In TM, no endmarkers are
present, end of string is considered when blank (B) occurs.
• LBA is a non-deterministic TM which has a single tape whose length is not infinite but
bounded by a linear function of the length of the input string.
Definition: LBA, M = (Q, Σ, Γ, δ, q0, B, ⊄, $, F)
where Q = Finite set of states
Σ = Input alphabet U {⊄, $}
Γ = Tape alphabet
q0, F, b are same as TM basic model.
⊄ and $ are two special symbols ⊄, $ ∈ Σ.
• Both symbols are end markers. ⊄ is called the left-end marker which is entered in the
leftmost cell of the input tape and prevents the R/W head from getting off the left-end of the
tape.
5.20
Theoretical Computer Science Turing Machine

• The $ is called the right-end marker which is entered in the rightmost cell of the input tape
and prevents the R/W head from getting off the right end of the tape. Both the end markers
should not appear on any other cell within the input tape, and the R/W head should not print
any other symbol over both the end markers.
5.3.1 Basic model of LBA
• The Fig. 5.10 shows the model of Linear Bounded Automata.

Fig. 5.10: LBA Model


• There are two tapes: input tape and working tape. On the input tape, the head never prints
and never moves to the left. On the working tape the head can modify the contents in any
way, without any restriction.
• The mapping function δ of LBA is denoted by → (q, w, k) where q ∈ Q, w ∈ Γ, k is some
integer between 1 and n.
o If R/W head moves to left, k changes to k – 1.
o If R/W head moves to right, k changes to k + 1.
• The language accepted by LBA is defined as the set,
{w ∈ (Σ – {⊄, $})* | (q0, ⊄ w $, 1) |*– (q, α, i)
for some q ∈ F and
for some integer i between 1 to n}

5.3.2 Context-Sensitive Grammar (CSG)


• The Context Sensitive Grammar (CSG) is used to represent Context Sensitive Language
(CSL).
• The CSG may have more than one symbol on the left hand side of their production rules. The
language that can be defined by context-sensitive grammar is called CSL.
• The set of strings accepted by non-deterministic LBA is the set of strings generated by the
context-sensitive grammars (CSG), excluding the null strings.
• If L is a context-sensitive language, then L is accepted by a LBA. The converse is also true.
5.21
Theoretical Computer Science Turing Machine

• Examples of language accepted by LBA:


1. L = {an bn cn | n ≥ 1}
2. L = {an! | n ≥ 1}
EXAMPLES
Example 1: Design TM to check palindrome over {a, b}.
Solution: We have to check odd and even palindrome.
Table 5.17
State a b X B
q0 (q1, X, R) (q4, X, R) (q6, X, R) –
q1 (q1, a, R) (q1, b, R) (q2, X, L) (q2, B, L)
q2 (q3, X, L) – (q6, X, R) –
q3 (q3, a, L) (q3, b, L) (q0, X, R) –
q4 (q4, a, R) (q4, b, R) (q5, X, L) (q5, B, L)
q5 – (q3, X, L) (q6, X, L) –
q6 Final state (q6, X, L)
Consider the string “aba”
q0 aba B → Xq1 baB |– Xbq1 aB
|– Xbaq1 B |– Xbq2 aB
|– Xq3 bXB |– q3 XbXB
|– Xq0 bXB |– XXq4 XB
|– Xq5 XXB |– q6 XXXB → accept
Example 2: Construct TM which will add three unary numbers.
Solution:
B 1 1 1 B 1 1 B 1 1 1 B
Logic:
1. Skip all 1’s till B while moving to right.
2. Change B to 1 and move to right.
3. Skip all 1’s till we get 2nd B.
4. Change B to 1 and move right to 3rd blank.
5. When 3rd B occurs move to left.
6. Two extra 1’s are changed to blanks.
5.22
Theoretical Computer Science Turing Machine

The TM is as follows:
Table 5.18
State 1 B
q0 (q0, 1, R) (q1, 1, R)
q1 (q1, 1, R) (q2, 1, R)
q2 (q2, 1, R) (q3, B, L)
q3 (q4, B, L) –
q4 (q5, B, L) –
q5 ← accept
Example 3: Construct TM which will subtract two unary numbers.
Solution:

Fig. 5.11
f (m, n) = m – n ∀ m ≥ n (proper subtraction)
Logic:
1. Skip all 1’s till B at state q0.
2. When B occurs change the state and search for last B. Change state to q2.
3. Change 1 to B and move to left at q2.
4. Repeat process till all 1’s are over.
Table 5.19
State 1 B
q0 (q0, 1, R) (q1, B, R)
q1 (q1, 1, R) (q2, B, L)
q2 (q3, B, L) (q7, B, L)
q3 (q3, 1, L) (q4, B, L)
q4 (q4, 1, L) (q5, B, R)
q5 (q0, B, R) (q6, 1, L)
q6 – (q7, B, L)
q7 ← accept
Output is,
B B B 1 1 1 B B B B ---
5.23
Theoretical Computer Science Turing Machine

Example 4: Construct TM accepting language,


L = {an, bm, cm + n | m, n ≥ 0}
Solution:
Logic:
1. Scan leftmost a or b and make X.
2. Scan rightmost c and make Y.
3. Find rightmost X.
4. Repeat the process.
5. q3 is reject state and q4 is used for checking extra a’s or b’s or c’s.
Table 5.20
State a b c X Y B
q0 (q1, X, R) (q1, X, R) – – (q4, b, R) –
q1 (q1, a, R) (q1, b, R) (q1, c, R) – (q2, Y, L) (q2, B, L)
q2 – – (q3, Y, L) – – –
q3 (q3, a, L) (q3, b, L) (q3, c, L) (q0, X, R) – –
q4 – – – – (q4, Y, R) (q5, B, L)
q5 ← accept
Example 5: Construct TM for language
L = {am bn | n ≥ m and m ≥ 1} [April 16]
Solution:
Table 5.21
State a b X Y B
q0 (q1, X, R) – – (q3, Y, L) –
q1 (q1, a, R) (q2, Y, L) – (q1, Y, R) –
q2 (q2, a, L) – (q0, X, R) (q2, Y, L) –
q3 – (q3, b, R) – (q3, Y, R) (q4, B, R)
q4 ← accept
Example 6: Design a TM which computes a language L = {02n 1n | n ≥ 1}
Solution:

Fig. 5.12
5.24
Theoretical Computer Science Turing Machine

Logic:
• At state q0, when 1st 0 is read, change the state q1 and 0 to X, move right.
• At q1, when 0 is read, change the state to q2 and 0 to X, move right.
• At q2 skip all 0’s till machine reads 1. State is changed to q3 and 1 changes to Y.
• At q3, move to left, skipping all 0’s till machine reads X. Change the state to q0.
This process repeats.
Table 5.22
State 0 1 X Y B
q0 (q1, X, R) – – (q4, Y, R) –
q1 (q2, X, R) – – – –
q2 (q2, 0, R) (q3, Y, L) – (q2, Y, R) –
q3 (q3, 0, L) – (q0, X, R) (q3, Y, L) –
q4 – – – (q4, Y, R) accept
Consider instantaneous description for string “001”.
q0 001B |– Xq101B
|– XXq2 1B
|– Xq3 XYB |– XXq0YB
|– XXYq4B |– XXYq4 → accept
Example 7: Design a TM that computes remainder when the number is divisible by 3.
Solution: First we draw finite automata for the machine and then we will design TM.
Finite automata is,

Fig. 5.13
Here we get 3 remainders 0, 1 and 2. So machine has 3 states where we get the outputs 0, 1
and 2.
Let we replace (0, 3, 6, 9) by X
(1, 4, 7) by Y
(2, 5, 8) by Z
5.25
Theoretical Computer Science Turing Machine

The TM is (using above FA):


Table 5.23
State X Y Z B
(0, 3, 6, 9) (1, 4, 7) (2, 5, 8)
q0 (q0, X, R) (q1, Y, R) (q2, Z, R) (q3, B, R)
q1 (q1, X, R) (q2, Y, R) (q0, Z, R) (q4, B, R)
q2 (q2, X, R) (q0, Y, R) (q1, Z, R) (q5, B, R)
q3 – – – (q6, 0, R)
q4 – – – (q6, 1, R)
q5 – – – (q6, 1, R)
q6 Final state
Consider the string “241” as input
q0 241B |– 2q2 41B |– 24q0 1B
|– 241q1 B |– 241Bq4 B
|– 241B1q6 = accept
↑ ↑
number remainder
Example 8: Design a TM to compute 2's complement of a given binary number.
Solution: If input is 1011, its 2's complement is 0101. If input is 1011100, its 2's complement
is 0100100. We clearly see that from the rightmost 1 leaving that bit every bit on its left is
negated i.e. 0 becomes 1 and 1 becomes 0.
We construct TM as follows:
TM, M = ({q0, q1, q2, q3}, {0, 1}, {0, 1, B}, δ, q0, B, {q3}), where δ function is as shown in
the Table 5.24.
Table 5.24
State 0 1 B
q0 (q0, 0, R) (q0, 1, R) (q1, B, L)
q1 (q1, 0, L) (q2, 1, L)
q2 (q2, 1, L) (q2, 0, L) (q3, B, R)
q3 final state
Computation of string 1011 is as follows:
q0 1011 B |– 1 q0 011 B |– 10 q1 11 B |– 101 q0 1 B
|– 1011 q0 B |– 101 q1 1 B |– 10 q2 11 B |– 1 q2 001 B
|– q2 1101 B |– q2 B 0101 B |– B q3 0101 B → accept.
5.26
Theoretical Computer Science Turing Machine

State q0 moves the machine to the right end of the input. State q1 moves the machine back till
it reaches to rightmost 1. In q2 machine moves back complementing each input symbol, till it
reaches blank and we get 2's complement of string.

PRACTICE QUESTIONS
Q.I Multiple Choice Questions:
1. Which is an accepting device which accepts the languages (recursively enumerable set)
generated by type 0 grammars and was invented in 1936 by Alan Turing?
(a) Turing Machine (TM) (b) Finite Machine (FM)
(c) Grammar Machine (GM) (d) Language Machine (LM)
2. The TM, is defined by how many tuples?
(a) 5 (b) 8
(c) 7 (d) 6
3. A TM consists of,
(a) input tape (b) read-write (R/W) head
(c) finite control (d) All of the mentioned
4. Which TM has multiple tapes where each tape is accessed with a separate head?
(a) Multi-tape (b) Multi-track
(c) nondeterministic TM (d) Two-way TM
5. Which is a multi-track non-deterministic TM with a tape of some bounded finite
length?
(a) Linear Finite Automaton (LFA) (b) Linear Bounded Automaton (LBA)
(c) Context Free Automaton (CFA) (d) None of the mentioned
6. A multi-track TM can be formally described as,
(a) 6 tuples (b) 8 tuples
(c) 7 tuples (d) 5 tuples
7. The mathematical notation for TM is,
(a) M = (Q, Σ, Γ, δ, q0, F) (b) M = (Q, Σ, Γ, δ, q0, B, ⊄, $, F)
(c) M = (Q, Σ, Γ, δ, q0, B, F) (d) None of the mentioned
8. The ID of the TM remembers the following at a given instance of time:
(a) The cell currently being scanned by the read–write head
(b) The state of the machine, and
(c) The contents of all the cells of the tape, starting from the rightmost cell up to at
least the last cell, containing a non-blank symbol and containing all cells up to the
cell being scanned
(d) All of the mentioned
5.27
Theoretical Computer Science Turing Machine

9. The TM accepts all the language even though they are recursively,
(a) contect-free (b) regular
(c) enumerable (d) All of the mentioned
Answers
1. (a) 2. (c) 3. (d) 4. (a) 5. (b) 6. (a) 7. (c) 8. (d) 9. (c)
Q.II Fill in the Blanks:
1. A ______ is a mathematical model consists of an infinite length tape divided into cells
on which input is given.
2. In TM each cell can store only ______ symbol.
3. A ______ is a nondetelministic TM which has a single tape whose length is not infinite
but bounded by a linear function of the length of the input string.
4. A Turing Machine (TM) accepts a language if it enters into a final state for any input
string ω and a language is recursively ______ (generated by Type-0 grammar) if it is
accepted by a TM.
5. The Turing machine was proposed by A.M. Turing in 1936 is the machine format of
______ language i.e., all types of languages are accepted by the TM.
6. The instantaneous description (ID) of a Turing machine remembers the contents of all
cells from the rightmost to at least the leftmost, the cell currently being scanned by the
read–write head and the state of the machine at a given ______ of time.
7. The Turing machine, in short TM, is defined by 7 tuples M = ______.
8. In ______multi-track TM a single tape head reads n symbols from n tracks at one step. It
accepts recursively enumerable languages like a normal single-track single-tape Turing
Machine accepts.
9. The computation of a non-deterministic TM is a tree of configurations that can be
reached from the ______ configuration.
10. According to the ______ hierarchy, type 0 language is called as unrestricted language
(URG)
11. A linear bounded automaton can be defined as an 8-tuple M = ______.
Answers
1. Turing 2. one 3. Linear Bounded Automaton 4. enumerable
Machine (TM) (LBA)
5. unrestricted 6. instance 7. (Q, Σ, Γ, δ, q0, B, F) 8. multi-track
9. start 10. Chomsky 11. (Q, Σ, Γ, δ, q0, B, ⊄, $, F)

5.28
Theoretical Computer Science Turing Machine

Q.III State True or False:


1. As an automaton, the Turing machine is the most general model which accepts type-0
languages.
2. In LBA because a linear function is used to restrict (to bound) the length of the tape.
3. The mechanical diagram of the Turing machine consists of the input tape, the fi nite
control and the read–write head.
4. A TM can be formally described as a 8-tuples.
5. TM is a machine (automaton) capable of enumerating some arbitrary subset of valid
strings of an alphabet; these strings are part of a recursively enumerable set.
6. The set of strings accepted by nondeterministic LBA is the set of strings generated by the
context-sensitive grammars, excluding the null strings.
7. If the TM reaches the final state, the input string is accepted, otherwise rejected.
8. A language is recursive if it is decided by a Turing Machine (TM).
9. Multi-track TM, a specific type of Multi-tape Turing machine, contain multiple tracks
but just one tape head reads and writes on all tracks.
10. A Multi-tape Turing machine can be formally described as a 7 tuple.
11. In a Non-Deterministic Turing Machine, for every state and symbol, there are a group of
actions the TM can have. So, here the transitions are not deterministic. T
12. A linear bounded automaton is a multi-track deterministic Turing machine with a tape of
some bounded finite length.
13. A context-sensitive language is equivalent to a linear bounded nondeterministic Turing
machine, also called a linear bounded automaton.
14. All types of languages are accepted by TM.
15. Computer is an example of TM.
16. Text editor is an example of TM.
17. A TM has a tape of infinite length on which it can perform read and write operations.
18. For TM, tape head moves to only right side.
19. CSL's can be accepted by LBA.
Answers
1. (T) 2. (T) 3. (T) 4. (F) 5. (T) 6. (T) 7. (T) 8. (T) 9. (T) 10. (F)
11. (T) 12. (F) 13. (T) 14. (T) 15. (T) 16. (F) 17. (T) 18. (F) 19. (T)
5.29
Theoretical Computer Science Turing Machine

Q.IV Answer the following Questions:


(A) Short Answer Questions:
1. Define Turing machine.
2. Define LBA.
3. What is two-way TM?
4. Define nondeterministic TM.
5. List problems for language recognizers.
6. What is multi-tape TM?
7. Define multi-track TM.
8. List types of TM
9. Compare multi-tape TM and multi-track TM (any two points).
(B) Long Answer Questions:
1. What is TM? How it works? Explain its model diagrammatically.
2. What is LBA? Describe in detail.
3. Construct a TM to accept the following languages:
(i) {0n 1n 0n | n ≥ 1}
(ii) {wwR | w is in (0 + 1)*}
(iii) L = (a + b)* (abc)
(iv) The set of strings with an equal number of 0's and 1's.
(v) {0p 1q 2p+q | p, q ≥ 1}
4. Design a TM to perform proper subtraction of two unary numbers.
5. Design a TM to check palindrome.
6. Design a TM which recognizes the following languages:
(i) L = {0m 1m 2m+2 | m ≥ 1}
(ii) L = {an b an | n ≥ 0}
(iii) L = {an bn an | n ≥ 1}
7. Construct a TM for accepting well-formed parenthesis over {(,)}.
8. Construct a TM which recognizes a language which contains equal number of a's and b's.
9. With the help of diagram basic model of LBA.
10. Write a shot note on: Language accepted by TM.
11. What is non-deterministic TM? Explain with example.
12. With the help of example explain multi-tape TM.
5.30
Theoretical Computer Science Turing Machine

13. What is two-way TM? Describe with diagram and example.


14. What is multi-track TM? State its use. Also explain with example.
15. Construct TM for L = {an bm cn | n, m ≥ 1}.
UNIVERSITY QUESTIONS AND ANSWERS
April 2016
1. Define tuples of LBA. [1 M]
Ans. Refer to Section 5.3.
2. Explain the types of Turning Machine (TM). [4 M]
Ans. Refer to Section 5.2.

October 2016
1. Define Multi-tape Turing Machine. [1 M]
Ans. Refer to Section 5.2.2.
2. Construct a TM for a language L, where L = {am + nbmcn | m, n ≥ 1} [5 M]
Ans. Refer to Section 5.1.3, Examples.
3. Differentiate between TM and LBA. [3 M]
Ans. Refer to Page 5.20.

April 2017
1. Write the tuples of Turning Machine. [1 M]
Ans. Refer to Section 5.1.2.
2. Construct a TM for L = [wcwR | w ∈ (a + b)*]. [5 M]
Ans. Refer to Section 5.1.3, Examples.

October 2017
1. Define non-deterministic Turning Machine. [1 M]
Ans. Refer to Section 5.2.4.

April 2018
1. Design a TM to recognize well-formedness of parenthesis( ). [4 M]
Ans. Refer to Page 5.1.3.

October 2018
1. Define Turing Machine (T.M.). [1 M]
Ans. Refer to Section 5.1.2.
5.31
Theoretical Computer Science Turing Machine

2. State two differences between TM and LBA. [1 M]


Ans. Refer to Page 5.20.

April 2019
1. Write the tuples of LBA. [1 M]
Ans. Refer to Section 5.3.
2. Construct TM for language: L = {am bn | n > m, m > 1} [5 M]
Ans. Refer to Section 5.1.3, Examples.

5.32

You might also like