1. Gyanastra
  2. Theory of Computation
  3. Properties of CFLs
  4. Classifying Languages as Regular, Deterministic Context-Free, or Context-Free
Free lesson, no account needed 9 min read 14 practice questions after it
Asked in past papers37 times
  • GATE CSE202620252022202020182017201620152014201320112010200920082007200620052004200320022001199819971996199519941987
  • ISRO202020182017
  • UGC NET2023202020172016201520142013

Theory of Computation · Properties of CFLs

Classifying Languages as Regular, Deterministic Context-Free, or Context-Free

Overview

Context-free languages split into nested subclasses according to the power of the machine that accepts them: finite automata accept regular languages, deterministic pushdown automata accept deterministic context-free languages, and any pushdown automaton accepts context-free languages.
Classifying a language into the correct subclass shows exactly how much memory and non-determinism its recognition requires.

Key concepts

  • Regular language — accepted by a finite automaton (no stack).
  • Deterministic context-free language (DCFL) — accepted by a deterministic pushdown automaton (DPDA).
  • Context-free language (CFL) — accepted by any pushdown automaton, deterministic or non-deterministic.
  • Inclusion chain — every regular language is a DCFL, and every DCFL is a CFL: Regular ⊂ DCFL ⊂ CFL.
  • Contrapositive — if a language is not context-free, then it is not deterministic context-free and not regular either.
  • Non-regularity test — a language fails to be regular when acceptance needs unbounded counting and unbounded comparison: the machine must save an arbitrarily long block of symbols and later compare it against another block.
    A finite automaton has no memory, so it cannot do this. If no such comparison is needed, the language is regular.
  • Determinism test — if at every step the current input symbol alone decides push or pop (no guessing), the PDA is deterministic and the language is a DCFL, hence also a CFL.
  • Non-context-freeness test — if the stack order makes the required comparison impossible (the symbol needed is buried under other symbols), the language is not context-free at all.
  • Neutral symbols — symbols that need no memory are simply ignored by the PDA (do nothing).

How it works

To classify a language, ask two questions in order.

  1. Is it regular? Check whether acceptance requires saving an arbitrarily long block of symbols and comparing it against another block.
    If such unbounded counting and comparison is needed, the language is not regular; otherwise it is regular.
  2. Is it context-free / deterministic context-free? Try to build a PDA:
    • If the machine always knows what to do from the current input symbol alone — push on the opening symbols, pop on the matching closing symbols, do nothing on the neutral symbols — there is no non-determinism, so the language is a DCFL, hence also a CFL.
    • If the machine would have to guess (for example, guess where the middle of the string is, or guess which of two blocks to match), the language is at best a CFL, not a DCFL.
    • If the stack order makes the required comparison impossible (the symbol needed is buried under other symbols), the language is not context-free at all.

Typical deterministic strategy: on seeing the first kind of symbol, push; on seeing the matching symbol, pop; on seeing symbols that need no memory, do nothing. Because push/pop decisions are forced by the input symbol, the PDA is deterministic.

Formulas

  • Regular⊂DCFL⊂CFL\text{Regular} \subset \text{DCFL} \subset \text{CFL} — the nested subclasses of context-free languages.
  • L={am+nbmcn∣m,n≥1}L = \{ a^{m+n} b^m c^n \mid m, n \ge 1 \} — not regular, DCFL, CFL.
  • L={anbn∣n≥0}L = \{ a^n b^n \mid n \ge 0 \} — not regular, DCFL, CFL.
  • L={anbncmdm∣n,m≥1}L = \{ a^n b^n c^m d^m \mid n, m \ge 1 \} — not regular, DCFL, CFL.
  • L={anbmcndm∣n,m≥1}L = \{ a^n b^m c^n d^m \mid n, m \ge 1 \} — not regular, not DCFL, not CFL.
  • L={anbmcm∣n,m≥1}L = \{ a^n b^m c^m \mid n, m \ge 1 \} — not regular, DCFL, CFL.
  • L={ambicmdk∣m,i,k≥0}L = \{ a^m b^i c^m d^k \mid m, i, k \ge 0 \} — not regular, DCFL, CFL.
  • L={ambn∣m>n}L = \{ a^m b^n \mid m > n \} — not regular, DCFL, CFL.
  • L={anb2n∣n≥0}L = \{ a^n b^{2n} \mid n \ge 0 \} — DCFL, CFL.

Worked examples

Example 1: L={am+nbmcn∣m,n≥1}L = \{ a^{m+n} b^m c^n \mid m, n \ge 1 \}

  • Not regular: the aa's must be saved and compared against both the bb's and the cc's — infinite counting plus infinite comparison.
  • Context-free: yes.
  • Deterministic context-free: yes. On seeing aa the machine is sure to push; on seeing bb and cc it is sure to pop. No non-determinism.
  • Conclusion: not regular, DCFL, CFL.

Example 2: L={anbn∣n≥0}L = \{ a^n b^n \mid n \ge 0 \}

  • Not regular: the aa's must be stored and compared against the bb's.
  • DCFL and CFL: push on every aa, pop on every bb — deterministic.

Example 3: L={anbncmdm∣n,m≥1}L = \{ a^n b^n c^m d^m \mid n, m \ge 1 \}

  • Not regular: an infinite string must be saved and counted.
  • Deterministic context-free: yes. The moves are known exactly: aa's are pushed, bb's are popped, cc's are pushed, dd's are popped.
  • Conclusion: DCFL and CFL.

