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

Geeksforgeeks Theory of Computation and Automata Lecture Notes

The document provides an overview of the Theory of Computation and Automata, detailing key concepts such as automata, languages, and the Chomsky hierarchy of grammars. It explains finite automata, including deterministic and nondeterministic types, as well as regular expressions and their relation to regular languages. Additionally, it discusses the design of finite automata from regular expressions and includes examples and exercises for better understanding.
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)
80 views

Geeksforgeeks Theory of Computation and Automata Lecture Notes

The document provides an overview of the Theory of Computation and Automata, detailing key concepts such as automata, languages, and the Chomsky hierarchy of grammars. It explains finite automata, including deterministic and nondeterministic types, as well as regular expressions and their relation to regular languages. Additionally, it discusses the design of finite automata from regular expressions and includes examples and exercises for better understanding.
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/ 227

GeeksForGeeks Theory Of Computation

and Automata Lecture Notes


From: https://www.geeksforgeeks.org/theory-of-computation-automata-
tutorials/
Introduction
Introduction of Theory of Computation
Automata theory (also known as Theory Of Computation) is a
theoretical branch of Computer Science and Mathematics, which mainly
deals with the logic of computation with respect to simple machines,
referred to as automata.

Automata* enables the scientists to understand how machines compute


the functions and solve problems. The main motivation behind
developing Automata Theory was to develop methods to describe and
analyse the dynamic behavior of discrete systems.

Automata is originated from the word “Automaton” which is closely


related to “Automation”.

Now, let’s understand the basic terminologies, which are important and
frequently used in Theory of Computation.

Symbol: Symbol is the smallest building block, which can be any


alphabet, letter or any picture.

Alphabets (Σ): Alphabets are set of symbols, which are always finite.
String: String is a finite sequence of symbols from some alphabet.
String is generally denoted as w and length of a string is denoted as
|w|.

Empty string is the string with


zero occurrence of symbols,
represented as ε.

Number of Strings (of length 2)


that can be generated over the alphabet {a, b} -
- -
a a
a b
b a
b b

Length of String |w| = 2


Number of Strings = 4

Conclusion:
For alphabet {a, b} with length n, number of
strings can be generated = 2n.

Note – If the number of Σ’s is represented by |Σ|, then number of


strings of length n, possible over Σ is |Σ|n.

Language: A language is a set of strings, chosen form some Σ* or we


can say- ‘A language is a subset of Σ* ‘. A language which can be
formed over ‘ Σ ‘ can be Finite or Infinite.

Powers of ‘ Σ ‘ :
Say Σ = {a,b} then
Σ0 = Set of all strings over Σ of length 0. {ε}
Σ1 = Set of all strings over Σ of length 1. {a, b}
Σ2 = Set of all strings over Σ of length 2. {aa, ab, ba, bb}
i.e. |Σ2|= 4 and Similarly, |Σ3| = 8

Σ* is a Universal Set.
Σ* = Σ0 U Σ1 U Σ2 ..........
= {ε} U {a, b} U {aa, ab, ba, bb}
= ............. //infinite language.

References –
cs.stanford.edu
Wikipedia
abhishek1
WorKing to be bEst veRsion of MySelf

If you like GeeksforGeeks and would like to contribute, you can also
write an article using contribute.geeksforgeeks.org or mail your article
to [email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any
issue with the above content.
Chomsky Hierarchy
According to chomsky hierarchy, grammars are divided of 4 types:

Type 0 known as unrestricted grammar.


Type 1 known as context sensitive grammar.
Type 2 known as context free grammar.
Type 3 Regular Grammar.

Type 0 ( Unrestricted Grammar )

In Type 0
Type-0 grammars include all formal grammars. Type 0 grammar
language are recognized by turing machine. These languages are also
known as the recursively enumerable languages.

Grammar Production in the form of


where

is ( V + T)* V ( V + T)*
V : Variables
T : Terminals.

is ( V + T )*.
In type 0 there must be at least one variable on Left side of production.

For example,

Sab –> ba
A –> S.

Here, Variables are S, A and Terminals a, b.

Type 1 (Context Sensitive )


Type-1 grammars generate the context-sensitive languages. The
language generated by the grammar are recognized by the Linear Bound
Automata
In Type 1
1. First of all Type 1 grammar should be Type 0.
2. Grammar Production in the form of

| | <= | |

i.e count of symbol in is less than or equal to


For Example,
S –> AB
AB –> abc
B –> b

Type 2 ( Context Free )


Type-2 grammars generate the context-free languages. The language
generated by the grammar is recognized by a Non Deterministic Push
down Automata. Type-2 grammars generate the context-free languages.
In Type 2,
1. First of all it should be Type 1.
2. Left hand side of production can have only one variable.

| | = 1.

Their is no restriction on .

For example,
S –> AB
A –> a
B –> b

Type 3 (Regular Grammar)


Type-3 grammars generate the regular languages.These languages are
exactly all languages that can be decided by a finite state automaton.
Type 3 is most restricted form of grammar.
Type 3 should be in the given form only :

V –> VT* / T*.


(or)
V –> T*V /T*

for example :
S –> ab.

REFERENCES
https://en.wikipedia.org/wiki/Chomsky_hierarchy

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Regular Languages and Finite Automata
Finite Automata Introduction
Finite Automata(FA) is the simplest machine to recognize patterns.

A Finite Automata consists of the following :

Q : Finite set of states.


∑ : set of Input Symbols.
q : Initial state.
F : set of Final States.
δ : Transition Function.

Formal specification of machine is


{ Q, ∑, q, F, δ }.

FA is characterized into two types:

1) Deterministic Finite Automata (DFA)

DFA consists of 5 tuples {Q, ∑, q, F, δ}.


Q : set of all states.
∑ : set of input symbols. ( Symbols which machine takes as inp
q : Initial state. ( Starting state of a machine )
F : set of final state.
δ : Transition Function, defined as δ : Q X ∑ --> Q.

In a DFA, for a particular input character, machine goes to one state only.
A transition function is defined on every state for every input symbol. Also
in DFA null (or ε) move is not allowe, i.e., DFA can not change state
without any input character.

For example, below DFA with ∑ = {0, 1} accepts all strings ending with 0.

One important thing to note is, there can be many possible DFAs for a
pattern. A DFA with minimum number of states is generally preferred.

2) Nondeterministic Finite Automata(NFA)


NFA is similar to DFA except following additional features:
1. Null (or ε) move is allowed i.e., it can move forward without reading
symbols.
2. Ability to transit to any number of states for a particular input.
However, these above features don’t add any power to NFA. If we
compare both in terms of power, both are equivalent.

Due to above additional features, NFA has a different transition function,


rest is same as DFA.

δ: Transition Function
δ: Q X (∑ U ϵ ) --> 2 ^ Q.

As you can see in transition function for any input including null (or
&epsilon), NFA can go to any state number of states.
For example, below is a NFA for above problem
One important thing to note is, in NFA, if any path for an input string
leads to a final state, then the input string accepted. For example, in
above NFA, there are multiple paths for input string “00”. Since, one of
the paths leads to a final state, “00” is accepted by above NFA.

Some Important Points:


1. Every DFA is NFA but not vice versa.
2. Both NFA and DFA have same power and each NFA can be translated
into a DFA.
3. There can be multiple final states in both DFA and NFA.
3. NFA is more of a theoretical concept.
4. DFA is used in Lexical Analysis in Compiler.

See Quiz on Regular Expression and Finite Automata.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Regular Expressions, Regular Grammar
and Regular Languages
As discussed in Chomsky Hierarchy, Regular Languages are the most
restricted types of languages and are accepted by finite automata.

Regular Expressions
Regular Expressions are used to denote regular languages. An
expression is regular if:

ɸ is a regular expression for regular language ɸ.


ɛ is a regular expression for regular language {ɛ}.
If a ∈ Σ (Σ represents the input alphabet), a is regular expression with
language {a}.
If a and b are regular expression, a + b is also a regular expression
with language {a,b}.
If a and b are regular expression, ab (concatenation of a and b) is
also regular.
If a is regular expression, a* (0 or more times a) is also regular.

Regular
Regular Languages
Expression

set of vovels (a∪e∪i∪o∪u) {a, e, i, o, u}

a followed by 0 or {a, ab, bb, abbb,


(a.b*)
more b abbbb,….}
{ ε , a ,aou, aiou, b,
abcd…..} where ε
any no. of vowels v*.c* ( where v – represent empty
followed by any no. vowels and c –
of consonants consonants) string (in case 0
vowels and o
consonants )

Regular Grammar : A grammar is regular if it has rules of form A -> a


or A -> aB or A -> ɛ where ɛ is a special symbol called NULL.

Regular Languages : A language is regular if it can be expressed in


terms of regular expression.

Closure Properties of Regular Languages


Union : If L1 and If L2 are two regular languages, their union L1 ∪ L2
will also be regular. For example, L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1 ∪ L2 = {an ∪ bn | n ≥ 0} is also regular.
Intersection : If L1 and If L2 are two regular languages, their
intersection L1 ∩ L2 will also be regular. For example,
L1= {am bn | n ≥ 0 and m ≥ 0} and L2= {am bn ∪ bn am | n ≥ 0 and m ≥
0}
L3 = L1 ∩ L2 = {am bn | n ≥ 0 and m ≥ 0} is also regular.
Concatenation : If L1 and If L2 are two regular languages, their
concatenation L1.L2 will also be regular. For example,
L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1.L2 = {am . bn | m ≥ 0 and n ≥ 0} is also regular.
Kleene Closure : If L1 is a regular language, its Kleene closure L1*
will also be regular. For example,
L1 = (a ∪ b)
L1* = (a ∪ b)*
Complement : If L(G) is regular language, its complement L’(G) will
also be regular. Complement of a language can be found by
subtracting strings which are in L(G) from all possible strings. For
example,
L(G) = {an | n > 3}
L’(G) = {an | n <= 3}

Note : Two regular expressions are equivalent if languages generated


by them are same. For example, (a+b*)* and (a+b)* generate same
language. Every string which is generated by (a+b*)* is also
generated by (a+b)* and vice versa.

How to solve problems on regular expression and regular


languages?

Question 1 : Which one of the following languages over the alphabet


{0,1} is described by the regular expression?
(0+1)*0(0+1)*0(0+1)*
(A) The set of all strings containing the substring 00.
(B) The set of all strings containing at most two 0’s.
(C) The set of all strings containing at least two 0’s.
(D) The set of all strings that begin and end with either 0 or 1.

Solution : Option A says that it must have substring 00. But 10101 is
also a part of language but it does not contain 00 as substring. So it is
not correct option.
Option B says that it can have maximum two 0’s but 00000 is also a
part of language. So it is not correct option.
Option C says that it must contain atleast two 0. In regular expression,
two 0 are present. So this is correct option.
Option D says that it contains all strings that begin and end with either
0 or 1. But it can generate strings which start with 0 and end with 1 or
vice versa as well. So it is not correct.

Question 2 : Which of the following languages is generated by given


grammar?
S -> aS | bS | ∊
(A) {an bm | n,m ≥ 0}
(B) {w ∈ {a,b}* | w has equal number of a’s and b’s}
(C) {an | n ≥ 0} ∪ {bn | n ≥ 0} ∪ {an bn | n ≥ 0}
(D) {a,b}*

Solution : Option (A) says that it will have 0 or more a followed by 0


or more b. But S -> bS => baS => ba is also a part of language. So
(A) is not correct.
Option (B) says that it will have equal no. of a’s and b’s. But But S ->
bS => b is also a part of language. So (B) is not correct.
Option (C) says either it will have 0 or more a’s or 0 or more b’s or a’s
followed by b’s. But as shown in option (A), ba is also part of
language. So (C) is not correct.
Option (D) says it can have any number of a’s and any numbers of b’s
in any order. So (D) is correct.

Question 3 : The regular expression 0*(10*)* denotes the same set


as
(A) (1*0)*1*
(B) 0 + (0 + 10)*
(C) (0 + 1)* 10(0 + 1)*
(D) none of these

Solution : Two regular expressions are equivalent if languages


generated by them are same.
Option (A) can generate 101 but 0*(10*)* cannot. So they are not
equivalent.
Option (B) can generate 0100 but 0*(10*)* cannot. So they are not
equivalent.
Option (C) will have 10 as substring but 0*(10*)* may or may not. So
they are not equivalent.

This article has been contributed by Sonal Tuteja.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any
issue with the above content.
Designing Finite Automata from Regular
Expression
In this article, we will see some popular regular expressions and how we
can convert them to finite automata.

Even number of a’s : The regular expression for even number of a’s
is (b|ab*ab*)*. We can construct a finite automata as shown in Figure
1.

The above automata will accept all strings which have even number
of a’s. For zero a’s, it will be in q0 which is final state. For one ‘a’, it
will go from q0 to q1 and the string will not be accepted. For two a’s at
any positions, it will go from q0 to q1 for 1st ‘a’ and q1 to q0 for
second ‘a’. So, it will accept all strings with even number of a’s.

String with ‘ab’ as substring : The regular expression for strings


with ‘ab’ as substring is (a|b)*ab(a|b)*. We can construct finite
automata as shown in Figure 2.
The above automata will accept all string which have ‘ab’ as
substring. The automata will remain in initial state q0 for b’s. It will
move to q1 after reading ‘a’ and remain in same state for all ‘a’
afterwards. Then it will move to q2 if ‘b’ is read. That means, the string
has read ‘ab’ as substring if it reaches q2.

String with count of ‘a’ divisible by 3 : The regular expression for


strings with count of a divisible by 3 is {a3n | n >= 0}. We can construct
automata as shown in Figure 3.
The above automata will accept all string of form a3n. The automata
will remain in initial state q0 for ɛ and it will be accepted. For string
‘aaa’, it will move from q0 to q1 then q1 to q2 and then q2 to q0. For
every set of three a’s, it will come to q0, hence accepted. Otherwise, it
will be in q1 or q2, hence rejected.

Note : If we want to design a finite automata with number of a’s as


3n+1, same automata can be used with final state as q1 instead of
q0.
If we want to design a finite automata with language {akn | n >= 0}, k
states are required. We have used k = 3 in our example.

Binary numbers divisible by 3 : The regular expression for binary


numbers which are divisible by three is (0|1(01*0)*1)*. The examples
of binary number divisible by 3 are 0, 011, 110, 1001, 1100, 1111,
10010 etc. The DFA corresponding to binary number divisible by 3
can be shown in Figure 4.

The above automata will accept all binary numbers divisible by 3. For
1001, the automata will go from q0 to q1, then q1 to q2, then q2 to q1
and finally q2 to q0, hence accepted. For 0111, the automata will go
from q0 to q0, then q0 to q1, then q1 to q0 and finally q0 to q1, hence
rejected.

String with regular expression (111 + 11111)* : The string accepted


using this regular expression will have 3, 5, 6(111 twice), 8 (11111
once and 111 once), 9 (111 thrice), 10 (11111 twice) and all other
counts of 1 afterwards. The DFA corresponding to given regular
expression is given in Figure 5.
Question : What will be the minimum number of states for strings with
odd number of a’s?
Solution : The regular expression for odd number of a is b*ab*(ab*ab*)*
and corresponding automata is given in Figure 6 and minimum number of
states are 2.

This article has been contributed by Sonal Tuteja.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Star Height of Regular Expression and
Regular Language
The star height relates to the field of theoretical of computation (TOC). It
is used to indicate the structural complexity of regular expressions and
regular languages. Here complexity, relates to the maximum nesting
depth of Kleene stars present in a regular expression.
It may be noted here that a regular language may be represented by
regular expressions that are not unique, yet equivalent. These regular
expressions may have different star heights depending on their structural
complexity(i.e nesting). But star height of a regular language is a
unique number and is equal to the least star height of any regular
expression representing that language. In this context generalized
star height is an appropriate terminology, that defines the minimum
nesting depth of Kleene stars to describe the language by means of a
generalized regular expression. For example:

The language “aba” over the set of alphabets {a, b} can be generated
using regular expressions,

(a + b)* ...... (1) Star height = 1


(a* b*)* ...... (2) Star height = 2

But we consider the least star height. Therefore the star height of the
regular language “aba” is one.

Star height is also defined for regular expressions as the maximum


nesting depth of Kleene stars appearing in that expression. In order
to state star height, “h” of a regular expression formally, one can write as,
h( ) = 0, where is the empty set
h( ) = 0, where is the empty string
h(t) = 0, where t may be any terminal symbol of an alphabet set
h(EF) = max(h(E), h(F)), where E, F denotes regular expressions
h(E*) = h(E) + 1

Some examples are:

h(a*(b a*)*) = 2
h((a b*) + (a* a b*)*b)*) = 3
h(a) = 0

Prof. Eggan tried to give a relationship between the loop complexity of an


automaton that accepts a language L, and the star height of the
language, L.
The star height of a language, L is equal to the minimal loop
complexity of an automaton that accepts, L. It can also be stated that
the loop complexity of an automata that is both accessible (i.e automata
constructed by deleting all non-accessible states and any
transitions to or from them) and co-accessible (i.e a state q of an
automaton is said to be co-accessible if there is a string s that takes
us from q to a marked state) is the minimum of star heights of regular
expressions obtained by different possible executions of the state
elimination method (or BMC algorithm).

It is apparent that a regular language of star height zero can represent


only a finite number of regular languages. The generalized star height
considers that taking complement of a regular expression would not lead
to an increase in star height. This consideration yields interesting results
at its disposal.

For example – consider the set of alphabets {x, y}. The star height of the
regular expression for the regular language “all strings beginning and
ending with x”, i.e

h(x(x + y)*x+x) = 1, since only one level of Kleene nesting ex

But the same language can also be represented by the regular


expression x ^c x + x, because ^c denotes set of all strings over the
input alphabets.

Now, h(x ^c x + x) = 0, as no Kleene nesting present

Therefore the generalized star height of the language is 0 even


though its star height is 1.
SaagnikAdhikary
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Generating regular expression from finite
automata
Prerequisite – Introduction of FA, Regular expressions, grammar and
language, Designing FA from Regular Expression

There are two methods to convert FA to regular expression –


1. State Elimination Method –

