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.
- 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. - 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
- — the nested subclasses of context-free languages.
- — not regular, DCFL, CFL.
- — not regular, DCFL, CFL.
- — not regular, DCFL, CFL.
- — not regular, not DCFL, not CFL.
- — not regular, DCFL, CFL.
- — not regular, DCFL, CFL.
- — not regular, DCFL, CFL.
- — DCFL, CFL.
Worked examples
Example 1:
- Not regular: the 's must be saved and compared against both the 's and the 's — infinite counting plus infinite comparison.
- Context-free: yes.
- Deterministic context-free: yes. On seeing the machine is sure to push; on seeing and it is sure to pop. No non-determinism.
- Conclusion: not regular, DCFL, CFL.
Example 2:
- Not regular: the 's must be stored and compared against the 's.
- DCFL and CFL: push on every , pop on every — deterministic.
Example 3:
- Not regular: an infinite string must be saved and counted.
- Deterministic context-free: yes. The moves are known exactly: 's are pushed, 's are popped, 's are pushed, 's are popped.
- Conclusion: DCFL and CFL.
Example 4: (the 's matched against the 's and the 's against the 's)
- Not regular: an infinite string must be saved and counted.
- Not context-free: by the time the 's are reached, the 's are on top of the stack, so the 's needed to match against the '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:
- Not regular: the 's must be saved and compared against the 's (unbounded comparison).
- Deterministic context-free: yes. On seeing , push ; on seeing , push ; on seeing , pop . The machine always knows whether to push or pop — no non-determinism.
- Hence also context-free.
Example 6:
- Infinite language; the 's must be compared against the 's, so not regular.
- Context-free and deterministic context-free: 's are pushed, 's do nothing, 's pop, 's do nothing. Every action is forced by the input symbol; no non-determinism is needed.
- Hence also context-free.
Example 7:
- Not regular: an unbounded comparison is required — all the 's must be saved and compared against the 's.
- Deterministic: push all 's, then pop one for every .
- Acceptance step: at the end, on epsilon input (the whole string is over), if the top of the stack contains (at least one is left over), go to the final state. The DPDA accepts exactly when at least one extra remains.
- Therefore a DCFL, and hence a CFL.
Example 8:
- A deterministic PDA exists: for every , push two stack symbols; for every , pop one .
- Therefore the language is a DCFL, and by extension a CFL.
Comparison
| Language | Regular? | DCFL? | CFL? | Reason |
|---|---|---|---|---|
| No | Yes | Yes | push on , pop on and — forced moves | |
| No | Yes | Yes | push , pop | |
| No | Yes | Yes | push , pop , push , pop | |
| No | No | No | needed symbol buried on stack | |
| No | Yes | Yes | push , pop on | |
| No | Yes | Yes | push , ignore , pop on , ignore | |
| , | No | Yes | Yes | push , pop per , accept on epsilon with on top |
| No | Yes | Yes | push two per , pop one per |
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 y ∈ L for all i ≥ 0; use it to prove languages like { } 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 , while languages with a free block 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 { } 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 { } is the canonical DCFL; adding a free block (e.g. { }) keeps it a DCFL, but requiring two independent equalities (e.g. { }) destroys context-freeness.
Asked inGATE 2022GATE 2017