Example 4: L={anbmcndm∣n,m≥1}L = \{ a^n b^m c^n d^m \mid n, m \ge 1 \} (the aa's matched against the cc's and the bb's against the dd's)

  • Not regular: an infinite string must be saved and counted.
  • Not context-free: by the time the cc's are reached, the bb's are on top of the stack, so the aa's needed to match against the cc's are buried and cannot be reached.
  • Since it is not context-free, it is also not deterministic context-free and not regular.

Example 5: L={anbmcm∣n,m≥1}L = \{ a^n b^m c^m \mid n, m \ge 1 \}

  • Not regular: the bb's must be saved and compared against the cc's (unbounded comparison).
  • Deterministic context-free: yes. On seeing aa, push aa; on seeing bb, push bb; on seeing cc, pop bb. The machine always knows whether to push or pop — no non-determinism.
  • Hence also context-free.

Example 6: L={ambicmdk∣m,i,k≥0}L = \{ a^m b^i c^m d^k \mid m, i, k \ge 0 \}

  • Infinite language; the aa's must be compared against the cc's, so not regular.
  • Context-free and deterministic context-free: aa's are pushed, bb's do nothing, cc's pop, dd's do nothing. Every action is forced by the input symbol; no non-determinism is needed.
  • Hence also context-free.

Example 7: L={ambn∣m>n}L = \{ a^m b^n \mid m > n \}

  • Not regular: an unbounded comparison is required — all the aa's must be saved and compared against the bb's.
  • Deterministic: push all aa's, then pop one aa for every bb.
  • Acceptance step: at the end, on epsilon input (the whole string is over), if the top of the stack contains aa (at least one aa is left over), go to the final state. The DPDA accepts exactly when at least one extra aa remains.
  • Therefore a DCFL, and hence a CFL.

Example 8: L={anb2n∣n≥0}L = \{ a^n b^{2n} \mid n \ge 0 \}

  • A deterministic PDA exists: for every aa, push two stack symbols; for every bb, pop one aa.
  • Therefore the language is a DCFL, and by extension a CFL.

Comparison

LanguageRegular?DCFL?CFL?Reason
am+nbmcna^{m+n} b^m c^nNoYesYespush on aa, pop on bb and cc — forced moves
anbna^n b^nNoYesYespush aa, pop bb
anbncmdma^n b^n c^m d^mNoYesYespush aa, pop bb, push cc, pop dd
anbmcndma^n b^m c^n d^mNoNoNoneeded symbol buried on stack
anbmcma^n b^m c^mNoYesYespush bb, pop bb on cc
ambicmdka^m b^i c^m d^kNoYesYespush aa, ignore bb, pop on cc, ignore dd
ambna^m b^n, m>nm > nNoYesYespush aa, pop per bb, accept on epsilon with aa on top
anb2na^n b^{2n}NoYesYespush two per aa, pop one per bb

Revision summary

  • Machine hierarchy: finite automaton → regular; deterministic PDA → DCFL; any PDA → CFL.
  • Regular ⊂ DCFL ⊂ CFL. A DCFL is always a CFL; a non-CFL is never a DCFL or regular.
  • Test for non-regularity: unbounded saving plus unbounded comparison of two blocks.
  • Test for determinism: at every step the input symbol alone decides push or pop (no guessing).
  • Test for non-context-freeness: the symbol that must be matched is buried under other stack symbols when the matching input is reached.
  • Neutral symbols (those needing no memory) are simply ignored by the PDA (do nothing).
  • Guessing (for example, where the middle of the string is, or which of two blocks to match) rules out DCFL but may still leave the language a CFL.

Previous-year exam insights

  • Pumping lemma for CFLs: if L is a CFL, some pumping length p exists such that every string of length ≥ p can be written uvwxy with |vwx| ≤ p, |vx| ≥ 1, and uviuv^{i} wxiwx^{i} y ∈ L for all i ≥ 0; use it to prove languages like {ana^{n} bnb^{n} cnc^{n}} and {ww} are not context-free.
    Asked inGATE 2022GATE 2017
  • DCFLs are closed under complement (unlike general CFLs) but not under union or intersection; CFLs are closed under union, concatenation, Kleene star and reversal, but not under intersection or complement.
    Asked inGATE 2020GATE 2018
  • A language is regular iff it is accepted by a finite automaton; a CFL that is not regular typically requires matching two dependent blocks (e.g.anbn)(e.g. a^{n} b^{n}), while languages with a free block (e.g.anbncm)(e.g. a^{n} b^{n} c^{m}) are still CFLs and often DCFLs.
    Asked inGATE 2022GATE 2025
  • Every DCFL is unambiguous and has an LR(1) grammar; there exist CFLs that are inherently ambiguous and hence not DCFLs, and DCFLs are a proper subclass of CFLs.
    Asked inGATE 2020
  • For languages of the form {ama^{m} bnb^{n} cpc^{p} dqd^{q}} with linear constraints, the language is context-free iff the constraint can be checked with one stack (one equality between two blocks);
    constraints linking three or more blocks (e.g. m = n = p) make it non-context-free.
    Asked inGATE 2026GATE 2018
  • A language of the form {ana^{n} bnb^{n}} is the canonical DCFL; adding a free block (e.g. {ana^{n} bnb^{n} cmc^{m}}) keeps it a DCFL, but requiring two independent equalities (e.g. {ana^{n} bnb^{n} cnc^{n}}) destroys context-freeness.
    Asked inGATE 2022GATE 2017
Pages 1–2 of 2