ToC Equation
ToC Equation
(i) even number of 0’s (ii) odd number of 0’s and (iii) even number of 0’s (iii) odd number of 0’s
and even number of 1’s; even number of 1’s; and odd number of 1’s; and odd number of 1’s;
→q0
State 0 1
{q0, q1} {q0}
Q-2.2: Convert the following NFA into DFA. {q0, q1} {q0, q1} {q0, q2}
Ans: We will construct the transition table for the *{q0, q2} {q0, q1} {q0}
given NFA:
→q0
State 0 1
∅
{q0, q1} {q0}
∅ ∅
q1 q2
* q2
→p
δ' 0 1
{p, q} {p}
∅
q {r} {r}
r {s}
*s {s} {s}
Ans: The given transition table for NFA:
Now, we construct a transition table for DFA:
δ 0 1
→p =A {p, q} =B {p} =A
{p, q} =B {p, q, r} =C {p, r} =D
{p, q, r} =C {p, q, r, s} =E {p, r} =D
{p, r} =D {p, q, s} =F {p} =A
*{p, q, r, s} = *E {p, q, r, s} =E {p, r, s} =G
*{p, q, s} = *F {p, q, r, s} =E {p, r, s} =G
*{p, r, s} = *G {p, q, s} =F {p, s} =H
*{p, s} = *H {p, q, s} =F {p, s} =H
Q-2.4: Design DFA that accepts (i) String start with 1 and end with 0. (ii) String with aa & bb, 𝚺 {a, b}.
Ans:
(i) String start with 1 and end with 0. (4) Create a transition from “q1” to a state “q2” on
(1) Create a start state “q0” that represents beginning input “0”. This represents the last character of the
of the string. (2) Create a transition from “q0” to a string being a “0”. (5) Make “q2” an accepting state,
state “q1” on input “1”. This represents the first since it represents the end of a string that starts with
character of the string being a “1”. (3) Create a “1” and ends with “0”; Here is visual representation
transition from “q1” to itself on input “1”. This allows of DFA:
for any number of “1” s to appear in the middle of
the string.
(ii) String with aa & bb, 𝚺 {a, b}. (5) Create a transition from “q3” to a state “q4” on
(1) Create a start state “q0” that represents beginning input “b”. This represents the second character of
of the string. (2) Create a transition from “q0” to a the substring “bb”. (6) Make “q4” an accepting state,
state “q1” on input “a”. This represents the first since it represents the end of a string containing
character of the substring “aa”. (3) Create a both “aa” and “bb”; Here is visual representation of
transition from “q1” to a state “q2” on input “a”. This DFA:
represents the second character of the substring
“aa”. (4) Create a transition from “q2” to a state “q3”
on input “b”. This represents the first character of
the substring “bb”.
Ans: From the given diagram we will construct the transition table with ε-move, the transition table will be:
δ (Y, a) δ (Y, b)
Here, the initial state is “0” Transition Diagram:
→ {0}
State Y=ε-closure
{0, 1, 2, 4, 7} {3, 8} {5}
{3, 8} {1, 2, 3, 4, 6, 7, 8} {3, 8} {5, 9}
{5} {1, 2, 4, 5, 6, 7} {3, 8} {5}
{5, 9} {1, 2, 4, 5, 6, 7, 9} {3, 8} {5, 10}
*{5, 10} {1, 2, 4, 5, 6, 7, 10} {3, 8} {5}
→A
State 0 1
B F
A B C D B G C
*C A C
E H F
F C G
E F G H G G E
H G C
Step-2: Construct a state transition table:
Step-1: State D is not accessible from the initial state
A; So, we eliminate state D and its associated edges
Step-3: Now using Equivalence Theorem,
For 0 Equivalence: ( {A, B, E, F, G, H} {C} )
For 1 Equivalence: ( {A, E, G} {B, H} {F} {C} )
For 2 Equivalence: ( {A, E} {G} {B, H} {F} {C} )
For 3 Equivalence: ( {A, E} {G} {B, H} {F} {C} )
Since, 2 Equivalence and 3 Equivalence both are
same. So, we stop.
from the DFA; The resulting DFA is_
Step-4: From 3 equivalence, we see that, States
A and E are equivalent and can be merged together;
States B and H are equivalent and can be merged
together.
So, Minimized DFA is given below:
Q-3.3: Use a δ^ to describe the processing of input So, the string 00101 is accepted as is reached the
00101 by the NFA of following figure: final state {q2}.
1) δ^ (q0, ε) = {q0}
A summary of the steps is given below:
2) δ^ (q0, 0) = {q0, 0}
={q0, q1} Fig: States an NFA is in during the processing of input
3) δ^ (q0, 00) = δ(q0, 0) 4) δ^ (q0, 001) = δ(q0, 1)
∪ δ(q1, 0) ={q0, q1} ∪ ∪ δ(q1, 1) ={q0} ∪ {q2}
sequence 00101.
Q-3.4: Write a regular expression for the following: (i) All string over {0, 1} with the substring ‘0101’. (ii) All
strings beginning with ‘11’ and end with ‘ab’. (iii) Set of all strings over {a, b} with 3 consecutive b’s. (iv) Set of
all strings that end with ‘1’ and has no substring ‘00’.
Ans: (i) All string over {0, 1} with the substring ‘0101’: (0 + 1)* 0101(0 + 1); (ii) All strings beginning with ‘11’
and end with ‘ab’: 11(0 + 1)*(a + b)*ab; (iii) Set of all strings over {a, b} with 3 consecutive b’s: (a + b)*bbb (a +
b); (iv) Set of all strings that end with ‘1’ and has no substring ‘00’: (01 + 1)*1;
Q-3.5: Write a regular expression for the following: (i) The set of strings that contains alternating 0’s and 1’s.
(ii) The set of strings of 0’s and 1’s with at most one pair of consecutive 1’s. (iii) The set of strings a’s and b’s
that start with a and ends with b.
of 0’s and 1’s with at most one pair of consecutive 1’s: (0 + 10)*(11 + ε)(0 + 10)* (iii) The set of strings a’s and
Ans: (i) The set of strings that contains alternating 0’s and 1’s: (01)*(10)* + (10)* + 1(01)*; (ii) The set of strings
E→ + EE E→ + EE
L.M.D: - R.M.D: -
(ii) Now, we can say that the grammar E→ + EE|*EE| -EE|x|y, has a derivation tree where each string of
this grammar has at least one parse tree, so we can say that the grammar is unambiguous.
If we observe carefully, then not only A have ∈ ∴S→a|Ab|b|aa|aBa; & A→b; B→b;
Given, S→a|Ab|aBa; A→b|∈; B→b|A; S→Ab; if A→∈; S→b; if B→∈; S→aa for S→aBa;
production but even B also has ∈ production; A→∈; Finally, the rules are,
Straight forward for B→A→∈; Now, apply the S → a|Ab|b|aa|aBa
method of replacement, A → b; B → b
Q-5.3: The grammar ({S, A, B}, {a, b}, P, S) that has the productions: S → bA|aB; A → bAA|aS|a; B → aBB|bS|
b;
Find an equivalent grammar in CNF.
The only production already in proper form are, So, B → CaD2 [let, D2 →BB]
A → a and B → b; Again, B → bS; B → CbS;
Now, S → bA; S → CbA [let, Cb→b] Now, the production for the grammar in CNF are
Again, S → aB; S → CaB; shown below-
Now, A → bAA; A → CbAA [let, Ca→a] S → CbA|CaB D1 →AA
A → CbD1 [let, D1 →AA] A → CaS|CbD1|a D2 →BB
Again, A → aS; A → CaS; B → CbS|CaD2|b Ca→a
Now, B → aBB; B → CaBB; Cb→b
Q-5.4: Find out the CFL for: S → aSb|aAb; A → bAa; A → ba;
Here in this case we need to find the language L for For the first derivation, we can write,
the CFG; Let derive some strings to get the patterns: L1 = {a2 (ba)1 a2 }
S → aSb [using S → aSb]; →aaAbb [using S→aAb]; For the second derivation, we can write,
→ aababb [using A → ba]; L2 = {a1 (ba)1 a1 }
Again, S → aAb [using S → aAb]; →abaa [using A In general, we can write the language,
→ba] L = {an (ba)m an : m= n - 1 }
Let’s have a look on the generated strings.
them if so; (ii) Eliminate ∈ productions. (iii) Eliminate unit productions. (iv) Put the grammar into CNF.
Q-5.5: For the grammar: S → ASB|∈; A → aAS|a; B → SbS|bb; (i) Are there any useless symbols? Eliminate
(i) Observe that A and B each derive terminal strings (iii) The only unit production is B → A. Thus, we
and therefore so does S. Thus, there are no useless replace this A by the bodies of all the A productions.
symbols. The result: S → ASB|AB; A → aAS|aA|a;
(ii) Here, only S has null production. So, we must B → SbS|bS|Sb|b|aAS|aA|a|bb; Here, the resulting
[S → ∈]
eliminate this rule. We have, S → ASB; grammar of (ii) is considered.
[S → ∈]
So, S → AB (iv) Introduce variables and production.
[S → ∈]
A → aAS; So, A → aA Ca → a; Cb → b; E → SB; F → AS; G →CbS [Cb → b];
[S → ∈]
B → SbS; B → Sb These new production will replace the old
[S → ∈]
Again, B → bS productions in order to sustain CNF.
Again, B → b In CNF we get, S → AE|AB; A → CaF|CaA|a;
The resulting grammar: B → SG|SCb|CbS|b|CaF|CaA|a|CbCb;
S → ASB|AB; A → aAS|aA|a; B → SbS|bS|Sb|b|A| Ca → a; Cb → b; E → SB; F → AS; G →CbS;
bb;
Q-5.6: Consider the grammar: S →AB|C; A →aAb|ab; B →cBd|cd; C →aCd|aDd; D →bDc|bc; Show in
particular that the string aa bb cc dd: (i) Parse tree (ii) Left most derivation. (iii) Right most derivation. (iv) Is
the grammar is ambiguous or not?
The string aa bb cc dd has two left most derivation And, S → C
and the two-parse tree. → aCd
(i) & (ii) S → AB → aaDdd
→ aAbB → aabDcdd
→ aabbB → aabbccdd
→ aabbcBd
→ aabbccdd
(iii) Right most derivation: (iv) Yes, the grammar is ambiguous as it has two left
S → AB most derivation for the same string “aa bb cc dd”.
→ AcBd
→ Accdd
→ aAbccdd
→ aabbccdd
Q-5.7: Consider the grammar: S → aB|bA; A →a|aS|bAA; B →b|bS|ABB; Derive the string ‘aaabbabbba’ as
left most and right most derivation and draw parse tree.
The left most derivation: The right most derivation:
S → aB S → aB
→ aABB → aABB
→ aaBB → aABbS
→ aaABBB → aABbbA
→ aaaBBB → aABbba
→ aaabBB → aAABBbba
→ aaabbB → aAABbbba
→ aaabbABB → aAAbSbbba
→ aaabbaBB → aAAbbAbbba
→ aaabbabB → aAAbbabbba
→ aaabbabbS → aAabbabbba
→ aaabbabbbA → aaabbabbba
→ aaabbabbba
Q-5.8: Consider the grammar: E→E + E|E*E|E – E|id. Show in particular that the string id + id * id – id. (i)
Parse tree (ii) Left most derivation (iii) Right most derivation (iv) Is the grammar ambiguous or not?