Toc Note
Toc Note
Q1. Draw a DFA for the language accepting strings containing 'abba' over the input alphabet Σ = {a, b}. Also, find the regular expression for the same language.
DFA: The DFA needs to track the progress of seeing the substring "abba".
q₄: The substring 'abba' has been seen. This is the final (accepting) state.
Alphabet : Σ = {a, b}
Start State: q₀
Transitions (δ):
δ(q₀, a) = q₁
δ(q₀, b) = q₀
δ(q₁, a) = q₁
δ(q₁, b) = q₂
δ(q₂, a) = q₁
δ(q₂, b) = q₃
δ(q₃, a) = q₄
δ(q₃, b) = q₀
δ(q₄, a) = q₄
δ(q₄, b) = q₄
Regular Expression: The language consists of any string that contains "abba". This means any sequence of 'a's and 'b's can precede "abba", and any sequence can
follow it.
DFA: This DFA must accept the union of two languages: L₁ (starts with 'a', ends with 'b') and L₂ (starts with 'b', ends with 'a').
q₃: Started with 'a', last seen was 'b' (Final State).
q₅: Started with 'b', last seen was 'a' (Final State).
Alphabet : Σ = {a, b}
Start State: q₀
Transitions (δ):
δ(q₀, a) = q₂
δ(q₀, b) = q₄
δ(q₂, a) = q₂
δ(q₂, b) = q₃
δ(q₃, a) = q₂
δ(q₃, b) = q₃
δ(q₄, a) = q₅
δ(q₄, b) = q₄
δ(q₅, a) = q₅
δ(q₅, b) = q₄
Regular Expression: The regular expression is the union (represented by '+') of the expressions for the two conditions.
For each state and input symbol, there is exactly one For each state and input symbol, there can be zero, one, or
Transitions
transition to a next state. multiple transitions.
Null (ε) Not allowed. The machine must consume an input symbol Allowed. The machine can change its state without
Moves to change state. consuming an input symbol.
Q4. State the difference between Kleene Closure and Positive Closure.
Kleene Closure (L)*: Denoted by an asterisk * , it represents zero or more concatenations of strings from a language L. The Kleene Closure of any language
always includes the empty string, ε.
Example: If L = {a}, then L* = {ε, a, aa, aaa, ...}.
Positive Closure (L⁺): Denoted by a plus sign + , it represents one or more concatenations of strings from a language L. It is equivalent to the Kleene Closure,
but excluding the empty string ε (unless ε was already part of the original language L).
Example: If L = {a}, then L⁺ = {a, aa, aaa, ...}.
Relationship: L* = L⁺ U {ε} and L⁺ = L L* .
Q5. Define the terms Alphabet, String, and Language with respect to the Theory of Computation. Give examples.
In formal language theory, a Grammar is a formal system used to generate the strings of a language. It is defined as a 4-tuple G = (V, T, P, S):
The language generated by the grammar, L(G), is the set of all strings of terminal symbols that can be derived from the start symbol S by applying the production
rules.
Q7. Prove that some languages can be generated by more than one grammar with suitable examples.
A language can indeed be generated by multiple different grammars. This can be proven by providing an example.
Language: Consider the language L over the alphabet Σ = {a} consisting of all strings with at least one 'a'.
L = {a, aa, aaa, ...}
Grammar 1 (G₁):
S → aA
A → aA | ε
Grammar 2 (G₂):
S → aS | a
Both G₁ and G₂ generate the exact same language, L. For instance, to generate "aaa":
The Chomsky Hierarchy is a classification of formal grammars, languages, and their corresponding automata, developed by Noam Chomsky. It organizes grammars
into four nested levels, or types, based on the restrictiveness of their production rules. Each type defines a class of formal languages.
The hierarchy is a true containment: the set of Regular languages is a proper subset of Context-Free languages, which is a proper subset of Context-Sensitive
languages, which is a proper subset of Recursively Enumerable languages.
(Note: There is a typo in the question. The conditions use 'a' and 'b' while the alphabet is {0, 1}. Assuming the alphabet should be Σ = {a, b}.)
A string must satisfy both conditions. The string must end with "bba", so its form is ...bba . This string must also contain "ab". There are two possibilities for where
the "ab" occurs:
1. The "ab" is completely contained before the final "bba": This means the string is of the form (a+b)*ab(a+b)*bba .
2. The "ab" is formed by the end of the prefix and the start of the "bba" suffix : This occurs if the string ends in ...abba . This form is (a+b)*abba .
The final regular expression is the union of these two cases. However, we can create a more compact expression. A string w is in the language if w = x.bba and w
contains ab . This is true if x contains ab OR x ends with a .
((a+b)*ab(a+b)* + (a+b)*a)bba
Q10. Describe the language over the alphabet Σ = {0, 1} represented by the regular expression: (0 + 1) 0 (0 + 1) 0 (0 + 1)***
This expression matches any string that contains at least two occurrences of the symbol '0'.
Language Description: The set of all binary strings containing at least two 0s.
A Deterministic Finite Automaton (DFA) is a mathematical model of computation defined as a 5-tuple M = (Q, Σ, δ, q₀, F), where:
Q12. Given a machine that produces the output sequence 1110000010 in response to an unknown input sequence: Find the input sequence if the initial state is A and
final state is F.
Note : There is a contradiction in the problem statement. The machine as defined in the table cannot produce the given output sequence.
Analysis:
1. State=A, Output=1: The only transition from A that produces output 1 is A --0/1--> B . So, the first input must be 0 , and the machine moves to state B.
2. State=B, Output=1: Both transitions from B ( --0/1--> D and --1/1--> B ) produce output 1. We must determine which path is correct by looking at the
next required output.
3. Next State=?, Output=1:
If the machine went to state D, it cannot produce an output of 1 (both transitions from D yield output 0). So this path is invalid.
Therefore, the machine must have taken the transition B --1/1--> B . The second input is 1 , and the state remains B.
4. State=B, Output=1: For the third output to be 1, the same logic applies. The input must be 1 , and the state remains B.
5. State=B, Output=0: At this point, the output sequence requires a '0'. However, looking at the row for state B in the table, there is no transition from state B that
produces an output of 0. Both transitions produce an output of 1.
Conclusion : The problem is ill-posed. No input sequence can generate the given output from the specified machine because the machine in state B is incapable of
producing an output of 0.
NFA:
DFA Construction (Subset Construction): We create DFA states that correspond to sets of NFA states.
Resulting DFA Table: The states can be renamed for simplicity (e.g., q₀=S0, q₁=S1, etc.).
DFA
NFA States Input 0 Input 1 Final?
State
→ S₀ {A} S₁ S₄ No
S₁ {B, C} S₂ S₃ No
S₂ {C} S₂ S₂ No
S₄ (trap) ∅ S₄ S₄ No
Q14. Consider the language L = {ab, c} over A = {a, b, c}. Find: (a) L⁰, (b) L¹, and (c) L²
(a) L⁰: The zeroth power of any language is the language containing only the empty string.
L⁰ = {ε}
(b) L¹: The first power of any language is the language itself.
L¹ = {ab, c}
(c) L² : The second power is the concatenation of the language with itself (L . L).
L² = {w₁w₂ | w₁ ∈ L and w₂ ∈ L}
ab · ab = "abab"
ab · c = "abc"
c · ab = "cab"
c · c = "cc"
L² = {abab, abc, cab, cc}
Q15. Let A = {a, b, c} and w = ac. Determine whether w belongs to L(r) where: (a) r = ab c (b) r = (a b U c)
(a) r = ab c: This expression generates strings starting with one 'a', followed by zero or more 'b's, followed by zero or more 'c's.
To generate "ac", we can choose zero 'b's (from b*) and one 'c' (from c*).
a · (ε) · (c) = "ac".
Yes, ac belongs to L(r).
(b) r = (a b U c): This expression generates strings by concatenating blocks, where each block is either 'c' or a string of the form a...ab .
Every block generated by (a*b U c) either is 'c' or ends with 'b'.
The string "ac" has a 'c' that is preceded by an 'a'. An 'a' cannot be the end of a valid block. Therefore, "ac" cannot be constructed from these blocks.
No, ac does not belong to L(r).
Q16. Suppose L is a language over alphabet A accepted by automaton M = (Q, Σ, δ, q₀, F). Find an automaton N which accepts the complement of L.
To construct an automaton N that accepts the complement of L (L'), we start with a DFA M for the language L. (If M is an NFA, it must first be converted to an
equivalent DFA).
1. Ensure M is a complete DFA : For every state and every input symbol, there must be a defined transition. If any are missing, add a non-final "trap" state and
direct all missing transitions there.
2. Construct N: The automaton N for the complement language L' is defined as N = (Q, Σ, δ, q₀, F'), where all components are the same as M, except for the set of
final states.
3. Define F': The set of final states F' in N is the set of all states in M that were not final.
F' = Q - F
The resulting DFA N accepts a string if and only if M rejects it, thus accepting the complement of L.
Q17. Let A = {a,b}. Construct an automaton M which accepts all words over A where the number of b's is divisible by 3.
q₀: Number of 'b's seen is 0 (mod 3). (Start and Final state).
Alphabet : Σ = {a, b}
Start State: q₀
Transitions (δ):
Input 'a' does not change the count, so the state remains the same.
Input 'b' increments the count, moving to the next state in the cycle.
→ q₀ * q₀ q₁
q₁ q₁ q₂
q₂ q₂ q₀
Q18. Given an automaton M (referenced in the figure – not included), determine whether M accepts the following words: (a) w = ababba (b) w = baab (c) w = λ (empty
string)
This question cannot be answered as the figure defining the automaton M is not included in the provided document. To answer it, one would need to trace each string
through the states of the given automaton and check if the final state reached is an accepting state.
Principle of Mathematical Induction: The Principle of Induction is a mathematical proof technique used to prove that a statement P(n) is true for all natural numbers n
(or for all n ≥ k for some integer k). It consists of three steps:
1. Base Case: Prove that the statement P(k) is true for the initial value k (usually k=0 or k=1).
2. Inductive Hypothesis: Assume that the statement P(n) is true for an arbitrary integer n ≥ k.
3. Inductive Step: Prove that if P(n) is true (the hypothesis), then P(n+1) must also be true.
If all three steps are completed, the statement P(n) is proven true for all n ≥ k.
ε-closure: The ε-closure of a state q , denoted ε-closure(q) , in an NFA with epsilon transitions (NFA-ε), is the set of all states that can be reached from state q by
following zero or more ε-transitions.
ε-closure(q₀) = {q₀, q₁, q₂} (You can get from q₀ to q₀, q₁, and q₂ using only ε-moves)
ε-closure(q₂) = {q₂}
Q20. Differentiate an Accepting Machine from a Mealy Machine. Write the tuple representation for both machines.
An "Accepting Machine" typically refers to an automaton like a DFA or NFA whose job is to accept or reject an input string. A Mealy Machine is a type of finite-state
machine with output.
The output is binary (yes/no), determined after the entire The output is generated on each transition. The length of
Output
string is processed, based on whether the final state is in F. the output string is equal to the length of the input string.
Tuple Representation:
Q21. Draw a Non-Deterministic Finite Automata (NFA) to accept strings containing the substring 0101.
Q22. Construct a DFA over the language {0, 1} such that it contains 000 as a substring. *
This DFA is similar to Q1, tracking the progress of seeing "000".
DFA
Input 0 Input 1 Final?
State
→ S₀ S₁ S₀ No
S₁ S₂ S₃ No
DFA:
Minimized DFA: Let's name the new states: A={P0}, B={P1}, C={P2}, D={P3,P4,P5}.
→A A B No
B C B No
C D B No
***
D D Yes
D**
Q25.
(i) Write the Chomsky hierarchy of languages. Prepare a table indicating the automata and grammars for the languages in the Chomsky hierarchy.
Grammar
Type Language Class Production Rule Form (α → β) Automaton
Name
Type- Recursively
Unrestricted No restrictions. Turing Machine
0 Enumerable
Type- Context- |α| ≤ |β|, with S → ε allowed if S does not appear Linear Bounded Automaton
Context-Sensitive
1 Sensitive on RHS. (LBA)
Type-
Context-Free Context-Free A → β, where A is a single non-terminal. Pushdown Automaton (PDA)
2
Type-
Regular Regular A → aB or A → a. Finite Automaton (DFA/NFA)
3
(ii) Write notes on the following: Decidable and undecidable problems, Halting problem of Turing machine
Decidable and Undecidable Problems: A problem is decidable if there exists a Turing Machine (an algorithm) that can solve it for any input in a finite amount of
time and is guaranteed to halt with a "yes" or "no" answer. An example is the problem of determining if a DFA accepts a given string. A problem is undecidable
if no such algorithm exists. For an undecidable problem, we cannot create a Turing Machine that will always halt and give the correct answer for every
possible instance of the problem.
Halting Problem of Turing Machine: The Halting Problem is the most famous example of an undecidable problem. It asks: "Given an arbitrary Turing Machine
M and an input string w, will M eventually halt when run with input w, or will it run forever?" Alan Turing proved in 1936 that it is impossible to construct a
single, universal algorithm (a single Turing Machine) that can solve the halting problem for all possible machine-input pairs. This has profound implications for
computer science, as it proves that there are fundamental limits to what can be computed.
Q26.
2. NFA for 0:
3. NFA for 1:
Simplified NFA: A simpler NFA can be constructed directly. It guesses when the "01" suffix begins.
Null Production (or ε-production): A production rule of the form A → ε , where A is a non-terminal and ε is the empty string. These productions can
complicate parsing and grammar analysis, so they are often removed during grammar normalization.
Example: In the grammar S → aS | ε , the rule S → ε is a null production.
Unit Production: A production rule of the form A → B , where both A and B are single non-terminal symbols. These productions add extra steps to derivations
without generating any terminals, and are also typically removed during grammar normalization (e.g., Chomsky Normal Form).
Example: In the grammar S → A | aa , A → B , B → b , the rules S → A and A → B are unit productions.
(iii) Construct a CFG for the set of strings that contain equal number of a's and b's over Σ = {a,b}.
Let L be the language with nₐ(w) = nₑ(w). The key insight is that any such string w can be seen as either:
1. Empty (ε).
2. Starting with 'a', followed by a string in L, followed by 'b', followed by another string in L ( aSbS ).
3. Starting with 'b', followed by a string in L, followed by 'a', followed by another string in L ( bSaS ).
S → aSbS | bSaS | ε
Example Derivation for "abba": S ⇒ aSbS ⇒ a(bSaS)bS ⇒ a(b(ε)a(ε))b(ε) ⇒ ababS ⇒ abab(ε) ⇒ abab. (Correction: this grammar generates abab, not abba. Let's fix it) .
Let's rethink. A string with an equal number of a's and b's is either:
1. Empty.
2. Starts with 'a' and ends with 'b', with an equal number of a's and b's inside. (e.g., abba )
3. Starts with 'b' and ends with 'a', with an equal number of a's and b's inside. (e.g., baab )
4. Is the concatenation of two smaller strings with equal numbers of a's and b's. (e.g., ab + ba = abba )
S → aSb | bSa | SS | ε
Example Derivation for "abba": S ⇒ aSb ⇒ a(SS)b ⇒ a(aSb)Sb ⇒ a(a(ε)b)Sb ⇒ a(ab)Sb ⇒ a(ab)(bSa) ⇒ a(ab)(b(ε)a) ⇒ abba
Q27.
(i) Prove that there exists an NFA with ε-transitions that accepts the regular expression r. This is a proof by structural induction on the definition of a regular
expression.
Base Cases:
1. r = ε: An NFA with a start state that is also the final state accepts ε.
2. r = a (for some a in Σ): An NFA with a start state, a transition on a to a final state, accepts a .
3. r = ∅: An NFA with a start state and no final states accepts ∅.
Inductive Hypothesis: Assume for any two regular expressions r₁ and r₂ , there exist NFA-ε N₁ and N₂ that accept L(r₁) and L(r₂), respectively.
Inductive Step: We must show that we can construct NFA-ε for r₁ + r₂ , r₁r₂ , and r₁* . This is done using Thompson's Construction :
1. Union (r₁ + r₂): Create a new start state. Add ε-transitions from the new start state to the start states of N₁ and N₂ . The final states of the new NFA
are the union of final states of N₁ and N₂ .
2. Concatenation (r₁r₂): The start state of the new NFA is the start state of N₁ . Add ε-transitions from all final states of N₁ to the start state of N₂ . The
final states of the new NFA are the final states of N₂ .
3. Kleene Star (r₁):* Create a new start state (which is also a final state to accept ε). Add an ε-transition from this new start state to the start state of N₁ .
Add ε-transitions from all final states of N₁ back to the start state of N₁ . Add an ε-transition from the old final states of N₁ to the new final state.
Since we can construct an NFA-ε for the base cases and for each of the inductive operations, an NFA-ε exists for any regular expression.
(ii) Prove any two closure properties of regular languages. Regular languages are closed under Union and Concatenation.
1. Proof of Closure under Union: Let L₁ and L₂ be two regular languages. By definition, there exist DFAs M₁ = (Q₁, Σ, δ₁, q₁, F₁) and M₂ = (Q₂, Σ, δ₂, q₂, F₂) such that
L(M₁) = L₁ and L(M₂) = L₂. We can construct a new DFA M = (Q, Σ, δ, q₀, F) that accepts L₁ U L₂ using a product construction:
2. Proof of Closure under Concatenation: Let L₁ and L₂ be two regular languages, accepted by NFA-ε N₁ and N₂ . We can construct a new NFA-ε N that accepts
L₁L₂ using Thompson's construction for concatenation (as described in Q27(i)):
The regular expression simplifies to (a+x)⁺(y+z) *. This represents one or more 'a's or 'x's, followed by zero or more 'y's or 'z's.
DFA Construction:
→ q₀ q₁ q₁ q_trap q_trap No
1. Start at q₀.
2. Input 'x': δ(q₀, x) → q₁
3. Input 'y': δ(q₁, y) → q₂
4. Input 'y': δ(q₂, y) → q₂
5. Input 'z': δ(q₂, z) → q₂ The final state is q₂, which is an accepting state. Therefore, the string xyyz is accepted.
Q28.
(i) What is the purpose of normalization? Construct the CNF for the following grammar and explain the steps: S → aAa | bBb | ε A → C | a B → C | b C → CDE | ε D → A |
B | ab
Purpose of Normalization: Normalization transforms a grammar into a standard form (like Chomsky Normal Form or Greibach Normal Form) without changing
the language it generates. This is useful because:
It simplifies algorithms that operate on grammars (e.g., parsing algorithms like CYK, which requires CNF).
It provides a standard basis for proving properties about context-free languages.
It removes ambiguity and redundancy (like ε-productions, unit productions, useless symbols).
CNF Construction: A grammar is in CNF if every production is of the form A → BC or A → a . The given grammar is not a good candidate as C → CDE is left-
recursive and D → A | B leads to circular dependencies. The process would be extremely complex and may not terminate properly without first simplifying
the grammar heavily. Let's assume a simplified version of the grammar after removing some of these issues, as a direct conversion is not straightforward.
Let's perform the steps on the provided grammar as best as possible. Step 1: Eliminate ε-productions.
Nullable non-terminals: C (from C→ε), S (from S→ε), A (from A→C), B (from B→C), D (from D→A or D→B). All are nullable. This is a problem.
Let's assume a simpler grammar for demonstration: S → aAa, A → bB | ε, B → c
A is nullable. We add a production for S where A is replaced by ε: S → aa .
New Grammar: S → aAa | aa, A → bB, B → c.
The language is L = {ab², aabbbb, aaabbbbbb, ...}. For every 'a' at the beginning, we must generate two 'b's at the end.
S → aSb² | ab²
Derivation for n=3 (aaabbbbbb): S ⇒ aSb² ⇒ a(aSb²)b² ⇒ aaSb⁴ ⇒ aa(ab²)b⁴ ⇒ aaab²b⁴ ⇒ aaabbbbbb.
Q29.
(i) What is ambiguous grammar? A grammar is ambiguous if there is at least one string in the language that can be generated by more than one distinct leftmost
derivation or, equivalently, has more than one parse tree . Ambiguity can be problematic for compilers, as it means a program could be interpreted in multiple ways.
(ii) What are the different ways through which a language is accepted by a PDA? Define them. A Pushdown Automaton (PDA) can accept a language in two equivalent
ways:
1. Acceptance by Final State: A PDA accepts an input string w if, after reading the entire string, the PDA is in one of its designated final states, regardless of
what is on the stack.
2. Acceptance by Empty Stack: A PDA accepts an input string w if, after reading the entire string, the PDA has emptied its stack , regardless of which state it is in.
The two models are equivalent in power for NFAs (a PDA that accepts by final state can be converted to one that accepts by empty stack, and vice-versa).
(iii) Define a parse tree with an example. A parse tree (or derivation tree) is an ordered, rooted tree that graphically represents the syntactic structure of a string
according to a formal grammar.
Example:
Grammar: S → aSb | ε
String: "aabb"
Parse Tree:
S
/ | \
a S b /|
aSb|ε
(iv) Is the language of Deterministic PDA and Non-Deterministic PDA same? Justify. No, the languages are not the same.
Non-deterministic PDAs (NPDAs) accept the full class of Context-Free Languages (CFLs).
Deterministic PDAs (DPDAs) accept a proper subset of CFLs, known as the Deterministic Context-Free Languages (DCFLs) .
Justification: There are context-free languages that are inherently non-deterministic, meaning no DPDA can be constructed to accept them. A classic example is the
language of palindromes:
L = {wwᴿ | w ∈ {a,b}*} (e.g., "abba", "aabbaa") An NPDA can accept this by non-deterministically guessing where the middle of the string is. It pushes the first
half onto the stack and then pops symbols to match the second half. A DPDA cannot do this because it has no way to deterministically find the middle of the
string. It doesn't know when to switch from pushing to popping. Therefore, L is a CFL but not a DCFL, proving that NPDAs are more powerful than DPDAs.
Q30.
Yes, the grammar is highly ambiguous. Let's consider the string (a)a(a) , assuming we can derive a from S (which is not shown, but we assume S can generate
terminal strings). Let's just use S itself for simplicity. Consider the string S(S)S .
Derivation 1 (using S → SS): S ⇒ SS ⇒ S (S)S (The first S generates S , the second S generates S(S)S )
Derivation 2 (using S → S(S)S): S ⇒ S(S)S (A direct derivation)
Since S(S)S has at least two different leftmost derivations (or parse trees), the grammar is ambiguous. The rule S → SS combined with S → S(S)S is a classic
source of ambiguity.
(ii) Convert the following grammar into an equivalent one with no unit productions and no useless symbols: S → aaB | abA | aaS A → aA B → aB | b C → ad
Generating Symbols:
B → b is generating.
S → aaB is generating because B is.
A is not generating. A → aA is a recursive loop that never produces a terminal-only string.
C is generating ( C → ad ).
Reachable Symbols:
Useless Symbols:
S → aaB | aaS
B → aB | b
Step 3: Remove Unit Productions. There are no unit productions (form X → Y ) in the remaining grammar.
S → aaB | aaS
B → aB | b