Step 1 –
If the start state is an accepting state or has transitions in, add a new
non-accepting start state and add an €-transition between the new
start state and the former start state.
Step 2 –
If there is more than one accepting state or if the single accepting
state has transitions out, add a new accepting state, make all other
states non-accepting, and add an €-transition from each former
accepting state to the new accepting state.
Step 3 –
For each non-start non-accepting state in turn, eliminate the state and
update transitions accordingly.

Example :-
Solution :-
Step 1

Step 2
Step 3
2. Arden’s Theorem – Let P and Q be 2 regular expressions. If P does
not contain null string, then following equation in R, viz R = Q + RP, Has
a unique solution by R = QP*

Assumptions –

The transition diagram should not have €-moves.


It must have only one initial state.

Using Arden’s Theorem to find Regular Expression of Deterministic


Finite automata –

1. For getting the regular expression for the automata we first create
equations of the given form for all the states
q1 = q1w11 +q2w21 +…+qnwn1 +€ (q1 is the initial state)
q2 = q1w12 +q2w22 +…+qnwn2
.
.
.
qn = q1w1n +q2w2n +…+qnwnn

wij is the regular expression representing the set of labels of edges


from qi to qj

Note – For parallel edges there will be that many expressions for that
state in the expression.

2. Then we solve these equations to get the equation for qi in terms of


wij and that expression is the required solution, where qi is a final
state.

Example :-
Solution :-
Here the initial state is q2 and the final state is q1.
The equations for the three states q1, q2, and q3 are as follows ?
q1 = q1a + q3a + € ( € move is because q1 is the initial state)
q2 = q1b + q2b + q3b
q3 = q2a
Now, we will solve these three equations ?
q2 = q1b + q2b + q3b
= q1b + q2b + (q2a)b (Substituting value of q3)
= q1b + q2(b + ab)
= q1b (b + ab)* (Applying Arden’s Theorem)
q1 = q1a + q3a + €
= q1a + q2aa + € (Substituting value of q3)
= q1a + q1b(b + ab*)aa + € (Substituting value of q2)
= q1(a + b(b + ab)*aa) + €
= € (a+ b(b + ab)*aa)*
= (a + b(b + ab)*aa)*
Hence, the regular expression is (a + b(b + ab)*aa)*.

GATE CS Corner Questions

Practicing the following questions will help you test your knowledge. All
questions have been asked in GATE in previous years or in GATE Mock
Tests. It is highly recommended that you practice them.

1. GATE CS 2008, Question 52


2. GATE CS 2007, Question 74
3. GATE CS 2014 (Set-1), Question 25
4. GATE CS 2014 (Set-1), Question 65
5. GATE IT 2006, Question 5
6. GATE CS 2013, Question 33
7. GATE CS 2012, Question 12
Ankit87
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
DFA for Strings not ending with “THE”
Problem – Accept Strings that not ending with substring “THE”. Check if
a given string is ending with “the” or not. The different forms of “the”
which are avoided in the end of the string are:

"THE", "ThE", "THe", "tHE", "thE", "The", "tHe" and "the"

All those strings that are ending with any of the above mentioned forms
of “the” are not accepted.

Determinitis finite automata (DFA) of strings that not ending with


“THE” –
The initial and starting state in this dfa is Qo
Approach used in the program –
In this program, consider the 4 states to be 0, 1, 2 and 3. Now let us take
a variable named DFA which will be initially 0. Whenever any transition
takes place, it will update the value of DFA with the number associated
with new state.

Example: If a transition occurs from state 0 to state 1 then the value of


DFA will be updated to 1. If a transition occurs from state 2 to state 3 then
the value of dfa will be updated to 3. In this way, apply this algorithm on
entire string and if in the end, then reach state 0, 1 or 2 then our string
will be accepted otherwise not.

Input : XYzabCthe
Output : NOT ACCEPTED

Input : Themaliktth
Output : ACCEPTED

// CPP program to implement DFS that accepts


// all string that do not end with "THE"
#include <stdio.h>
#include <string.h>

// dfa tells the number associated


// with the present state
int dfa = 0;

// This function is for


// the starting state (zeroth) of DFA
void start(char c)
{
// On receiving 'T' or 't' goto first state (1)
if (c == 't' || c == 'T')
dfa = 1;
}

// This function is for the first state of DFA


void state1(char c)
{
// On receiving 'T' or 't' goto first state (1)
if (c == 't' || c == 'T')
dfa = 1;

// On receiving 'H' or 'h' goto second state (2)


else if (c == 'h' || c == 'H')
dfa = 2;

// else goto starting state (0)


else
dfa = 0;
}

// This function is for the second state of DFA


void state2(char c)
{
// On receiving 'E' or 'e' goto third state (3)
// else goto starting state (0)
if (c == 'e' || c == 'E')
dfa = 3;
else
dfa = 0;
}

// This function is for the third state of DFA


void state3(char c)
{
// On receiving 'T' or 't' goto first state (1)
// else goto starting state (0)
if (c == 't' || c == 'T')
dfa = 1;
else
dfa = 0;
}

bool isAccepted(char str[])


{
// store length of string
int len = strlen(str);

for (int i=0; i < len; i++) {


if (dfa == 0)
start(str[i]);

else if (dfa == 1)
state1(str[i]);

else if (dfa == 2)
state2(str[i]);

else
state3(str[i]);
}

return (dfa != 3);


}

// driver code
int main()
{
char str[] = "forTHEgeeks";
if (isAccepted(str) == true)
printf("\nACCEPTED\n");
else
printf("NOT ACCEPTED\n");
return 0;
}

Output:

ACCEPTED

The time complexity of this program is O(n)


RishabhMalik
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

Pattern Searching
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
DFA of a string with at least two 0’s and
at least two 1’s
Problem – Draw detereministic finite automata (DFA) of a string with at
least two 0’s and at least two 1’s.

The first thing that come to mind after reading this question us that we
count the number of 1’s and 0’s. Thereafter if they both are at least 2 the
string is accepted else not accepted. But we do not have any concept of
memory in a DFA so we cannot do it by this method.

Input : 1 0 1 1 0 0
Output : Accepted

Input : 1 1 1 0 1
Output : Not accepted

Approach Used –
The first thing we observe is that both 0’s and 1’s should be at least 2. If
any of these is less than 2, then string will not be accepted. In this string
will be accepted in only last case where both 0’s and 1’s will be at least 2.

State Count of 0 Count of 1

Q0 0 0

Q1 0 1
Q2 0 >=2
Q3 1 0

Q4 1 1
Q5 1 >=2

Q6 >=2 0

Q7 >=2 1
Q8 ACCEPTED >=2 >=2

Initially count of both 0 and 1 is zero and we are on state Q0.

Step-1: If input is 1 then count of 1 increases to 1. Goto state Q1


If input is 0 then count of 0 increases to 1. Goto state Q3
Step-2: If input is 1 then count of 1 increases to 2. Goto state Q2
If input is 0 then count of 0 increases to 1. Goto state Q4

Step-3: If input is 1 then count of 1 keeps increasing by 1. Remain in


the same state
If input is 0 then count of 0 increases to 1. Goto state Q5

Step-4: If input is 1 then count of 1 increases to 1. Goto state Q4


If input is 0 then count of 0 increases to 2. Goto state Q6

Step-5: If input is 1 then count of 1 increases to 2. Goto state Q5


If input is 0 then count of 0 increases to 2. Goto state Q7

Step-6: If input is 1 then count of 1 keeps increasing by 1. Remain in


the same state.
If input is 0 then count of 0 increases to 2. Goto state Q8

Step-7: If input is 1 then count of 1 increases to 1. Goto state Q7


If input is 0 then count of 0 keeps increasing by 1. Remain in the
same state.

Step-8: If input is 1 then count of 1 increases to 2. Goto state Q8


If input is 0 then count of 0 keeps increasing by 1. Remain in the
same state.

Step-9: If input is 1 then count of 1 keeps increasing by 1. Remain in


the same state.
If input is 0 then count of 0 keeps increasing by 1. Remain in the
same state.
If string is finished then ACCEPTED
RishabhMalik
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
DFA in LEX code which accepts even
number of zeros and even number of
ones
Lex is a computer program that generates lexical analyzers, which is
commonly used with the YACC parser generator. Lex, originally written
by Mike Lesk and Eric Schmidt and described in 1975,is the standard
lexical analyzer generator on many Unix systems, and an equivalent tool
is specified as part of the POSIX standard. Lex reads an input stream
specifying the lexical analyzer and outputs source code implementing the
lexer in the C programming language.

Deterministic Finite Acceptor –


In the theory of computation, a branch of theoretical computer science, a
deterministic finite automaton (DFA)— also known as a deterministic
finite acceptor (DFA) and a deterministic finite state machine (DFSM)— is
a finite-state machine that accepts and rejects strings of symbols and
only produces a unique computation (or run) of the automaton for each
input string. Deterministic refers to the uniqueness of the computation. In
search of the simplest models to capture finite-state machines,
McCulloch and Pitts were among the first researchers to introduce a
concept similar to finite automata in 1943.

Approach –
LEX provides us with an INITIAL state by default. So in order to make a
DFA, use this initial state as the initial state of the DFA. Define two more
states A and B where B is the dead state that would be use if encounter
a wrong or invalid input. When user get input which is an invalid input,
move to state B and print message “INVALID” and if user reaches
INITIAL state from state A with a “\n” then display a message “Not
Accepted”. But if user get a \n on the initial state, user display a message
“Accepted”.

Examples –

Input : 1001
Output : Accepted

Input : hjabdba
Output : INVALID

To implement the above DFA, user needs to write the below code in a lex
file with a .l extention.

NOTE :

To compile a lex program, user need a UNIX system and flex which
can be installed using sudo apt-get install flex
With all the above specification open unix terminal and do the
following:
1. Use the lex program to change the specification file into a C
language program. The resulting program is in the lex.yy.c file.
2. Use the cc command with the -ll flag to compile and link the
program with a library of lex subroutines. The resulting executable
program is in the a.out file.

lex lextest
cc lex.yy.c -lfl

Code –

Recommended: Please try your approach on {IDE} first,


before moving on to the solution.

%{
%}

%s A B

%%
<INITIAL>1 BEGIN INITIAL;
<INITIAL>0 BEGIN A;
<INITIAL>[^0|\n] BEGIN B;
<INITIAL>\n BEGIN INITIAL; printf("Accepted\n");
<A>1 BEGIN A;
<A>0 BEGIN INITIAL;
<A>[^0|\n] BEGIN B;
<A>\n BEGIN INITIAL; printf("Not Accepted\n");
<B>0 BEGIN B;
<B>1 BEGIN B;
<B>[^0|\n] BEGIN B;
<B>\n {BEGIN INITIAL; printf("INVALID\n");}
%%

void main()
{
yylex();
}

Output –

nickhil@NICKHIL:~$ lex prpg11.l


nickhil@NICKHIL:~$ cc lex.yy.c -lfl
nickhil@NICKHIL:~$ ./a.out
1000
Not Accepted
hello
INVALID
01010101
Accepted
nickhilrawat
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

Misc
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Conversion from NFA to DFA
An NFA can have zero, one or more than one move from a given state on
a given input symbol. An NFA can also have NULL moves (moves
without input symbol). On the other hand, DFA has one and only one
move from a given state on a given input symbol.

Conversion from NFA to DFA


Suppose there is an NFA N < Q, ∑, q0, δ, F > which recognizes a
language L. Then the DFA D < Q’, ∑, q0, δ’, F’ > can be constructed for
language L as:
Step 1: Initially Q’ = ɸ.
Step 2: Add q0 to Q’.
Step 3: For each state in Q’, find the possible set of states for each input
symbol using transition function of NFA. If this set of states is not in Q’,
add it to Q’.
Step 4: Final state of DFA will be all states with contain F (final states of
NFA)

Example
Consider the following NFA shown in Figure 1.

Following are the various parameters for NFA.


Q = { q0, q1, q2 }
∑ = ( a, b )
F = { q2 }
δ (Transition Function of NFA)

Step 1: Q’ = ɸ
Step 2: Q’ = {q0}
Step 3: For each state in Q’, find the states for each input symbol.
Currently, state in Q’ is q0, find moves from q0 on input symbol a and b
using transition function of NFA and update the transition table of DFA.

δ’ (Transition Function of DFA)

Now { q0, q1 } will be considered as a single state. As its entry is not in


Q’, add it to Q’.
So Q’ = { q0, { q0, q1 } }
Now, moves from state { q0, q1 } on different input symbols are not
present in transition table of DFA, we will calculate it like:
δ’ ( { q0, q1 }, a ) = δ ( q0, a ) ∪ δ ( q1, a ) = { q0, q1 }
δ’ ( { q0, q1 }, b ) = δ ( q0, b ) ∪ δ ( q1, b ) = { q0, q2 }
Now we will update the transition table of DFA.

δ’ (Transition Function of DFA)

Now { q0, q2 } will be considered as a single state. As its entry is not in


Q’, add it to Q’.
So Q’ = { q0, { q0, q1 }, { q0, q2 } }

Now, moves from state {q0, q2} on different input symbols are not
present in transition table of DFA, we will calculate it like:
δ’ ( { q0, q2 }, a ) = δ ( q0, a ) ∪ δ ( q2, a ) = { q0, q1 }
δ’ ( { q0, q2 }, b ) = δ ( q0, b ) ∪ δ ( q2, b ) = { q0 }
Now we will update the transition table of DFA.

δ’ (Transition Function of DFA)


As there is no new state generated, we are done with the conversion.
Final state of DFA will be state which has q2 as its component i.e., { q0,
q2 }

Following are the various parameters for DFA.


Q’ = { q0, { q0, q1 }, { q0, q2 } }
∑ = ( a, b )
F = { { q0, q2 } } and transition function δ’ as shown above. The final DFA
for above NFA has been shown in Figure 2.

Note : Sometimes, it is not easy to convert regular expression to DFA.


First you can convert regular expression to NFA and then NFA to DFA.

Question : The number of states in the minimal deterministic finite


automaton corresponding to the regular expression (0 + 1)* (10) is
____________.
Solution : First, we will make an NFA for the above expression. To make
an NFA for (0 + 1)*, NFA will be in same state q0 on input symbol 0 or 1.
Then for concatenation, we will add two moves (q0 to q1 for 1 and q1 to
q2 for 0) as shown in Figure 3.
This article has been contributed by Sonal Tuteja.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Program to Implement NFA with epsilon
move to DFA Conversion
Non-determinestic Finite Automata (NFA) : NFA is a finite automaton
where for some cases when a single input is given to a single state, the
machine goes to more than 1 states, i.e. some of the moves cannot be
uniquely determined by the present state and the present input symbol.

An NFA can be represented as M = { Q, ∑, ∂, q0, F}

Q → Finite non-empty set of states.


∑ → Finite non-empty set of input symbols.
∂ → Transitional Function.
q0 → Beginning state.
F → Final State

NFA with (null) or ∈ move : If any finite automata contains ε (null) move
or transaction, then that finite automata is called NFA with ∈ moves

Example :
Consider the following figure of NFA with ∈ move :

Transition state table for the above NFA


STATES 0 1 epsilon

A B, C A B

B – B C

C C C –

Epsilon (∈) – closure : Epsilon closure for a given state X is a set of


states which can be reached from the states X with only (null) or ε moves
including the state X itself. In other words, ε-closure for a state can be
obtained by union operation of the ε-closure of the states which can be
reached from X with a single ε move in recursive manner.

For the above example ∈ closure are as follows :

∈ closure(A) : {A, B, C}
∈ closure(B) : {B, C}
∈ closure(C) : {C}

Deterministic Finite Automata (DFA) : DFA is a finite automata where,


for all cases, when a single input is given to a single state, the machine
goes to a single state, i.e., all the moves of the machine can be uniquely
determined by the present state and the present input symbol.

Steps to Convert NFA with ε-move to DFA :

Step 1 : Take ∈ closure for the beginning state of NFA as beginning


state of DFA.
Step 2 : Find the states that can be traversed from the present for
each input symbol
(union of transition value and their closures for each states of NFA
present in current state of DFA).

Step 3 : If any new state is found take it as current state and repeat
step 2.
Step 4 : Do repeat Step 2 and Step 3 until no new state present in
DFA transition table.
Step 5 : Mark the states of DFA which contains final state of NFA as
final states of DFA.

Transition State Table for DFA corresponding to above NFA

STATES 0 1

A, B, C B, C A, B, C

B, C C B, C

C C C

DFA STATE DIAGRAM

Examples :

Input : 6
2
FC - BF
- C -
- - D
E A -
A - BF
- - -

Output :
STATES OF NFA : A, B, C, D, E, F,

GIVEN SYMBOLS FOR NFA: 0, 1, eps

NFA STATE TRANSITION TABLE

STATES |0 |1 eps
--------+------------------------------------
A |FC |- |BF
B |- |C |-
C |- |- |D
D |E |A |-
E |A |- |BF
F |- |- |-

e-Closure (A) : ABF

e-Closure (B) : B

e-Closure (C) : CD

e-Closure (D) : D

e-Closure (E) : BEF

e-Closure (F) : F

********************************************************

DFA TRANSITION STATE TABLE


STATES OF DFA : ABF, CDF, CD, BEF,

GIVEN SYMBOLS FOR DFA: 0, 1,

STATES |0 |1
--------+-----------------------
ABF |CDF |CD
CDF |BEF |ABF
CD |BEF |ABF
BEF |ABF |CD

Input :
9
2
- - BH
- - CE
D - -
- - G
- F -
- - G
- - BH
I - -
- - -

Output :

STATES OF NFA : A, B, C, D, E, F, G, H, I,

GIVEN SYMBOLS FOR NFA: 0, 1, eps

NFA STATE TRANSITION TABLE


STATES |0 |1 eps
--------+------------------------------------
A |- |- |BH
B |- |- |CE
C |D |- |-
D |- |- |G
E |- |F |-
F |- |- |G
G |- |- |BH
H |I |- |-
I |- |- |-

e-Closure (A) : ABCEH

e-Closure (B) : BCE

e-Closure (C) : C

e-Closure (D) : BCDEGH

e-Closure (E) : E

e-Closure (F) : BCEFGH

e-Closure (G) : BCEGH

e-Closure (H) : H

e-Closure (I) : I

********************************************************

DFA TRANSITION STATE TABLE


STATES OF DFA : ABCEH, BCDEGHI, BCEFGH,

