Theory of Computation · Properties of CFLs
Proving Languages Non-Context-Free (a^n b^{n^2}, a^n b^{2^n}, ww, a^n b^n c^m)
Overview
This section classifies a list of languages as regular, deterministic context-free (DCFL), context-free (CFL), or none of these, and justifies each classification from the limits of a pushdown automaton (PDA).
The central tool is the observation that a PDA loop can only add or remove a constant amount per iteration, so it can match counts that form an arithmetic progression (AP) in but never counts such as , , or .
Key concepts
- PDA capabilities: a PDA can only push symbols on the stack, pop symbols from the top of the stack, and run a finite number of loop states.
- Fixed-multiple comparison: a PDA can compare two counts only when the second count is a fixed multiple of the first (e.g. vs ): put the second block in a loop and pop a fixed number of stack symbols per loop iteration.
- Non-AP counts: a PDA cannot handle a second count that is not an arithmetic progression in — e.g. , , — because a loop can only add a constant amount per iteration.
- Top-only access: a PDA cannot compare an input symbol with the bottom of the stack; it sees only the top.
- No re-reading, no empty-stack comparison: a PDA cannot go back and re-read input already consumed, and once the stack is emptied by an earlier comparison there is nothing left to compare a later block against.
- Non-determinism: non-determinism can guess a centre (so is a CFL) but cannot fix the bottom-of-stack problem (so is not a CFL).
- Finite languages: every finite language is regular; every regular language is deterministic context-free; every DCFL is context-free.
Hierarchy: regular DCFL CFL; if a language is not context-free it is neither DCFL nor regular.
How it works
- — not context-free. Not regular: an unbounded number of 's must be saved and compared against a count derived from .
Not context-free: push 's; accepting would require a loop whose iteration count is , but a loop can pop only a fixed number of 's per iteration (e.g. 1 per iteration, as in ).
Since is not in AP, no loop can realise the count . Hence no PDA — deterministic or non-deterministic — exists. - — not context-free. Not regular (unbounded comparison). Not context-free: a loop would need to contain 's, and is not in AP, so no loop can have as its count.
Contrast: is context-free because is in AP — one loop iteration pops exactly 1 . - — CFL but not DCFL, not regular. This is exactly the set of all even-length palindromes. Not regular: an unbounded cannot be saved in a finite automaton and compared with .
Context-free: push , then pop while matching — a non-deterministic PDA guesses the centre. Not deterministic context-free: the centre is unknown, so a DPDA cannot know up to which point to push and from which point to pop.
It is a non-deterministic CFL. - — not context-free. Not regular (unbounded must be stored). Example string: must be accepted; we must save and then compare with the second .
Non-determinism can guess the centre (at every point guess "centre has come / not come") — that part is fine.
The fatal problem: after guessing the centre, the first input symbol of the second copy must be compared with the bottom of the stack (the first symbol of pushed earliest).
A PDA can compare the input only with the top of the stack. Since the stack cannot examine its bottom, no PDA accepts . Hence not context-free. - with a comparison between and — not context-free. Not regular, since is already inside it. Attempt with a PDA: push one for every ; pop one for every .
Then is accepted and the stack is empty when the 's start.
Now the 's must be compared with (i.e. with the 's/'s), but (i) nothing is left on the stack, and (ii) the input already read cannot be re-read. So there is nothing to compare the 's against.
Attempted fix: push 2 's per , pop 1 per . Then you can compare 's with 's, but you cannot compare how many 's are left with the 's. Nothing works.
Counter-example showing the "push 2, pop 1" trick is wrong: with that scheme, is accepted (4 's pushed, 2 popped, compared against the 2 remaining) — but is also accepted (4 's pushed, 1 popped, 2 remaining, compared) even though it should not be.
So the logic fails. Conclusion: not context-free. - — regular (hence DCFL, hence CFL). The bound makes the language finite; every finite language is regular; every regular language is deterministic context-free; every DCFL is context-free.
- — not context-free. Push 2 's per , pop 1 per : the -vs- comparison succeeds, but by then the stack top is empty, so the 's cannot be compared with the 's. Not context-free.
Attempted fix: push 5 's per and pop 1 per and per . Flaw: this accepts any string with ( 's, 's, 's) — e.g. it accepts (one , four 's) and other wrong strings.
So the language recognised would be , not the required one. Hence not context-free. - — regular (hence DCFL, hence CFL). This is just the set of all strings containing as a substring — a regular language, so deterministic context-free and context-free.
- — regular (hence DCFL and CFL). With the length fixed to exactly , the number of possible strings is — a finite language. Finite regular (deterministic) context-free.
- — not context-free. It contains the pattern; since cannot be recognised by any PDA, adding one more part does not make it context-free. Hence not context-free, not DCFL, not regular.
- — not context-free. Push all 's; the 's would need a loop containing 's. is not in AP, so — for the same reason as and — no PDA exists. Hence not context-free, not DCFL, not regular.
- — context-free and deterministic context-free. Take the ordinary PDA for equal counts (push 's, pop for 's) and accept in two ways:
- if after all 's the stack still contains 's (input finished, stack not empty) ;
- if the stack reaches (bottom marker) while 's are still remaining . It is deterministic: we know exactly when to push (on 's) and when to pop (on 's). So it is a DCFL.
- — context-free (deterministic). Construction: in a loop over the 's, push one for every two 's, and leave one unrecorded (the "+1"). Then for every , pop one .
Equivalently: for the first push nothing (leave it), and for every second push one ; then pop one per .
Formulas
- : second block count , not in AP no PDA loop can generate it.
- : second block count , not in AP no PDA.
- : second block count , not in AP no PDA.
- : second block count , in AP CFL (pop 1 per loop iteration).
- , : push 1 per 2 's, keep 1 aside; pop 1 per .
- "Push 5 's per , pop 1 per and per " recognises — the wrong language.
- (finite regular).
Worked examples
Example A – under the "push 2 's per , pop 1 per " scheme.
- Read : push 2 's; read : push 2 's stack has 4 's.
- Read : pop 1 3 's; read : pop 1 2 's.
- Read : compare with the 2 's on the stack accepted (correct here).
Example B – under the same scheme (the flaw).
- Read : push 2; read : push 2 4 's.
- Read : pop 1 3 's.
- Read : compare with the remaining 's accepted, although should be rejected. Hence the scheme is wrong; the language is not context-free.
Example C – "push 5 's per , pop 1 per and per ".
- String : read push 5 's; read 4 's pop 4 's 1 left; input ends with stack non-empty accepted, but . The scheme in fact recognises .
Example D – with , string .
- Push , push ( saved). Guess centre. Next input is , but the stack top is ; the needed for comparison is at the bottom of the stack, which a PDA cannot see no PDA.
Example E – , .
- Input (, ): push , push ; read , pop stack has one left, input finished, stack not empty accept.
- Input (, ): push ; read , pop stack ; another arrives while stack is accept.
- Input (): stack exactly when input ends reject.
Comparison
| Language | Regular? | DCFL? | CFL? | Reason |
|---|---|---|---|---|
| No | No | No | not in AP; no PDA loop can count | |
| No | No | No | not in AP | |
| No | No | No | not in AP | |
| No | No | Yes | centre unknown non-deterministic PDA only | |
| No | No | No | needs bottom-of-stack comparison | |
| No | No | No | contains | |
| ( vs compared) | No | No | No | stack empty after ; no re-reading |
| No | No | No | stack empty after -vs- comparison | |
| , | Yes | Yes | Yes | finite |
| , | Yes | Yes | Yes | strings containing as substring |
| , | Yes | Yes | Yes | finite ( strings) |
| , | No | Yes | Yes | push on , pop on ; accept on leftover 's or on with 's remaining |
| , | No | Yes | Yes | push 1 per 2 's, keep 1 aside, pop 1 per |
Revision summary
- A PDA loop can add or pop only a constant amount per iteration, so it can match counts in AP (, , , ) but never , , .
- A PDA sees only the top of the stack, never the bottom, so is impossible.
- A PDA cannot re-read input and cannot compare a new block after the stack has been emptied, so (with vs compared) and are not CFLs.
- Non-determinism can guess a centre (so is a CFL) but cannot fix the bottom-of-stack problem (so is not a CFL).
- "Push 's per , pop 1 per " changes the language to — always check with a small counter-example string (, ).
- Finite languages are regular; regular DCFL CFL. Use this for bounded languages (, ).
- "Contains as a substring" is regular.
- is a DCFL: accept either on leftover 's () or on reaching with 's remaining ().
Previous-year exam insights
- The CFL pumping lemma (uvwxy form): if L is context-free, there is a pumping length p such that every string s in L with |s| ≥ p can be written s = uvwxy with |vwx| ≤ p, |vx| ≥ 1, and y in L for all i ≥ 0.
Use it (or closure properties) to prove non-context-freeness; note that satisfying the pumping lemma does NOT prove a language is context-free.
Asked inGATE 2005 - CFLs are closed under union, concatenation, Kleene star, reversal, homomorphism and intersection with a regular language, but NOT under intersection or complementation.
DCFLs are closed under complement but not under union or intersection.
Asked inGATE 2022GATE 2021GATE 2020 - A language of the form { } or { with n compared to m} is not context-free, but { | m,n ≥ 0} (no comparison between n and m) IS context-free, and { } is context-free (indeed DCFL).
Asked inGATE 2022GATE 2016 - A language whose strings have a fixed marker separating two parts, e.g. {w c }, is deterministic context-free (the marker tells the DPDA where to switch from pushing to popping), whereas {} without a marker is still context-free but needs the centre guessed.
Asked inGATE 2014GATE 2019 - If L is not context-free, then neither is L intersected with a regular set when the intersection still contains the non-CFL core; more usefully, to show a language is not CFL, intersect it with a regular language to obtain a known non-CFL (closure under intersection with regular languages).
Asked inGATE 2020GATE 2006 - Every regular language is a DCFL and every DCFL is a CFL; the inclusions are strict (e.g. { } is DCFL but not regular, and {} is CFL but not regular).
A finite language is always regular.
Asked inGATE 2020GATE 2015