All NEC Computer Engineering topics / Theory of Computation & Computer GraphicsProperties of Context Free Language Theory of Computation & Computer Graphics β Introduction to Context Free Language, NEC licence examination syllabus (Nepal Engineering Council).
Properties of Context Free Language
What survives the operations, what can be decided, and why the answers differ so sharply from the regular case.
π Where this lives: The undecidability results in this topic have a blunt practical consequence: no tool can tell you whether two grammars describe the same language . When a language committee rewrites a grammar to make it LR-parsable, or a compiler team refactors a parser, there is no automatic way to verify the new grammar accepts exactly what the old one did. Verification is by testing and human review, permanently β not because the tooling is immature, but because the problem is provably unsolvable. Search "context free grammar equivalence undecidable compiler verification" .
Closure properties
A CLASS OF LANGUAGES IS CLOSED UNDER AN OPERATION IF APPLYING
THAT OPERATION TO MEMBERS OF THE CLASS ALWAYS YIELDS A MEMBER OF
THE CLASS.
ββ CLOSED: THE THREE EASY CASES ββββββββββββββββββββββββββββ
Given grammars Gβ with start Sβ and Gβ with start Sβ (rename
variables so they share none):
UNION S β Sβ | Sβ
CONCATENATION S β Sβ Sβ
KLEENE STAR S β Sβ S | Ξ΅
THE PROOFS ARE ONE LINE EACH, and that is not an accident:
these are precisely the operations regular expressions are
built from, and a grammar can express them directly.
ββ ALSO CLOSED βββββββββββββββββββββββββββββββββββββββββββββ
REVERSAL β reverse the right-hand side of every production.
SUBSTITUTION and HOMOMORPHISM β replace each terminal by a
language or a string.
INVERSE HOMOMORPHISM.
INTERSECTION WITH A REGULAR LANGUAGE β the most useful
closure property in the topic.
THE PRODUCT CONSTRUCTION for CFL β© REGULAR:
Run the PDA and the DFA IN PARALLEL. The states of the new
machine are pairs (PDA state, DFA state); the stack is THE
PDA'S ONLY.
Ξ΄β²( (q,s), a, X ) = { ((p, Ξ΄_DFA(s,a)), Ξ³) :
(p,Ξ³) β Ξ΄_PDA(q,a,X) }
Accept when both components accept.
IT WORKS BECAUSE ONLY ONE STACK IS NEEDED β the DFA
contributes only finite state, which folds into the state
component. THAT SINGLE OBSERVATION IS ALSO THE REASON THE
NEXT SECTION FAILS.
ββ NOT CLOSED: INTERSECTION ββββββββββββββββββββββββββββββββ
THE STANDARD COUNTEREXAMPLE, worth memorising exactly:
Lβ = { aβΏ bβΏ cα΅ : n, m β₯ 0 }
CONTEXT FREE β match the a's with the b's, let the
c's be free:
S β A C, A β aAb | Ξ΅, C β cC | Ξ΅
Lβ = { aβΏ bα΅ cα΅ : n, m β₯ 0 }
CONTEXT FREE β let the a's be free, match b's with
c's:
S β A B, A β aA | Ξ΅, B β bBc | Ξ΅
Lβ β© Lβ = { aβΏ bβΏ cβΏ } β NOT CONTEXT FREE
Each language matches ONE pair using its single stack. The
intersection demands BOTH pairs matched simultaneously,
which would need two stacks β and a machine with two stacks
is, as the next section shows, a full Turing machine.
ββ NOT CLOSED: COMPLEMENT ββββββββββββββββββββββββββββββββββ
THIS FOLLOWS FROM THE ABOVE BY DE MORGAN, and the argument is
a standard exam answer:
Suppose CFLs were closed under complement. They are
closed under union. Then for any CFLs Lβ, Lβ:
Lβ β© Lβ = complement( complement(Lβ) βͺ complement(Lβ) )
would be built entirely from operations preserving
context-freeness, so CFLs would be closed under
intersection β CONTRADICTING the counterexample above. β
NOTE THE FORM OF THE ARGUMENT: it proves non-closure under
complement WITHOUT exhibiting a single explicit
counterexample, purely from closure under union and
non-closure under intersection.
ββ NOT CLOSED: DIFFERENCE ββββββββββββββββββββββββββββββββββ
Since Lβ β© Lβ = Lβ β (Lβ β Lβ), closure under difference
would give closure under intersection.
NOTE HOWEVER that L β R IS context free when R is REGULAR,
because L β R = L β© complement(R) and regular languages ARE
closed under complement.
ββ THE COMPARISON TABLE ββββββββββββββββββββββββββββββββββββ
OPERATION REGULAR CONTEXT FREE DCFL
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
union YES YES NO
concatenation YES YES NO
Kleene star YES YES NO
intersection YES **NO** NO
complement YES **NO** **YES**
difference YES NO NO
reversal YES YES NO
β© with a regular
language YES YES YES
homomorphism YES YES NO
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
THE DCFL COLUMN IS STRIKING AND OFTEN EXAMINED: DETERMINISTIC
CONTEXT-FREE LANGUAGES ARE CLOSED UNDER COMPLEMENT BUT NOT
UNDER UNION β the exact reverse of the general CFL case.
COMPLEMENT WORKS because a DPDA is deterministic, so
swapping accepting and non-accepting states works much as
for a DFA (with care over Ξ΅-moves and non-halting
computations).
UNION FAILS because Lβ = {aβΏbβΏcα΅} and Lβ = {aβΏbα΅cα΅} are both
DETERMINISTIC, but their union is not β a deterministic
machine reading the a's cannot know whether it will need to
match them against the b's or ignore them, and it must
commit before finding out.
Decision properties, and the summary
ββ DECIDABLE QUESTIONS βββββββββββββββββββββββββββββββββββββ
Given a context-free grammar G:
1. MEMBERSHIP β is w β L(G)?
DECIDABLE. Convert to Chomsky normal form and run CYK in
O(nΒ³Β·|G|). Alternatively, since CNF derivations of a
length-n string take exactly 2n β 1 steps, exhaustive
search of bounded depth also works.
2. EMPTINESS β is L(G) = β
?
DECIDABLE. Compute the GENERATING variables (those
deriving some terminal string) by fixed-point iteration;
L(G) is empty exactly when S is not generating.
3. FINITENESS β is L(G) finite?
DECIDABLE. Remove useless symbols, convert to CNF, and
look for a CYCLE in the dependency graph among the
remaining variables. A cycle means a variable can derive
a string containing itself, hence infinitely many
strings.
THESE THREE ARE THE ENTIRE LIST OF USEFUL DECIDABLE
PROPERTIES, which is a much shorter list than the regular case
offered.
ββ UNDECIDABLE QUESTIONS βββββββββββββββββββββββββββββββββββ
Given context-free grammars G, Gβ, Gβ:
Β· IS L(Gβ) = L(Gβ)? UNDECIDABLE
Β· IS L(Gβ) β L(Gβ)? UNDECIDABLE
Β· IS L(Gβ) β© L(Gβ) = β
? UNDECIDABLE
Β· IS L(G) = Ξ£*? UNDECIDABLE
Β· IS L(G) REGULAR? UNDECIDABLE
Β· IS G AMBIGUOUS? UNDECIDABLE
Β· IS L(G) INHERENTLY AMBIGUOUS? UNDECIDABLE
Β· IS THE COMPLEMENT OF L(G) CONTEXT FREE? UNDECIDABLE
ALL ARE PROVED BY REDUCTION FROM POST'S CORRESPONDENCE
PROBLEM, which appears in the Turing machine section. The
pattern of such a proof: show that if the question could be
answered, PCP could be solved β and PCP is known to be
unsolvable.
NOTE THE PARTIAL EXCEPTION: FOR **DETERMINISTIC** CONTEXT-FREE
LANGUAGES, EQUIVALENCE **IS** DECIDABLE β a deep result proved
by SΓ©nizergues in 1997, over thirty years after the question
was posed. Containment for DCFLs remains undecidable.
THIS IS ANOTHER REASON PROGRAMMING LANGUAGES ARE DESIGNED TO
BE DETERMINISTIC: the deterministic subclass is better
behaved in almost every respect.
ββ THE COMPARISON WITH REGULAR LANGUAGES βββββββββββββββββββ
QUESTION REGULAR CONTEXT FREE
ββββββββββββββββββββββββββββββββββββββββββββββ
w β L? decidable decidable (O(nΒ³))
L = β
? decidable decidable
L finite? decidable decidable
Lβ = Lβ? DECIDABLE **UNDECIDABLE**
Lβ β Lβ? DECIDABLE **UNDECIDABLE**
L = Ξ£*? DECIDABLE **UNDECIDABLE**
Lβ β© Lβ = β
? DECIDABLE **UNDECIDABLE**
ββββββββββββββββββββββββββββββββββββββββββββββ
THE DIVIDING LINE IS EXACTLY WHERE ONE MIGHT EXPECT: questions
about a SINGLE string or the EXISTENCE of strings remain
decidable; questions COMPARING TWO LANGUAGES become
undecidable.
THE UNDERLYING REASON IS THE LOSS OF A CANONICAL FORM. For
regular languages the minimal DFA is unique, so equivalence
reduces to isomorphism. CONTEXT-FREE GRAMMARS HAVE NO
COMPUTABLE CANONICAL FORM β CNF and GNF are not unique β SO
THERE IS NOTHING TO COMPARE.
ββ THE WHOLE SECTION IN ONE VIEW βββββββββββββββββββββββββββ
GENERATOR: context-free grammar
RECOGNISER: non-deterministic pushdown automaton
NORMAL FORMS: Chomsky (binary trees, for CYK)
Greibach (one terminal per step, for the
PDA proof)
NOTATION: BNF and EBNF
BOUNDARY: the pumping lemma, aβΏbβΏcβΏ excluded
CLOSURE: union, concatenation, star, β© with regular
β but NOT intersection or complement
DECIDABLE: membership, emptiness, finiteness
UNDECIDABLE: equivalence, ambiguity, and everything
comparative
AND THE SINGLE SENTENCE THAT EXPLAINS NEARLY ALL OF IT:
A PUSHDOWN AUTOMATON HAS ONE STACK, READ
LAST-IN-FIRST-OUT.
One stack matches one nested pair β hence aβΏbβΏ yes, aβΏbβΏcβΏ
no, hence no closure under intersection, hence no closure
under complement. EVERY PROPERTY IN THIS TOPIC TRACES BACK
TO THAT ONE ARCHITECTURAL FACT.
CLOSURE β AND THE DCFL COLUMN IS THE SURPRISE
OPERATION
REGULAR
CONTEXT FREE
DCFL
union YES YES NO
concatenation YES YES NO
Kleene star YES YES NO
intersection YES NO NO
complement YES NO YES β
β© with a REGULAR language YES YES YES
DCFLs ARE CLOSED UNDER COMPLEMENT BUT NOT UNION β the exact reverse of general CFLs.
Complement works because the machine is deterministic. Union fails: reading the a's, a DPDA cannot know whether it must match them against b's or ignore them β and it must commit before finding out.
WHY INTERSECTION FAILS β THE COUNTEREXAMPLE TO MEMORISE
Lβ = { aβΏbβΏcα΅ }
match a with b, c free β CF
β©
Lβ = { aβΏbα΅cα΅ }
a free, match b with c β CF
=
{ aβΏbβΏcβΏ }
NOT context free β
Each language matches ONE pair with its ONE stack. The intersection demands BOTH pairs at once β which needs two stacks, and two stacks is a Turing machine.
COMPLEMENT then fails by DE MORGAN: closure under complement + union would give closure under intersection. No explicit counterexample needed.
DECIDABILITY β THE LINE FALLS WHERE TWO LANGUAGES ARE COMPARED
DECIDABLE: w β L? (CYK, O(nΒ³)) Β· L = β
? Β· L finite?
UNDECIDABLE: Lβ = Lβ? Β· Lβ β Lβ? Β· L = Ξ£*? Β· is G ambiguous?
Nearly every property in this topic follows from one architectural fact: a pushdown automaton has one stack, read last-in-first-out . One stack matches one nested pair β hence aβΏbβΏ yes and aβΏbβΏcβΏ no, hence no closure under intersection, hence by De Morgan no closure under complement.
π Go further: Why did equivalence become undecidable when it was decidable for regular languages? The answer is the loss of a canonical form . A regular language has a unique minimal DFA, so testing equivalence reduces to testing isomorphism of two computed objects. Context-free grammars have no computable canonical form β CNF and GNF are not unique, and many normal-form grammars generate the same language β so there is simply nothing to compare. The correspondence generalises across the whole subject: wherever a formalism admits a computable canonical form, equivalence tends to be decidable; where it does not, it tends not to be . Search "canonical form decidability equivalence problem formal languages" .
π‘ Exam angle: reproduce the closure table across regular, context-free and deterministic context-free β the DCFL column reversing the CFL one (complement yes, union no) is a favourite question. Give the one-line grammar constructions for union, concatenation and star . The non-closure under intersection counterexample {aβΏbβΏcα΅} β© {aβΏbα΅cα΅} = {aβΏbβΏcβΏ} must be memorised, and derive non-closure under complement from it by De Morgan rather than seeking a separate example. Know intersection with a regular language is closed , via the product construction using one stack. List the three decidable questions and the undecidable ones, noting that all the latter reduce from Post's Correspondence Problem.
Syllabus points Closure properties Decision properties Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Introduction to Context Free Language