Sub topic 18
GATE problem patterns and strategies
This chapter assumes you know the grammar, PDA, and pumping-lemma material from the earlier chapters: derivations, parse trees, normal forms, deterministic versus nondeterministic PDAs, and the closure and decision tables. Here you turn that knowledge into fast, reliable answers to the question types GATE keeps repeating.
The recurring question types
Most GATE questions on this topic fall into a small number of patterns. Recognising the pattern first saves time.
| Pattern | Typical wording | Fastest method |
|---|---|---|
| Classify a language | "Which of these is context-free / regular / deterministic CFL?" | Ask what must be remembered, then use the flow below |
| Closure | "Which operation preserves the class?" | Recall the table, then test with a counterexample |
| Decidability | "Which problem is undecidable for CFGs?" | Membership, emptiness, finiteness are decidable. Equality, universality, ambiguity, and intersection-emptiness are not. |
| Derivation counting | "How many steps derive a string of length n in CNF?" | Use the formula, then check on a tiny string |
| Ambiguity | "Is this grammar ambiguous?" | Find one string with two parse trees |
| Machine design | "Minimum stack symbols / which PDA type accepts L?" | Trace the stack on a short input |
Pattern 1: classify the language
Decide what the machine must remember, and whether it can predict where the string switches modes.
Languages worth memorising:
- Regular: ab, strings with an even number of a's, any finite set. Every context-free language over a one-letter alphabet is also regular.
- DCFL: a^n b^n, w c w^R, balanced parentheses.
- CFL but not DCFL: w w^R (the machine must guess the middle), and { a^i b^j c^k : i = j or j = k }.
- Not CFL: a^n b^n c^n, ww, a^(n^2), and a^p for prime p. Each needs two independent comparisons, a crossing match, or a non-linear count.
Complements are a common trap. The complement of a^n b^n c^n is context-free, because a string can fail in several ways and a PDA can guess which one. The complement of ww is also context-free, but the language ww itself is not.
Pattern 2: closure and decision properties
| Operation | CFL | DCFL |
|---|---|---|
| Union | closed | not closed |
| Concatenation, star | closed | not closed |
| Reversal | closed | not closed |
| Intersection | not closed | not closed |
| Intersection with a regular language | closed | closed |
| Complement | not closed | closed |
| Homomorphism | closed | not closed |
| Inverse homomorphism | closed | closed |
Tests that settle most options:
- To refute closure under intersection, use { a^n b^n c^m } ∩ { a^m b^n c^n } = { a^n b^n c^n }.
- To refute closure under complement, note that if CFLs were closed under complement, then L1 ∩ L2 = complement of (complement L1 ∪ complement L2) would be a CFL, which contradicts the previous test.
- Difference L − R is context-free when R is regular. Difference of two CFLs need not be.
Decidable for CFGs: membership (CYK), emptiness (test whether S is productive), finiteness. Undecidable for CFGs: whether L = Σ*, whether two grammars are equivalent, whether a grammar is ambiguous, and whether L(G1) ∩ L(G2) is empty.
Pattern 3: counting steps and nodes
For a grammar in Chomsky normal form and a string of length n ≥ 1:
- A derivation takes exactly 2n − 1 steps. There are n − 1 binary rules and n terminal rules.
- The parse tree has 2n − 1 internal nodes, counting the variable nodes.
For a grammar in Greibach normal form, every step produces one terminal, so a derivation takes exactly n steps.
Check the formula on aabb with the CNF grammar S → AB | AX, X → SB, A → a, B → b. The derivation is S ⇒ AX ⇒ aX ⇒ aSB ⇒ aABB ⇒ aaBB ⇒ aabB ⇒ aabb. That is 7 steps, and 2·4 − 1 = 7.
Pattern 4: ambiguity
A grammar is ambiguous if some string has two distinct parse trees (equivalently, two leftmost derivations). You only need one such string, so try the shortest string that uses a symbol on both sides of a recursive rule. For E → E + E | E E | id, the string id + id id is enough.
You can also count parse trees by program. The function below counts the trees of an expression over spans of tokens.
from functools import lru_cache
def count_trees(tokens):
"""Count parse trees of E -> E+E | E*E | id for a list of tokens."""
tokens = tuple(tokens)
@lru_cache(None)
def c(i, j):
total = 1 if (j - i == 1 and tokens[i] == "id") else 0
for k in range(i + 1, j - 1):
if tokens[k] in ("+", "*"):
total += c(i, k) * c(k + 1, j)
return total
return c(0, len(tokens))
print(count_trees("id".split())) # 1
print(count_trees("id + id * id".split())) # 2
print(count_trees("id + id + id + id".split())) # 5The counts 1, 2, 5 are Catalan numbers, which is what an operator with no precedence or associativity produces. A count above 1 proves ambiguity. A count of 1 on one string proves nothing.
Pattern 5: the pumping argument
Write the proof as a fixed template and fill in the blanks:
- Assume L is context-free with pumping length p.
- Choose z in L with |z| ≥ p, such as a^p b^p c^p.
- Any split z = uvwxy with |vwx| ≤ p and |vx| ≥ 1 must be handled.
- Because |vwx| ≤ p, the window cannot touch both the first and last block. Case on which blocks it touches.
- Pump with i = 0 or i = 2 to break the count in every case, which contradicts the lemma.
In a multiple-choice question you rarely need the whole proof. You only need to spot whether the language compares three quantities, or one quantity against a non-linear function.
Time strategy
- Classification and closure questions take under a minute if you have the tables memorised.
- Eliminate options with one counterexample each instead of proving the right one.
- For machine-design questions, trace a string of length 2 or 3 to test a claimed PDA.
- Treat "always" and "never" in options with suspicion. Test them on the empty string and on a one-letter alphabet.
Exercise
A CNF grammar generates a string of length 9. How many derivation steps does it take, and how many if the grammar were in GNF?
Show answer
CNF takes 2n − 1 = 17 steps. GNF takes n = 9 steps.
Exercise
Is { a^i b^j c^k : i = j or j = k } a deterministic CFL? Give the closure argument.
Show answer
No. It is context-free, but its complement intersected with the regular language abc would give { a^i b^j c^k : i ≠ j and j ≠ k }. Working through the standard argument, if the language were a DCFL, the complement would be a DCFL. Intersecting that complement with abc and combining the cases leads to a^n b^n c^n being context-free, which is false. So the language is a CFL that is not deterministic.
Exercise
Which of these is undecidable for context-free grammars: membership, emptiness, finiteness, or whether L(G) = Σ*?
Show answer
Whether L(G) = Σ*. Membership is decidable by CYK, emptiness by checking whether S is productive, and finiteness by looking for cycles in the simplified grammar.