UNIT -I
1. Design DFA for language L={ab5wb4 : w є{a, b}*}
Solution
2. Write the following definitions with example for each
a. Symbols b. Alphabets c. String d. Language e. Kleene Closure
Solution:
a. Symbols
A symbol is a single object, an abstract entity that has no meaning by itself.
Example: A, a, π, 0, 1 etc
b. Alphabets
Alphabets are defined as a finite set of symbols.
Examples:
∑ = {0, 1} is an alphabet of binary digits
∑ = {A, B, C, …., Z} is an alphabet.
c. String
A string is a finite sequence of symbols selected from some alphabet. It is generally
denoted as w.For example for alphabet ∑ = {0, 1} w = 010101 is a string.
Length of a string is denoted as |w|
d. Language
A language is a set of strings taken from a given alphabet.
Example: Take the alphabet Σ = {a,b}.
Language L = set of strings having a length less than or equal to 2.
L= {ε,a,b,aa,ab,ba,bb}
e. Kleene Closure
If S is a set of words then by S* we mean the set of all finite strings formed by
concatenating words from S, where any word may be used as often we like, and where
the null string is also included.
S* is the Kleen closure for S.
For example for ∑ = {a}
∑* = {є, a, aa, aaa, ….}
3. Define the term NFA with its tuples. Construct NFA of all binary string in which the
second last bit is 1 where ∑={0,1}.
An NDFA can be represented by a 5-tuple (Q, ∑, δ, q0, F) where −
• Q is a finite set of states.
• ∑ is a finite set of symbols called the alphabets.
• δ is the transition function where δ: Q × ∑ → 2Q
• q0 is the initial state from where any input is processed (q0 ∈ Q).
• F is a set of final state/states of Q (F ⊆ Q).
4. Convert the given NFA to DFA
5. Draw the block diagram of finite automata. Design a DFA that accepts the strings ends
with 011 over the alphabet ∑={0,1}.
7. Design DFA to accept set of all strings over {a,b} that contain even number of a’s and
even number of b’s.
8. Convert the following Non-Deterministic Finite Automata (NFA) to Deterministic Finite
Automata (DFA)-
Solution:
Transition table for the given Non-Deterministic Finite Automata (NFA) is-
State / Alphabet a b
→q0 {q1, q2} Ø
{q1, q2} {q1, q2} q2
q2 {q1, q2} q2
Ø Ø Ø
Transition table for Deterministic Finite Automata (DFA) is-
State / Alphabet a B
→q0 *{q1, q2} Ø
*{q1, q2} *{q1, q2} q2
q2 *{q1, q2} q2
Ø Ø Ø
Deterministic Finite Automata (DFA) may be drawn as-
9. Differentiate Deterministic Finite Automata (DFA) and Non Deterministic Finite
Automata with an example.
Example of NFA and DFA that accepts strings ending with ‘ab’
10. Describe the model of finite automata with the block diagram.
UNIT -II
1. Explain closure properties of regular expression.
The operation of the union on RE:
If R1 and R2 are two REs over Σ, then L(R1 ∪ R2) is denoted by L(R1 + R2).
L(R1 ∪ R2) is a string from R1 or a string from R2.
L(R1 + R2) = L(R1) ∪ L(R2).
The operation of concatenation on RE:
If R1 and R2 are two REs over Σ, then L(R1 ∩ R2) is denoted by L(R1R2).
L(R1 ∩ R2) is a string from R1 followed by a string from R2.
L(R1.R2) = L(R1).L(R2)
The operation of closure on RE:
If R1 and R2 are two REs over Σ, then L(R*) is a string obtained by concatenating n
elements for n ≥ 0.
Kleene’s Closure on regular expression:
It is defined as a set of all strings including null (Λ) obtained from the alphabets of the
set Σ. It is denoted as Σ*.
If Σ = {0}, then Σ* = {Λ, 0, 00, 000…}
If Σ = {0, 1}, then Σ* = {Λ, 0, 1, 01, 00, 11, 010, 011, 100…}
2 . Check whether the given grammar is ambiguous or not-
S → AB / C
A → aAb / ab
B → cBd / cd
C → aCd / aDd
D → bDc / bc
Solution:
Let us consider a string w generated by the given grammar-
w = aabbccdd
Now, let us draw parse trees for this string w.
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
3. Define ambiguous grammar. Check whether the grammar
S -> SbS / a
is ambiguous or not for the given string: a b a b a b a
Solution
A grammar is said to be ambiguous if for at least one string generated by it, it produces more
than one-
• parse tree
• or derivation tree
• or syntax tree
• or leftmost derivation
• or rightmost derivation
Let w= abababa
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
4. Prove that L= {0i1i / i>=1} is not regular by using Pumping Lemma.
Solution:
Assume that L = {0i 1i | i ≥ 1} is regular.
Let, w = 0 1 such that |w| = 2n. By pumping lemma we can write
w = xyz such that |xy| ≤ n and |y| ≠ 0.
Now if xy1z ϵ L then the language L is said to be regular.
There are many cases - i) y has only 0's ii) y has only 1's iii) y has both 0's and 1's. i) If y has only O's
then the string
w = 0n-k|n = xz since y = 0k and i = 0
Surely n-k ≠ n. Hence xz € L.
Hence our assumption of being L regular is wrong.
ii) If y has only 1's then, for i = 0 and y = 1k
w = xz = 0n 1n-k
As n ≠ n-k, xz = w € L
Again L is not regular.
iii) If y has 0's and 1's then
w = 0n-k 0k 1j 1n-j = xyi z
If i = 2 then
w = 0n-k 02k 12j 1n-1 € L
Hence from all these 3 cases it is clear that language L is not regular.
5. Write various Algebraic Laws for Regular Expression.
Theorem: Let P,Q,R be regular expressions. The following identities hold.
6. Find regular expression for the following DFA using Arden’s Theorem-
Solution-
Step-01:
Form a equation for each state-
• q1 = ∈ ……(1)
• q2 = q1.a ……(2)
• q3 = q1.b + q2.a + q3.a …….(3)
Step-02:
Bring final state in the form R = Q + RP.
Using (1) in (2), we get-
q2 = ∈.a
q2 = a …….(4)
Using (1) and (4) in (3), we get-
q3 = q1.b + q2.a + q3.a
q3 = ∈.b + a.a + q3.a
q3 = (b + a.a) + q3.a …….(5)
Using Arden’s Theorem in (5), we get-
q3 = (b + a.a)a*
Thus, Regular Expression for the given DFA = (b + aa)a*
7. Prove that L= {0n12n / n>=1} is not regular by using Pumping Lemma.
Solution
To prove that the language L = { 0ⁿ 1²ⁿ | n ≥ 1 } is not regular using the Pumping Lemma, we
proceed as follows:
Step 1: Assume L is Regular
We assume, for contradiction, that L is a regular language. By the Pumping Lemma, there
exists a pumping length p such that any string w in L with |w| ≥ p can be split into three parts:
w=xyzw = xyzw=xyz
satisfying the following conditions:
1. |xy| ≤ p (the first p characters of the string)
2. |y| > 0 (y must be non-empty)
3. xyⁱz ∈ L for all i ≥ 0 (the pumped string must still belong to L)
Step 2: Choose a String in L
We choose a string w from L such that |w| ≥ p. A good choice is:
w=0p12p
where n = p (since w ∈ L requires twice as many 1s as 0s).
Step 3: Split w into xyz
Since |xy| ≤ p, and y ≠ ε, the substring y consists only of 0s because the first p
characters of w are all 0s.
Let’s assume:
x=0a, y=0b, z=0 p−a−b 12p where b ≥ 1 (since y is non-empty) and a + b ≤ p.
Step 4: Pump y (i = 2)
If we pump y (i.e., repeat it), the new string becomes:
w′=xyyz=0a(0b0b)0p−a−b12p
=0p+b12p
Step 5: Check if w' ∈ L
For w' to be in L, it must follow the pattern 0ⁿ 1²ⁿ. However, the number of 0s in w' is
p + b, while the number of 1s remains 2p.
This means the new string has more than n zeros but still 2n ones, breaking the
required ratio n zeros and 2n ones.
Step 6: Contradiction
Since w' ∉ L, we have contradicted our assumption that L is regular.
Thus, L is not a regular language.
8. Consider the grammar-
S → bB / aA
A → b / bS / aAA
B → a / aS / bBB
For the string w = bbaababa, find-
1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree
Solution:
1. Leftmost Derivation-
S → bB
→ bbBB (Using B → bBB)
→ bbaB (Using B → a)
→ bbaaS (Using B → aS)
→ bbaabB (Using S → bB)
→ bbaabaS (Using B → aS)
→ bbaababB (Using S → bB)
→ bbaababa (Using B → a)
2. Rightmost Derivation-
S → bB
→ bbBB (Using B → bBB)
→ bbBaS (Using B → aS)
→ bbBabB (Using S → bB)
→ bbBabaS (Using B → aS)
→ bbBababB (Using S → bB)
→ bbBababa (Using B → a)
→ bbaababa (Using B → a)
3. Parse Tree-
• Whether we consider the leftmost derivation or rightmost derivation, we get the above parse
tree.
• The reason is given grammar is unambiguous.
9. For the following grammar give the leftmost and rightmost derivation for the
following string ‘00101’
S-> A|B
A -> 0A | є
B -> 0B|1B| є
Solution
1. Leftmost Derivation -
S → A1B
→ 0A1B (Using A → 0A)
→ 00A1B (Using A → 0A)
→ 001B (Using A → ∈)
→ 0010B (Using B → 0B)
→ 00101B (Using B → 1B)
→ 00101 (Using B → ∈)
2. Rightmost Derivation-
S → A1B
→ A10B (Using B → 0B)
→ A101B (Using B → 1B)
→ A101 (Using B → ∈)
→ 0A101 (Using A → 0A)
→ 00A101 (Using A → 0A)
→ 00101 (Using A → ∈)
2. Parse Tree-
• Whether we consider the leftmost derivation or rightmost derivation, we get the above
parse tree.
• The reason is given grammar is unambiguous.
10. Check whether the given grammar is ambiguous or not-
S → aSbS / bSaS / ∈
Solution
Let us consider a string w generated by the given grammar-
w = abab
Now, let us draw parse trees for this string w.
Since two different parse trees exist for string w, therefore the given grammar is
ambiguous.
UNIT- III
1. Describe the model of PDA with a neat diagram.
Solution:
o Pushdown automata is a way to implement a CFG in the same way we design DFA for
a regular grammar. A DFA can remember a finite amount of information, but a PDA
can remember an infinite amount of information.
o Pushdown automata is simply an NFA augmented with an "external stack memory".
The addition of stack is used to provide a last-in-first-out memory management
capability to Pushdown automata. Pushdown automata can store an unbounded amount
of information on the stack. It can access a limited amount of information on the stack.
A PDA can push an element onto the top of the stack and pop off an element from the
top of the stack. To read an element into the stack, the top elements must be popped off
and are lost.
o A PDA is more powerful than FA. Any language which can be acceptable by FA can
also be acceptable by PDA. PDA also accepts a class of language which even cannot
be accepted by FA. Thus PDA is much more superior to FA.
PDA Components:
Input tape: The input tape is divided in many cells or symbols. The input head is read-
only and may only move from left to right, one symbol at a time.
Finite control: The finite control has some pointer which points the current symbol
which is to be read.
Stack: The stack is a structure in which we can push and remove the items from one end
only. It has an infinite size. In PDA, the stack is used to store the items temporarily.
2. Write various types of Turing Machine.
Solution
Turing Machines with Two Dimensional Tapes
This is a kind of Turing machines that have one finite control, one read-write head and one
two dimensional tape. The tape has the top end and the left end but extends indefinitely to
the right and down. It is divided into rows of small squares. For any Turing machine of this
type there is a Turing machine with a one dimensional tape that is equally powerful, that
is, the former can be simulated by the latter.
Turing Machines with Multiple Tapes :
This is a kind of Turing machines that have one finite control and more than one tapes each
with its own read-write head.
Turing Machines with Multiple Heads :
This is a kind of Turing machines that have one finite control and one tape but more than
one read-write heads.
It can be easily seen that this type of Turing machines are as powerful as one tape Turing
machines.
Turing Machines with Infinite Tape :
This is a kind of Turing machines that have one finite control and one tape which extends
infinitely in both directions.
It turns out that this type of Turing machines are only as powerful as one tape Turing
machines whose tape has a left end.
Nondeterministic Turing Machines
A nondeterministic Turing machine is a Turing machine which, like nondeterministic finite
automata, at any state it is in and for the tape symbol it is reading, can take any action
selecting from a set of specified actions rather than taking one definite predetermined
action. Even in the same situation it may take different actions at different times. Here an
action means the combination of writing a symbol on the tape, moving the tape head and
going to a next state.
3. Define pushdown automata, its tuples and various types.
Solution:
A Pushdown Automaton (PDA) is a computational model used to recognize Context-Free
Languages (CFLs). It extends the concept of a finite automaton by adding a stack, which
provides additional memory for handling nested and recursive structures, such as balanced
parentheses or nested expressions.
A PDA is formally defined as a 7-tuple (Q, Σ, Γ, δ, q₀, Z₀, F), where:
• Q: A finite set of states.
• Σ: The input alphabet (set of symbols).
• Γ: The stack alphabet (symbols that can be pushed or popped from the stack).
• δ: The transition function, defined as:
δ: Q × (Σ ∪ {ε}) × Γ → P(Q × Γ*)
This means the PDA can transition based on the current state, input symbol (or ε for no
input), and the top of the stack, and update the stack accordingly.
• q₀: The start state, where the computation begins.
• Z₀: The initial stack symbol, present on the stack at the start.
• F: A set of accept states, where the computation ends if the PDA reaches one of these
states.
A PDA accepts a string in one of two ways:
• By reaching an accept state.
• By emptying its stack (depending on the type of PDA).
PDA is of two types:
a. Deterministic PDA
Going to only one state and pushing one element to Top Of Stack
b. Non – deterministic PDA
Going to more than one state and print more than one element to Top Of Stack
4. Design a Turing Machine for L= {an bn cn / n>=1}
Solution
5. Design pushdown automata for the language L={an b2n / n>=1}.
Solution:
The strings which are generated by the given language are as follows −
L={abb,aabbbb,aaabbbbbb,….}
Here a’s are followed by double the b’s
Whenever ‘a’ comes, push ‘a’ two times in the stack and if ‘a’ comes again then do the
same.
When ‘b’ comes then pop one ‘a’ from the stack each time. Note that b comes after ‘a’.
Finally at the end of the strings, if nothing is left in the STACK, then we can declare that
language is accepted in the PDA.
The PDA for the problem is as follows −
Transition functions
The transition functions are as follows −
Step 1: δ(q0, a, Z) = (q0, aaZ)
Step 2: δ(q0, a, a) = (q0, aaa)
Step 3: δ(q0, b, a) = (q1, ε)
Step 4: δ(q1, b, a) = (q1, ε)
Step 5: δ(q1, ε, Z) = (qf, Z)