GIVEN SYMBOLS FOR DFA: 0, 1,

STATES |0 |1
--------+-----------------------
ABCEH |BCDEGHI |BCEFGH
BCDEGHI |BCDEGHI |BCEFGH
BCEFGH |BCDEGHI |BCEFGH

Recommended: Please try your approach on {IDE} first,


before moving on to the solution.

Explanation :
First line of the input contains the number of states (N) of NFA. Second
line of the input says the number of input symbols (S). In example1
number of states of NFA is 6 i.e.( A, B, C, D, E, F) and 2 input symbols
i.e. ( 0, 1). Since we are working on NFA with ∈ move, ∈ will be added
as an extra input symbol. The next N lines contains the transition values
for every state of NFA. The value of ith row, jth column indicates
transition value for ith state on jth input symbol. Here in example1
transition(A, 0) : FC.

Output contains the NFA, ∈ closure for every states of the corresponding
NFA and DFA obtained by converting the input NFA. States and input
symbols of the DFA are also specified.

Below is the implementation of above approach :

// C Program to illustrate how to convert e-nfa to DFA

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100

char NFA_FILE[MAX_LEN];
char buffer[MAX_LEN];
int zz = 0;

// Structure to store DFA states and their


// status ( i.e new entry or already present)
struct DFA {
char *states;
int count;
} dfa;

int last_index = 0;
FILE *fp;
int symbols;

/* reset the hash map*/


void reset(int ar[], int size) {
int i;

// reset all the values of


// the mapping array to zero
for (i = 0; i < size; i++) {
ar[i] = 0;
}
}
// Check which States are present in the e-closure

/* map the states of NFA to a hash set*/


void check(int ar[], char S[]) {
int i, j;

// To parse the individual states of NFA


int len = strlen(S);
for (i = 0; i < len; i++) {

// Set hash map for the position


// of the states which is found
j = ((int)(S[i]) - 65);
ar[j]++;
}
}

// To find new Closure States


void state(int ar[], int size, char S[]) {
int j, k = 0;

// Combine multiple states of NFA


// to create new states of DFA
for (j = 0; j < size; j++) {
if (ar[j] != 0)
S[k++] = (char)(65 + j);
}

// mark the end of the state


S[k] = '\0';
}

// To pick the next closure from closure set


int closure(int ar[], int size) {
int i;

// check new closure is present or not


for (i = 0; i < size; i++) {
if (ar[i] == 1)
return i;
}
return (100);
}

// Check new DFA states can be


// entered in DFA table or not
int indexing(struct DFA *dfa) {
int i;

for (i = 0; i < last_index; i++) {


if (dfa[i].count == 0)
return 1;
}
return -1;
}

/* To Display epsilon closure*/


void Display_closure(int states, int closure_ar[],
char *closure_table[],
char *NFA_TABLE[][symbols + 1],
char *DFA_TABLE[][symbols]) {
int i;
for (i = 0; i < states; i++) {
reset(closure_ar, states);
closure_ar[i] = 2;

// to neglect blank entry


if (strcmp(&NFA_TABLE[i][symbols], "-") != 0) {

// copy the NFA transition state to buffer


strcpy(buffer, &NFA_TABLE[i][symbols]);
check(closure_ar, buffer);
int z = closure(closure_ar, states);
// till closure get completely saturated
while (z != 100)
{
if (strcmp(&NFA_TABLE[z][symbols], "-") != 0) {
strcpy(buffer, &NFA_TABLE[z][symbols]);

// call the check function


check(closure_ar, buffer);
}
closure_ar[z]++;
z = closure(closure_ar, states);
}
}

// print the e closure for every states of NFA


printf("\n e-Closure (%c) :\t", (char)(65 + i));

bzero((void *)buffer, MAX_LEN);


state(closure_ar, states, buffer);
strcpy(&closure_table[i], buffer);
printf("%s\n", &closure_table[i]);
}
}

/* To check New States in DFA */


int new_states(struct DFA *dfa, char S[]) {

int i;

// To check the current state is already


// being used as a DFA state or not in
// DFA transition table
for (i = 0; i < last_index; i++) {
if (strcmp(&dfa[i].states, S) == 0)
return 0;
}
// push the new
strcpy(&dfa[last_index++].states, S);

// set the count for new states entered


// to zero
dfa[last_index - 1].count = 0;
return 1;
}

// Transition function from NFA to DFA


// (generally union of closure operation )
void trans(char S[], int M, char *clsr_t[], int st,
char *NFT[][symbols + 1], char TB[]) {
int len = strlen(S);
int i, j, k, g;
int arr[st];
int sz;
reset(arr, st);
char temp[MAX_LEN], temp2[MAX_LEN];
char *buff;

// Transition function from NFA to DFA


for (i = 0; i < len; i++) {

j = ((int)(S[i] - 65));
strcpy(temp, &NFT[j][M]);

if (strcmp(temp, "-") != 0) {
sz = strlen(temp);
g = 0;

while (g < sz) {


k = ((int)(temp[g] - 65));
strcpy(temp2, &clsr_t[k]);
check(arr, temp2);
g++;
}
}
}

bzero((void *)temp, MAX_LEN);


state(arr, st, temp);
if (temp[0] != '\0') {
strcpy(TB, temp);
} else
strcpy(TB, "-");
}

/* Display DFA transition state table*/


