TOC Sanchit Sir Notes
TOC Sanchit Sir Notes
PROBLEM
• Now a day’s machines (digital, analog, mechanical) play a very important role in the
development of human, we need some mechanism (language) to communicate with the
machines.
SOLUTION
ALPHABET- An alphabet is a finite non empty set of symbols, (every language has its own
alphabet). here in toc, we use symbol Σ for depicting alphabet. e.g. Σ = {0,1}. for English Σ = {a,
b, c, …., z} (in English also alphabet is a set of letters, thought in general we called them as
alphabet).
STRING - It is a sequence of symbols (which are the member of set alphabet). E.g. Σ = {a, b}
String- aabb, aa, b, so on. (in English we called them as words).
Length of a string - It is defined as number symbol in the string. Denoted like |W|, e.g.
length of string |00110| = 5.
E.g. w = ab, x= ba
wx = abba
xw = baab
w = w1w2w3……wm x = x1x2x3……xn
wx = w1w2w3……wmx1x2x3……xn
|wx| = |w| + |x|
e.g. W = w1w2w3……wn
wr = wn……w3w2w1
|w|=|wr|
Empty/Null String- The string with zero occurrence of symbols. It is denoted by ∈, |∈|=0.
If there is a string w, then wn stands for the strings obtained by repeating w, n times.
w1 = w
w2 = ww
w3 = www
w0 = ε
w∈ = ∈w = w
Substring- Any string of consecutive symbols in some string ‘w’ can be collectively said as a
substring. E.g. w= abab its substrings can be ab, a, ba…etc.
• If w is any string than empty string ∈ and the string w itself is called a trivial substring
and the remaining of the other are the non-trivial sub string[[n(n+1)/2]-1]
• If a string has ‘n’ distinct symbols then total number of different sub string will be
[n(n+1)/2] +1
• If there is a string of length n then no of prefix or suffix will be n+1
Q Consider a sting ‘GATE’ find the total number of prefix and suffix possible?
• if symbols are in order but not necessarily consecutive, then it is called subsequence.
Q if ∑ = {a, b} then, find the following?
∑0 =
∑1 =
∑2 =
∑3 =
∑K is the set of all the strings from the alphabet ∑ of length exactly K.
Kleene closure- If ∑ is a set of symbols, then we use ∑* to denote the set of strings
obtained by concatenating zero or more symbols from ∑ of any length, in general any string of
any length which can have only symbols specified in ∑.
∑* = ⋃𝑖=∞
𝑖=0 {𝑤 | |𝑤| = 𝑖} (using the symbols from the alphabet ∑)
Positive closure – If ∑ is a set of symbols, then we use ∑+ to denote the set of strings
obtained by concatenating one or more symbols from ∑ of any length, in general any string of
any length which can have only symbols specified in ∑ (except ∈).
∑* = ⋃𝑖=∞
𝑖=0 {𝑤 | |𝑤| = 𝑖} (using the symbols from the alphabet ∑)
it is denoted by ∑+ = ∑* - ∈
LANGUAGES
Since languages are sets, the union, intersection and difference of two languages are
immediately defined.
Q Given the language L = {ab, aa, baa}, which of the following strings are in L*? (GATE-2012) (1
Marks)
Q The number of substrings that can be formed from string given by ‘adefbghnmp’ is? (NET-
DEC-2018)
a) 10 b) 45 c) 55 d) 56
Ans: d
Q Suppose 𝐿1 = {10, 1} and 𝐿2 = {011, 11}. How many distinct elements are there in L = 𝐿1 𝐿2
a) 4 b) 3 c) 2 d) None of these
Q Let 𝑥𝑛 denote the number of binary strings of length n that contain no consecutive 0s, which
of the following recurrences does 𝑥𝑛 satisfy?
a) 𝑥𝑛 = 2𝑥𝑛−1 b) 𝑥𝑛 = 𝑥[𝑛] + 1
2
Ans: d
E. n – 1
F. 2n
a) 1-A, 2-B, 3-D, 4-F b) 1-B, 2-C, 3-E, 4-A
c) 1-D, 2-B, 3-C, 4-F d) 1-E, 2-F, 3-B, 4-C
Ans: c
Automaton
An automaton is defined as a system where energy, materials and information are transformed,
transmitted and used for performing some functions without direct participation of man.
Example: automatic machine tools, automatic packing machines, and automatic photo printing
machines.
• Below is the figure of a discrete automaton:
• Output. O0, O1 … Oq are the outputs of the model, each of which can take a finite number
of fixed values from an output O.
• States. At any instant of time the automaton can be in one of the states ql, q2, ... qn
• State relation. The next state of an automaton at any instant of time is determined by the
present state and the present input.
• Output relation. The output is related to either state only or to both the input and the
state. It should be noted that at any instant of time the automaton is in some state. On
'reading' an input symbol, the automaton moves to a next state which is given by the state
relation.
Only finite amount of information can be in the control unit by placing the unit into a specific
state but since the number of states is finite, a finite automaton can only deal with situation in
which the information to be stored at any time is strictly bounded.
DETERMINISTIC FINITE AUTOMATA
A deterministic finite automaton (DFA) is defined by 5-tuple (Q,,,S,F) where:
is a transition function, ( : Q × → Q)
NOTE-
• Produces a unique computation (or run) of the automaton for each input string.
• Deterministic refers to the uniqueness of the computation.
• DFAs are useful for doing lexical analysis (Spell check) in compiler design
Representation
TRANSITION STATE DIAGRAM- (Graphical, easy to understand, easy to design) A transition
graph or a transition system is a finite directed labelled graph in which each circle represents a
state and the directed edges indicate the transition of one state to another state. The initial
state is represented by a circle with an arrow pointing towards it, the final state by two
concentric circles.
TRANSITION TABLE- it is a two-dimensional table where number of columns is equal to
number of input alphabets and number of rows is equal to number of states.
a b
Q q0
q1
TRANSITION ID- δ {qi, a) = qj, this means that on a state qi take an input symbol a machine will
make a transition qj
ACCEPTANCE BY DFA
Let ‘w’ be any string designed from the alphabet , corresponding to w, if there exist a
transition for which it starts at the initial state and ends in any One of the final states, then the
string ‘w’ is said to be accepted by the finite automata. δ (q0, w) = qf for some qf ∈ F.
IF the final state is 𝑞4 , the which of the following strings will be accepted?
1. aaaaa 2. aabbaabbbbb 3. bbabababbb
a) 1 and 2 b) 2 and 3 c) 3 and 1 d) all of the above
Ans: d
Q In the automaton below, s is the start state and t are the only final state. (GATE-2006) (2
Marks)
Consider the strings u = ababab, v = bab, and w = aabb. Which of the following statements is
true?
(A) The automaton accepts u and v but not w
(B) The automaton accepts each of u, v, and w
(C) The automaton rejects each of u, v, and w
(D) The automaton accepts u but rejects v and w
Answer: (D)
DFA Designing
A language a said to be regular language if it can be accepted by a DFA
The best knowledge about DFA can be only understood by designing a number of DFA, by doing
so we will first understand the process of DFA designing and secondly, we will understand
Regular Language.
Q Draw a DFA for the language accepting strings ending with ’01’ over input alphabets ∑ = {0,
1}?
Ans. The required DFA for the above problem will be:
Q Draw a DFA for the language accepting strings ending with ‘abb’ over input alphabets ∑ = {a,
b}?
Ans. The required DFA for the above problem will be:
Q Draw a DFA for the language accepting strings starting with ‘ab’ over input alphabets ∑ = {a,
b}?
Ans.
• If the string starts with b we direct the transition to a dead state such that it never gets
accepted.
• If we encounter two simultaneous a’s in the beginning, we direct the transition to dead
state.
Q Construct a DFA that accepts a language L over input alphabets ∑ = {a, b} such that L is the set
of all strings starting with ‘aa’ or ‘bb’?
Ans.
• In this case D acts as a dead state, if we encounter ab or ba as starting inputs we put the
transitions into a dead state.
Q Design a minimal DFA that accepts a language ‘L’, where L={a} over the alphabet ∑={a}.
Q design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, where every
accepted string ‘w’ starts with substring s
i) s = b ii) s = ab iii) s = abb
conclusion→ if w = sx, |s| = m, then no of states in the DFA is m+2
Q design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}. where every
accepted string ‘w’ ends with substring ‘s’.
i) s = ab ii) s = aa iii) s = bab
conclusion→ if w = xs, |s| = m, then no of states in the DFA is m+1
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}. where every
accepted string ‘w’ contains sub string s.
i) abb ii) aba
conclusion→ if w = xsx, |s| = m, then no of states in the DFA is m+1
Q No of states in a minimal in a that accepts all the strings of a, b such that every string must
have ‘aabab’ as sub stray
a) 5 b) 6 c) 7 d) none
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string start and end with a.
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string start and end with same symbol.
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string start and end with different symbol.
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string w, is like w=SX.
i) s= aa/bb ii) s=aaa/bbb
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string w, is like w=XS.
i) s= aa/bb ii) s=aaa/bbb
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string w, is like w=XSX.
i) s= aa/bb ii) s=aaa/bbb
a) L(M) = {words starting with aa or bb} b) L(M) = {words ending with aa or bb}
c) L(M) = {words containing aa or bb as a sub word} d) None of these
Q Let L be the set of all binary strings who’s last two symbols are the same. The number of
states in the minimum state deterministic finite 0 state automation accepting L is (GATE-
1998) (1 Marks)
a) 2 b) 5 c) 8 d) 3
Ans: b
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string ‘w’ accepted must be like
i) |w| = 3 ii) |w|<=3 iii) |w|>=3
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain exactly two a’s.
Conclusion→ no of states will be n+2, if number of symbols is n
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain at least two a’s.
Conclusion→ no of states will be n+1, if number of symbols is n
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain at most two a’s.
Conclusion→ no of states will be n+2, if number of symbols is n
Q The following DFA accepts the set of all strings over {0, 1} that (GATE-2009) (1 Marks)
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string ‘w’ accepted must be like
i) |w| = 0(mod 3) ii) |w|<=1(mod 4)
Conclusion→ no of states will be n, if the equation is on format m (mod n)
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain
i) number of a’s = 0(mod 2) ii) number of b’s = 1(mod 2)
iii) number of a’s = 2(mod 3) iv) number of a’s = 1(mod 4)
Conclusion→ no of states will be n, if the equation is on format m (mod n)
Q How many minimum states are required in a DFA to find whether a given binary string has
odd number of 0’s, there can be any number of 1’s. (GATE-2015) (2 Marks)
(A) 1 (B) 2 (C) 3 (D) 4
Answer: (B)
Q The smallest finite automation which accepts the language {x | length of x is divisible by 3}
has (GATE-2002) (2 Marks)
(A) 2 states (B) 3 states (C) 4 states (D) 5 states
Answer: (B)
Q Let ∑ = {0, 1}, then an automaton A accepting only those words from ∑ having an odd
number of 1’s requires _________ states including the start state
a) 2 b) 3 c) 4 d) 5
Ans: a
Q The smallest finite automaton which accepts the language {x length of x is divisible by 500}
has
a) 200 states b) 300 states c) 400 states d) 500 states
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain odd occurrence of the substring ‘ab’.
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain even occurrence of the substring ‘ab’.
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain even occurrence of the substring ‘baa’.
Q how many DFA can be designed, with a fixed initial state, over an alphabet ∑ = {a, b}, and the
number of states be two.
Q how many DFA can be designed, with a fixed initial state, over an alphabet ∑ = {a, b}, and the
number of states be three.
Conclusion → if no of states is |Q| = n, and no of input alphabet |∑| = m
Then no of states will be nn*m * 2n
Q There are exactly __________ different finite automata with three states x, y and z over the
alphabet {a, b} where x is always the start state. (NET-DEC-2015)
a) 64 b) 56 c) 1024 d) 5832
Ans d
Q The possible number of finite automata with 3 states x, y and z, where x is always initial
state, over the alphabet {a, b} are
a) 5830 b) 5832 c) 5834 d) 583
Q how many DFA can be constructed over the alphabet, such that no of states |Q| = 2 and size
of input alphabet is |∑| = 2, that accepts empty language(Φ)?
Q how many DFA can be constructed over the alphabet, such that no of states |Q| = 2 and size
of input alphabet is |∑| = 2, that accepts universal language (∑*)?
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that for every
accepted string 2nd from left end is always b.
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that for every
accepted string 4th from left end is always a.
Conclusion→ no of states will be n+2, if position is n
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that for every
accepted string 2nd from right end is always b.
Conclusion→ no of states will be 2n, if position is n
Q The no of final states in a minimal DFA, that accept all string over a, b such that 2nd symbol
from right end is always b.
a) 1 b) 2 c) 4 d) none
Ans: 2
Q Design a minimal DFA that accepts all strings over the alphabet ∑= {a, b}, such that every
string ‘w’ which is accepted starts with ‘a’ & length is divisible by 3, i.e. 0(mod 3)?
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string ‘w’ which is accepted starts with ‘ab’ & length is divisible by 2(mod 4)?
Conclusion→ no of states will be n, n = first condition + second condition -1
Q No of states in a minimal DFA that accept all string over a, b such that every string starts
with ‘abb’ and length is concurrent to 4 (mod 7) is _______________
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string ‘w’ which is accepted starts with ‘abbb’ & length is divisible by 3(mod 15)?
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain be like, no of a = 0(mod 2) || no of b = 0(mod 2) ? cross product
method(one more approach , where concentrate on even and odd logic)
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain be like, no of a = 0(mod 2) && no of b = 0(mod 2)?
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must contain be like, a = 0(mod 2) && no of b = 0(mod 3) && no of c = 0(mod
5)?
Conclusion → if, n (mod m) && p (mod r), then the no of states will be n*m, if n & m are
relatively prime to each other
Q A minimum state deterministic finite automaton accepting the language L={w | w ε {0,1} *,
number of 0s and 1s in w are divisible by 3 and 5, respectively} has (GATE-2007) (2 Marks)
(A) 15 states (B) 11 states (C) 10 states (D) 9 states
Answer: (A)
Q Consider a DFA over ∑ = {a, b} accepting all strings which have number of a’s divisible by 6
and number of b’s divisible by 8. What is the minimum number of states that the DFA will
have? (GATE-2001) (2 Marks)
(A) 8 (B) 14 (C) 15 (D) 48
Answer: (D)
Q The following finite state machine accepts all those binary strings in which the number of l’s
and 0’s is respectively. (GATE-2004) (2 Marks)
Q Given below are two finite state automata (→ indicates the start state and F indicates a final
state) Which of the following represents the product automaton Z×Y? (GATE-2008) (2 Marks)
Answer: (A)
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {0,1}, such that every
string ‘w’ which is accepted has a decimal equivalent
i) 0(mod 3) ii) 2(mod 4) iii) 0(mod 5) iv) 2(mod 6) v) 3(mod 8)
Conclusion→ on a format of m (mod n)
1) if n is odd, number of states will be n
2) if n is even and n=2m, then number of states will be m+1
3) if n is even and n! =2m, then for number of states no direct formula
COMPLIMENT OF DFA
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}, such that every
string accepted must not contain a substring aaa?
Note: -
• The finite automata which is obtained by interchanging final & non-final states is known
as complement of the finite automata.
• L(FA) U L(FAC) = ∑* L(FA) ∩ L(FAC) = Φ
• L(FA) subset of ∑* and L(FA)C subset of ∑*
• No of states in FA = no of states in complement of FA
• Complement always exist for DFA (as it is a complete system)
Q Deterministic automata of a language over alphabets {0, 1}, which does not contain 3
consecutive 0’s. Minimum how many states ‘S’ DFA will have and how many of them will final
states, F?
a) ISI = 5 and IFI = 1 b) ISI = 5 and IFI = 4
c) ISI = 4 and IFI = 1 d) ISI = 4 and IFI = 3
Ans: d
Q construct the DFA for the following languages:
i) L= {am bn | m, n >= 0}
ii) L = {am bn cp | m, n, p >= 0}
conclusion →if type is L = {am bn | m>= 0, n>=0}, then no of states is no of alphabet +1
Q Which should be the final state(s) of the DFA if it should accept strings starting with ‘a’ and
ending with ‘b’?
a) 𝑞0 b) 𝑞1 c) 𝑞0 , 𝑞1 d) 𝑞3
Ans: b
Q Which of the following strings will be accepted if 𝑞0 and 𝑞1 are accepting states?
1. ababab 2. Babaaa 3. aaaba
a) 1,2 b) 2,3 c) 1,3 d) None of these
Ans: c
Q Which of the following represents the set of accepting states if the language to be accepted
contains strings having the same starting and ending symbols?
a) {𝑞0 } b) {𝑞0 , 𝑞3 } c) {𝑞3 } d) {𝑞3 , 𝑞1 }
Ans: b
Q Consider the DFAs M and N given above. The number of states in a minimal DFA that
accepts the language L(M) ∩ L(N) is __________. (GATE-2015) (2 Marks)
Q Consider the following DFA in which 𝑠0 is the start and 𝑠1 , 𝑠3 are the final sates. What
language does this DFA recognize? (GATE-2007) (2 Marks)
a) All strings of x and y
b) All strings of x and y and even either even number of x and even number of y of odd
number or x and odd number of y
c) All strings of x and y which have equal number of x and y
d) All strings of x and y with either even number of x and odd number of y or odd number of x
and even number of y.
Ans: D
MINIMIZATION OF FINITE AUTOMATA
It is sometimes difficult to design a minimal DFA directly so, a better approach is to first design
the DFA and then minimize it.
Based on productivity, the states of DFA can be mainly classified in two types-
The process of elimination of states whose presence or absence doesn’t affect the language
accepting capability of deterministic Finite Automata is called minimization of automata and
the result is minimal deterministic finite automata or commonly called as minimal finite
automata as MFA.
Q A deterministic finite automation (DFA)D with alphabet {a, b} is given below (GATE-2011) (2
Marks)
Which of the following finite state machines is a valid minimal DFA which accepts the same
language as D?
Answer: (A)
Q IF a DFA is represented by the following transition table, then how many states does the
corresponding minimal DFA contains?
a) 2 b) 3 c) 4 d) 5
Q Which of the following set can be recognized by a deterministic Finite state Automation?
(GATE–1998) (2 Marks)
a) The numbers 1,2,4,8, 2𝑛 , written in binary
b) The numbers 1,2,4, 2𝑛 , written in unary
c) The set of binary string in which the number of zeroes is the same as the number of ones.
d) The set { 1, 101, 11011, 1110111,}
Ans a
Q Minimal deterministic finite automaton for the language L = {0n | n ≥ 0, n ≠ 4} will have:
(NET-JUNE-2015)
a) 1 final state among 5 states b) 4 final states among 5 states
c) 1 final state among 6 states d) 5 final states among 6 states
Ans: d
a) 4 b) 3 c) 6 d) 5
(NET-DEC-2018)
a) N b) N+1 c) n(n+1)/2 d) 2^n
Ans: b
Q The transition function for the language L = {w | na (w) and nb(w) are both odd} is given by:
δ (q0, a) = q1
δ (q0, b) = q2
δ (q1, a) = q0
δ (q1, b) = q3
δ (q2, a) = q3
δ (q2, b) = q0
δ (q3, a) = q2
δ (q3, b) = q1
The initial and final states of the automata are: (NET-JUNE-2015)
A) q0 and q0 respectively b) q0 and q1 respectively
c) q0 and q2 respectively d) q0 and q3 respectively
Non determinism means choice of move for an automaton. So rather than prescribing a
unique move in each situation, we allow a set of possible moves.
Non deterministic machine are only theoretical machine i.e. in the first place they are not
implementable and neither we want to implement them, the only reason we study non-
determinism is because they are easy to design and easily be converted into deterministic
machine.
is a transition function : Q × → 2Q
NOTE- A null transition is also possible for NFA, such special NFA are called Null-NFA.
PROPERTIES OF NFA
ii) NDFA is a theoretical engine and is not implementable, but it is very easy to design compare
to DFA.
iii) No concept of dead state, therefore complementation of DFA is also not possible.
iv) NDFA will respond for only valid strings and no need to respond for invalid strings. (it is a
complete system.
ACCEPTANCE BY NDFA
Let ‘w’ be any string defined over the alphabet , corresponding to w, there can be multiple
transitions for NFA starting from initial state, if there exist at least one transition for which we
start at the initial state and ends in any One of the final state, then the string ‘w’ is said to be
accepted by the non-deterministic finite automata, otherwise not.
Q design a NDFA that accepts all strings over the alphabet ∑ = {a, b}, where every accepted
string ‘w’ starts with substring s
i) s = ab ii) s = bb
conclusion→ if w = sx, |s| = m, then no of states in the NDFA is m+1
Q Design a NDFA that accepts all strings over the alphabet ∑ = {a, b}. where every accepted
string ‘w’ ends with substring ‘s’.
i) s = ab ii) s = bab
conclusion→ if w = sx, |s| = m, then no of states in the NDFA is m+1
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b}. where every
accepted string ‘w’ contains sub string s.
i) s = bb ii) s = aba
conclusion→ if w = xsx, |s| = m, then no of states in the DFA is m+1
Q Let w be any string of length n is {0,1}*. Let L be the set of all substrings of w. What is the
minimum number of states in a non-deterministic finite automaton that accepts L? (GATE-
2010) (1 Marks)
(A) n-1 (B) n (C) n+1 (D) 2n-1
Answer: (C)
Q Design a NDFA that accepts all strings over the alphabet ∑ = {a, b} such that every accepted
string start and end with same symbol.
Q Design a NDFA that accepts all strings over the alphabet ∑ = {a, b} such that every accepted
string start and end with different symbol.
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string w, is like w=SX.
i) s= aa/bb ii) s=aaa/bbb
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string w, is like w=XS.
i) s= aa/bb ii) s=aaa/bbb
Q Design a minimal DFA that accepts all strings over the alphabet ∑ = {a, b} such that every
accepted string w, is like w=XSX.
i) s= aa/bb ii) s=aaa/bbb
Q Design a NFA that accepts all strings over the alphabet ∑ = {a, b}, such that every string ‘w’
accepted must be like
i) |w| = 3 ii) |w|<=3 iii) |w|>=3
Q Design a NFA that accepts all strings over the alphabet ∑ = {a, b}, such that every string
accepted must contain exactly two a’s.
Q Design a NFA that accepts all strings over the alphabet ∑ = {a, b}, such that every string
accepted must contain at least two a’s.
Q Design a NFA that accepts all strings over the alphabet ∑ = {a, b}, such that every string
accepted must contain at most two a’s.
Q Design a NDFA that accepts all strings over the alphabet ∑ = {a, b} such that for every
accepted string 2nd from right end is always A.
Q The FSM shown in the figure accepts
Q (GATE-2014) (2 Marks)
(A) {q0, q1, q2} (B) {q0, q1} (C) {q0, q1, q2, q3} (D) {q3}
Answer: (A)
Let the language accepted by M be L. Let 𝐿1 be the language accepted by the NFA 𝑀1 ,
obtained by changing the accepting state and by changing the non – accepting state of M to
accepting states. Which of the following statements is True?
a) 𝐿1 = {0, 1} * - L b) 𝐿1 = {0, 1} * c) 𝐿1 ⊂ L d) 𝐿1 = L
Ans: b
Q Consider the following two finite automata. 𝑀1 accepts 𝐿1 and 𝑀2 accepts 𝐿2 Which one of
the following is TRUE? (GATE-2008) (2 Marks)
a) 𝐿1 = 𝐿2 b) 𝐿1 ⊂ 𝐿2 c) 𝐿1 ∩ ̅̅̅
𝐿2 = ф d) 𝐿1 ∪ 𝐿2 ≠ 𝐿1
Ans: a
Q Consider the non-deterministic finite automaton (NFA) shown in the figure. (GATE-2005) (2
Marks)
State X is the starting state of the automaton. Let the language accepted by the NFA with Y as
the only accepting state be L1. Similarly, let the language accepted by the NFA with Z as the
only accepting state be L2. Which of the following statements about L1 and L2 is TRUE?
(A) L1 = L2 (B) L1 ⊂ L2 (C) L2 ⊂ L1 (D) None of the above
Answer: (D)
Q The minimum number of states of the non-deterministic finite automation which accepts
the language {a b a bn|n >=0} U {a b an|n >= 0} is (NET-DEC-2012)
(A) 3 (B) 4 (C) 5 (D) 6
Ans: c
Q Let w be any string of length n is {0, 1}*. Let L be the set of all substrings of w. What is the
minimum number of states in a non-deterministic finite automaton that accepts L? (GATE-
2010) (2 Marks)
a) n-1 b) n c) n+1 d) 2n-1
ANSWER C
NFA and DFA Equivalence
• In this topic we will be learning about the equivalence of NFA and DFA and how an NFA
can be converted to equivalent DFA. Let us take an example and understand the
conversion.
• Since every NFA and DFA has equal power that means, for every language if a NFA is
possible, then DFA is also possible.
• So, every NFA can be converted to DFA and vice versa.
• The process of conversion of an NFA into a DFA is called Subset Construction.
• If NFA have ‘n’ states which is converted into DFA which ‘m’ states than the relationship
between n and m will be 1<= m <= 2n
• Consider the following NFA:
• Now, the transitions such as: q0, q1 are treated as a new state.
• Now, we convert the NFA into equivalent DFA such that we will be taking transitions from
the new states too. The transitions over the alphabets of the new states will be the union
of all the transitions over a particular input symbol. Such that:
• For instance, state {q0, q1, b} => {q0, q2} as q1 on b initially transitions to q2 and q0 on b
transitions to q0, so we take the union of the two and treat it as a new state. Similarly,
after the final transitions we get the table as:
Q Given an arbitrary non-deterministic finite automaton (NFA) with N states, the maximum
number of states in an equivalent minimized DFA is at least (GATE-2001) (2 Marks)
(A) N2 (B) 2N (C) 2N (D) N!
Answer: (B)
Q Let N be an NFA with n states and let M be the minimized DFA with m states recognizing the
same language. Which of the following in NECESSARILY true? (GATE-2008) (2 Marks)
(A) m ≤ 2n (B) n ≤ m (C) M has one accept state (D) m = 2n
Answer: (A)
Q Let N be an NFA with n states. Let k be the number of states of a minimal DFA which is
equivalent to N. Which one of the following is necessarily true? (GATE-2018) (1 Marks)
a) k ≥ 2n b) k ≥ n c) k ≤ n2 d) k ≤ 2n
(ANSWER- D)
• Start the construction of ’ with the initial state & continue for every new state that comes
under the input column and terminate the process whenever no new state appears under
the input column.
• Every subset of states that contain the final state of the NFA is a final state in the resulting
DFA.
For e.g.
Q Given a Non-deterministic Finite Automation (NFA) with states p and r as initial and final
states respectively and transition table as given below:
a b
P – q
Q r s
R r s
S r s
The minimum number of states required in Deterministic Finite Automation (DFA) equivalent
to NFA is (NET-JUNE-2013)
(A) 5 (B) 4 (C) 3 (D) 2
Ans: b
NFA WITH EPSILON MOVES (€-NFA)
An automaton that consist of null transitions is called a Null- NFA i.e. we allow a transition on
null means empty string.
For e.g.
NULL CLOSURES-
• All the states will get the status of the final state in the resulting NFA, whose €-closure
contains at least one final state in the initial €-NFA.
Consider the following null NFA and convert it into corresponding DFA
Q ε-Closure
q1 {q1, q2}
q2 {q2}
Now, we check out for the transitions that will be made using: ẟ’(qi, x) = ε-Closure [ẟ[ε-
Closure(qi), x]]
Let us check for ẟ(q0, a) = ε-Closure [ẟ[ε-Closure(q0), a]]
• first, we find ε-Closure of q0: {q0, q1, q2}
• Now check transition of all three states on input symbol a we get: q0
• After that we again calculate the ε-Closure of the above result q0, we get the result as:
{q0, q1, q2}
• Similarly, we will check it for every state.
The resulting table of NFA will be:
NFA a b c
→ q0 {q0, q1, q2} {q1, q2} {q2}
q2 Φ Φ {q2}
• All those states will be considered as a final state in which we have q2(final state in initial
ε-NFA) in there ε-Closure, i.e. all states will be final in this case.
Q if the given NFA is converted to NFA without ϵ - moves, which of the following denotes the
set of final states?
a) {𝑞2 } b) {𝑞1 , 𝑞2 } c) {𝑞0 , 𝑞1 , 𝑞2 } d) can’t be determined
Q Which of the following strings will not be accepted by the given NFA?
a) 00 11 22 b) 11 22 c) 21 d) 22
Q Let δ denote the transition function and δ’ denoted the extended transition function of the
ε-NFA whose transition table is given below: (GATE-2017) (2 Marks)
Then, δ’ (q2, aba) is
Ans: c
L = {w c wr | w Σ*}
L = {w c w | w Σ *}
L = {w c wr | w Σ +}
L= {w c w | w Σ +}
L = {w w | w Σ *}
L= {w wr | w Σ +}
L = {w c wr | c, w Σ *}
L = {c w wr | c, w Σ *}
L= {w wr c | c, w Σ *}
L = {w c wr | c, w Σ +
L = {c w wr | c, w Σ +}
L= {w wr c | c, w Σ +}
L = {w c w | c, w Σ *}
L = {c w w | c, w Σ *}
L= {w w c | c, w Σ *}
L = {w c w | c, w Σ +}
L = {c w w | c, w Σ +}
L= {w w c | c, w Σ +}
(A) I and IV only (B) I and III only (C) I only (D) IV only
Answer: (A)
Which one of the following is CORRECT? (GATE-2014) (2 Marks)
(A) Only (I) (B) Only (II) (C) Both (I) and (II) (D) Neither (I) nor (II)
Answer: (A)
Answer: (C)
Q If L is a regular language over ∑ = {a, b}, which one of the following languages is NOT
regular? (GATE-2019) (2 Marks)
a) L⋅ LR {xy | x ∈ L, yR ∈ L}
b) Suffix (L) = {y ∈ ∑* | ∃x ∈ ∑* such that xy ∈ L}
c) Prefix (L) = {x ∈ ∑* | ∃y ∈ ∑* such that xy ∈ L}
d) {w wR | w ∈ L}
Ans: d
(NET-DEC-2018)
(A) is True. Though at first look both L1L1 and L2L2 looks non-regular, L1L1 is in fact regular.
The reason is the relation between 110110 and 011011.
We cannot have two 110′110′s in a string without a 011011 or vice verse. And this would
mean that we only need a finite number of states to check for acceptance of any word in
this language.
That was just an intuitive explanation. Now I say that L contains all binary strings starting
with 1111. Yes, if a binary string starts with 1111, it can never have more no.
of 011011 than 110110.
Q If s is a string over (0 + 1)* then let n0(s) denote the number of 0’s in s and n1(s) the
number of 1’s in s. Which one of the following languages is not regular? (GATE-2006) (2
Marks)
Answer: (C)
Q Let Σ be the set of all bijections from {1, ..., 5} to {1, ..., 5}, where id denotes the identity
function, i.e. id(j) = j, ∀j. Let ° denote composition on functions. For a string x = x1x2 ... xn ∈
Σn, n ≥ 0, let π(x) = x1°x2° ... °xn. Consider the language L = {x ∈ Σ* | π(x) = id}. The minimum
number of states in any DFA accepting L is _________. (GATE-2019) (2 Marks)
Answer: a
REGULAR EXPRESSIONS
One way of describing regular language is via the notation of regular expression. An expression
of strings which represents regular language is called regular expression.
Regular
Expression
Semi Unrestricted
Restricted RE
Restricted RE RE
+, ., * +, ., *, ⋂ +, ., *, ⋂, ¬
The regular expressions are useful for representing certain sets of strings in an algebraic
fashion. We give a formal recursive definition of regular expressions over ∑ as follows:
Any terminal symbol (i.e. an element of ∑), ∈ and Φ are regular expressions (Primitive Regular
Expressions). A regular expression is valid iff it can be derived from a primitive regular by a finite
number of applications of the above regular set.
• When we view a in ∑ as a regular expression, we denote it by a.
• The union of two regular expressions r1 and r2, written as r1 + r2, is also a regular
expression.
• The concatenation of two regular expressions r1 and r2, written as r1 r2, is also a regular
expression.
• The iteration (or closure) of a regular expression r written as r*, is also a regular
expression.
• If R is a regular expression, then (r) is also a regular expression.
Any set(language) represented by a regular expression is called a regular set (regular language).
If for example, a, b ∈ ∑, then
• a denotes the L = {a}
• a + b denotes L = {a, b}
• a.b denotes L = {ab} concatenation
• a* denotes the set {∈, a, aa, aaa, ...} known as Kleene closure.
• (a + b)* denotes {a, b}*
• a+ Positive closure {a, aa, aaa…}
• R = {a.b + a}b
The precedence order to solve is: * (Kleene Closure) > + Positive Closure > Concatenation > Union
IDENTITIES FOR REGULAR EXPRESSIONS- Two regular expressions P and Q are equivalent (we
write P = Q) if P and Q represent the same set of strings.
Q Let S and T be language over Σ = {a, b} represented by the regular expressions (a + b*)* and
(a + b)*, respectively. Which of the following is true? (GATE-2000) (1 Marks)
(A) S ⊂ T (B) T ⊂ S (C) S = T (D) S ∩ T = φ
Answer: (C)
• Every regular expression can generate only one regular language but, a regular language
can be generated by more than one regular expression I.e. means two different regular
expression can generate same language.
• Two regular expression are said to be equal if they generate same language.
• For e.g., r1 = a*, r2 = (aa)*
FINITE AUTOMATA AND REGULAR EXPRESSIONS
Q Design a regular express that represent a language ‘L’, where L={a} over the alphabet ∑={a}.
Q design a regular express that represent all strings over the alphabet ∑ = {a, b}, where every
accepted string ‘w’ starts with substring s
i) s = b ii) s = ab iii) s = abb
Q design a regular express that represent all strings over the alphabet ∑ = {a, b}. where every
accepted string ‘w’ ends with substring ‘s’.
i) s = ab ii) s = aa iii) s = bab
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b}. where every
accepted string ‘w’ contains sub string s.
i) abb ii) aba
Q No of states in a minimal in a that accepts all the strings of a, b such that every string must
have ‘aabab’ as sub stray
a) 5 b) 6 c) 7 d) none
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b} such that
every accepted string start and end with a.
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b} such that
every accepted string start and end with same symbol.
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b} such that
every accepted string start and end with different symbol.
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b} such that
every accepted string w, is like w=SX.
i) s= aa/bb ii) s=aaa/bbb
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b} such that
every accepted string w, is like w=XS.
i) s= aa/bb ii) s=aaa/bbb
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b} such that
every accepted string w, is like w=XSX.
i) s= aa/bb ii) s=aaa/bbb
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b}, such that
every string ‘w’ accepted must be like
i) |w| = 3 ii) |w|<=3 iii) |w|>=3
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b}, such that
every string accepted must contain exactly two a’s.
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b}, such that
every string accepted must contain at least two a’s.
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b}, such that
every string accepted must contain at most two a’s.
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b} such that for
every accepted string 2nd from left end is always b.
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b} such that for
every accepted string 4th from left end is always a.
Q Design a regular express that represent all strings over the alphabet ∑= {a, b}, such that
every string ‘w’ where |W| = 0(mod 3)?
Q Design a regular express that represent all strings over the alphabet ∑= {a, b}, such that
every string ‘w’ where |W| = 3(mod 4)?
Q Design a regular express that represent all strings over the alphabet ∑= {a, b}, such that
every string ‘w’ where |W|a = 0(mod 3)?
Q Design a regular express that represent all strings over the alphabet ∑= {a, b}, such that
every string ‘w’ where |W|a = 2(mod 3)?
Q Design a regular express that represent all strings over the alphabet ∑ = {a, b}, such that
every string accepted must contain be like, no of a = 0(mod 2) || no of b = 0(mod 2)?
Q L = {am bn | m, n >= 0}
Q L = {am bn cp | m, n, p >= 1}
Q L = {an bn | n >= 1}
Q L = {am bn | m + n = even}
Algebraic Properties of regular expressions-
• Closure Property - Regular expressions satisfy closure property with respect to Union,
Concatenation and kleene closure. If R1 and R2 are regular expression then the following
will also be regular expression.
o r1 + r2
o r1.r2
o r1*
o r1+
• Associative Property- Regular expression satisfy associative property with respect to
union and intersection.
o (r1 + r2) + r3 = r1 + (r2 + r3)
o (r1. r2). r3 = r1. (r2. r3)
• Identity Property- The identity property is satisfied as follows-
o r. ε =r
o r+ ϕ =r
• Inverse Property- The inverse property is only satisfied with respect to concatenation,
for union inverse property isn’t satisfied.
o r. () = ε
o r+ () = ϕ
• Commutative Property-Regular expressions are commutative with respect to union but
not with respect to concatenation.
o r1+ r2 = r2+ r1
o r1. r2 != r2. r1
• Distributive Property-Regular expression satisfy this property as follows-
o r1(r2+r3) = r1r2+r1r3
o (r1+r2) r3 = r1r3+r2r3
o r1+(r2. r3) != (r1+r2)(r1+r3)
o (r1.r2) + r3 = (r1 +r3). (r2 + r3)
• Idempotent Property-Regular expressions satisfies idempotent property with respect to
union but not with respect to concatenation.
o r1+r1=r1
o r1. r1 != r1
Identities for regular expressions
• φ+r=r
• φ. r = r. φ = φ
• ∈. r = r. ∈ = r
• ∈ * = ∈ and ф * = ∈
• r*r* = r*
• r. r* = r*. r
• r+. r* =r+
• r+ r+ =r+-r
• r+ u r* =r*
• r+ ⋂ r* = r+
• (r*)* = r*
• (r+)*=r*
• ((r+)*)* = r*
• ((r*)*)r+ = r+
Q 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)*? (GATE-2009) (2 Marks)
(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.
Answer: (C)
Q Which of the following regular expressions describes the language over {0, 1} consisting of
strings that contain exactly two 1’s? (GATE-2008) (1 Marks)
(A) (0 + 1) * 11(0 + 1) * (B) 0 * 110 *
(C) 0 * 10 * 10 * (D) (0 + 1) * 1(0 + 1) * 1 (0 + 1) *
Answer: (C)
Q Consider the following identities for regular expressions: (a) (r + s)* = (s + r)* (b) (r*)* = r* (c)
(r* s*)* = (r + s)* Which of the above identities are true? (NET-AUG-2016)
a) (a) and (b) only b) (b) and (c) only
c) (c) and (a) only d) (a), (b) and (c)
Ans: d
Q The regular expression 0*(10*)* denotes the same set as (GATE-2003) (1 Marks)
(A) (1*0)*1* (B) 0 + (0 + 10)* (C) (0 + 1)* 10(0 + 1)* (D) none of these
Answer: (A)
Q Which one of the following regular expressions is NOT equivalent to the regular expression
(a + b + c) *? (GATE-2004) (2 Marks)
(A) (a* + b* + c*)* (B) (a*b*c*)* (C) ((ab)* + c*)* (D) (a*b* + c*)*
Answer: (C)
Q Consider the languages L1 = Φ and L2 = {a}. Which one of the following represents L1 L2*U
L1* (GATE-2013) (1 Marks)?
Answer:(A)
Q Consider the languages L1 = φ and L2 = {1}. Which one of the following represents
L1 * ∪ L1 * L2 *? (NET-JAN-2017)
a) {∈} b) {∈, 1} c) Φ d) 1*
Ans: d
(NET-DEC-2018)
a) Only I b) Only ii c) Both i and ii d) Neither i nor ii
Ans: a
Q Which one of the following regular expressions represents the language: the set of all binary
strings having two consecutive 0s and two consecutive 1s? (GATE-2016) (2 Marks)
Answer: (B)
Q The regular expression for the complement of the language L = {an bm | n ≥ 4, m ≤ 3} is: (NET-
JULY-2016)
a) (∈ + a + aa + aaa) b* + a* bbbb* + (a + b)* ba(a + b)*
b) (∈ + a + aa + aaa) b* + a* bbbbb* + (a + b)* ab(a + b)*
c) (∈ + a + aa + aaa) + a* bbbbb* + (a + b)* ab(a + b)*
d) (∈ + a + aa + aaa) b* + a* bbbbb* + (a + b)* ba(a + b)*
Ans: d
Q The regular expression corresponding to the language L where L = { x ϵ {0, 1}*|x ends with 1
and does not contain substring 00 } is: (NET-JUNE-2015)
a) (1 + 01) * (10 + 01) b) (1 + 01) * 01
c) (1 + 01) * (1 + 01) d) (10 + 01) * 01
Ans: c
Q Regular expression for the language L = {w ∈ {0, 1}* | w has no pair of consecutive zeros} is
(NET-SEP-2013)
(A) (1 + 010)* (B) (01 + 10)* (C) (1 + 010)* (0 + ∈) (D) (1 + 01)* (0 + ∈)
Ans: d
Q The number of strings of length 4 that are generated by the regular expression
(0|∈ )1+2(3|∈), where | is an alternation character and {+, *} are quantification characters, is:
(NET-AUG-2016)
a) 08 b) 09 c) 10 d) 12
Ans: d
ARDEN’S THEOREM is the mechanism for the construction of a regular expression from a
finite automaton.
Steps used-
1.For, every individual state of the DFA, write an expression for every incoming and
outgoing input alphabet.
a) If P is free from NULL, then equation R=Q+RP has unique solution, R=QP*
b) If P contains NULL, then equation R=Q+RP has many infinitely many solutions.
Q Consider a DFA and convert it into regular expression using Arden’s theorem?
(A, a) = A
(A, b) = B
(B, a) = B
(B, b) = B
A is the initial state and B Is the final state
Q Consider a DFA and convert it into regular expression using Arden’s theorem?
(A, b) = A
(A, b) = B
(B, a) = C
(C, b) = C
A is the initial state and B,C Is the final state
Q Consider alphabet ∑ = {0, 1}, the null/empty string ∈ and the sets of strings X0, X1 and
X2 generated by the corresponding non-terminals of a regular grammar. X0, X1 and X2 are
related as follows (GATE-2015) (2 Marks)
X0 = 1 X 1 X1 = 0 X1 + 1 X2 X2 = 0 X1 + {∈}
Which one of the following choices precisely represents the strings in X0?
(A) 10 (0* + (10)*)1 (B) 10 (0* + (10)*)*1
(C) 1(0* + 10)*1 (D) 10 (0 + 10)*1 + 110 (0 + 10)*1
Answer: (C)
Conversion from Finite Automata to Regular expression
(A) (a + b)* a(a + b)b (B) (abb)* (C) (a + b)* a(a + b)* b(a + b)* (D) (a + b)*
Answer: (A)
Q Let L = {w ∈ (0 + 1)* | w has even number of 1s}, i.e. L is the set of all bit strings with even
number of 1s. Which one of the regular expressions below represents L? (GATE-2010) (2
Marks)
a) (0 * 10 * 1)* b) 0 * (10 * 10*)*
c) 0*(10 * 1*)*0* d) 0 * 1(10 * 1)*10*
ANSWER B
Q Consider the following Finite State Automaton. The language accepted by this automaton is
given by the regular expression (GATE-2007) (2 Marks)
Answer: (C)
Q Consider the automata given in previous question. The minimum state automaton
equivalent to the above FSA has the following number of states (GATE-2007) (1 Marks)
(A) 1 (B) 2 (C) 3 (D) 4
Answer: (B)
Is (NET-JUNE-2012)
(A) ab*(b + aa*b)* (B) a*b(b + aa*b)*
(C) a*b(b* + aa*b) (D) a*b(b * + aa*b)*
Ans: d
Q Which of the regular expressions given below represent the following DFA? (GATE-2014) (2
Marks)
I) 0*1(1+00*1)* II) 0*1*1+11*0*1 III) (0+1)*1
(A) I and II only (B) I and III only (C) II and III only (D) I, II, and III
Answer: (B)
Q If the final states and non-final states in the DFA below are interchanged, then which of the
following languages over the alphabet {a, b} will be accepted by the new DFA? (GATE-2008) (1
Marks)
Q Consider the regular language L = (111 + 11111)*. The minimum number of states in any
DFA accepting this language is: (GATE-2006) (1 Marks)
(A) 3 (B) 5 (C) 8 (D) 9
Answer: (D)
Q The number of states in the minimal deterministic finite automaton corresponding to the
regular expression (0 + 1)*(10) is ____________ (GATE-2015) (1 Marks)
(A) 2 (B) 3 (C) 4 (D) 5
Answer: (B)
Q The number of states in the minimum sized DFA that accepts the language defined by the
regular expression (0+1)*(0+1)(0+1)* is __________________ (GATE-2016) (2 Marks)
[Note that this question was originally asked as Fill-in-the-Blanks type]
(A) 2 (B) 3 (C) 4 (D) 5
Answer: (A)
Q Let T be the language represented by the regular expression Σ∗0011Σ∗ where Σ = {0, 1}.
What is the minimum number of states in a DFA that recognizes L’ (complement of L)? (GATE-
2015) (2 Marks)
(A) 4 (B) 5 (C) 6 (D) 8
Answer: (B)
Q Consider the language L given by the regular expression (a + b) *b(a +b) over the alphabet
{a, b}. The smallest number of states needed in a deterministic finite-state automaton (DFA)
accepting L is ______. (GATE-2017) (1 Marks)
ANSWER 4
Q Which of the following statements is TRUE about the regular expression 01*0? (GATE-2005)
(1 Marks)
(A) It represents a finite set of finite strings.
(B) It represents an infinite set of finite strings.
(C) It represents a finite set of infinite strings.
(D) It represents an infinite set of infinite strings
Answer: (B)
Q Which of the following regular expressions, each describing a language of binary numbers
(MSB to LSB) that represents non-negative decimal values, does not include even values?
(NET-NOV-2017)
a) 0*1+0*1* b) 0*1*0+1* c) 0*1*0*1+ d) 0+1*0*1*
Ans: c
Q The length of the shortest string NOT in the language (over Σ = {a, b}) of the following
regular expression is ______________. (GATE-2014) (2 Marks)
a*b*(ba)*a*
(A) 2 (B) 3 (C) 4 (D) 5
Answer: (B)
Q Consider the regular expression (a + b) (a + b) ... (a + b) (n-times). The minimum number of
states in finite automaton that recognizes the language represented by this regular expression
contains (NET-JUNE-2012)
(A) n states (B) n + 1 states
(C) n + 2 states (D) 2n states
Ans: b
Q which of the following re, over the Σ = {0, 1} denotes the set of all strings that not contain
100 as sub-string
a) 0*(0+1)* b) 0*1010* c) 0*1*01 d) 0*(10+1)*
(A) P – 2, Q – 1, R – 3, S – 4 (B) P – 1, Q – 3, R – 2, S – 4
(C) P – 1, Q – 2, R – 3, S – 4 (D) P – 3, Q – 2, R – 1, S – 4
Answer: (C)
Q Pushdown automata can recognize language generated by_________. (NET-JULY-2018)
(1) Only context free grammar (2) Only regular grammar
(3) Context free grammar or regular grammar (4) Only context sensitive grammar
Ans: c
Q Which of the following definitions generates the same Language as L, where L = {WWR |
W ∈ {a, b}*} (NET-DEC-2012)
(A) S → asb | bsa | ∈ (B) S → asa | bsb | ∈
(C) S → asb | bsa | asa | bsb | ∈ (D) S → asb | bsa | asa | bsb | ∈
Ans: b
Q A context free grammar for L = {w | n0 (w) > n1 (w)} is given by: (NET-JUNE-2015)
a) S → 0 | 0 S | 1 S S b) S → 0 S | 1 S | 0 S S | 1 S S | 0 | 1
c) S → 0 | 0 S | 1 S S | S 1 S | S S 1 d) S → 0 S |1 S | 0 | 1
Ans: c
Q Identity the language generated by following grammar where S is the start variable.
(GATE-2017) (2 Marks)
S-->XY
X--> aX | a
Y--> aYb | ∈
a) {am bn| m >= n, n > 0} b) {am bn| m >= n, n >= 0}
c) {am bn| m > n, n >= 0} d) {am bn| m > n, n > 0}
Ans: c
Q Which one of the following grammars generates the language L = {ai bj | i ≠ j} (GATE-
2006) (1 Marks)
Answer: (D)
Q Which of the following definitions below generates the same language as L, where (GATE
-1995) (2 Marks)
L = { 𝑥 𝑛 𝑦 𝑛 such that n > = 1} ?
I. E → xEy | xy II. xy | (𝑥 + sy𝑦 + ) III. 𝑥 + 𝑦 +
a) I only b) I and II c) II and III d) II only
Ans: a
Ans: d
Q In the context-free grammar below, S is the start symbol, a and b are terminals, and ϵ
denotes the empty string (GATE-2006) (1 Marks)
S → aSa | bSb | a | b | ϵ
Which of the following strings is NOT generated by the grammar?
(A) aaaa (B) baba (C) abba (D) babaaabab
Answer: (B)
Q Consider the context-free grammars over the alphabet {a, b, c} given below. S and T are
non-terminals. (GATE-2017) (2 Marks)
G1: S → aSb | T, T → cT | ϵ
G2: S → bSa | T, T → cT | ϵ
The language L(G1) ∩ L(G2) is
(A) Finite (B) Not finite but regular
(c) Context-Free but not regular (D) Recursive but not context-free
ANSWER B
Q Identity the language generated by following grammar where S is the start variable.
(GATE-2017) (2 Marks)
S --> XY
X --> aX | a
Y --> aYb | ∈
a) {am bn| m>=n, n>0 } b) {am bn| m>=n, n>=0 }
c) {am bn| m>n, n>=0 } d) {am bn| m>n, n>0 }
ANSWER C
Q The two-grammar given below generate a language over alphabet {x, y, z) (GATE – 2007)
(2 Marks)
G1: S → x | z | xS | zS | yB
B → y | z | yB | zB | zB
G2: S → y | z | yS | zS | xB
B → y | y | yS
Which one of the following choices describes the properties satisfied by the strings in these
languages?
a) G1: No y appears before any x, G2: Every x is followed by at least one y
b) G1: No y appears before any x, G2: No x appears before any y
c) G1: No y appears after any x, G2: Every x is followed by at least one y
d) G1: No y appears after any x, G2 Every y is followed by at least one x
Ans: a
Q Consider the following context-free grammar over the alphabet ∑ = {a, b, c} with S as the
start symbol: (GATE-2017) (2 Marks)
S → abScT | abcT
T → bT | b
Which of the following represents the language generated by the above grammar?
a) {(ab)n(cb)n | n >= 1 }
b) {((ab)n c bm1 c bm2...c bmn | n, m1, m2, ....., mn >= 1 }
c) {(ab)n (cbm)n | n >= 1 }
d) {(ab)n (cbn)m | m, n >= 1 }
ANSWER B
T→ bT | b, this production will generate any number of b’s > 1
S→ abScT | abcT, this production will generate equal number of “ab” and “c” and for every
“abc” any number of b’s ( > 1) after “abc”.
Q The context free grammar for the language L = {an bm ck | k = |n – m|, n > 0, m > 0,
k > 0} is (NET-JUNE-2014)
(A) S → S1 S3, S1 → aS1c | S2 | λ, S2 → aS2 b |λ, S3 → aS3 b| S4 | λ, S4 → bS4 c |λ
(B) S → S1 S3, S1→ aS1 S2 c | λ, S2 → aS2 b| λ, S3 → aS3 b| S4 |λ, S4 → bS4 c |λ
(C) S → S1|S2, S1→ aS1 S2 c | λ, S2 → aS2 b | λ, S3 → aS3 b | S4 |λ, S4 → bS4 c |λ
(D) S → S1 | S3 , S1→ aS1 c|S2 | λ, S2 → aS2 b | λ, S3 → a S3 b| S4 | λ, S4 → bS4 c | λ
Q The equivalent grammar corresponding to the grammar (NET-JUNE-2012)
G: S → aA
A → BB
B → aBb | ∈ is
(A) S → aA
A → BB
B → aBb
(B) S → a | aA
A → BB
B → aBb | ab
(C) S → a | aA
A → BB | B
B → aBb
(D) S → a |aA
A→ BB | B
B→ aBb | ab
Ans: d
Q A CFG G is given with the following productions where S is the start symbol, A is a non-
terminal and a and b are terminals.
S → aS ∣ A
A → aAb ∣ bAa ∣ ϵ
Which of the following strings is generated by the grammar above?
(A) aabbaba (B) aabaaba (C) abababb (D) aabbaab
Answer: (D)
Q In the context – free grammar below, S is the start symbol, a and b are terminals, and ε
denotes the empty string.
S → aSAb | ε
A → bA | ε
The grammar generates the language
a) ((a + b)*b) b) {am bn |m ≤ n}
c) {am bn |m = n} d) a*b*
i) Emptiness
ii) Non-emptiness
iii) Finiteness
iv) Infiniteness
v) Membership
i) Equality
ii) Ambiguity
Q Let L1 and L2 be any context-free language and R be any regular language. Then, which of the
following is correct? (GATE-2017) (2 Marks)
I. L1 ∪ L2 is context-free. II. L1' is context-free.
III. L1 - R is context-free. IV. L1 ∩ L2
a) I, II and IV only b) I and III only
c) II and IV only d) I only
Ans: b
Q Let L = 𝐿1 ∩ 𝐿2 , where 𝐿1 and 𝐿2 are languages as defined below: (GATE – 2009) (2 Marks)
𝐿1 = {𝑎𝑚 𝑏 𝑚 c 𝑎𝑛 𝑏 𝑛 | m, n ≥ 0} 𝐿2 = {𝑎𝑖 𝑏 𝑗 𝑐 𝑘 | I, j, k ≥ 0}
a) Not recursive b) Regular
c) Context – free but not regular d) Recursively enumerable but not context – free
Ans: c
Q Let L be a regular language and M be a context-free language, both over the alphabet Σ. Let
Lc and Mc denote the complements of L and M respectively. Which of the following statements
about the language Lc ∪ Mc is TRUE? (GATE-2005) (2 Marks)
(A) It is necessarily regular but not necessarily context-free
(B) It is necessarily context-free.
(C) It is necessarily non-regular.
(D) None of the above
Answer: (D)
Q Let L1 is context free language and L2 is a regular language which of the following is/are false
(GATE – 1999) (1 Marks)
a) L 1 – L2 is not context free b) L1 ∩ L2 is context free
c) ~ L1 is context free d) ~ L2 is regular
Ans: c
Q Let L be a context - free language and M a regular language. Then the language L ∩ M is
(GATE-2006) (1 Marks)
a) Always regular b) Never regular
c) Always a deterministic context free language d) Always a context – free language
Ans: d
Q Let L = {0n1n|n ≥ 0} be a context free language. Which of the following is correct? (NET-JULY-
2016)
a) L` is context free and Lk is not context free for any k ≥ 1.
b) L` is not context free and Lk is not context free for any k ≥ 1.
c) Both L` and Lk is for any k ≥ 1 are context free.
d) Both L` and Lk is for any k ≥ 1 are not context free.
Ans: c
a)
b)
c)
d)
Q Context – free languages and regular languages are both closed under the operations (s) of
i) Union ii) Intersection iii) Concatenation
a) i) and ii) only b) ii) and iii) only c) i) and iii) only d) All of the above
Q Let L1 and L2 are context free languages, L3 and L4 are regular languages then which of the
following languages are always context-free
a) (L1 L2) – (L1 L4) b) (L1 L3) – (L2 L4)
c) (L1 L2) / (L3 L4) d) (L3 L4) / (L1 L2)
Q Let G1 and G2 be Context Free Grammars and R be a regular expression. Then which of the
following are decidable.
a) Is L(G1) L(G2) = ? b) Is L(G1) = L(G2)?
c) Is L(G1) = L(R)? d) None of the above
Q (GATE-2006) (1 Marks)
Q Which of the following language is true about the language L = (𝑎𝑃 |P is a prime)
a) It is not accepted by a T.M b) It is regular but not C.F.L
c) It is CFL but not regular d) It is neither regular non CFL
Q Which of the following languages over {a, b, c} is accepted by deterministic push down
automata?
a) {ω| ω is palindrome over {a, b, c}} b) {ω ω𝑅 | ω ϵ {a, b, c}*}
c) {a𝑛 b𝑛 c 𝑛 | n ≥ 0} d) {ω c ω𝑅 | ω ϵ {a, b, ) ∗}
Q Which of the following statement must always be true for A and B? Suppose A and B are
two sets of strings from ∑*, such that B ⊆ A
i) If A is finite then, B is finite
ii) If A is regular then, B is regular
iii) If A is context free then, B is context free
a) i) only b) ii) only c) iii) only d) All three
Q L = {𝑎𝑚 𝑏 𝑛 𝑐 𝑚+𝑛 | m, n ≥ 1}
a) Regular b) C.F.L but not regular
c) C.S.L but not CFL d) Type – 0 but not context sensitive
(NET-JULY-2018)
a) Only L1 is Context Free Language
b) Only L2 is Context Free Language
c) Both L1 and L2 are Context Free Languages
d) Neither L1 Nor L2 are context free languages
Ans: a
Q The language L = {0i21i | i≥0} over the alphabet {0,1, 2} is: (GATE-2007) (2 Marks)
a) not recursive. b) is recursive and is a deterministic CFL.
c) is a regular language. d) is not a deterministic CFL but a CFL
Q Consider the following languages:
L1 = {an bm cn : m, n >= 1} L2 = {an bn c2n : n >= 1}
Which one of the following is TRUE? (GATE-2016) (2 Marks)
a) Both L1 and L2 are context-free
b) L1 is context-free while L2 is not context-free.
c) L2 is context-free while L1 is not context-free.
d) Neither L1 nor L2 is context-free.
ANSWER B
Q The language L= {0i 2 1i | i ≥ 0} over the alphabet {0,1, 2} is: (GATE-2007) (1 Marks)
(A) not recursive (B) is recursive and is a deterministic CFL.
(C) is a regular language. (D) is not a deterministic CFL but a CFL.
Answer: (B)
Examples: The below table shows the transition table of a Moore Machine.
Here, For the input string 0111, the transition of states is given by q 0 🡪 q3 🡪q0 🡪q1🡪q2. The
output string is 00010.
Q construct a Moore machine take all the string of a’s and b’s as i/p and counts the no of a’s in
the i/p string in terms of 1, ∑ = {a, b}, Δ = {0, 1}?
Q construct a Moore machine take all the string of a’s and b’s as i/p and counts the no of
occurrence of sub-string ‘ab’ in terms of 1, ∑ = {a, b}, Δ = {0, 1}?
Q construct a Moore machine take all the string of a’s and b’s as i/p and counts the no of
occurrence of sub-string ‘aa’ in terms of 1, ∑ = {a, b}, Δ = {0, 1}?
Q construct a Moore machine where ∑ = {0, 1}, Δ = {a, b, c}, machine should give o/p a, if the
i/p string ends with 10, b if i/p string ends with 11, c otherwise?
Mealy Machine
• Mealy machine is a six-tuple (Q, ∑, Δ, δ, λ, q0), where all the symbols except λ have the
same meaning as in the Moore machine. λ is the output function mapping ∑ x Q into Δ.
• in case of mealy machine, the output symbol depends on the transition.
• If the length of i/p string is n, then length of o/p string will be n
• Mealy machine do not response for empty string
Example: The below table shows the transition table of a Mealy Machine.
For the input string 0011, the transition of states is given by q 1 🡪q3 🡪q2🡪q4🡪q3 and the output
string is 0100.
Q construct a Mealy machine take all the string of a’s and b’s as i/p and counts the no of a’s in
the i/p string in terms of 1, ∑ = {a, b}, Δ = {0, 1}?
Q construct a Mealy machine take all the string of a’s and b’s as i/p and counts the no of
occurrence of sub-string ‘ab’ in terms of 1, ∑ = {a, b}, Δ = {0, 1}?
Q construct a Mealy machine take all the string of a’s and b’s as i/p and counts the no of
occurrence of sub-string ‘aa’ in terms of 1, ∑ = {a, b}, Δ = {0, 1}?
Q construct a Mealy machine where ∑ = {0, 1}, Δ = {a, b, c}, machine should give o/p a, if the
i/p string ends with 10, b if i/p string ends with 11, c otherwise?
CONVERSION OF MOORE TO MEALY MACHINE
• Let us take an example to understand the conversion:
• Convert the following Moore machine into its equivalent Mealy machine.
• To convert a mealy machine to moore machine all you need to do is just push out the
outputs of states onto to the incoming transitions.
• While conversion from moore to mealy machine, the number of states will we same and
there will be no extra states.
• The equivalent Moore Machine will be:
PROCEDURE FOR TRANSFORMING A MEALY MACHINE INTO A MOORE MACHINE
Consider the Mealy Machine:
• For state q1, there is only one incident edge with output 0. So, we don't need to split
this state in Moore machine.
• For state q2, there is 2 incident edge with output 0 and 1. So, we will split this state into
two states q20 (state with output 0) and q21(with output 1).
• For state q3, there is 2 incident edge with output 0 and 1. So, we will split this state into
two states q30 (state with output 0) and q31 (state with output 1).
• For state q4, there is only one incident edge with output 0. So, we don't need to split
this state in Moore machine.
• if mealy machine have m states and the resultant moore machine have n states then
m <= n <= m* Δ
Transition table for Moore machine will be:
If the initial state is A=0, B=0, what is the minimum length of an input string which will take the
machine to the state A=0, B=1 with Output = 1? (GATE-2009) (2 Marks)
(A) 3 (B) 4 (C) 5 (D) 6
Answer: (A)
Explanation: (0, 0) –1–> (0, 1) –0–> (1, 0) –1–> (0, 1) and output 1
Q The following diagram represents a finite state machine which takes as input a binary
number from the least significant bit. (GATE-2005) (1 Marks)
Q The Finite state machine described by the following state diagram with A as starting state,
where an arc label is x / y and x stand for 1-bit input and y stands for 2- bit output (GATE-2002)
(2 Marks)
(A) Outputs the sum of the present and the previous bits of the input.
(B) Outputs 01 whenever the input sequence contains 11.
(C) Outputs 00 whenever the input sequence contains 10.
(D) None of these
Answer: (A)
Q The Moore machine has six tuples (Q, ∑, ∆, δ, λ, 𝑑0 ). Which of the following is true?
a) δ is the output function
b) δ is the transition function ∑ into Q
c) λ is the transition function ∑ x Q into Q
d) λ is the output function mapping Q into Δ
Problem
after this much study now we understand that there are number of languages which cannot
be accepted by finite automata, actually finite automata is able to perform to generic
operations it can count and it can remember order. But a task like comparison between two
symbols cannot be performed by finite automata.
Solution
We require memory so that we can compare between two or more different symbols, though
there are a number of options available for memory like array, stack, queue, link list, trees etc.
out of them all stack is the most appropriate here as indexing is not required in stack as it is a
zero-address data structure. It is also easy to check extreme conditions like over flow under
flow.
Context free Grammar generates context free language which is accepted by push down
automata
BLOCK DIAGRAM OF PDA
• Finite control unit is also called as memory unit it is static and limited. So to process the
i/p string if the static memory is not sufficient then we can use the stack.
• i/p tape is divided into cells where is cell is capable of holding one symbol at a time. At
stack of infinite size, which support three operations push, pop and skip.
• The accepting power of a pda is more than that of finite automata and less than that of
linear bounder automata.
• The power of non-deterministic pda is more than the power of deterministic pda.
Formal Definition of PDA
A PDA is a 7-tuple, namely (Q, ∑, Γ, δ, q0, Z0, F), where
(i) Q – is a finite nonempty set of states,
(ii) ∑ – is a finite nonempty set of input symbols,
(iii) Γ – is a finite nonempty set of pushdown symbols,
(iv) q0 – is a special state called the initial state,
(v) Z0 – is a special pushdown symbol called the initial symbol on the pushdown store.
(vi) F – is a set of final states, a subset of Q and
(vii) δ – is a transition function from Q x (∑ U {∈}) x Γ to the set of finite subsets of Q x Γ*.
Representation of States
(1) PUSH – one symbol can be inserted into the stack at one time.
(2) POP – one symbol can be deleted from the stack at one time.
(3) SKIP – IT means no stack operation, status of the stack will remain same, before a after the
operation
note- if pda perform a push or a pop operation at least one’s during processing of string than
we say that pda is using the stack.
There is no change in the language acceptance capability of the pda either we accept by final
state of empty state.
Example: Design a Deterministic Push Down Automata for {an bn | n >= 1}
• The a's in the given string are added to the stack. When the symbol b is encountered in
the input string, an a is removed from the stack. Thus, the matching of number of a's
and the number of b's is accomplished.
Steps:
• First, we have to count number of a's and number of b should be equal.
• We will achieve this by pushing a's in STACK and then we will pop a's whenever "b"
comes.
• So, in the end of the strings if nothing is left in the STACK then we can say that language
is accepted in the PDA.
Non- Deterministic PDA can also be defined using 7 tuples s.t. the transition function δ is
defined as: Q x (∑ U {∈}) x Γ to the set of finite subsets of Q x Γ*.
δ: Q x {∑ U ∈} x Γ → 2(Q x Γ* )
i.e. on a given input symbol and stack symbol a NPDA can move to more than one state.
Rest all other tuples are same as DPDA.
Let us take an example to understand about NPDA.
Example: Design a NPDA for wwr | w∈ (a, b)+
An example string for such case is: abba
As seen in the previous example we were aware about the center, but in this case we are not
aware about the center. So we will be making a NPDA where the NPDA will be assuming two
cases:
1. Center has been reached when we have two same symbols in input and top of stack as
in case of abba, as this is the only way to determine center.
2. Center has not been reached and the NPDA will have to continue pushing symbol as in
an example case of ‘aaaa’, in this case we cannot assume that center is reached when
we have two same symbols in input and top of stack.
Q Let Nf and Np denote the classes of languages accepted by non-deterministic finite automata
and non-deterministic push-down automata, respectively. Let Df and Dp denote the classes of
languages accepted by deterministic finite automata and deterministic push-down automata,
respectively. Which one of the following is TRUE? (GATE-2005) (1 Marks)
(A) Df ⊂ Nf and Dp ⊂ Np (B) Df ⊂ Nf and Dp = Np
(C) Df = Nf and Dp = Np (D) Df = Nf and Dp ⊂ Np
Answer: (D)
Q The language accepted by a Pushdown Automation in which the stack is limited to 10 items
is best described as (GATE-2003) (1 Marks)
(A) Context Free (B) Regular (C) Deterministic Context Free (D) Recursive
Answer: (B)
Q 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-2016) (2 Marks)
Answer: (D)
Q The languages
1. {0n 1n | n>=1} and 2. { 0n 12n | n >= 1} U {0n 1n | n >= 1} are
a) both accepted by deterministic push down automata
b) 1can be accepted by a DPDA but 2 requires a NDPDA
c) 2 can be accepted by a DPDA but 1 requires a NDPDA
d) None of the above
Q The pushdown automation M = ({q0, q1, q2}, {a, b}, {0, 1}, δ, q0, 0, {q0}) with (NET-DEC-2014)
δ(q0, a, 0) = {(q1, 10)}
δ(q1, a, 1) = {(q1, 11)}
δ(q1, b, 1) = {(q2, ∈)}
δ(q2, b, 1) = {(q2, ∈)}
δ(q2, ∈, 0) = {(q0, ∈)}
Accepts the language
(A) L = {an bm | n, m ≥ 0} (B) L = {an bn| n ≥ 0}
(C) L = {an bm | n, m > 0} (D) L = {an bn| n > 0}
Ans: d
Q Consider the pushdown automaton (PDA) below which runs over the input alphabet (a, b, c).
It has the stack alphabet {Z0 , X} where Z0 is the bottom of – stack marker. The set of states of
the PDA is {s, t, u, f} where s is the start state and f is the final state. The transition of the PDA
given below is depicted in a standard manner. For example, the transition. For example the
transition (s, b, X) → (t, X, Z0 ) means that if the PDA is in state s and the symbol on the top of
the stack is X, then it can read b from the input and move to state after popping the top of
stack and pushing the symbol Z0 and X (in that order) on the stack.(GATE-2003) (2 Marks)
(s, a, Z0 ) → (s, XXZ0 )
(s, ε, Z0 ) → (f, ε)
(s, a, X) → (s, XXX)
(t, b, X) → (t, ε)
(t, b, X) → (t, ε)
(t, c, X) → (u, ε)
(u, c, X) → (u, ε)
(u, ε, Z0 ) → (f, ε)
The language accepted by the PDA is
a) {al bm c n | l = m = n} b) {al bm c n | l = m}
c) {al bm c n | 21 = m + n} d) {al bm c n |m = n}
Ans: c
Q Given the following statements: (NET-JUNE-2012)
(i) The power of deterministic finite state machine and non- deterministic finite state machine
are same.
(ii) The power of deterministic pushdown automaton and non- deterministic pushdown
automaton are same.
Which of the above is the correct statement(s)?
(A) Both (i) and (ii) (B) Only (i)
(C) Only (ii) (D) Neither (i) nor (ii)
Ans: b
Introduction
Language usually contains infinite number of strings (string length is finite), we cannot
tabulate each and every string to represent the language, therefore like automata,
grammar is also a mathematical model of representing a language, using which we can
generate the entire language. Therefore, a grammar is usually thought of as a language
generator.
Formal Grammar
BASIC DEFINITIONS
A phrase-structure grammar (or simply a grammar) is a 4-tuple (VN, ∑, P, S), where
1. VN is a finite nonempty set whose elements are called variables,
2. ∑ is a finite nonempty set whose elements are called terminals, VN ⋂ ∑= Ф.
3. S is a special variable (i.e., an element of VN (S Vn)) called the start symbol. Like
every automaton has exactly one initial state, similarly every grammar has exactly
one start symbol.
4. P is a finite set whose elements are α → β. where α and β are strings on VN ⋃ ∑. α has
at least one symbol from VN, the element of P are called productions or production
rules or rewriting rules. {Σ U Vn}* some writer refers it as total alphabet
α→β
α {Σ U Vn}* Vn {Σ U Vn}*
β {Σ U Vn}*
L (G) = {w | w ∑* , S →* W}
• →*(reflexive, transitive closure) means from s we can derive w in zero or more steps
• Using same idea, we do processing of natural languages in computers, Actively used
in compliers
• L (G) is the set of all terminal strings derived from the start symbol S.
• G1 and G2 are equivalent if L (G1) = L (G2).
Example:
Ans. S → 0S1 → 02S12 →…→ 0n1n, The string will end using S→ ^
Therefore,
0n1n ∈ L (G)
Chomsky Classification of Languages
• Chomsky classified the grammars into four types in terms of productions (types 0-3).
• This hierarchy of grammars was described by Noam Chomsky in 1956. From type 0 to
type 3, we will be putting more and more restrictions. We will see that more restrictive
is grammar easy will be the language and more liberal is the grammar difficult will be the
language. Based on the production rules of the grammar, we can classify the formal
grammar into four types, based on which we generate different languages.
LANGUAGES AND AUTOMATA
Following are the machines that accepts the following grammars.
Important Remark
• Two grammars of different types may generate the same language.
Type 0 Grammar
• also known as Unrestricted Grammar, phase structured grammar, recursively
enumerable grammar used to generate recursive enumerable language which is
accepted by a Turing machine.
• A type 0 grammar is without any restrictions.
• No restriction on the production rule, that is if there is a production from
α→β
α {Σ U Vn}* Vn {Σ U Vn}*
β {Σ U Vn}*
Type 1 Grammar
• Also known as case sensitive language, length increasing grammar, non-contracting
grammar, used to generate context sensitive language which is accepted by a linear
bounded automaton.
αAβ→αδβ
α , β {Σ U Vn}* A Vn δ {Σ U Vn}+
or
α→β
α {Σ U Vn}* Vn {Σ U Vn}*
β {Σ U Vn}+
• As from the rule we can understand that we cannot have null production, in order to
solve that problem, Production S→, is allowed if S do not appear on the right-hand
side of the production.
• A grammar is called type 1 or context-sensitive or context dependent if all its
productions are type 1 productions.
• Very difficult to have a parse tree
• FORTRAN, PL1 with CGF we use STD
Type 2 Grammar
• Also known as Context Free Grammar, which will generate context free language that
will be accepted by push down automata. (NPDA default case)
• if there is a production, from
α→β
α Vn |α| = 1
β {Σ U Vn} *
Used to generate Regular Grammar which will generate regular language which will be
accepted by finite machine.
Regular grammar can be of two types either left linear or right linear.
A → a / Ba
A, B Vn |A| = |B| = 1
a ∑*
A → a / aB
A, B Vn |A| = |B| = 1
a ∑*
– however, if left-linear rules and right-linear rules are combined, the language need no
longer be regular.
Examples:
Q Consider the grammar
S → aaaS | a | aa
L(G) =?
a) L(G) = {w: |w| mod 3 = 0} b) L(G) = {w: |w| mod 3 = 1 or 2}
c) L(G)= L(a*) d) d) L(G) = L(a*)- (λ)
Q The regular grammar for the language L = {an bm | n + m is even} is given by (NET-AUG-
2016)
a) S → S1 | S2
S1 → a S1| A1
A1 → b A1| λ
S2 → aaS2| A2
A2 → b A2| λ
b) S → S1 | S2
S1 → a S1| aA1
S2 → aaS2| A2
A1 → b A1| λ
A2 → b A2| λ
c) S → S1 | S2
S1 → aaa S1| aA1
S2 → aaS2| A2
A1 → b A1| λ
A2 → b A2| λ
d) S → S1 | S2
S1 → aa S1| A1
S2 → aaS2| A2
A1 → bb A1| λ
A2 → bb A2| λ
Ans: d
Q Let Σ = {a, b} and language L = {aa, bb}. Then, the complement of L is (NET-AUG-2016)
a) {λ, a, b, ab, ba} ∪ {w ∈ {a, b}* | |w| > 3}
b) {a, b, ab, ba} ∪ {w ∈ {a, b}* | |w| > 3}
c) {w ∈ {a, b}* | |w| > 3} ∪ {a, b, ab, ba}
d) {λ, a, b, ab, ba} ∪ {w ∈ {a, b}* | |w| ≥ 3}
Ans: d
Q The regular grammar for the language L = {w | na (w) and nb (w) are both even, w ∈ {a,
b}*} is given by : (Assume, p, q, r and s are states) (NET-DEC-2014)
(A) p → aq | br | λ
q → bs | ap
r → as | bp
s → ar | bq
p and s are initial and final states.
(B) p → aq | br
q → bs | ap
r → as | bp
s → ar | bq
p and s are initial and final states.
(C) p → aq | br | λ
q → bs | ap
r → as | bp
s → ar | bq
p is both initial and final states.
(D) p → aq | br
q → bs | ap
r → as | bp
s → ar | bq
p is both initial and final states.
Ans: c
Q A regular grammar for the language L = {an bm | n is even and m is even}is given by (NET-
SEP-2013)
(A) S → aSb | S1
S1 → bS1a | λ
(B) S → aaS | S1
S1 → bSb | λ
(C) S → aSb | S1
S1 → S1ab | λ
(D) S → aaS | S1
S1 → bbS1 | λ
Ans: d
Q Consider a language A defined over the alphabet sum = {0, 1} as The expression A = left {
0^{left lfloor n / 2 right rfloor}1^{n}: n> = 0 right } The expression left lfloor n / 2 right rfloor
means the floor of n/2, or what you get by rounding n/2 down to the nearest integer.
Which of the following is not an example of a string in A? (NET-DEC-2015)
a) 011 b) 0111 c) 0011 d) 001111
Ans: c
Answer: (D)
Codes :
a b c d
(A) ii i iv iii
(B) iii iv i ii
(C) iii i iv ii
(D) ii iv i iii
Ans: c
Q If all the production rules have single non - terminal symbol on the left side, the grammar
defined is: (NET-JUNE-2015)
a) context free grammar b) context sensitive grammar
c) unrestricted grammar d) phrase grammar
Ans: a
CONSTRUCTION OF A REGULAR GRAMMAR
Let us understand this with the help of an example:
Construct a regular grammar G generating the regular set represented by P = a*b(a + b)*.
• We draw the corresponding DFA to the given problem.
Now since we have the DFA it becomes fairly easy to generate the grammar.
Let G = ({A0, A1}, {a, b}, P, A0), A0 and A1 are representing the states q0 and q1
respectively.
where P is given by:
A0 → aA0, A0→ bA1, A0→ b and
A1→ aA1, A1→ bA1, A1→ a, A1→ b
G is the required regular grammar.
CONSTRUCTION OF A TRANSITION SYSTEM M ACCEPTING L(G) FOR A GIVEN REGULAR
GRAMMAR G
Let us take an example to understand:
Let G =({A0, A1}, {a, b}, P, A0), where P consists of
A0 → aA1, Al → bA1, A1 → a, A1→ bA0.
Construct a transition system M accepting L(G).
Solution:
Let M = ({ q0, q1, qf}, {a, b}, δ, q0 {qf}), where q0 and q1 correspond to A0 and A1
respectively and qf is the new (final) state introduced.
A0→ aA1 induces a transition from q0 to q1 with label a.
Similarly, A1→ bAl and A1 → bA0 induce transitions from q1 to q1 with label b and from
ql to q0 with label b, respectively.
Al→ a induces a transition from q1 to qf with label a. So the resulting transition system is:
Decision properties
• Approximately all the properties are decidable in case a finite automaton. Here we will use
machine model to proof decision properties.
i) Emptiness
ii) Non-emptiness
iii) Finiteness
iv) Infiniteness
v) Membership
vi) Equality
• Emptiness & Non-emptiness
Step 1: - select the state that cannot be reached from the initial states & delete them (remove
unreachable states)
Step 2: - if the resulting machine contains at least one final states, so then the finite automata
accepts the non-empty language.
Step 3: - if the resulting machine is free from final state, then finite automata accepts empty
language.
Step 2: - select the state from which we cannot reach the final state & delete them (remove
dead states)
Step 3: - if the resulting machine contains loops or cycles then the finite automata accepts
infinite language
Step 4: - if the resulting machine do not contain loops or cycles then the finite automata
accepts infinite language
• Membership
Membership is a property to verify an arbitrary string is accepted by a finite automaton or
not i.e. it is a member of the language or not.
Let M is a finite automata that accepts some strings over an alphabet, and let ‘w’ be any
string defined over the alphabet, if there exist a transition path in M, which starts at initial
state & ends in anyone of the final state, then string ‘w’ is a member of M, otherwise ‘w’ is
not a member of M.
• Equality
Two finite state automata M1 & M2 is said to be equal if and only if, they accept the
same language.
Minimise the finite state automata and the minimal DFA will be unique.
Closure Properties of Regular Languages
• Regular languages are closed under following operations
o Kleen Closure
o Positive closure
o Complement
o Reverse Operator
o Prefix Operator
o Complement
o Union
o Intersection
o Set Difference operator
o Prefix operator
o Symmetric Difference
o Quotient Operator
o Substitution
o Homomorphism
o Inverse Homomorphism
o Max
o Min
o Cycle
Q Which of the following is TRUE? (GATE-2007) (2 Marks)
a) Every subset of a regular set is regular.
b) The union of two non-regular sets is not regular.
c) Every finite subset of a non-regular set is regular.
d) Infinite union of finite sets is regular
ANSWER C
Q Which of the following statements about regular languages is NOT true? (GATE-2006) (1-
Marks)
a) Every language has a regular superset
b) Every language has a regular subset
c) Every subset of a regular language is regular
d) Every subset of a finite language is regular
Ans: c
Q Let 𝐿1 and 𝐿2 are regular sets defined over alphabet ∑*. Mark the false statement
a) 𝐿1 ∪ 𝐿2 is regular b) 𝐿1 ∩ 𝐿2 is not regular
c) ∑* -𝐿1 is regular d) 𝐿1 ∗ is regular
Q If L is a regular language over ∑ = {a, b}, which one of the following languages
is NOT regular? (GATE – 2019) (1 Marks)
a) L⋅ LR {xy | x ∈ L, yR∈ L}
b) Suffix (L) = {y ∈ ∑* | ∃x ∈ ∑* such that xy ∈ L}
c) Prefix (L) = {x ∈ ∑* | ∃y ∈ ∑* such that xy ∈ L}
d) {w wR | w ∈ L}
Answer: (B)
Q Let L1, L2 are regular languages and L3 & L4 are non-regular languages then which of the
following need not be regular?
a) (L1 L2) / (L3 L4) b) (L1 L2) / (L3 L4)
c) (L1 L3) / (L2 L4) d) L1 / L3
0𝑛 1𝑛
Q Let X = {0, 1}, L = X* and R = { > 0} then the language L ∪ R and R respectively
𝑛
a) Regular, Regular b) None regular, Regular
c) Regular, Not regular d) Not regular, Not regular