Socks
Socks
o Σ: Input alphabet
o δ: Transition function
4. Write down the regular expression of all strings of 0's and 1's such that
the start symbol and end symbol are always the same/different.
Answer:
o Same: 0(0+1)*0 + 1(0+1)*1
1
6. Draw the transition table of the machine depicted in Figure 1.
Answer:
State Input: 0 Input: 1
q0 q1 q0
q1 q2 q1
q2 q2 q2
7.
What change do you suggest in the machine, shown in Figure 1, so that
it’ll accept only those strings, which end with 11?
Answer: Add a transition such that q1 transitions to q2 on input 1, and
make q2 the final state.
9. Consider a grammar G with the production rules S -> Ab; A -> aAb; A -> λ.
Describe the language generated by this grammar.
Answer: The language generated is L = { a^n b^n b | n >= 0 }.
10. Consider a grammar G with the production rules S -> aSb; S -> λ.
Describe the language generated by this grammar.
Answer: The language generated is L = { a^n b^n | n >= 0 }.
2
12. State three differences between a DFA and an NFA.
Answer:
o DFA has a single transition for each input from a state, while NFA can
14. The formal definition of a Push Down Automata uses a 7-tuple (Q,
Σ, Γ, δ, q0, Z0, F), where δ: Q × (Σ ∪ {ε}) × Γ -> finite subsets of Q × Γ.
Explain.*
Answer: The transition function δ takes the current state, input symbol (or
epsilon), and top stack symbol, and maps them to a set of next states with
updated stack configurations.
15. Give an example each of (i) A ‘Null Production’ and (ii) A ‘Unit
Production’.
Answer:
o Null Production: A -> λ
3
17. State the rules allowed when a grammar is in Chomsky Normal
Form.
Answer:
o Productions are of the form A -> BC or A -> a, where A, B, C are non-
19. Which closure properties are valid for CFL, but not for RL? Which
closure properties are valid for RL, but not for CFL?
Answer:
o CFL is closed under union, concatenation, and Kleene star but not
union.
20. List the types of automatons and its acceptable languages and
grammars.
Answer:
o Finite Automaton (FA): Accepts Regular Languages (RL), generated by
4
21. Define Left and Right Linear Grammar.
Answer:
• Left Linear Grammar: The productions are of the form A -> Ba or A -> a,
• Σ: Input alphabet
• Γ: Stack alphabet
• Σ: Input alphabet
• B: Blank symbol
5
24. Turing recognizable languages are closed under union and
complementation – Explain.
Answer:
• Union: If two languages L1 and L2 are Turing recognizable, a Turing
a subset of CFLs.
• Undecidable vs Decidable Problems:
a solution.
o Undecidable problems cannot have an algorithm that halts for all
than RG.
o RG (Regular Grammar) generates RLs and is less expressive but easier
to parse.
• CFL vs RL vs CSL:
PDAs.
o RL (Regular Languages) are generated by RGs and accepted by FAs.
6
4. Consider a grammar with the rules: S → aSb | SS | λ. Draw the
leftmost derivation tree for the string aababbabab.
o Answer:
Leftmost Derivation:
o S → aSb
o → aaSbb
o → aaSbabb
o → aaSSbabb
o → aaSbSbabb
o → aaabSbSbabb
o → aababbabab
o Derivation Tree:
o S
o / \
o a b
o S
o / \
o a S
o / \
o S b
o / \
o S b
o / \
o a b
o
o 5. Consider a grammar with the rules: S → AB; A → aA | λ; B → bbB
| λ. Draw the leftmost derivation tree for the string aaabbbbbbb.
o Answer:
Leftmost Derivation:
o S → AB
o → ABBB
o → aABBB
o → aaABBB
o → aaaABBB
o → aaaBBB
7
o → aaaBbbBB
o → aaaBbbbBB
o → aaaBbbbbB
o → aaaBbbbbbb
o → aaabbbbbbb
o Derivation Tree:
o S
o /\
o A B
o / \
o aA B
o / \
o aA B
o / \
o aA B
o / \
o λ B
o / \
o bb B
o / \
o bb B
o / \
o bb λ
o
o 6. Consider a grammar with the productions: G: S → aSbb | λ; S1 →
bSa. Find the language generated by this grammar.
o Answer:
o The production S → aSbb | λ generates strings of the form an bbn,
where n ≥ 0.
o The production S1 → bSa introduces additional b and a at the
beginning and end, respectively.
o Combining both, the language generated by the grammar is:
L = { b an bbn a | n ≥ 0 }
o
8
o 7. The transition table of a DFA is shown in Figure 2. Compute step
by step, the acceptance (or rejection) of the string 01001011.
o Answer:
o Transition Table:
o δ(q, 0) → q1
o δ(q, 1) → q2
o Step-by-Step:
o Start state: q0
o Input: 0 → δ(q0, 0) = q1
o Input: 1 → δ(q1, 1) = q2
o Input: 0 → δ(q2, 0) = q2
o Input: 0 → δ(q2, 0) = q2
o Input: 1 → δ(q2, 1) = q2
o Input: 0 → δ(q2, 0) = q2
o Input: 1 → δ(q2, 1) = q2
o Input: 1 → δ(q2, 1) = q2
o Final State: q2 (Accepting State)
Result: String 01001011 is accepted.
o
o 8. Design a DFA that accepts all strings of 0’s and 1’s, that either
begins or ends (or both) with 01.
o Answer:
o DFA Design:
States:
o q0: Start state
o q1: Transition after seeing 0
o q2: Accepting state after seeing 01
o q3: Other transitions
o Transitions:
o δ(q0, 0) → q1
o δ(q1, 1) → q2
o δ(q2, 0) → q2
o δ(q2, 1) → q2
o
9
o 9. Design a DFA that accepts all strings of 0’s and 1’s, that either
begins or ends (or both) with 00.
o Answer:
Similar to Question 8 but modified for "00."
Transitions:
o δ(q0, 0) → q1
o δ(q1, 0) → q2
o δ(q2, 0) → q2
o δ(q2, 1) → q2
o
o 10. Create an NFA for accepting decimal numbers characterized by:
o Answer:
o The NFA will include:
o An optional + or - at the start.
o A sequence of digits.
o An optional decimal point followed by digits.
o States and Transitions:
o q0 → (+, -) → q1
o q1 → (digit) → q2
o q2 → (digit) → q2
o q2 → (.) → q3
o q3 → (digit) → q4
o q4 → (digit) → q4
o
o 11. Convert the NFA given in Figure 3, to the equivalent DFA using
subset construction method.
o Answer:
Subset Construction Steps:
o Start with the ε-closure of the start state.
o Compute transitions for subsets of states for each input.
o Mark states containing final NFA states as final DFA states.
o Let me know if you'd like this in tabular form!
o
10
14. Consider the Grammar X → X+X | X*X | X | a. Draw two different leftmost
derivation trees of the string a*a+a
Answer:
Leftmost Derivation 1:
X → X+X
→ X*X + X
→ a*X + X
→ a*a + X
→ a*a + a
Leftmost Derivation Tree 1:
X
/\
X X
/\ \
X X a
| |
a a
Leftmost Derivation 2:
X → X*X
→X+X*X
→a+X*X
→a+a*X
→a+a*a
Leftmost Derivation Tree 2:
X
/\
X X
| /\
a X X
| |
a a
11
15. Design a FA from the given regular expression 10 + (0 + 11)0*1.
Answer:
• Simplify the Regular Expression: The expression accepts strings starting
DFA construction is best drawn visually, but I can describe transitions in text.
12
18. Eliminate left recursion from the grammar: A → Abd | Aa | λ; B → Be | b.
Answer:
• Eliminate left recursion from A:
• A → Abd | Aa | λ
• Rewrite as:
• A → AbdA'
• A' → dA' | λ
• B → Be | b
• Rewrite as:
• B → bB'
• B' → eB' | λ
• S → aS | a
• Rewrite as:
• S → aS'
• S' → S' | λ
• X → aX | a | λ
• Rewrite as:
• X → aX'
• X' → X' | λ
13
20. Prove that L = {anbn : n ≥ 0} is not regular using the pumping lemma.
Answer:
Pumping Lemma: If L is regular, there exists a pumping length p such that any
string s ∈ L with length ≥ p can be split into xyz, satisfying:
1. |xy| ≤ p
2. |y| > 0
3. For all i ≥ 0, xy^iz ∈ L.
• Let s = apbp (choose n = p).
• Split s as xyz where x = a^i, y = a^j (with j > 0), and z = a^(p-i-j)b^p.
which is not in L.
• Conclusion: L is not regular.
21. Design a PDA to accept strings of the form 0n1n, n ≥ 0. Show the steps for
the string 011.
Answer:
PDA Construction:
• Push each 0 onto the stack.
op).
• Transition rules will directly correspond to the arcs in the diagram.
14
25. (a) Describe the Chomsky hierarchy of languages.
The Chomsky hierarchy includes:
1. Type 0: Recursively Enumerable Languages
o Generated by unrestricted grammars.
15
26. Context-Sensitive Grammar
(a) Definition
A context-sensitive grammar has rules of the form αAβ→αγβαAβ → αγβ, where
∣α∣≤∣γ∣|α| ≤ |γ|.
(b) Grammar for anbncna^n b^n c^n:
Rules:
1. S→aSBCS → aSBC
2. BC→CBBC → CB
3. CB→εCB → ε.
(c) Derivation for a2b2c2a^2b^2c^2:
1. S→aSBC→aaSBCBC→aaBCBCBC→aabCBCBC→aabbCCBC→aabbcBC→aabb
ccS → aSBC → aaSBCBC → aaBCBCBC → aabCBCBC → aabbCCBC →
aabbcBC → aabbcc.
input.
• DPDA: Deterministic PDA has only one possible transition per input.
17
33. Types of Turing Machines
1. Deterministic Turing Machine (DTM)
2. Non-Deterministic Turing Machine (NDTM)
3. Multi-Tape Turing Machine
4. Oracle Turing Machine
5. Universal Turing Machine
determinism.
• Complementation: Closure under complementation is not guaranteed for
recognizable languages.
18
1. Definition of Turing Machine:
A Turing machine is a mathematical model of computation consisting of
the following tuples:
M=(Q,Σ,Γ,δ,q0,qaccept,qreject)M = (Q, \Sigma, \Gamma, \delta, q_0,
q_{accept}, q_{reject})
o QQ: Finite set of states
2. CFG Constructions:
a. L={anbn:n≥0}L = \{a^nb^n: n \geq 0\}:
S→aSb ∣ ϵS \rightarrow aSb \,|\, \epsilon
b. L={anb2n:n≥0}L = \{a^nb^{2n}: n \geq 0\}:
S→aSbb ∣ ϵS \rightarrow aSbb \,|\, \epsilon
c. L={anbmcm+n:m≥0,n≥0}L = \{a^nb^mc^{m+n}: m \geq 0, n \geq 0\}:
S→AB,A→aAb ∣ ϵ,B→cB ∣ ϵS \rightarrow AB, A \rightarrow aAb \,|\,
\epsilon, B \rightarrow cB \,|\, \epsilon
d. L={ambm+ncn:m≥0,n≥0}L = \{a^mb^{m+n}c^n: m \geq 0, n \geq 0\}:
S→aSb ∣ B,B→bBc ∣ ϵS \rightarrow aSb \,|\, B, B \rightarrow bBc \,|\,
\epsilon
e. L={am+nbmcn:m≥0,n≥0}L = \{a^{m+n}b^mc^n: m \geq 0, n \geq 0\}:
S→aSb ∣ aSc ∣ ϵS \rightarrow aSb \,|\, aSc \,|\, \epsilon
f. L={wcwR:w∈{a,b}∗}L = \{wcw^R: w \in \{a, b\}^*\}:
S→aSa ∣ bSb ∣ cS \rightarrow aSa \,|\, bSb \,|\, c
g. L={wwR:w∈{a,b}∗}L = \{ww^R: w \in \{a, b\}^*\}:
S→aSa ∣ bSb ∣ ϵS \rightarrow aSa \,|\, bSb \,|\, \epsilon
3. Closure Properties of CFLs:
o CFLs are closed under union, concatenation, and Kleene closure.
4. Ambiguous Grammar:
A grammar is ambiguous if there exists a string with two or more distinct
parse trees.
Example: S→SS ∣ aS \rightarrow SS \,|\, a.
Inherently CFL: A language for which no unambiguous CFG exists. Example:
L={anbmcm:n,m≥0}L = \{a^nb^mc^m: n, m \geq 0\}.
5. Derivations:
o Leftmost Derivation: Replacing the leftmost variable in each step.
20
Example Grammar:
E→E+E ∣ E−E ∣ a ∣ bE \rightarrow E + E \,|\, E - E \,|\, a \,|\, b
For input a−b+aa - b + a, draw both leftmost and rightmost derivation trees.
6. CNF and GNF:
o CNF: All productions are of the form A→BCA \rightarrow BC or A→aA
\rightarrow a.
o GNF: All productions are of the form A→aαA \rightarrow a\alpha,
Their expressive powers are not the same. NPDA can recognize all
CFLs, but DPDA cannot.
9. PDA Constructions:
a. L={anbn:n≥0}L = \{a^nb^n: n \geq 0\}: Push aa onto the stack, pop for
bb.
b. L={anb2n:n≥0}L = \{a^nb^{2n}: n \geq 0\}: Push aa, pop for every two
bb.
c. L={anbmcm+n:m,n≥0}L = \{a^nb^mc^{m+n}: m, n \geq 0\}: Use stack to
count aa and bb, ensuring counts align with cc.
d. L={ambm+ncn:m,n≥0}L = \{a^mb^{m+n}c^n: m, n \geq 0\}: Track mm
and nn transitions using the stack.
e. L={am+nbmcn:m,n≥0}L = \{a^{m+n}b^mc^n: m, n \geq 0\}: Similar stack-
based logic for sequential symbols.
f. L={wcwR:w∈{a,b}∗}L = \{wcw^R: w \in \{a, b\}^*\}: Push ww onto the
stack, match wRw^R after cc.
g. L={wwR:w∈{a,b}∗}L = \{ww^R: w \in \{a, b\}^*\}: Push ww and match
wRw^R directly.
21