Recall warm-up
- Are DCFLs closed under complement?
Stuck? Peek at 3.4.1 Closure properties of CFLs and DCFLs
- CFL closed: ∪, concat, *, reversal, h, h⁻¹, substitution, ∩R, −R. Not: ∩, complement, difference.
- DCFL closed: complement, ∩R, ∪R, −R, h⁻¹. Not: ∪, ∩, concat, *, reversal, h.
- Counterexample: aⁿbⁿcᵐ ∩ aᵐbⁿcⁿ = aⁿbⁿcⁿ. Non-CFL proof: L ∩ R non-CFL ⇒ L non-CFL.
- Which grammar symbols are “generating”?
Stuck? Peek at 3.2.1 Removing useless symbols, ε- and unit productions
- Useful = generating + reachable. Remove non-generating first, then unreachable.
- Nullable: A ⇒* ε. Add all versions dropping nullable occurrences; delete ε-rules; add S₀ → S | ε if needed.
- Unit pairs by reachability over unit rules; copy non-unit bodies; delete unit rules.
- Order: ε → unit → useless.
- How do you test emptiness for a DFA?
Stuck? Peek at 2.4.2 Algorithms for regular languages
- Membership: run. Emptiness: reachable F? Finiteness: trim + cycle (or lengths n..2n−1). Universality: complement + emptiness. Equivalence: minimize & compare, or symmetric difference empty. Containment: L₁ ∩ L̅₂ = ∅.
- All these have always-terminating algorithms for regular languages.
The Hook
A compiler must answer “is this program grammatical?” in time proportional to a small power of its length, for any grammar. And tools must answer “does this grammar generate anything?” or “infinitely many strings?”. These three questions have algorithms for every CFG. Others — like “do two grammars generate the same language?” — have none, a fact you will be able to prove in Phase 5.
Predict First
Grammar: S → AB, A → a, B → BB. Does it generate any string at all?
Core Concept
Prediction check
No. B only rewrites to BB and never finishes, so B is non-generating, so S is non-generating ⇒ L(G) = ∅.
Emptiness
Compute the generating symbols (3.2.1). L(G) = ∅ iff S is not generating. Always terminates.
Finiteness
Clean the grammar (no useless symbols, no ε- or unit rules; CNF works). Draw a graph with an edge A → B when B appears in a body of A. L(G) is infinite iff this graph has a cycle (a variable can derive a string containing itself).
Membership: CYK
For a CNF grammar and w of length n: fill a triangular table where cell (i, ℓ) = set of variables deriving the substring starting at i of length ℓ. Length 1 from terminal rules; longer substrings by trying every split into two parts and every rule A → BC. w ∈ L iff S is in the top cell. Time O(n³ · |G|).
What has no algorithm (for general CFGs)
For arbitrary CFGs no algorithm exists for: equivalence L(G₁) = L(G₂), universality L(G) = Σ*, ambiguity of G, whether L(G) is regular, whether L(G₁) ∩ L(G₂) = ∅. (Phase 5 builds the tools to prove such statements.) For DCFLs, universality does have an algorithm (complement is a DCFL; test emptiness).
Putting it together
- Emptiness, finiteness and membership have algorithms for every CFG.
- CYK fills a triangle bottom-up in O(n³).
- Equivalence, universality and ambiguity have no general algorithm.
Visual
CYK table (cell = variables deriving that substring) w = b a a b a
len 5 │ {S,A,C}
len 4 │ ∅ {S,A,C}
len 3 │ ∅ {B} {B}
len 2 │ {S,A} {B} {S,C} {S,A}
len 1 │ {B} {A,C} {A,C} {B} {A,C}
b a a b a S ∈ top cell → ACCEPTWorked Examples
S → AB, A → a, B → b. Is L(G) finite?
Show the full solution
Graph S → A, S → B, no cycle ⇒ finite: {ab}.
CYK on w of length 6 with a CNF grammar: how many cells?
Show the full solution
n(n+1)/2 = 21 cells.
Which have always-terminating algorithms for arbitrary CFGs G, G₁, G₂? (i) w ∈ L(G) (ii) L(G) = ∅ (iii) L(G) finite (iv) L(G₁) = L(G₂)
Show the full solution
(i), (ii), (iii) yes. (iv) no algorithm exists. Answer: (i), (ii), (iii).
GATE Traps
- CYK needs the grammar in CNF.
- Equivalence and universality of CFGs have no algorithm; for regular languages they do.
- Finiteness: check for a cycle in the cleaned variable graph — useless symbols must be removed first.
Memory Hook
CYK is a pyramid: build each block from two blocks underneath it.
Connect the Dots
We can test membership, but how do we prove a language is not context-free? A pumping lemma again — this time with two pumps that move together.
Self-Test
- NAT
CYK table for n = 8 has how many cells?
Write your answer before opening the key.
- MSQ
For an arbitrary CFG, which questions have an always-terminating algorithm?
- emptiness
- finiteness
- membership
- equivalence with another CFG
- MCQ
CYK runs in time (n = |w|):
- O(n)
- O(n²)
- O(n³)
- O(2ⁿ)
- MCQ
L(G) for a cleaned CNF grammar is infinite iff:
- G has more than one rule
- the variable graph has a cycle
- S has two rules
- some rule is A → a
Rule Card
- Emptiness: is S generating? Finiteness: cycle in the cleaned variable graph. Membership: CYK O(n³|G|), CNF required.
- No algorithm for general CFGs: equivalence, universality, ambiguity, regularity, emptiness of intersection.
Concept Ledger
- CFL emptiness test
- CFL finiteness test (variable graph)
- CYK algorithm
- questions with no algorithm (CFG)
Answer Vault
Reveal answers when you’re ready
Warm-up answers
- Yes.
- Those that derive some terminal string.
- Is an accepting state reachable from the start?
Self-test answers
- 36 — 8·9/2.
- ABC — Equivalence has none.
- D: No algorithm exists.
- C — n² cells × n splits.
- A: Too fast.
- B: Misses the splits.
- D: Too slow.
- B — A variable can reproduce itself.
- A: Irrelevant.
- C: Irrelevant.
- D: Irrelevant.
Flashcards
- CYK complexity?
Reveal answer
O(n³ · |G|), grammar in CNF. - How to test L(G) = ∅?
Reveal answer
Check whether S is generating. - Is CFG equivalence algorithmically solvable?
Reveal answer
No.