void Display_DFA(int last_index, struct DFA *dfa_states,
char *DFA_TABLE[][symbols]) {
int i, j;
printf("\n\n************************************************
printf("\t\t DFA TRANSITION STATE TABLE \t\t \n\n");
printf("\n STATES OF DFA :\t\t");

for (i = 1; i < last_index; i++)


printf("%s, ", &dfa_states[i].states);
printf("\n");
printf("\n GIVEN SYMBOLS FOR DFA: \t");

for (i = 0; i < symbols; i++)


printf("%d, ", i);
printf("\n\n");
printf("STATES\t");

for (i = 0; i < symbols; i++)


printf("|%d\t", i);
printf("\n");

// display the DFA transition state table


printf("--------+-----------------------\n");
for (i = 0; i < zz; i++) {
printf("%s\t", &dfa_states[i + 1].states);
for (j = 0; j < symbols; j++) {
printf("|%s \t", &DFA_TABLE[i][j]);
}
printf("\n");
}
}

// Driver Code
int main() {
int i, j, states;
char T_buf[MAX_LEN];

// creating an array dfa structures


struct DFA *dfa_states = malloc(MAX_LEN * (sizeof(dfa)));
states = 6, symbols = 2;

printf("\n STATES OF NFA :\t\t");


for (i = 0; i < states; i++)

printf("%c, ", (char)(65 + i));


printf("\n");
printf("\n GIVEN SYMBOLS FOR NFA: \t");

for (i = 0; i < symbols; i++)

printf("%d, ", i);


printf("eps");
printf("\n\n");
char *NFA_TABLE[states][symbols + 1];

// Hard coded input for NFA table


char *DFA_TABLE[MAX_LEN][symbols];
strcpy(&NFA_TABLE[0][0], "FC");
strcpy(&NFA_TABLE[0][1], "-");
strcpy(&NFA_TABLE[0][2], "BF");
strcpy(&NFA_TABLE[1][0], "-");
strcpy(&NFA_TABLE[1][1], "C");
strcpy(&NFA_TABLE[1][2], "-");
strcpy(&NFA_TABLE[2][0], "-");
strcpy(&NFA_TABLE[2][1], "-");
strcpy(&NFA_TABLE[2][2], "D");
strcpy(&NFA_TABLE[3][0], "E");
strcpy(&NFA_TABLE[3][1], "A");
strcpy(&NFA_TABLE[3][2], "-");
strcpy(&NFA_TABLE[4][0], "A");
strcpy(&NFA_TABLE[4][1], "-");
strcpy(&NFA_TABLE[4][2], "BF");
strcpy(&NFA_TABLE[5][0], "-");
strcpy(&NFA_TABLE[5][1], "-");
strcpy(&NFA_TABLE[5][2], "-");
printf("\n NFA STATE TRANSITION TABLE \n\n\n");
printf("STATES\t");

for (i = 0; i < symbols; i++)


printf("|%d\t", i);
printf("eps\n");

// Displaying the matrix of NFA transition table


printf("--------+------------------------------------\n");
for (i = 0; i < states; i++) {
printf("%c\t", (char)(65 + i));

for (j = 0; j <= symbols; j++) {


printf("|%s \t", &NFA_TABLE[i][j]);
}
printf("\n");
}
int closure_ar[states];
char *closure_table[states];

Display_closure(states, closure_ar, closure_table, NFA_TABLE


strcpy(&dfa_states[last_index++].states, "-");

dfa_states[last_index - 1].count = 1;
bzero((void *)buffer, MAX_LEN);

strcpy(buffer, &closure_table[0]);
strcpy(&dfa_states[last_index++].states, buffer);

int Sm = 1, ind = 1;
int start_index = 1;

// Filling up the DFA table with transition values


// Till new states can be entered in DFA table
while (ind != -1) {
dfa_states[start_index].count = 1;
Sm = 0;
for (i = 0; i < symbols; i++) {

trans(buffer, i, closure_table, states, NFA_TABLE, T_buf

// storing the new DFA state in buffer


strcpy(&DFA_TABLE[zz][i], T_buf);

// parameter to control new states


Sm = Sm + new_states(dfa_states, T_buf);
}
ind = indexing(dfa_states);
if (ind != -1)
strcpy(buffer, &dfa_states[++start_index].states);
zz++;
}
// display the DFA TABLE
Display_DFA(last_index, dfa_states, DFA_TABLE);

return 0;
}

Use of NFA with ∈ move : If we want to construct an FA which accepts


a language, sometimes it becomes very difficult or seems to be
impossible to construct a direct NFA or DFA. But if NFA with ∈ moves is
used, then the transitional diagram can be constructed and described
easily.

Agnibha_Chandra
Btech(3rd year) in Information Technology from IIEST shibpur

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to
share more information about the topic discussed above.

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Minimization of DFA
DFA minimization stands for converting a given DFA to its equivalent DFA
with minimum number of states.

Minimization of DFA
Suppose there is a DFA D < Q, ∑, q0, δ, F > which recognizes a
language L. Then the minimized DFA D < Q’, ∑, q0, δ’, F’ > can be
constructed for language L as:
Step 1: We will divide Q (set of states) into two sets. One set will contain
all final states and other set will contain non-final states. This partition is
called P0.
Step 2: Initialize k = 1
Step 3: Find Pk by partitioning the different sets of Pk-1. In each set of Pk-
1, we will take all possible pair of states. If two states of a set are
distinguishable, we will split the sets into different sets in Pk.
Step 4: Stop when Pk = Pk-1 (No change in partition)
Step 5: All states of one set are merged into one. No. of states in
minimized DFA will be equal to no. of sets in Pk.

How to find whether two states in partition Pk are distinguishable ?


Two states ( qi, qj ) are distinguishable in partition Pk if for any input
symbol a, δ ( qi, a ) and δ ( qj, a ) are in different sets in partition Pk-1.
Example
Consider the following DFA shown in figure.
Step 1. P0 will have two sets of states. One set will contain q1, q2, q4
which are final states of DFA and another set will contain remaining
states. So P0 = { { q1, q2, q4 }, { q0, q3, q5 } }.
Step 2. To calculate P1, we will check whether sets of partition P0 can be
partitioned or not:

For set { q1, q2, q4 } :


δ ( q1, 0 ) = δ ( q2, 0 ) = q2 and δ ( q1, 1 ) = δ ( q2, 1 ) = q5, So q1 and
q2 are not distinguishable.
Similarly, δ ( q1, 0 ) = δ ( q4, 0 ) = q2 and δ ( q1, 1 ) = δ ( q4, 1 ) = q5, So
q1 and q4 are not distinguishable.
Since, q1 and q2 are not distinguishable and q1 and q4 are also not
distinguishable, So q2 and q4 are not distinguishable. So, { q1, q2, q4 }
set will not be partitioned in P1.

For set { q0, q3, q5 } :


δ ( q0, 0 ) = q3 and δ ( q3, 0 ) = q0
δ ( q0, 1) = q1 and δ ( q3, 1 ) = q4
Moves of q0 and q3 on input symbol 0 are q3 and q0 respectively which
are in same set in partition P0. Similarly, Moves of q0 and q3 on input
symbol 1 are q3 and q0 which are in same set in partition P0. So, q0 and
q3 are not distinguishable.
δ ( q0, 0 ) = q3 and δ ( q5, 0 ) = q5 and δ ( q0, 1 ) = q1 and δ ( q5, 1 ) =
q5
Moves of q0 and q5 on input symbol 0 are q3 and q5 respectively which
are in different set in partition P0. So, q0 and q5 are distinguishable. So,
set { q0, q3, q5 } will be partitioned into { q0, q3 } and { q5 }. So,
P1 = { { q1, q2, q4 }, { q0, q3}, { q5 } }

To calculate P2, we will check whether sets of partition P1 can be


partitioned or not:
For set { q1, q2, q4 } :
δ ( q1, 0 ) = δ ( q2, 0 ) = q2 and δ ( q1, 1 ) = δ ( q2, 1 ) = q5, So q1 and
q2 are not distinguishable.
Similarly, δ ( q1, 0 ) = δ ( q4, 0 ) = q2 and δ ( q1, 1 ) = δ ( q4, 1 ) = q5, So
q1 and q4 are not distinguishable.
Since, q1 and q2 are not distinguishable and q1 and q4 are also not
distinguishable, So q2 and q4 are not distinguishable. So, { q1, q2, q4 }
set will not be partitioned in P2.

For set { q0, q3 } :


δ ( q0, 0 ) = q3 and δ ( q3, 0 ) = q0
δ ( q0, 1 ) = q1 and δ ( q3, 1 ) = q4
Moves of q0 and q3 on input symbol 0 are q3 and q0 respectively which
are in same set in partition P1. Similarly, Moves of q0 and q3 on input
symbol 1 are q3 and q0 which are in same set in partition P1. So, q0 and
q3 are not distinguishable.

For set { q5 }:
Since we have only one state in this set, it can’t be further partitioned. So,
P2 = { { q1, q2, q4 }, { q0, q3 }, { q5 } }
Since, P1=P2. So, this is the final partition. Partition P2 means that q1,
q2 and q4 states are merged into one. Similarly, q0 and q3 are merged
into one. Minimized DFA corresponding to DFA of Figure 1 is shown in
Figure 2 as:

Question : Consider the given DFA. Which of the following is false?


1. Complement of L(A) is context-free.
2. L(A) = L ( ( 11 * 0 + 0 ) ( 0 + 1 )* 0* 1* )
3. For the language accepted by A, A is the minimal DFA.
4. A accepts all strings over { 0, 1 } of length atleast two.

A. 1 and 3 only
B. 2 and 4 only
C. 2 and 3 only
D. 3 and 4 only

Solution : Statement 4 says, it will accept all strings of length atleast 2.


But it accepts 0 which is of length 1. So, 4 is false.
Statement 3 says that the DFA is minimal. We will check using the
algorithm discussed above.
P0 = { { q2 }, { q0, q1 } }
P1 = { q2 }, { q0, q1 } }. Since, P0 = P1, P1 is the final DFA. q0 and q1
can be merged. So minimal DFA will have two states. Therefore,
statement 3 is also false.
So correct option is (D).

This article has been contributed by Sonal Tuteja.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Mealy and Moore Machines
Moore Machines: Moore machines are finite state machines with output value and its
present state. It can be defined as (Q, q0, ∑, O, δ, λ) where:

Q is finite set of states.


q0 is the initial state.
∑ is the input alphabet.
O is the output alphabet.
δ is transition function which maps Q×∑ → Q.
λ is the output function which maps Q → O.

Figure 1

In the moore machine shown in Figure 1, the output is represented with each input sta
of output for a moore machine is greater than input by 1.
Input: 11
Transition: δ (q0,11)=> δ(q2,1)=>q2
Output: 000 (0 for q0, 0 for q2 and again 0 for q2)

Mealy Machines: Mealy machines are also finite state machines with output value a
on
present state and current input symbol. It can be defined as (Q, q0, ∑, O, δ, λ’) where

Q is finite set of states.


q0 is the initial state.
∑ is the input alphabet.
O is the output alphabet.
δ is transition function which maps Q×∑ → Q.
‘λ’ is the output function which maps Q×∑→ O.
Figure 2

In the mealy machine shown in Figure 1, the output is represented with each input sym
state separated
by /. The length of output for a mealy machine is equal to the length of input.

Input:11
Transition: δ (q0,11)=> δ(q2,1)=>q2
Output: 00 (q0 to q2 transition has Output 0 and q2 to q2 transition also has Outp

Conversion from Mealy to Moore Machine

Let us take the transition table of mealy machine shown in Figure 2.

Input=0 Input=1

Present State Next State Output Next State Output

q0 q1 0 q2 0

q1 q1 0 q2 1
q2 q1 1 q2 0

Table 1

Step 1. First find out those states which have more than 1 outputs associated with the
which have both output 0 and 1 associated with them.

Step 2. Create two states for these states. For q1, two states will be q10 (state with o
output 1). Similarly for q2, two states will be q20 and q21.

Step 3. Create an empty moore machine with new generated state. For moore machi
each state irrespective of inputs.

Input=0 Input=1
Present State Next State Next State Output

q0
q10

q11

q20
q21

Table 2

Step 4. Fill the entries of next state using mealy machine transition table shown in Tab
state is q10 (q1 with output 0). Similarly, for q0 on input 1, next state is q20 (q2 with o

Input=0 Input=1
Present State Next State Next State Output

q0 q10 q20 0

q10 q10 q21 0

q11 q10 q21 1

q20 q11 q20 0


q21 q11 q20 1

Table 3

This is the transition table of moore machine shown in Figure 1.

Conversion from moore machine to mealy machine

Let us take the moore machine of Figure 1 and its transition table is shown in Table 3.
Step 1. Construct an empty mealy machine using all states of moore machine as sho

Input=0 Input=1
Present State Next State Output Next State Output

q0

q10
q11

q20
q21

Table 4

Step 2: Next state for each state can also be directly found from moore machine trans

Input=0 Input=1

Present State Next State Output Next State Output

q0 q10 q20

q10 q10 q21


q11 q10 q21

q20 q11 q20


q21 q11 q20

Table 5

Step 3: As we can see output corresponding to each


input in moore machine transition table. Use this to fill the Output
entries. e.g.; Output corresponding to q10, q11, q20 and q21 are 0, 1, 0 and 1 respec
Input=0 Input=1

Present State Next State Output Next State Output

q0 q10 0 q20 0
q10 q10 0 q21 1

q11 q10 0 q21 1

q20 q11 1 q20 0


q21 q11 1 q20 0

Table 6

Step 4: As we can see from table 6, q10 and q11 are similar to each other (same va
different Input). Similarly, q20 and q21 are also similar. So, q11 and q21 can be elimin

Input=0 Input=1

Present State Next State Output Next State Output


q0 q10 0 q20 0

q10 q10 0 q21 1

q20 q11 1 q20 0

Table 7

This is the same mealy machine shown in Table 1. So we have converted mealy to m
back moore to mealy.

Note: Number of states in mealy machine can’t be greater than


number of states in moore machine.

Example: The Finite state machine described by the following state diagram with A a
label is x / y and x stands for 1-bit input and y stands for 2- bit output?

Outputs the sum of the present and the previous bits of the input.

1. Outputs 01 whenever the input sequence contains 11.


2. Outputs 00 whenever the input sequence contains 10.
3. None of these.

Solution: Let us take different inputs and its output and check which option works:

Input: 01

Output: 00 01 (For 0, Output is 00 and state is A. Then, for 1, Output is 01 and state

Input: 11

Output: 01 10 (For 1, Output is 01 and state is B. Then, for 1, Output is 10 and state

As we can see, it is giving the binary sum of present and previous bit. For first bit, pre

This article is contributed by Sonal Tuteja. Please write comments if you


find anything incorrect, or you want to share more information about the
topic discussed above
GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Practice problems on finite automata
Que-1: Draw a deterministic and non-deterministic finite automate which
accept 00 and 11 at the end of a string containing 0, 1 in it, e.g.,
01010100 but not 000111010.

Explanation – Design a DFA and NFA of a same string if input value


reaches the final state then it is acceptable otherwise it is not acceptable.
NFA of the given string is as follows:

DFA of the given string is as follows:


Here, q0 shows the initial state, q1 and q2 are the transition states, and
q3 and q4 are the final states.

Note – NFA and DFA both have same power that means if NFA can
recognized an language L then DFA can also be defined to do so and if
DFA can recognized an language L then NFA can also be defined to do
so.

Que-2: Draw a deterministic and non-deterministic finite automata which


accept a string containing “the” anywhere in a string of {a-z}, e.g., “there”
but not “those”.
Explanation – Design a DFA and NFA of a same string if input value
reaches the final state then it is acceptable otherwise it is not acceptable.
It is applicable for all the DFA and NFA. Since, NFA is quit easier then
DFA, so first make its NFA and then go through the DFA.
NFA of the given string is as follows:

DFA of the given string is as follows:


Here, q0 shows the initial state, q1 and q2 are the transition states, and
q3 is the final state.

Que-3: Draw a deterministic and non-deterministic finite automata which


accept a string containing “ing” at the end of a string in a string of {a-z},
e.g., “anything” but not “anywhere”.

Explanation – Design a DFA and NFA of a same string if input value


reaches the final state then it is acceptable otherwise it is not acceptable.
It is applicable for all the DFA and NFA.
NFA of the given string is as follows:

DFA of the given string is as follows:


Here, q0 shows the initial state, q1 and q2 are the transition states, and
q3 is the final state.
ujjwal57
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Context Free Grammar and Context Free
Languages
Relationship between grammar and
language
A grammar is a set of production rules which are used to generate strings
of a language. In this article, we have discussed how to find the language
generated by a grammar and vice versa as well.

Language generated by a grammar –

Given a grammar G, its corresponding language L(G) represents the set


of all strings generated from G. Consider the following grammar,

G: S-> aSb|ε

In this grammar, using S-> ε, we can generate ε. Therefore, ε is part of


L(G). Similarly, using S=>aSb=>ab, ab is generated. Similarly, aabb can
also be generated.
Therefore,

L(G) = {a^nb^n, n>=0}

In language L(G) discussed above, the condition n = 0 is taken to accept


ε.
Key Points –

For a given grammar G, its corresponding language L(G) is unique.


The language L(G) corresponding to grammar G must contain all
strings which can be generated from G.
The language L(G) corresponding to grammar G must not contain any
string which can not be generated from G.
Let us discuss questions based on this:

Que-1. Consider the grammar: (GATE-CS-2009)

S -> aSa|bSb|a|b

The language generated by the above grammar over the alphabet {a,b} is
the set of:
(A) All palindromes
(B) All odd length palindromes.
(C) Strings that begin and end with the same symbol
(D) All even length palindromes

Solution: Using S->a and S->b, a and b can be generated. Similarly


using S=>aSa=>aba, aba can be generated. Other strings which can be
generated from grammar are: a, b, aba, bab, aaa, bbb, ababa, …
Therefore, option (B) is correct.

Que-2. Consider the following context-free grammars: (GATE-CS-2016)

Which one of the following pairs of languages is generated by G1 and


G2, respectively?
Solution: Consider the grammar G1:
Using S=>B=>b, b can be generated.
Using S=>B=>bB, bb can be generated.
Using S=>aS=>aB=>ab can be generated.
Using S=>aS=>aB=>abB=>abb can be generated.
As we can see, number of a’s can be zero or more but number of b is
always greater than zero.
Therefore,

L(G1) = {a^mb^n| m>=0 and n>0}

Consider the grammar G2:


Using S=>aA=>a, a can be generated.
Using S=>bB=>b, b can be generated.
Using S=>aA=>aaA=>aa can be generated.
Using S=>bB=>bbB=>bb can be generated.
Using S=>aA=>aB=>abB=>abb can be generated.

As we can see, either a or b must be greater than 0.


Therefore,

L(G2) = {a^mb^n| m>0 or n>0}

Grammar generating a given language –

Given a language L(G), its corresponding grammar G represents the


production rules which produces L(G). Consider the language L(G):

L(G) = {a^nb^n, n>=0}

The language L(G) is set of strings ε, ab, aabb, aaabbb….


For ε string in L(G), the production rule can be S->ε.
For other strings in L(G), the production rule can be S->aSb| ε.
Therefore, grammar G corresponding to L(G) is:

S->aSb| ε

Key Points –

For a given language L(G), there can be more than one grammar
which can produce L(G).
The grammar G corresponding to language L(G) must generate all
possible strings of L(G).
The grammar G corresponding to language L(G) must not generate
any string which is not part of L(G).

Let us discuss questions based on this:

Que-3. Which one of the following grammar generates the language L =


{a^ib^j | i≠j}? (GATE-CS-2006)
Solution: The given language L contains the strings :

{a, b, aa, bb, aaa, bbb, aab, abb…}

It means either the string must contain one or more number of a OR one
or more number of b OR a followed by b having unequal number of a and
b.

If we consider grammar in option (A), it can generate ab as:

S=>AC=>aAC=>aC=>ab

However, ab can’t be generated by language L. Therefore, grammar in


option (A) is not correct.

Similarly, grammar in option (B) can generate ab as:

S=>aS=>ab
However, ab can’t be generated by language L. Therefore, grammar in
option (B) is not correct.

Similarly, grammar in option (C) can generate ab as:

S=>AC=>C=>aCb=>ab

However, ab can’t be generated by language L. Therefore, grammar in


option (C) is not correct.
Therefore, using method of elimination, option (D) is correct.

This article is contributed by Sonal Tuteja. If you like GeeksforGeeks and


would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Simplifying Context Free Grammars
The definition of context free grammars (CFGs) allows us to develop a
wide variety of grammars. Most of the time, some of the productions of
CFGs are not useful and are redundant. This happens because the
definition of CFGs does not restrict us from making these redundant
productions.

By simplifying CFGs we remove all these redundant productions from a


grammar , while keeping the transformed grammar equivalent to the
original grammar. Two grammars are called equivalent if they produce
the same language. Simplifying CFGs is necessary to later convert them
into Normal forms.

Types of redundant productions and the procedure of removing them are


mentioned below.
1. Useless productions – The productions that can never take part in
derivation of any string , are called useless productions. Similarly , a
variable that can never take part in derivation of any string is called a
useless variable. For eg.

S -> abS | abA | abB


A -> cd
B -> aB
C -> dc

In the example above , production ‘C -> dc’ is useless because the


variable ‘C’ will never occur in derivation of any string. The other
productions are written in such a way that variable ‘C’ can never reached
from the starting variable ‘S’.

Production ‘B ->aB’ is also useless because there is no way it will ever


terminate . If it never terminates , then it can never produce a string.
Hence the production can never take part in any derivation.

To remove useless productions , we first find all the variables which will
never lead to a terminal string such as variable ‘B’. We then remove all
the productions in which variable ‘B’ occurs.
So the modified grammar becomes –

S -> abS | abA


A -> cd
C -> dc

We then try to identify all the variables that can never be reached from
the starting variable such as variable ‘C’. We then remove all the
productions in which variable ‘C’ occurs.
The grammar below is now free of useless productions –

S -> abS | abA


A -> cd

2. λ productions – The productions of type ‘A -> λ’ are called λ


productions ( also called lambda productions and null productions) .
These productions can only be removed from those grammars that do
not generate λ (an empty string). It is possible for a grammar to contain
null productions and yet not produce an empty string.
To remove null productions , we first have to find all the nullable
variables. A variable ‘A’ is called nullable if λ can be derived from ‘A’. For
all the productions of type ‘A -> λ’ , ‘A’ is a nullable variable. For all the
productions of type ‘B -> A1A2…An ‘ , where all ’Ai’s are nullable
variables , ‘B’ is also a nullable variable.

After finding all the nullable variables, we can now start to construct the
null production free grammar. For all the productions in the original
grammar , we add the original production as well as all the combinations
of the production that can be formed by replacing the nullable variables in
the production by λ. If all the variables on the RHS of the production are
nullable , then we do not add ‘A -> λ’ to the new grammar. An example
will make the point clear. Consider the grammar –

S -> ABCd (1)


A -> BC (2)
B -> bB | λ (3)
C -> cC | λ (4)

Lets first find all the nullable variables. Variables ‘B’ and ‘C’ are clearly
nullable because they contain ‘λ’ on the RHS of their production. Variable
‘A’ is also nullable because in (2) , both variables on the RHS are also
nullable. Similarly , variable ‘S’ is also nullable. So variables ‘S’ , ‘A’ , ‘B’
and ‘C’ are nullable variables.

Lets create the new grammar. We start with the first production. Add the
first production as it is. Then we create all the possible combinations that
can can be formed by replacing the nullable variables with λ. Therefore
line (1) now becomes ‘S -> ABCd | ABd | ACd | BCd | Ad | Bd |Cd | d ’.We
apply the same rule to line (2) but we do not add ‘A -> λ’ even though it is
a possible combination. We remove all the productions of type ‘V -> λ’.
The new grammar now becomes –

S -> ABCd | ABd | ACd | BCd | Ad | Bd |Cd | d


A -> BC | B | C
B -> bB | b
C -> cC | c

3. Unit productions – The productions of type ‘A -> B’ are called unit


productions.
To create a unit production free grammar ‘Guf’ from the original grammar
‘G’ , we follow the procedure mentioned below.

First add all the non-unit productions of ‘G’ in ‘Guf’. Then for each
variable ‘A’ in grammar ‘G’ , find all the variables ‘B’ such that ‘A *=> B’.
Now , for all variables like ‘A ’ and ‘B’, add ‘A -> x1 | x2 | …xn’ to ‘Guf’
where ‘B -> x1 | x2 | …xn ‘ is in ‘Guf’ . None of the x1 , x2 … xn are single
variables because we only added non-unit productions in ‘Guf’. Hence
the resultant grammar is unit production free. For eg.

S -> Aa | B
A -> b | B
B -> A | a

Lets add all the non-unit productions of ‘G’ in ‘Guf’. ‘Guf’ now becomes –

S -> Aa
A -> b
B -> a

Now we find all the variables that satisfy ‘X *=> Z’. These are ‘S *=> A’ ,
‘S*=>B’, ‘A *=> B’ and ‘B *=> A’. For ‘A *=> B’ , we add ‘A -> a’ because ‘B
->a’ exists in ‘Guf’. ‘Guf’ now becomes
S -> Aa
A -> b | a
B -> a

For ‘B *=> A’ , we add ‘B -> b’ because ‘A -> b’ exists in ‘Guf’. The new
grammar now becomes

S -> Aa
A -> b | a
B -> a | b

We follow the same step for ‘S *=> A’ and ‘S*=>B’ and finally get the
following grammar –

S -> Aa | b | a
A -> b | a
B -> a | b

Note: To remove all kinds of productions mentioned above, first remove


the null productions, then the unit productions and finally , remove the
useless productions. Following this order is very important to get the
correct result.

This article is contributed by Nitish Joshi. Please write comments if you


find anything incorrect, or you want to share more information about the
topic discussed above.

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Closure Properties of Context Free
Languages
Context Free languages are accepted by pushdown automata but not by
finite automata. Context free languages can be generated by context free
grammar which has the form :

A -> ρ (where A ∈ N and ρ ∈ (T ∪ N)* and N is a non-terminal and T is a


terminal)

Properties of Context Free Languages


Union : If L1 and If L2 are two context free languages, their union L1 ∪
L2 will also be context free. For example,
L1 = { anbncm | m >= 0 and n >= 0 } and L2 = { anbmcm | n >= 0 and m >=
0}
L3 = L1 ∪ L2 = { anbncm ∪ anbmcm | n >= 0, m >= 0 } is also context free.
L1 says number of a’s should be equal to number of b’s and L2 says
number of b’s should be equal to number of c’s. Their union says either
of two conditions to be true. So it is also context free language.

Concatenation : If L1 and If L2 are two context free languages, their


concatenation L1.L2 will also be context free. For example,
L1 = { anbn | n >= 0 } and L2 = { cmdm | m >= 0 }
L3 = L1.L2 = { anbncmdm | m >= 0 and n >= 0} is also context free.
L1 says number of a’s should be equal to number of b’s and L2 says
number of c’s should be equal to number of d’s. Their concatenation says
first number of a’s should be equal to number of b’s, then number of c’s
should be equal to number of d’s. So, we can create a PDA which will
first push for a’s, pop for b’s, push for c’s then pop for d’s. So it can be
accepted by pushdown automata, hence context free.

Kleene Closure : If L1 is context free, its Kleene closure L1* will also be
context free. For example,
L1 = { anbn | n >= 0 }
L1* = { anbn | n >= 0 }* is also context free.

Intersection and complementation : If L1 and If L2 are two context free


languages, their intersection L1 ∩ L2 need not be context free. For
example,
L1 = { anbncm | n >= 0 and m >= 0 } and L2 = (ambncn | n >= 0 and m >= 0
}
L3 = L1 ∩ L2 = { anbncn | n >= 0 } need not be context free.
L1 says number of a’s should be equal to number of b’s and L2 says
number of b’s should be equal to number of c’s. Their intersection says
both conditions need to be true, but push down automata can compare
only two. So it cannot be accepted by pushdown automata, hence not
context free.
Similarly, complementation of context free language L1 which is ∑* – L1,
need not be context free.

Deterministic Context-free Languages


Deterministic CFL are subset of CFL which can be recognized by
Deterministic PDA. Deterministic PDA has only one move from a given
state and input symbol. For example, L1= { anbncm | m >= 0 and n >= 0}
is a DCFL because for a’s, we can push on stack and for b’s we can pop.
It can be recognized by Deterministic PDA. On the other hand, L3 = {
anbncm ∪ anbmcm | n >= 0, m >= 0 } cannot be recognized by DPDA
because either number of a’s and b’s can be equal or either number of
b’s and c’s can be equal. So, it can only be implemented by NPDA. Thus,
it is CFL but not DCFL.
Note : Out of union, concatenation, complementation, intersection and
kleene-closure, DCFL are closed only under complementation.

Question : Consider the language L1,L2,L3 as given below.


L1 = { ambn | m, n >= 0 }
L2 = { anbn | n >= 0 }
L3 = { anbncn | n >= 0 }
Which of the following statements is NOT TRUE?
A. Push Down Automata (PDA) can be used to recognize L1 and L2
B. L1 is a regular language
C. All the three languages are context free
D. Turing machine can be used to recognize all the three languages

Solution : Option (A) says PDA can be used to recognize L1 and L2. L1
contains all strings with any no. of a followed by any no. of b. So, it can
be accepted by PDA. L2 contains strings with n no. of a’s followed by n
no. of b’s. It can also be accepted by PDA. So, option (A) is correct.
Option (B) says that L1 is regular. It is true as regular expression for L1 is
a*b*.
Option (C) says L1, L2 and L3 are context free. L3 languages contains all
strings with n no. of a’s followed by n no. of b’s followed by n no. of c’s.
But it can’t be accepted by PDA. So option ( C) is not correct.
Option (D) is correct as Turing machine can be used to recognize all the
three languages.
Question : The language L = { 0i12i | i ≥ 0 } over the alphabet {0, 1, 2} is :
A. Not recursive
B. Is recursive and deterministic CFL
C. Is regular
D. Is CFL bot not deterministic CFL.

Solution : The above language is deterministic CFL as for 0’s, we can


push 0 on stack and for 2’s we can pop corresponding 0’s. As there is no
ambiguity which moves to take, it is deterministic. So, correct option is
(B). As CFL is subset of recursive, it is recursive as well.

Question : Consider the following languages:


L1 = { 0n1n| n≥0 }
L2 = { wcwr | w ɛ {a,b}* }
L3 = { wwr | w ɛ {a,b}* }
Which of these languages are deterministic context-free languages?
A. None of the languages
B. Only L1
C. Only L1 and L2
D. All three languages

Solution : Languages L1 contains all strings in which n 0’s are followed


by n 1’s. Deterministic PDA can be constructed to accept L1. For 0’s we
can push it on stack and for 1’s, we can pop from stack. Hence, it is
DCFL.
L2 contains all strings of form wcwr where w is a string of a’s and b’s and
wr is reverse of w. For example, aabbcbbaa. To accept this language, we
can construct PDA which will push all symbols on stack before c. After c,
if symbol on input string matches with symbol on stack, it is popped. So,
L2 can also be accepted with deterministic PDA, hence it is also DCFL.
L3 contains all strings of form wwr where w is a string of a’s and b’s and
wr is reverse of w. But we don’t know where w ends and wr starts. e.g.;
aabbaa is a string corresponding to L3. For first a, we will push it on
stack. Next a can be either part of w or wr where w=a. So, there can be
multiple moves from a state on an input symbol. So, only non-
deterministic PDA can be used to accept this type of language. Hence, it
is NCFL not DCFL.
So, correct option is (C). Only, L1 and L2 are DCFL.

Question : Which one of the following grammars generate the language


L = { aibj | i ≠ j }
S -> AC | CB, C -> aCb | a | b, A -> aA | ɛ, B -> Bb | ɛ
S -> aS | Sb | a | b
S -> AC | CB, C -> aCb | ɛ, A -> aA | ɛ, B -> Bb | ɛ
S -> AC | CB, C -> aCb | ɛ, A -> aA | a, B -> Bb | b

Solution : The best way to solve these type of questions is to eliminate


options which do not satisfy conditions. The conditions for language L is
no. of a’s and no. of b’s should be unequal.
In option (B), S => aS => ab. It can generate strings with equal a’s and
b’s. So, this option is incorrect.
In option (C), S => AC => C => ɛ. In ɛ, a’s and b’s are equal (0), so it is
not correct option.
In option (A), S will be replaced by either AC or CB. C will either generate
no. of a’s more than no. of b’s by 1 or no. of b’s more than no. of a’s by 1.
But one more a or one more b can be compensated by B -> bB | ɛ or A ->
aA | ɛ respectively. So it may give strings with equal no. of a’s and b’s.
So, it is not a correct option.
In option (D), S will be replaced by either AC or CB. C will always
generate equal no. of a’s and b’s. If we replace S by AC, A with add
atleast one extra a. and if we replace S by CB, B will add atleast one
extra b. So this grammar will never generate equal no. of a’s and b’s. So,
option (D) is correct.

This article has been contributed by Sonal Tuteja.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Union & Intersection of Regular
languages with CFL
Prerequisite – Chomsky Hierarchy, Regular Languages

As we all are aware that languages accepted by finite automata are


called regular languages and those which are accepted by push down
automata are called context free languages But, when it comes to the
union or intersection of these two languages some people find it difficult
to analyse whether the intersection results in a regular or a context-free
language.

The first thing to observe is every regular language is actually context-


free,the reason is quite simple. One way to call a language regular is by
designing its equivalent finite automata or in other words if we can design
a finite automata for a particular language then only we can call that
language – regular and same exists for context free languages, if it is
possible to design a push down automata for a particular language then
only it is called context free.

Now a push down automata in simple words is actually a finite automata


with a memory provided to it as infinite stack. What you need to
observe that is possible to design a finite automata for a particular
language then it is also possible to design its equivalent push down
automata what we have to do is just not to use the infinite stack
available in it. Its that simple. Observe this and finally what will you get is
that for every regular language it is possible to design finite automata and
thus push down automata. This is the reason why every regular language
can also be called context-free or in other words regular languages are
subset of context-free languages.

Union of Regular language with context free language –


As all regular languages are context-free the union of both results in a
context-free language. But it is always good to understand with the help
of an example.
Let’s take a language L1 = {0*1*} (regular) and L2 = {0^n1^n |n>=0}
(context-free)
And let L=L1 U L2 which will result in union of both these languages and
that will be :

L = {0*1*} which is regular language but since every regular language is


context-free. So, we can say the union of two always results in context-
free language.

Intersection of Regular language with context-free language –


Because now we know that all regular languages are subset of context-
free there is no problem in understanding the union of two but when we
talk about intersection again the answer is context-free language. Yes,
the intersection of a regular and a context-free language always result in
a context-free language.

Let’s again take the above example in which L1 and L2 are same but
now let
L= L1 ∩ L2 which will easily result in
L={0^n1^n | n>=0} which is context-free .
You can take more such examples and verify that the union and
intersection of a regular language and a context-free language always
results in a context-free language.

Read Next article – Closure Properties of Context Free Languages

This article is contributed by Dimpy Varshni. If you like GeeksforGeeks


and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Converting Context Free Grammar to
Chomsky Normal Form
Prerequisite – Simplifying Context Free Grammars

A context free grammar (CGF) is in Chomsky Normal Form (CNF) if all


production rules satisfy one of the following conditions:

A non-terminal generating a terminal (e.g.; X->x)


A non-terminal generating two non-terminals (e.g.; X->YZ)
Start symbol generating ε. (e.g.; S-> ε)

Consider the following grammars,

G1 = {S->a, S->AZ, A->a, Z->z}


G2 = {S->a, S->aZ, Z->a}

The grammar G1 is in CNF as production rules satisfy the rules specified


for CNF. However, the grammar G2 is not in CNF as the production rule
S->aZ contains terminal followed by non-terminal which does not satisfy
the rules specified for CNF.

Note –

For a given grammar, there can be more than one CNF.


CNF produces the same language as generated by CFG.
CNF is used as a preprocessing step for many algorithms for CFG
like CYK, bottom-up parsers etc.
How to convert CFG to CNF?

Step 1. Eliminate start symbol from RHS.


If start symbol S is at the RHS of any production in the grammar, create a
new production as:
S0->S
where S0 is the new start symbol.

Step 2. Eliminate null, unit and useless productions.


If CFG contains null, unit or useless production rules, eliminate them. You
can refer the this article to eliminate these types of production rules.

Step 3. Eliminate terminals from RHS if they exist with other terminals or
non-terminals. e.g,; production rule X->xY can be decomposed as:
X->ZY
Z->x

Step 4. Eliminate RHS with more than two non-terminals.


e.g,; production rule X->XYZ can be decomposed as:
X->PZ
P->XY

Example – Let us take an example to convert CFG to CNF. Consider the


given grammar G1:

S → ASB
A → aAS|a|ε
B → SbS|A|bb

Step 1. As start symbol S appears on the RHS, we will create a new


production rule S0->S. Therefore, the grammar will become:
S0->S
S → ASB
A → aAS|a|ε
B → SbS|A|bb

Step 2. As grammar contains null production A-> ε, its removal from the
grammar yields:

S0->S
S → ASB|SB
A → aAS|aS|a
B → SbS| A|ε|bb

Now, it creates null production B→ ε, its removal from the grammar


yields:

S0->S
S → AS|ASB| SB| S
A → aAS|aS|a
B → SbS| A|bb

Now, it creates unit production B->A, its removal from the grammar
yields:

S0->S
S → AS|ASB| SB| S
A → aAS|aS|a
B → SbS|bb|aAS|aS|a

Also, removal of unit production S0->S from grammar yields:

S0-> AS|ASB| SB| S


S → AS|ASB| SB| S
A → aAS|aS|a
B → SbS|bb|aAS|aS|a
Also, removal of unit production S->S from grammar yields:

S0-> AS|ASB| SB| S


S → AS|ASB| SB
A → aAS|aS|a
B → SbS|bb|aAS|aS|a

Step 3. In production rule A->aAS |aS and B-> SbS|aAS|aS, terminals a


and b exist on RHS with non-terminates. Removing them from RHS:

S0-> AS|ASB| SB| S


S → AS|ASB| SB
A → XAS|XS|a
B → SYS|bb|XAS|XS|a
X →a
Y→b

Also, B->bb can’t be part of CNF, removing it from grammar yields:

S0-> AS|ASB| SB| S


S → AS|ASB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b

Step 4: In production rule S0->ASB, RHS has more than two symbols,
removing it from grammar yields:

S0-> AS|PB| SB| S


S → AS|ASB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS

Similarly, S->ASB has more than two symbols, removing it from grammar
yields:

S0-> AS|PB| SB| S


S → AS|QB| SB
A → XAS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS

Similarly, A->XAS has more than two symbols, removing it from grammar
yields:

S0-> AS|PB| SB| S


S → AS|QB| SB
A → RS|XS|a
B → SYS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA

Similarly, B->SYS has more than two symbols, removing it from grammar
yields:

S0 -> AS|PB| SB| S


S → AS|QB| SB
A → RS|XS|a
B → TS|VV|XAS|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA
T → SY

Similarly, B->XAX has more than two symbols, removing it from grammar
yields:

S0-> AS|PB| SB|S


S → AS|QB| SB
A → RS|XS|a
B → TS|VV|US|XS|a
X → a
Y → b
V → b
P → AS
Q → AS
R → XA
T → SY
U → XA

So this is the required CNF for given grammar.

This article is contributed by Sonal Tuteja. If you like GeeksforGeeks and


would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Converting Context Free Grammar to
Greibach Normal Form
Prerequisite – Context Free Grammars, Simplifying Context Free
Grammars

A context free grammar (CGF) is in Geribach Normal Form (GNF) if all


production rules satisfy one of the following conditions:

A non-terminal generating a terminal (e.g.; X->x)


A non-terminal generating a terminal followed by any number of non-
terminals (e.g.; X->X1X2…Xn)
Start symbol generating ε. (e.g.; S-> ε)

Consider the following grammars:

G1 = {S->aA|bB, B->bB|b, A->aA|a}


G2 = {S->aA|bB, B->bB|ε, A->aA|ε}

The grammar G1 is in GNF as production rules satisfy the rules specified


for GNF. However, the grammar G2 is not in GNF as the production rules
B-> ε and A-> ε do not satisfy the rules specified for GNF (only start
symbol can generate ε).
Note –

For a given grammar, there can be more than one GNF


GNF produces the same language as generated by CFG

How to convert CFG to GNF –


Step 1. Convert the grammar into CNF.
If the given grammar is not in CNF, convert it to CNF. You can refer
following article to convert CFG to CNF: Converting Context Free
Grammar to Chomsky Normal Form

Step 2. Eliminate left recursion from grammar if it exists.


If CFG contains left recursion, eliminate them. You can refer following
article to eliminate left recursion: Parsing | Set 1 (Introduction, Ambiguity
and Parsers)

Step 3. Convert the production rules into GNF form.


If any production rule is not in the GNF form, convert them. Let us take
an example to convert CFG to GNF. Consider the given grammar G1:

S → XA|BB
B → b|SB
X → b
A → a

As G1 is already in CNF and there is not left recursion, we can skip step
1and 2 and directly move to step 3.
The production rule B->SB is not in GNF, therefore, we substitute S ->
XA|BB in production rule B->SB as:

S → XA|BB
B → b|XAB|BBB
X → b
A → a

The production rules S->XA and B->XAB is not in GNF, therefore, we


substitute X->b in production rules S->XA and B->XAB as:

S → bA|BB
B → b|bAB|BBB
X → b
A → a

Removing left recursion (B->BBB), we get:

S → bA|BB
B → bC|bABC
C → BBC| ε
X → b
A → a

Removing null production (C-> ε), we get:

S → bA|BB
B → bC|bABC|b|bAB
C → BBC|BB
X → b
A → a

The production rules S->BB is not in GNF, therefore, we substitute B →


bC|bABC|b|bAB in production rules S->BB as:

S → bA| bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C → BBC|BB
X → b
A → a

The production rules C->BB is not in GNF, therefore, we substitute B →


bC|bABC|b|bAB in production rules C->BB as:
S → bA| bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C → BBC
C → bCB|bABCB|bB|bABB
X → b
A → a

The production rules C->BBC is not in GNF, therefore, we substitute B →


bC|bABC|b|bAB in production rules C->BBC as:

S → bA| bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C → bCBC|bABCBC|bBC|bABBC
C → bCB|bABCB|bB|bABB
X → b
A → a

This is the GNF form for the grammar G1.

This article is contributed by Sonal Tuteja. If you like GeeksforGeeks and


would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Pumping Lemma
There are two Pumping Lemmas, which are defined for
1. Regular Languages, and
2. Context – Free Languages

Pumping Lemma for Regular Languages


For any regular language L, there exists an integer n, such that for all x
∈ L with |x| ≥ n, there exists u, v, w ∈ Σ∗, such that x = uvw, and
(1) |uv| ≤ n
(2) |v| ≥ 1
(3) for all i ≥ 0: uviw ∈ L

In simple terms, this means that if a string v is ‘pumped’, i.e., if v is


inserted any number of times, the resultant string still remains in L.

Pumping Lemma is used as a proof for irregularity of a language. Thus, if


a language is regular, it always satisfies pumping lemma. If there exists
at least one string made from pumping which is not in L, then L is surely
not regular.
The opposite of this may not always be true. That is, if Pumping Lemma
holds, it does not mean that the language is regular.

For example, let us prove L01 = {0n1n | n ≥ 0} is irregular.


Let us assume that L is regular, then by Pumping Lemma the above
given rules follow.
Now, let x ∈ L and |x| ≥ n. So, by Pumping Lemma, there exists u, v, w
such that (1) – (3) hold.

We show that for all u, v, w, (1) – (3) does not hold.


If (1) and (2) hold then x = 0n1n = uvw with |uv| ≤ n and |v| ≥ 1.
So, u = 0a, v = 0b, w = 0c1n where : a + b ≤ n, b ≥ 1, c ≥ 0, a + b + c = n
But, then (3) fails for i = 0
uv0w = uw = 0a0c1n = 0a + c1n ∉ L, since a + c ≠ n.

Pumping Lemma for Context-free Languages (CFL)


Pumping Lemma for CFL states that for any Context Free Language L, it
is possible to find two substrings that can be ‘pumped’ any number of
times and still be in the same language. For any language L, we break its
strings into five parts and pump second and fourth substring.
Pumping Lemma, here also, is used as a tool to prove that a language is
not CFL. Because, if any one string does not satisfy its conditions, then
the language is not CFL.
Thus, if L is a CFL, there exists an integer n, such that for all x ∈ L with
|x| ≥ n, there exists u, v, w, x, y ∈ Σ∗, such that x = uvwxy, and
(1) |vwx| ≤ n
(2) |vx| ≥ 1
(3) for all i ≥ 0: uviwxiy ∈ L
For above example, 0n1n is CFL, as any string can be the result of
pumping at two places, one for 0 and other for 1.
Let us prove, L012 = {0n1n2n | n ≥ 0} is not Context-free.
Let us assume that L is Context-free, then by Pumping Lemma, the
above given rules follow.
Now, let x ∈ L and |x| ≥ n. So, by Pumping Lemma, there exists u, v, w,
x, y such that (1) – (3) hold.
We show that for all u, v, w, x, y (1) – (3) do not hold.

If (1) and (2) hold then x = 0n1n2n = uvwxy with |vwx| ≤ n and |vx| ≥ 1.
(1) tells us that vwx does not contain both 0 and 2. Thus, either vwx has
no 0’s, or vwx has no 2’s. Thus, we have two cases to consider.
Suppose vwx has no 0’s. By (2), vx contains a 1 or a 2. Thus uwy has ‘n’
0’s and uwy either has less than ‘n’ 1’s or has less than ‘n’ 2’s.
But (3) tells us that uwy = uv0wx0y ∈ L.
So, uwy has an equal number of 0’s, 1’s and 2’s gives us a contradiction.
The case where vwx has no 2’s is similar and also gives us a
contradiction. Thus L is not context-free.

Source : John E. Hopcroft, Rajeev Motwani, Jeffrey D. Ullman (2003).


Introduction to Automata Theory, Languages, and Computation.
This article has been contributed by Nirupam Singh.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Ambiguity in Context free Grammar and Context
Prerequisite – Pushdown Automata and Context Free Languages .

Suppose we have a context free grammar G with production rules: S-


>aSb|bSa|SS|ɛ

Left most derivation (LMD) and Derivation Tree: Leftmost derivation of


a string from staring symbol S is done by replacing leftmost non-terminal
symbol by RHS of corresponding production rule. For example: The
leftmost derivation of string abab from grammar G above is done as:

S => aSb => abSab => abab

The symbols underlined are replaced using production rules.

Derivation tree: It tells how string is derived using production rules from
S and has been shown in Figure 1.

Figure 1

Right most derivation (RMD): Rightmost derivation of a string from


staring symbol S is done by replacing rightmost non-terminal symbol by
RHS of corresponding production rule. For Example: The rightmost
derivation of string abab from grammar G above is done as:

S => SS => SaSb => Sab => aSbab => abab

The symbols underlined are replaced using production rules. The


derivation tree for abab using rightmost derivation has been shown in
Figure 2.

Figure 2

A derivation can be either LMD or RMD or both or none. For Example:

S => aSb => abSab => abab is LMD as well as RMD

but S => SS => SaSb => Sab => aSbab => abab is RMD but not LMD

Ambiguous Context Free Grammar: A context free grammar is called


ambiguous if there exists more than one LMD or more than one RMD for
a string which is generated by grammar. There will also be more than one
derivation tree for a string in ambiguous grammar. The grammar
described above is ambiguous because there are two derivation trees
(Figure 1 and Figure 2). There can be more than one RMD for string
abab which are:

S => SS => SaSb => Sab => aSbab => abab

S => aSb => abSab => abab

Ambiguous Context Free Languages: A context free language is called


ambiguous if there is no unambiguous grammar to define that language
and it is also called inherently ambiguous Context Free Languages.

Note:

If a context free grammar G is ambiguous, language generated by


grammar L(G) may or may not be ambiguous
It is not always possible to convert ambiguous CFG to unambiguous
CFG. Only some ambiguous CFG can be converted to unambiguous
CFG.
There is no algorithm to convert ambiguous CFG to unambiguous
CFG.
There always exist a unambiguous CFG corresponding to
unambiguous CFL.
Deterministic CFL are always unambiguous.

Question: Consider the following statements about the context free


grammar

G = {S->SS, S->ab, S->ba, S->ɛ}

I G is ambiguous
II G produces all strings with equal number of a’s and b’s
III G can be accepted by a deterministic PDA
Which combination below expresses all the true statements about G?

1. I only
2. I and III only
3. II and III only
4. I, II and III

Solution: There are different LMD’s for string abab which can be

S => SS => SSS => abSS => ababS => abab

S => SS => abS => abab

So the grammar is ambiguous. So statement I is true.

Statement II states that the grammar G produces all strings with equal
number of a’s and b’s but it can’t generate aabb string. So statement II is
incorrect.

Statement III is also correct as it can be accepted by deterministic PDA.


So correct option is (B).

Question: Which one of the following statements is FALSE?

1. There exist context-free languages such that all the context-free


grammars generating them are ambiguous.
2. An unambiguous context free grammar always has a unique parse
tree for each string of the language generated by it.
3. Both deterministic and non-deterministic pushdown automata always
accept the same set of languages.
4. A finite set of string from one alphabet is always a regular language.
Solution: (A) is correct because for ambiguous CFL’s, all CFG
corresponding to it are ambiguous.

(B) is also correct as unambiguous CFG has a unique parse tree for each
string of the language generated by it.

(C) is false as some languages are accepted by Non- deterministic PDA


but not by deterministic PDA.

(D) is also true as finite set of string is always regular.

So option (C) is correct option.

This article is contributed by Sonal Tuteja.Please write comments if you


find anything incorrect, or you want to share more information about the
topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Operator grammar and precedence
parser
A grammar that is generated to define the mathematical operators is
called operator grammar with some restrictions on grammar. An
operator precedence grammar is a context-free grammar that has the
property that no production has either an empty right-hand side (null
productions) or two adjacent non-terminals in its right-hand side.

Examples –
This is the example of operator grammar:

E->E+E/E*E/id

However, grammar that is given below is not an operator grammar


because two non-terminals are adjacent to each other:

S->SAS/a
A->bSb/b

Although, we can convert it into an operator grammar:

S->SbSbs/SbS/a
A->bSb/b

Operator precedence parser –


An operator precedence parser is a one of the bottom-up parser that
interprets an operator-precedence grammar. This parser is only used for
operator grammars. Ambiguous grammars are not allowed in case of any
parser except operator precedence parser.
There are two methods for determining what precedence relations should
hold between a pair of terminals:

1. Use the conventional associativity and precedence of operator.


2. The second method of selecting operator-precedence relations is first
to construct an unambiguous grammar for the language, a grammar
that reflects the correct associativity and precedence in its parse
trees.

This parser relies on the following three precedence relations: ⋖, ≐, ⋗


a ⋖ b This means a “yields precedence to” b.
a ⋗ b This means a “takes precedence over” b.
a ≐ b This means a “has precedence as” b.

Figure – Operator precedence relation table for grammar E->E+E/E*E/id

There is not given any relation between id and id as id will not be


compared and two variables can not come side by side. There is also a
disadvantage of this table as if we have n operators than size of table will
be n*n and complexity will be 0(n2). In order to increase the size of table,
use operator function table.

The operator precedence parsers usually do not store the precedence


table with the relations; rather they are implemented in a special way.
Operator precedence parsers use precedence functions that map
terminal symbols to integers, and so the precedence relations between
the symbols are implemented by numerical comparison. The parsing
table can be encoded by two precedence functions f and g that map
terminal symbols to integers. We select f and g such that:

1. f(a) < g(b) whenever a is precedence to b


2. f(a) = g(b) whenever a and b having precedence
3. f(a) > g(b) whenever a takes precedence over b

Example – Consider the following grammar:

E -> E + E/E * E/( E )/id

The directed graph representing the precedence function:

Since there is not any cycle in the graph so we can make function table:
fid -> g* -> f+ ->g+ -> f$
gid -> f* -> g* ->f+ -> g+ ->f$

Size of the table is 2n.

One disadvantage of function table is that evev though we have blank


entries in relation we got non-blank entries in function table. Blank entries
are also called error. Hence error detection capability of relational table is
greater than function table.

#include <stdio.h>
#include <string.h>

// function f to exit from the loop


// if given condition is not true
void f()
{
printf("Not operator grammar");
exit(0);
}

void main()
{
char grm[20][20], c;

// Here using flag variable,


// considering grammar is not operator grammar
int i, n, j = 2, flag = 0;

// taking number of productions from user


scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%s", grm[i]);

for (i = 0; i < n; i++) {


c = grm[i][2];

while (c != '&#092;&#048;') {

if (grm[i][3] == '+' || grm[i][3] == '-'


|| grm[i][3] == '*' || grm[i][3] == '/')

flag = 1;

else {

flag = 0;
f();
}

if (c == '$') {
flag = 0;
f();
}

c = grm[i][++j];
}
}

if (flag == 1)
printf("Operator grammar");
}

Input :3
A=A*A
B=AA
A=$
Output : Not operator grammar

Input :2
A=A/A
B=A+A

Output : Operator grammar

$ is a null production here which are also not allowed in operator


grammars.

Advantages –

1. It can easily be constructed by hand


2. It is simple to implement this type of parsing

Disadvantages –

1. It is hard to handle tokens like the minus sign (-), which has two
different precedence (depending on whether it is unary or binary)
2. It is applicable only to small class of grammars
Pragati_Agrawal
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Context-sensitive Grammar (CSG) and
Language (CSL)
Context-Sensitive Grammar –
A Context-sensitive grammar is an Unrestricted grammar in which all the
productions are of form –

Where α and β are strings of non-terminals and terminals.

Context-sensitive grammars are more powerful than context-free


grammars because there are some languages that can be described by
CSG but not by context-free grammars and CSL are less powerful than
Unrestricted grammar. That’s why context-sensitive grammars are
positioned between context-free and unrestricted grammars in the
Chomsky hierarchy.
Context-sensitive grammar has 4-tuples. G = {N, Σ, P, S}, Where
N = Set of non-terminal symbols
Σ = Set of terminal symbols
S = Start symbol of the production
P = Finite set of productions
All rules in P are of the form α1 A α2 –> α1 β α2

Context-sensitive Language: The language that can be defined by


context-sensitive grammar is called CSL. Properties of CSL are :

Union, intersection and concatenation of two context-sensitive


languages is context-sensitive.
Complement of a context-sensitive language is context-sensitive.

Example –

Consider the following CSG.


S → abc/aAbc
Ab → bA
Ac → Bbcc
bB → Bb
aB → aa/aaA
What is the language generated by this grammar?

Solution:
S → aAbc
→ abAc
→ abBbcc
→ aBbbcc
→ aaAbbcc
→ aabAbcc
→ aabbAcc
→ aabbBbccc
→ aabBbbccc
→ aaBbbbccc
→ aaabbbccc
The language generated by this grammar is {anbncn | n≥1}.

GATE CS Corner Questions

Practicing the following questions will help you test your knowledge. All
questions have been asked in GATE in previous years or in GATE Mock
Tests. It is highly recommended that you practice them.

1. GATE CS 2005, Question 55


2. GATE CS 2004, Question 87

References –
CSG – Wikipedia
CSG – csa.iisc.ernet.in
abhishek1
WorKing to be bEst veRsion of MySelf

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to
share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Pushdown Automata
Pushdown Automata
We have already discussed finite automata. But finite automata can be
used to accept only regular languages.
Pushdown Automata is a finite automata with extra memory called stack
which helps Pushdown automata to recognize Context Free Languages.

A Pushdown Automata (PDA) " can be defined as :"

Q is the set of states


∑ is the set of input symbols
Γ is the set of pushdown symbols (which can be pushed and popped
from stack)
q0 is the initial state
Z is the initial pushdown symbol (which is initially present in stack)
F is the set of final states
δ is a transition function which maps Q x { ∑ ∪ ɛ } x Γ into Q x Γ *. In a
given state, PDA will read input symbol and stack symbol (top of the
stack) and move to a new state and change the symbol of stack.

Example : Define the pushdown automata for language {anbn | n > 0}


Solution : M = " where Q = { q0, q1 } and ∑ = { a, b } and Γ = { A, Z } and
δ is given by :"

δ( q0, a, Z ) = { ( q0, AZ ) }
δ( q0, a, A) = { ( q0, AA ) }
δ( q0, b, A) = { ( q1, ɛ) }
δ( q1, b, A) = { ( q1, ɛ) }
δ( q1, ɛ, Z) = { ( q1, ɛ) }

Let us see how this automata works for aaabbb.

Explanation : Initially, the state of automata is q0 and symbol on stack is


Z and the input is aaabbb as shown in row 1. On reading ‘a’ (shown in
bold in row 2), the state will remain q0 and it will push symbol A on stack.
On next ‘a’ (shown in row 3), it will push another symbol A on stack. After
reading 3 a’s, the stack will be AAAZ with A on the top. After reading ‘b’
(as shown in row 5), it will pop A and move to state q1 and stack will be
AAZ. When all b’s are read, the state will be q1 and stack will be Z. In
row 8, on input symbol ‘ɛ’ and Z on stack, it will pop Z and stack will be
empty. This type of acceptance is known as acceptance by empty
stack.

Note :

The above pushdown automaton is deterministic in nature because


there is only one move from a state on an input symbol and stack
symbol.
The non-deterministic pushdown automata can have more than one
move from a state on an input symbol and stack symbol.
It is not always possible to convert non-deterministic pushdown
automata to deterministic pushdown automata.
Power of non-deterministic PDA is more as compared to deterministic
PDA as some languages which are accepted by NPDA but not by
deterministic PDA which will be discussed in next article.
The push down automata can either be implemented using empty
stack or by final state and one can be converted to another.

Question : Which of the following pairs have DIFFERENT expressive


power?

A. Deterministic finite automata(DFA) and Non-deterministic finite


automata(NFA)
B. Deterministic push down automata(DPDA)and Non-deterministic push
down automata(NPDA)
C. Deterministic single-tape Turing machine and Non-deterministic
single-tape Turing machine
D. Single-tape Turing machine and multi-tape Turing machine

Solution : Every NFA can be converted into DFA. So, there expressive
power is same. As discussed above, every NPDA can’t be converted to
DPDA. So, the power of NPDA and DPDA is not same. Hence option (B)
is correct.

This article has been contributed by Sonal Tuteja.


Please write comments if you find anything incorrect, or you want to
share more information about the topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Pushdown Automata Acceptance by
Final State
We have discussed Pushdown Automata (PDA) and its acceptance by
empty stack article. Now, in this article, we will discuss how PDA can
accept a CFL based on final state. Given a PDA P as:

P = (Q, Σ, Γ, δ, q0, Z, F)

The language accepted by P is the set of all strings consuming which


PDA can move from initial state to final state irrespective to any symbol
left on the stack which can be depicted as:

L(P) = {w |(q0, w, Z) =>(qf, ɛ, s)}

Here, from start state q0 and stack symbol Z, final state qf ɛ F is reached
when input w is consumed. The stack can contain string s which is
irrelevant as final state is reached and w will be accepted.

Example: Define the pushdown automata for language {a^nb^n | n > 0}


using final state.
Solution: M = where Q = {q0, q1, q2, q3} and ∑ = {a, b} and Γ = { A, Z }
and F={q3} and δ is given by:

δ( q0, a, Z ) = { ( q1, AZ ) }
δ( q1, a, A) = { ( q1, AA ) }
δ( q1, b, A) = { ( q2, ɛ) }
δ( q2, b, A) = { ( q2, ɛ) }
δ( q2, ɛ, Z) = { ( q3, Z) }

Let us see how this automata works for aaabbb:


Explanation: Initially, the state of automata is q0 and symbol on stack is
Z and the input is aaabbb as shown in row 0. On reading a (shown in
bold in row 1), the state will be changed to q1 and it will push symbol A
on stack. On next a (shown in row 2), it will push another symbol A on
stack and remain in state q1. After reading 3 a’s, the stack will be AAAZ
with A on the top.

After reading b (as shown in row 4), it will pop A and move to state q2
and stack will be AAZ. When all b’s are read, the state will be q2 and
stack will be Z. In row 7, on input symbol ɛ and Z on stack, it will move to
q3. As final state q3 has been reached after processing input, the string
will be accepted.
This type of acceptance is known as acceptance by final state.

Next we will see how this automata works for aab:


As we can see in row 4, the input has been processed and PDA is in
state q2 which is non-final state, the string aab will not be accepted.

Let us discuss question based on this:

Que-1. Consider the transition diagram of a PDA given below with input
alphabet ∑ = {a, b}and stack alphabet Γ = {X, Z}. Z is the initial stack
symbol. Let L denote the language accepted by the PDA. (GATE-CS-
2016)
Solution: We first label the state of given PDA as:

Next, the given PDA P can be written as:

Q = {q0, q1, q2} and ∑ = {a, b}


And Γ = {A, Z} and F={q0,q2} and δ is given by :
δ( q0, a, Z ) = {( q0, XZ)}
δ( q0, a, X) = {( q0, XX )}
δ( q0, b, X) = {( q1, ɛ)}
δ( q1, b, X) = {( q1, ɛ)}
δ( q1, ɛ, Z) = {( q2, Z)}

As we can see, q0 is initial as well as final state, ɛ will be accepted. For


every a, X is pushed onto stack and PDA remains in final state.
Therefore, any number of a’s can be accepted by PDA.

If input contains b, X is popped from stack for every b. Then PDA is


moved to final state if stack becomes empty after processing input (δ( q1,
ɛ, Z) = {( q2, Z)}). Therefore, number of b must be equal to number of b if
they exist.

As there is only one move for a given state and input, the PDA is
deterministic. So, correct option is (D).
This article is contributed by Sonal Tuteja. If you like GeeksforGeeks and
would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Construct Pushdown Automata for given
languages
Prerequisite – Pushdown Automata, Pushdown Automata Acceptance by
Final State
A push down automata is similar to deterministic finite automata except
that it has a few more properties than a DFA.The data structure used for
implementing a PDA is stack. A PDA has an output associated with every
input. All the inputs are either pushed into a stack or just ignored. User
can perform the basic push and pop operations on the stack which is use
for PDA. One of the problems associated with DFAs was that could not
make a count of number of characters which were given input to the
machine. This problem is avoided by PDA as it uses a stack which
provides us this facility also.

A Pushdown Automata (PDA) can be defined as –


M = (Q, Σ, Γ, δ, q0, Ζ, F) where

Q is a finite set of states


Σ is a finite set which is called the input alphabet
Γ is a finite set which is called the stack alphabet
δ is a finite subset of Q X ( Σ ∪ {ε} X Γ X Q X Γ*) the transition relation.
q0 ∈ Q is the start state
Ζ ∈ Γ is the initial stack symbol
F ⊆ Q is the set of accepting states

Representation of State Transition –


Representation of Push in a PDA –
Representation of Pop in a PDA –
Representation of Ignore in a PDA –

Q) Construct a PDA for language L = {0n1m2m3n | n>=1, m>=1}

Approach used in this PDA –


First 0’s are pushed into stack. Then 1’s are pushed into stack.
Then for every 2 as input a 1 is popped out of stack. If some 2’s are still
left and top of stack is a 0 then string is not accepted by the PDA.
Thereafter if 2’s are finished and top of stack is a 0 then for every 3 as
input equal number of 0’s are popped out of stack. If string is finished and
stack is empty then string is accepted by the PDA otherwise not
accepted.

Step-1: On receiving 0 push it onto stack. On receiving 1, push it onto


stack and goto next state
Step-2: On receiving 1 push it onto stack. On receiving 2, pop 1 from
stack and goto next state
Step-3: On receiving 2 pop 1 from stack. If all the 1’s have been
popped out of stack and now receive 3 then pop a 0 from stack and
goto next state
Step-4: On receiving 3 pop 0 from stack. If input is finished and stack
is empty then goto last state and string is accepted

Examples:

Input : 0 0 1 1 1 2 2 2 3 3
Result : ACCEPTED

Input : 0 0 0 1 1 2 2 2 3 3
Result : NOT ACCEPTED

Q) Construct a PDA for language L = {0n1m | n >= 1, m >= 1, m > n+2}

Approach used in this PDA –


First 0’s are pushed into stack.When 0’s are finished, two 1’s are ignored.
Thereafter for every 1 as input a 0 is popped out of stack. When stack is
empty and still some 1’s are left then all of them are ignored.

Step-1: On receiving 0 push it onto stack. On receiving 1, ignore it


and goto next state
Step-2: On receiving 1, ignore it and goto next state
Step-3: On receiving 1, pop a 0 from top of stack and go to next state
Step-4: On receiving 1, pop a 0 from top of stack. If stack is empty, on
receiving 1 ingore it and goto next state
Step-5: On receiving 1 ignore it. If input is finished then goto last state

Examples:

Input : 0 0 0 1 1 1 1 1 1
Result : ACCEPTED

Input : 0 0 0 0 1 1 1 1
Result : NOT ACCEPTED
RishabhMalik
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Construct Pushdown Automata for all
length palindrome
A Pushdown Automaton (PDA) is like an epsilon Non deterministic Finite
Automata (NFA) with infinite stack. PDA is a way to implement context
free languages. Hence, it is important to learn, how to draw PDA.

Here, take the example of odd length palindrome:


Que-1: Construct a PDA for language L = {wcw’ | w={0, 1}*} where w’ is
the reverse of w.

Approach used in this PDA –


Keep on pushing 0’s and 1’s no matter whatever is on the top of stack
until reach the middle element. When middle element ‘c’ is scanned then
process it without making any changes in stack. Now if scanned symbol
is ‘1’ and top of stack also contain ‘1’ then pop the element from top of
stack or if scanned symbol is ‘0’ and top of stack also contain ‘0’ then pop
the element from top of stack. If string becomes empty or scanned
symbol is ‘$’ and stack becomes empty, then reach to final state else
move to dead state.

Step 1: On receiving 0 or 1, keep on pushing it on top of stack without


going to next state.
Step 2: On receiving an element ‘c’, move to next state without
making any change in stack.
Step 3: On receiving an element, check if symbol scanned is ‘1’ and
top of stack also contain ‘1’ or if symbol scanned is ‘0’ and top of stack
also contain ‘0’ then pop the element from top of stack else move to
dead state. Keep on repeating step 3 until string becomes empty.
Step 4: Check if symbol scanned is ‘$’ and stack does not contain
any element then move to final state else move to dead state.

Examples:

Input : 1 0 1 0 1 0 1 0 1
Output :ACCEPTED

Input : 1 0 1 0 1 1 1 1 0
Output :NOT ACCEPTED

Now, take the example of even length palindrome:


Que-2: Construct a PDA for language L = {ww’ | w={0, 1}*} where w’ is
the reverse of w.

Approach used in this PDA –


For construction of even length palindrome, user has to use Non
Deterministic Pushdown Automata (NPDA). A NPDA is basically an NFA
with a stack added to it.
The NPDA for this language is identical to the previous one except for
epsilon transition. However, there is a significant difference, that this PDA
must guess when to stop pushing symbols, jump to the final state and
start matching off of the stack. Therefore this machine is decidedly non-
deterministic.
Keep on pushing 0’s and 1’s no matter whatever is on the top of stack
and at the same time keep a check on the input string, whether reach to
the second half of input string or not. If reach to last element of first half
of the input string then after processing the last element of first half of
input string make an epsilon move and move to next state. Now if
scanned symbol is ‘1’ and top of stack also contain ‘1’ then pop the
element from top of stack or if scanned symbol is ‘0’ and top of stack also
contain ‘0’ then pop the element from top of stack. If string becomes
empty or scanned symbol is ‘$’ and stack becomes empty, then reach to
final state else move to dead state.

Step 1: On receiving 0 or 1, keep on pushing it on top of stack and at


a same time keep on checking whether reach to second half of input
string or not.
Step 2: If reach to last element of first half of input string, then push
that element on top of stack and then make an epsilon move to next
state.
Step 3: On receiving an element, check if symbol scanned is ‘1’ and
top of stack also contain ‘1’ or if symbol scanned is ‘0’ and top of stack
also contain ‘0’ then pop the element from top of stack else move to
dead state. Keep on repeating step 3 until string becomes empty.
Step 4: Check if symbol scanned is ‘$’ and stack does not contain
any element then move to final state else move to dead state.

Examples:

Input : 1 0 0 1 1 1 1 0 0 1
Output :ACCEPTED

Input : 1 0 0 1 1 1
Output :NOT ACCEPTED

Now, take the example of all length palindrome, i.e. a PDA which can
accept both odd length palindrome and even length palindrome:
Que-3: Construct a PDA for language L = {ww’ | wcw’, w={0, 1}*} where
w’ is the reverse of w.

Approach used in this PDA –


For construction of all length palindrome, user has to use NPDA.
The approach is similar to above example, except now along with epsilon
move now user has to show one more transition move of symbol ‘c’ i.e. if
string is of odd length and if reach to middle element ‘c’ then just process
it and move to next state without making any change in stack.
Step 1: On receiving 0 or 1, keep on pushing it on top of stack and at
a same time keep on checking, if input string is of even length then
whether reach to second half of input string or not, however if the
input string is of odd length then keep on checking whether reach to
middle element or not.
Step 2: If input string is of even length and reach to last element of
first half of input string, then push that element on top of stack and
then make an epsilon move to next state or if the input string is of odd
length then on receiving an element ‘c’, move to next state without
making any change in stack.
Step 3: On receiving an element, check if symbol scanned is ‘1’ and
top of stack also contain ‘1’ or if symbol scanned is ‘0’ and top of stack
also contain ‘0’ then pop the element from top of stack else move to
dead state. Keep on repeating step 3 until string becomes empty.
Step 4: Check if symbol scanned is ‘$’ and stack does not contain
any element then move to final state else move to dead state.

Examples:
Input : 1 1 0 0 1 1 1 1 0 0 1 1
Output :ACCEPTED

Input : 1 0 1 0 1 0 1
Output :ACCEPTED

AmishTandon
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Turing Machine
Turing Machine
Turing Machine was invented by Alan Turing in 1936 and it is used to
accept Recursive Enumerable Languages (generated by Type-0
Grammar).

A turing machine consists of a tape of infinite length on which read and


writes operation can be performed. The tape consists of infinite cells on
which each cell either contains input symbol or
a special symbol called blank. It also consists of a head pointer which
points to cell currently being read and it can move in both directions. A
TM is expressed as a 7-tuple (Q, T, B, ∑, δ, q0, B, F) where:

Q is a finite set of states


T is the tape alphabet (symbols which can be written on Tape)
B is blank symbol (every cell is filled with B except input alphabet
initially)
∑ is the input alphabet (symbols which are part of input alphabet)
δ is a transition function which maps Q × T → Q × T × {L,R}.
Depending on its present state and present tape alphabet (pointed by
head pointer), it will move to new state, change the tape symbol (may
or may not) and move head pointer to either left or right.
q0 is the initial state
F is the set of final states. If any state of F is reached, input string is
accepted.

Let us construct a turing machine for L={0n1n|n>=1}

Q = {q0,q1,q2,q3} where q0 is initial state.


T = {0,1,X,Y,B} where B represents blank.
∑ = {0,1}
F = {q3}

Transition function δ is given in Table 1 as:

Illustration

Let us see how this turing machine works for 0011. Initially head points to
0 which is underlined and state is q0 as:

The move will be δ(q0, 0) = (q1, X, R). It means, it will go to state q1,
replace 0 by X and head will move to right as:

The move will be δ(q1, 0) = (q1, 0, R) which means it will remain in same
state and without changing any symbol, it will move to right as:
The move will be δ(q1, 1) = (q2, Y, L) which means it will move to q2
state and changing 1 to Y, it will move to left as:

Working on it in the same way, the machine will reach state q3 and head
will point to B as shown:

Using move δ(q3, B) = halt, it will stop and accepted.

Note:

In non-deterministic turing machine, there can be more than one


possible move for a given state and tape symbol, but non-
deterministic TM does not add any power.
Every non-deterministic TM can be converted into deterministic TM.
In multi-tape turing machine, there can be more than one tape and
corresponding head pointers, but it does not add any power to turing
machine.
Every multi-tape TM can be converted into single tape TM.

Question: A single tape Turing Machine M has two states q0 and q1, of
which q0 is the starting state. The tape alphabet of M is {0, 1, B} and its
input alphabet is {0, 1}. The symbol B is the blank symbol used to
indicate end of an input string. The transition function of M is described in
the following table.

The table is interpreted as illustrated below. The entry (q1, 1, R) in row q0


and column 1 signifies that if M is in state q0 and reads 1 on the current
tape square, then it writes 1 on the same tape square, moves its tape
head one position to the right and transitions to state q1. Which of the
following statements is true about M?

1. M does not halt on any string in (0 + 1)+


2. M does not halt on any string in (00 + 1)*
3. M halts on all string ending in a 0
4. M halts on all string ending in a 1

Solution: Let us see whether machine halts on string ‘1’. Initially state will
be q0, head will point to 1 as:

Using δ(q0, 1) = (q1, 1,


R), it will move to state q1 and head will move to right as:

Using δ(q1, B) = (q0,


B, L), it will move to state q0 and head will move to left as:

It will run in the same way again and again and not halt.

Option D says M halts on all string ending with 1, but it is not halting for 1.
So, option D is incorrect.

Let us see whether machine halts on string ‘0’. Initially state will be q0,
head will point to 1 as:

Using δ(q0, 0) = (q1, 1,


R), it will move to state q1 and head will move to right as:
Using δ(q1,B)=(q0,B,L),
it will move to state q0 and head will move to left as:

It will run in the same way again and again and not halt.

Option C says M halts on all string ending with 0, but it is not halting for 0.
So, option C is incorrect.

Option B says that TM does not halt for any string (00 + 1)*. But NULL
string is a part of (00 + 1)* and TM will halt for NULL string. For NULL
string, tape will be,

Using δ(q0, B) = halt, TM will halt. As TM is halting for NULL, this option
is also incorrect.
So, option (A) is correct.

This article is contributed by Sonal Tuteja. Please write comments if you


find anything incorrect, or you want to share more information about the
topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Construct a Turing Machine for language
L = {wwr | w ∈ {0, 1}}
Prerequisite – Turing Machine
The language L = {wwr | w ∈ {0, 1}} represents a kind of language where
you use only 2 character, i.e., 0 and 1. The first part of language can be
any string of 0 and 1. The second part is the reverse of the first part.
Combining both these parts out string will be formed. Any such string
which falls in this category will be accepted by this language. The
beginning and end of string is marked by $ sign.

For example, if first part w = 1 1 0 0 1 then second part wr = 1 0 0 1 1. It


is clearly visible that wr is the reverse of w, so the string 1 1 0 0 1 1 0 0 1
1 is a part of given language.

Examples –

Input : 0 0 1 1 1 1 0 0
Output : Accepted
Input : 1 0 1 0 0 1 0 1
Output : Accepted

Basic Representation –
Assumption: We will replace 0 by Y and 1 by X.

Approach Used –
First check the first symbol, if it’s 0 then replace it by Y and by X if it’s 1.
Then go to the end of string. So last symbol is same as first. We replace
it also by X or Y depending on it.
Now again come back to the position next to the symbol replace from the
starting and repeat the same process as told above.

One important thing to note is that since wr is reverse of w of both of


them will have equal number of symbols. Every time replace a nth
symbol from beginning of string, replace a corresponding nth symbol
from the end.

Step-1:
If symbol is 0 replace it by Y and move right, Go to state Q2
If symbol is 1 replace it by X and move right, Go to state Q1

Step-2:
If symbol is 0 replace it by 0 and move right, remain on same state
If symbol is 1 replace it by 1 and move right, remain on same state
——————————————————————-
If symbol is X replace it by X and move right, Go to state Q3
If symbol is Y replace it by Y and move right, Go to state Q3
If symbol is $ replace it by $ and move right, Go to state Q3

Step-3:
If symbol is 1 replace it by X and move left, Go to state Q4
If symbol is 0 replace it by Y and move left, Go to state Q5

Step-4:
If symbol is 1 replace it by 1 and move left
If symbol is 0 replace it by 0 and move left
Remain on same state

Step-5:
If symbol is X replace it by X and move right
If symbol is Y replace it by Y and move right
Go to state Q0

Step-6:
If symbol is X replace it by X and move right
If symbol is Y replace it by Y and move right
Go to state Q6
ELSE
Go to step 1

Step-7:
If symbol is X replace it by X and move right, Remain on same state
If symbol is Y replace it by Y and move right, Remain on same state
If symbol is $ replace it by $ and move left, STRING IS ACCEPTED,
GO TO FINAL STATE Q7
RishabhMalik
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Construct a Turing Machine for language
L = {ww | w ∈ {0,1}}
Prerequisite – Turing Machine
The language L = {ww | w ∈ {0, 1}} tells that every string of 0’s and 1’s
which is followed by itself falls under this language. The logic for solving
this problem can be divided into 2 parts:

1. Finding the mid point of the string


2. After we have found the mid point we match the symbols

Example – Lets understand it with the help of an example. Lets string 1 0


1 1 0 1, so w = 1 0 1 and string is of form (ww).
The first thing that we do is to find mid point. For his we convert 1 in the
beginning into Y and move right till end of string. Here we convert 1 into
y.

Now our string would look like Y 0 1 1 0 Y. Now move left till, find a X or
Y. When we do so, convert the 0 or 1 right of it to X or Y respectively and
then do the same on right end. Now our string would look like Y X 1 1 X
Y. Thereafter, convert these 1’s also and finally it would look like Y X Y Y
X Y.

At this point you have achieved first objective which was to find the mid
point. Now convert all X and Y on the left of mid point into 0 and 1
respectively, so string becomes 1 0 1 Y X Y. Now, convert the 1 into Y
and move right till, find Y in the beginning of right part of string and
convert this Y into a blank (denoted by B). Now string looks like Y 0 1 B X
Y.
Similarly, apply this on 0 and x followed by 1 and Y. After this string looks
like Y X Y B B B. Now that you have no 0 and 1 and all X and Y on right
part of string are converted into blanks so our string will be accepted.

Assuption: We will replace 0 by X and 1 by Y.

Approach Used –
The first thing is to find the mid point of the string, convert a 0 or 1 from
the beginning of the string into X or Y respectively and a corresponding 0
or 1 into X or Y from the end of the string. After continuously doing it a
point is reached when all 0’s and 1’s have been converted into X and Y
respectively. At this point you are on the mid point of the string. So, our
first objectively is fulfilled.

Now, convert all X’s and Y’s on the left of the mid point into 0’s and 1’s. At
this point the first half the string is in the form of 0 and 1. The second half
of the string is in the form of X and Y.

Now, start from the beginning of the string. If you have a 0 then convert it
into X and move right till reach the second half, here if we find X then
convert it into a blank(B). Then traverse back till find an X or a Y. We
convert the 0 or 1 on right of it into X or Y respectively and
correspondingly, convert its X or Y in the second half of string into a
blank(B).

Keep doing this till, converted all symbols on left part of string into X and
Y and all symbols on right of string into blanks. If any one part is
completely converted but still some symbols in other half are left
unchanged then string will not be accepted. If you did not find an X or Y
in second half for a corresponding 0 or 1 respectively in the first half.
Then also string will not be accepted.

Examples:

Input : 1 1 0 0 1 1 0 0
Output : Accepted

Input : 1 0 1 1 1 0 1
Output : Not accepted

Step-1:
If symbol is 0 replace it by X and move right
If symbol is 1 replace it by Y and move right,
Go to state Q1 and step 2
———————————————
If symbol is X replace it by X and move left or
If symbol is Y replace it by Y and move left,
Go to state Q4 and step 5

Step-1:
If symbol is 0 replace it by 0 and move right, remain on same state
If symbol is 1 replace it by 1 and move right, remain on same state
———————————————
If symbol is X replace it by X and move left or
If symbol is Y replace it by Y and move left or
If symbol is $ replace it by $ and move left, Go to state Q2 and step 3

Step-1:
If symbol is 0 replace it by X and move left, or
If symbol is 1 replace it by Y and move left,
Go to state Q3 and step 4

Step-1:
If symbol is 0 replace it by 0 and move left, remain on same state
If symbol is 1 replace it by 1 and move left, remain on same state
———————————————
If symbol is X replace it by X and move right or
If symbol is Y replace it by Y and move right,
Go to state Q0 and step 1

Step-1:
If symbol is X replace it by X and move left or
If symbol is Y replace it by Y and move left
Go to state Q4 and step 6

Step-1:
If symbol is X replace it by 0 and move left, remain on same state
If symbol is Y replace it by 1 and move left, remain on same state
–––––––––—–––––––––—
If symbol is $ replace it by $ and move right
Go to state Q4 and step 7

Step-1:
If symbol is 0 replace it by X and move right, go to state Q6 and step
8
If symbol is 1 replace it by Y and move right, go to state Q7 and step
9
–––––––––—–––––––––—
If symbol is B replace it by B and move left, STRING ACCEPTED, GO
TO FINAL STATE Q9

Step-1:
If symbol is 0 replace it by 0 and move right, remain on same state
If symbol is 1 replace it by 1 and move right, remain on same state
If symbol is B replace it by B and move right, remain on same state
–—––––––––––––––––––
If symbol is X replace it by B and move left
Go to state Q8 and step 10

Step-1:

If symbol is 0 replace it by 0 and move right, remain on same state


If symbol is 1 replace it by 1 and move right, remain on same state
If symbol is B replace it by B and move right, remain on same state
–—––––––––––––––––––
If symbol is Y replace it by B and move left
Go to state Q8 and step 10

Step-1:
If symbol is 0 replace it by 0 and move left, remain on same state
If symbol is 1 replace it by 1 and move left, remain on same state
If symbol is B replace it by B and move left, remain on same state
–—––––––––––––––––––
If symbol is Y replace it by Y and move right or
If symbol is X replace it by X and move right
Go to state Q5 and step 7
RishabhMalik
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write
an article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Recursive and Recursive Enumerable
Languages
Recursive Enumerable (RE) or Type -0 Language

RE languages or type-0 languages are generated by type-0 grammars.


An RE language can be accepted or recognized by Turing machine
which means it will enter into final state for the strings of language and
may or may not enter into rejecting state for the strings which are not part
of the language. It means TM can loop forever for the strings which are
not a part of the language. RE languages are also called as Turing
recognizable languages.

Recursive Language (REC)

A recursive language (subset of RE) can be decided by Turing machine


which means it will enter into final state for the strings of language and
rejecting state for the strings which are not part of the language. e.g.; L=
{anbncn|n>=1} is recursive because we can construct a turing machine
which will move to final state if the string is of the form anbncn else move
to non-final state. So the TM will always halt in this case. REC languages
are also called as Turing decidable languages. The relationship between
RE and REC languages can be shown in Figure 1.
Closure Properties of Recursive Languages

Union: If L1 and If L2 are two recursive languages, their union L1∪L2


will also be recursive because if TM halts for L1 and halts for L2, it will
also halt for L1∪L2.
Concatenation: If L1 and If L2 are two recursive languages, their
concatenation L1.L2 will also be recursive. For Example:

L1= {anbncn|n>=0}
L2= {dmemfm|m>=0}
L3= L1.L2
= {anbncndm emfm|m>=0 and n>=0} is also recursive.

L1 says n no. of a’s followed by n no. of b’s followed by n no. of c’s.


L2 says m no. of d’s followed by m no. of e’s followed by m no. of f’s.
Their concatenation first matches no. of a’s, b’s and c’s and then
matches no. of d’s, e’s and f’s. So it can be decided by TM.
Kleene Closure: If L1is recursive, its kleene closure L1* will also be
recursive. For Example:

L1= {anbncn|n>=0}
L1*= { anbncn||n>=0}* is also recursive.

Intersection and complement: If L1 and If L2 are two recursive


languages, their intersection L1 ∩ L2 will also be recursive. For
Example:

L1= {anbncndm|n>=0 and m>=0}


L2= {anbncndn|n>=0 and m>=0}
L3=L1 ∩ L2
= { anbncndn |n>=0} will be recursive.

L1 says n no. of a’s followed by n no. of b’s followed by n no. of c’s


and then any no. of d’s. L2 says any no. of a’s followed by n no. of b’s
followed by n no. of c’s followed by n no. of d’s. Their intersection
says n no. of a’s followed by n no. of b’s followed by n no. of c’s
followed by n no. of d’s. So it can be decided by turing machine,
hence recursive.
Similarly, complementof recursive language L1 which is ∑*-L1, will
also be recursive.

Note: As opposed to REC languages, RE languages are not closed


under complementon which means complement of RE language need
not be RE.

GATE Questions

Question 1: Which of the following statements is/are FALSE?


1.For every non-deterministic TM, there exists an equivalent deterministic
TM.
2.Turing recognizable languages are closed under union and
complementation.
3.Turing decidable languages are closed under intersection and
complementation.
4.Turing recognizable languages are closed under union and
intersection.

A.1 and 4
B.1 and 3
C.2
D.3

Solution:

Statement 1 is true as we can convert every non-deterministic TM to


deterministic TM.
Statement 2 is false as Turing recognizable languages (RE languages)
are not closed under complementation.
Statement 3 is true as Turing decidable languages (REC languages) are
closed under intersection and complementation.
Statement 4 is true as Turing recognizable languages (RE languages)
are closed under union and intersection.

Question 2 : Let L be a language and L’ be its complement. Which


one of the following is NOT a viable possibility?
A.Neither L nor L’ is RE.
B.One of L and L’ is RE but not recursive; the other is not RE.
C.Both L and L’ are RE but not recursive.
D.Both L and L’ are recursive.
Solution:

Option A is correct because if L is not RE, its complementation will not be


RE. Option B is correct because if L is RE, L’ need not be RE or vice
versa because RE languages are not closed under complementation.
Option C is false because if L is RE, L’ will not be RE. But if L is recursive,
L’ will also be recursive and both will be RE as well because REC
languages are subset of RE. As they have mentioned not to be REC, so
option is false.
Option D is correct because if L is recursive L’ will also be recursive.

Question 3: Let L1 be a recursive language, and let L2 be a recursively


enumerable but not a recursive language. Which one of the following is
TRUE?

A.L1′ is recursive and L2′ is recursively enumerable


B.L1′ is recursive and L2′ is not recursively enumerable
C.L1′ and L2′ are recursively enumerable
D.L1′ is recursively enumerable and L2′ is recursive
Solution:

Option A is False as L2’ can’t be recursive enumerable (L2 is RE and RE


are not closed under complementation).
Option B is correct as L1’ is REC (REC languages are closed under
complementation) and L2’ is not recursive enumerable (RE languages
are not closed under complementation).
Option C is False as L2’ can’t be recursive enumerable (L2 is RE and RE
are not closed under complementation).
Option D is False as L2’ can’t be recursive enumerable (L2 is RE and RE
languages are not closed under complementation). As REC languages
are subset of RE, L2’ can’t be REC as well.

This article is contributed by Sonal Tuteja. Please write comments if you


find anything incorrect, or you want to share more information about the
topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Decidability
Decidable and undecidable problems
Prerequisite – Turing Machine

A problem is said to be Decidable if we can always construct a


corresponding algorithm that can answer the problem correctly. We can
intuitively understand Decidable problems by considering a simple
example. Suppose we are asked to compute all the prime numbers in the
range of 1000 to 2000. To find the solution of this problem, we can easily
devise an algorithm that can enumerate all the prime numbers in this
range.

Now talking about Decidability in terms of a Turing machine, a problem is


said to be a Decidable problem if there exist a corresponding Turing
machine which halts on every input with an answer- yes or no. It is also
important to know that these problems are termed as Turing Decidable
since a Turing machine always halts on every input, accepting or
rejecting it.

Semi- Decidable Problems –


Semi-Decidable problems are those for which a Turing machine halts on
the input accepted by it but it can either halt or loop forever on the input
which is rejected by the Turing Machine. Such problems are termed as
Turing Recognisable problems.

Examples – We will now consider few important Decidable problems:


Are two regular languages L and M equivalent?
We can easily check this by using Set Difference operation.
L-M =Null and M-L =Null.
Hence (L-M) U (M-L) = Null, then L,M are equivalent.
Membership of a CFL?
We can always find whether a string exist in a given CFL by using on
algorithm based on dynamic programming.
Emptiness of a CFL
By checking the production rules of the CFL we can easily state
whether the language generates any strings or not.

Undecidable Problems –
The problems for which we can’t construct an algorithm that can answer
the problem correctly in a finite time are termed as Undecidable
Problems. These problems are not even partially decidable and can lead
a Turing machine to loop forever without answering at all.

We can understand Undecidable Problems intuitively by considering


Fermat’s Theorem, a popular Undecidable Problem which states that no
three positive integers a, b and c for any n>=2 can ever satisfy the
equation: a^n + b^n = c^n.

If we feed this problem to a Turing machine to find such a solution which


gives a contradiction then a Turing Machine might run forever, to find the
suitable values of n, a, b and c. But we are always unsure whether a
contradiction exists or not and hence we term this problem as an
Undecidable Problem.

Examples – These are few important Undecidable Problems:


Whether a CFG generates all the strings or not?
As a CFG generates infinite strings ,we can’t ever reach up to the last
string and hence it is Undecidable.
Whether two CFG L and M equal?
Since we cannot determine all the strings of any CFG , we can predict
that two CFG are equal or not.
Ambiguity of CFG?
There exist no algorithm which can check whether for the ambiguity of
a CFL. We can only check if any particular string of the CFL
generates two different parse trees then the CFL is ambiguous.
Is it possible to convert a given ambiguous CFG into corresponding
non-ambiguous CFL?
It is also an Undecidable Problem as there doesn’t exist any algorithm
for the conversion of an ambiguous CFL to non-ambiguous CFL.
Is a language Learning which is a CFL, regular?
This is an Undecidable Problem as we can not find from the
production rules of the CFL whether it is regular or not.

Some more Undecidable Problems related to Turing machine:

Membership problem of a Turing Machine?


Finiteness of a Turing Machine?
Emptiness of a Turing Machine?
Whether the language accepted by Turing Machine is regular or CFL?

Read next articles – Decidability, Undecidability and Reducibility


This article is contributed by Aishwarya Agarwal. If you like
GeeksforGeeks and would like to contribute, you can also write an article
using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above.

GATE CS
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.
Decidability
Identifying languages (or problems*) as decidable, undecidable or
partially decidable is a very common question in GATE. With correct
knowledge and ample experience, this question becomes very easy to
solve.

Lets start with some definitions:-

Recursive language(REC) – A language ‘L’ is said to be recursive if


there exists a Turing machine which will accept all the strings in ‘L’ and
reject all the strings not in ‘L’. The Turing machine will halt every time and
give an answer(accepted or rejected) for each and every string input.

Recursively enumerable language(RE) – A language ‘L’ is said to be a


recursively enumerable language if there exists a Turing machine which
will accept (and therefore halt) for all the input strings which are in ‘L’ but
may or may not halt for all input strings which are not in ‘L’. By definition ,
all REC languages are also RE languages but not all RE languages are
REC languages.

Decidable language – A language ‘L’ is decidable if it is a recursive


language. All decidable languages are recursive languages and vice-
versa.
Partially decidable language – A language ‘L’ is partially decidable if ‘L’
is a RE language.

Undecidable language – A language is undecidable if it is not decidable.


An undecidable language maybe a partially decidable language or
something else but not decidable. If a language is not even partially
decidable , then there exists no Turing machine for that language.

Now lets solve some examples –

One way to solve decidability problems is by trying to reduce an already


known undecidable problem to the given problem. By reducing a problem
P1 to P2, we mean that we are trying to solve P1 by using the algorithm
used to solve P2.

If we can reduce an already known undecidable problem P1 to a given


problem P2 , then we can surely say that P2 is also undecidable. If P2
was decidable, then P1 would also be decidable but that becomes a
contradiction because P1 is known to be undecidable.

For eg.

1. Given a Turing machine ‘M’, we need to find out whether a state


‘Q’ is ever reached when a string ‘w’ is entered in ‘M’. This problem
is also known as the ‘State Entry problem’.

Now lets try to reduce the Halting problem to the State Entry problem. A
Turing machine only halts when a transition function δ (qi , a) is not
defined. Change every undefined function δ(qi,a) to δ(qi,a) = (Q, a, L or
R). Note that the state Q can only be reached when the Turing machine
halts.

Suppose we have have an algorithm for solving the State Entry problem
which will halt every time and tell us whether state Q can be reached or
not. By telling us that we can or cannot reach state Q every time, it is
telling us that the Turing machine will or will not halt, every time. But we
know that is not possible because the halting problem is undecidable.
That means that our assumption that there exists an algorithm which
solves the State Entry problem and halts and gives us an answer every
time, is false. Hence, the state entry problem is undecidable.

2. Given two regular languages L1 and L2, is the problem of finding


whether a string ‘w’ exists in both L1 and L2, a decidable problem or
not.

First we make two Turing machines TM1 and TM2 which simulate the
DFAs of languages L1 and L2 respectively. We know that a DFA always
halts, so a Turing machine simulating a DFA will also always halt. We
enter the string ‘w’ in TM1 and TM2. Both Turing machines will halt and
give us an answer. We can connect the outputs of the Turing machines to
a modified ‘AND’ gate which will output ‘yes’ only when both the Turing
machines answer ‘yes’. Otherwise it will output ‘no’.

Since this system of two Turing machines and a modified AND gate will
always stop, this problem is a decidable problem.
There are a lot of questions on this topic. There is no universal algorithm
to solve them. Most of the questions require unique and ingenious proofs.
Here is where experience is needed. By solving a lot of these problems,
one can become very quick in coming up with proofs for these problems
on the spot. So, keep practicing.

*The words ‘language’ and ‘problem’ can be used synonymously in


Theory of computation. For eg. The ‘Halting problem’ can also be written
as ‘L = {<M, w> | Turing machine ‘M’ halts on input ‘w’}’. Here ‘L’ is a
language.

This article has been contributed by Nitish Joshi.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Undecidability and Reducibility

Decidable Problems
A problem is decidable if we can construct a Turing machine which will
halt in finite amount of time for every input and give answer as ‘yes’ or
‘no’. A decidable problem has an algorithm to determine the answer for a
given input.

Examples

Equivalence of two regular languages: Given two regular


languages, there is an algorithm and Turing machine to decide
whether two regular languages are equal or not.
Finiteness of regular language: Given a regular language, there is
an algorithm and Turing machine to decide whether regular language
is finite or not.
Emptiness of context free language: Given a context free
language, there is an algorithm whether CFL is empty or not.

Undecidable Problems
A problem is undecidable if there is no Turing machine which will always
halt in finite amount of time to give answer as ‘yes’ or ‘no’. An
undecidable problem has no algorithm to determine the answer for a
given input.

Examples
Ambiguity of context-free languages: Given a context-free
language, there is no Turing machine which will always halt in finite
amount of time and give answer whether language is ambiguous or
not.
Equivalence of two context-free languages: Given two context-free
languages, there is no Turing machine which will always halt in finite
amount of time and give answer whether two context free languages
are equal or not.
Everything or completeness of CFG: Given a CFG and input
alphabet, whether CFG will generate all possible strings of input
alphabet (∑*)is undecidable.
Regularity of CFL, CSL, REC and REC: Given a CFL, CSL, REC or
REC, determining whether this language is regular is undecidable.

Note: Two popular undecidable problems are halting problem of TM and


PCP (Post Correspondence Problem). Semi-decidable Problems
A semi-decidable problem is subset of undecidable problems for which
Turing machine will always halt in finite amount of time for answer as
‘yes’ and may or may not halt for answer as ‘no’.
Relationship between semi-decidable and decidable problem has been
shown in Figure 1 as:
Rice’s Theorem
Every non-trivial (answer is not known) problem on Recursive
Enumerable languages is undecidable.e.g.; If a language is Recursive
Enumerable, its complement will be recursive enumerable or not is
undecidable.

Reducibility and Undecidability


Language A is reducible to language B (represented as A≤B) if there
exists a function f which will convert strings in A to strings in B as:

w ɛ A <=> f(w) ɛ B

Theorem 1: If A≤B and B is decidable then A is also decidable.


Theorem 2: If A≤B and A is undecidable then B is also undecidable.

Question: Which of the following is/are undecidable?

1. G is a CFG. Is L(G)=ɸ?
2. G is a CFG. Is L(G)=∑*?
3. M is a Turing machine. Is L(M) regular?
4. A is a DFA and N is an NFA. Is L(A)=L(N)?
A. 3 only
B. 3 and 4 only
C. 1, 2 and 3 only
D. 2 and 3 only

Explanation:

Option 1 is whether a CFG is empty or not, this problem is decidable.


Option 2 is whether a CFG will generate all possible strings
(everything or completeness of CFG), this problem is undecidable.
Option 3 is whether language generated by TM is regular is
undecidable.
Option 4 is whether language generated by DFA and NFA are same is
decidable. So option D is correct.

Question: Which of the following problems are decidable?

1. Does a given program ever produce an output?


2. If L is context free language then L’ is also context free?
3. If L is regular language then L’ is also regular?
4. If L is recursive language then L’ is also recursive?

A. 1,2,3,4
B. 1,2
C. 2,3,4
D. 3,4

Explanation:

As regular and recursive languages are closed under


complementation, option 3 and 4 are decidable problems.
Context free languages are not closed under complementation, option
3 is undecidable.
Option 1 is also undecidable as there is no TM to determine whether
a given program will produce an output. So, option D is correct.

Question: Consider three decision problems P1, P2 and P3. It is


known that P1 is decidable and P2 is undecidable. Which one of the
following is TRUE?

A. P3 is undecidable if P2 is reducible to P3
B. P3 is decidable if P3 is reducible to P2’s complement
C. P3 is undecidable if P3 is reducible to P2
D. P3 is decidable if P1 is reducible to P3
Explanation:

Option A says P2≤P3. According to theorem 2 discussed, if P2 is


undecidable then P3 is undecidable. It is given that P2 is undecidable,
so P3 will also be undecidable. So option (A) is correct.
Option C says P3≤P2. According to theorem 2 discussed, if P3 is
undecidable then P2 is undecidable. But it is not given in question
about undecidability of P3. So option (C) is not correct.
Option D says P1≤P3. According to theorem 1 discussed, if P3 is
decidable then P1 is also decidable. But it is not given in question
about decidability of P3. So option (D) is not correct.
Option (B) says P3≤P2’. According to theorem 2 discussed, if P3 is
undecidable then P2’ is undecidable. But it is not given in question
about undecidability of P3. So option (B) is not correct.

Quiz on Undecidability
This article is contributed by Sonal Tuteja. Please write comments if you
find anything incorrect, or you want to share more information about the
topic discussed above

Theory of Computation & Automata


Please write to us at [email protected] to report any issue
with the above content.
Quick Links
Last Minute Notes – Theory of
Computation
See Last Minute Notes on all subjects here.

We will discuss the important key points useful for GATE exams in
summarized form. For details you may refer this.

Finite Automata: It is used to recognize patterns of specific type


input. It is the most restricted type of automata which can accept only
regular languages (languages which can be expressed by regular
expression using OR (+), Concatenation (.), Kleene Closure(*) like
a*b*, (a+b) etc.)

Deterministic FA and Non-Deterministic FA: In deterministic FA,


there is only one move from every state on every input symbol but in
Non-Deterministic FA, there can be zero or more than one move from
one state for an input symbol.
Note:
Language accepted by NDFA and DFA are same.
Power of NDFA and DFA is same.
No. of states in NDFA is less than or equal to no. of states in
equivalent DFA.
For NFA with n-states, in worst case, the maximum states possible
in DFA is 2n
Every NFA can be converted to corresponding DFA.

Identities of Regular Expression :


Φ+R=R+Φ=R
Φ*R=R*Φ=Φ
ε*R=R*ε=R
ε* = ε
Φ* = ε
ε + RR* = R*R + ε = R*

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


(ab*)*

Push Down Automata: Pushdown Automata has extra memory


called stack which gives more power than Finite automata. It is used
to recognize context free languages.

Deterministic and Non-Deterministic PDA: In deterministic PDA,


there is only one move from every state on every input symbol but in
Non-Deterministic PDA, there can be more than one move from one
state for an input symbol.
Note:
Power of NPDA is more than DPDA.
It is not possible to convert every NPDA to corresponding DPDA.
Language accepted by DPDA is subset of language accepted by
NPDA.
The languages accepted by DPDA are called DCFL (Deterministic
Context Free Languages) which are subset of NCFL (Non
Deterministic CFL) accepted by NPDA.

Linear Bound Automata: Linear Bound Automata has finite amount


of memory called tape which can be used to recognize Context
Sensitive Languages.
LBA is more powerful than Push down automata.

FA < PDA < LBA < TM

Turing Machine :Turing machine has infinite size tape and it is used
to accept Recursive Enumerable Languages.
Turing Machine can move in both directions. Also, it doesn’t accept
ε.
If the string inserted in not in language, machine will halt in non-
final state.

Deterministic and Non-Deterministic Turing Machines: In


deterministic turing machine, there is only one move from every state
on every input symbol but in Non-Deterministic turing machine, there
can be more than one move from one state for an input symbol.
Note:
Language accepted by NTM, multi-tape TM and DTM are same.
Power of NTM, Multi-Tape TM and DTM is same.
Every NTM can be converted to corresponding DTM.

Chomsky Classification of Languages:

Grammar Production Language


Automata Closed Und
Type Rules Accepted
A→a or A→aB Union,
Type-3 where A,B Intersection,
Finite
(Regular N(non Regular Complementa
Automata
Gramar) terminal) and Concatenation
a T(Terminal) Kleene Closur

Type-2 A->ρ where A


N Push
(Context Context
Down Concatenatio
Free and ρ Free
Automata Kleene Closu
Grammar) (T N)*

α→β where α,
Type-1 β (T N)* Union,
and len(α) <= Linear Intersection,
(Context Context
len(β) and α Bound Complementa
Sensitive Sensitive
should contain Automata Concatenation
Grammar)
atleast 1 non Kleene Closur
terminal.

α → β where α,
Type-0 β (T N)*
Recursive Turing
(Recursive and α contains
Enumerable) atleast 1 non- Enumerable Machine Concatenatio
terminal Kleene Closu

Relationship between these can be represented as:

Decidable and Undecidable Problems:


A language is Decidable or Recursive if a Turing machine can be
constructed which accepts the strings which are part of language and
rejects others. e.g.; A number is prime or not is a decidable problem.
A language is Semi–Decidable or Recursive Enumerable if a turing
machine can be constructed which accepts the strings which are part
of language and it may loop forever for strings which are not part of
language.
A problem is undecidable if we can’t construct an algorithms and
Turing machine which can give yes or no answer. e.g.; Whether a
CFG is ambiguous or not is undecidable.

Countability :
Set of all strings over any finite alphabet are countable.
Every subset of countable set is either finite or countable.
Set of all Turing Machines are countable.
The set of all languages that are not recursive enumerable is
Uncountable.

This article has been contributed by Sonal Tuteja.

Please write comments if you find anything incorrect, or you want to


share more information about the topic discussed above
Theory of Computation & Automata
Please write to us at [email protected] to report any issue
with the above content.

You might also like