4.6.8 · D5Theory of Computation
Question bank — Context-free grammars (CFG) — productions, derivations, parse trees
True or false — justify
Grammar means "language"
False. A grammar is one specific recipe; many different grammars can generate the exact same language . Confusing the recipe with the dish is the root of most CFG mistakes.
Every regular language is context-free
True. Any DFA can be mechanically turned into a right-linear CFG, so regular languages sit strictly inside the context-free languages in the Chomsky hierarchy (the inner boxes of figure s01).
Every context-free language is regular
False. The classic witness is : unbounded nesting needs a stack, which finite states (a DFA) cannot provide, but recurses freely.
"Context-free" means the output string ignores symbol order
False. Order in the output matters completely — it's a string! "Context-free" only says the left-hand side of every rule is a single variable, so a rule's applicability never depends on neighbours.
A grammar with the rule can still generate an infinite language
True. An -rule merely lets a variable vanish; infinity comes from recursion (e.g. ), not from erasing.
If a string has two different derivations, the grammar is ambiguous
False. You need two derivations that yield distinct parse trees (equivalently, two distinct leftmost derivations). Two derivations that differ only in the order you expanded variables are the same tree.
Ambiguity is a property of the language, not the grammar
False — it's the reverse. Ambiguity is a property of the grammar: a language can have both ambiguous and unambiguous grammars generating it.
Every parse tree corresponds to exactly one leftmost derivation
True. Leftmost-derivation ↔ parse-tree is a bijection. That's precisely why we count distinct trees, not distinct derivations, when hunting ambiguity.
The empty string can never be in
False. If (e.g. via ), then . The matched-parentheses grammar generates .
A sentential form is always a member of the language
False. A sentential form is anything derivable from (it may still contain variables). Only fully-terminal strings, , belong to .
Making a grammar unambiguous changes the language it generates
False. (ambiguous) and (precedence-layered) generate the same arithmetic language; only the set of parse trees changes, collapsing to one tree per string.
The two trees behind the biggest trap
Spot the error
— is this a valid CFG production?
No. The LHS has a terminal next to the variable, so the LHS is not a single variable — that's a context-sensitive rule, outside CFG.
where never appears on any RHS — problem?
is an unreachable variable — no derivation from ever produces — so it contributes nothing to . The grammar stays valid, but such a rule is useless and is removed by grammar cleanup (see figure s04).
" is bad because it's left-recursive."
Wrong diagnosis. 's flaw is ambiguity (no precedence), not left-recursion. is deliberately left-recursive () to get correct left-associativity — recursion isn't the disease.
"The string a+a*a has two parse trees in , so generates two languages."
Nonsense — two trees for one string still means one language. Ambiguity is about multiple structures for the same string, not multiple languages.
" is harmless."
It's harmless to the language (it derives nothing new) but it makes the grammar have infinitely many derivations of any string via useless loops — a cycle that also blocks some parsing algorithms.
"A grammar with no -rules and no unit rules must be unambiguous."
False. has neither -rules nor unit rules yet is ambiguous. Removing /unit rules (a Chomsky normal form step) does not remove ambiguity.
Why questions
Why is the LHS restricted to a single variable?
Because that single restriction is literally the definition of "context-free": the rule leaves the context untouched, so a variable can be rewritten regardless of neighbours.
Why do parse trees exist if derivations already describe the process?
A derivation is a linear list whose order is often arbitrary; the tree discards that incidental ordering and keeps only the structural truth (parent–child), which is what actually determines meaning (compare figures s02 and s03).
Why does ambiguity matter for compilers?
The parse tree dictates meaning;
a+a*a could mean or . Two trees = two possible values, so the compiler cannot decide what the program means.Why do we split arithmetic into three layers ?
Each layer encodes one precedence tier — (factor) tightest, (term,
*) next, (expression, +) loosest — so * is forced to group before +, killing the ambiguity of .Why can a CFG describe matched brackets but a regular expression cannot?
Matching requires counting unboundedly deep nesting. A CFG's recursion gives an implicit stack (formalised by a pushdown automaton); a regex/DFA has only finitely many states and cannot count without bound.
Why does the pumping lemma for CFLs not help us prove a language is context-free?
It states a necessary property of context-free languages, not a sufficient one; it can only ever disprove membership (by finding an unpumpable string), never confirm it.
Why is left-recursion () used instead of right-recursion for +
Left-recursion makes the operator left-associative:
a-a-a groups as , matching how subtraction actually works. Right-recursion would wrongly group .Edge cases
Is — what is ?
. No production ever reaches a terminal-only string, so nothing is in the language despite having a rule.
Grammar only — what is ?
, the single empty string. Note this is not the empty language : one member exists, it just happens to be empty.
Can a CFG generate a language with but the grammar be in Chomsky normal form?
Yes, with the standard exception rule: CNF forbids everywhere except an allowed (with not on any RHS), specifically so that can still be expressed.
Two grammars generate the same language — must they have the same parse trees?
No. Same language () says nothing about structure; the grammars can assign totally different trees to the same string, and one may be ambiguous while the other isn't.
Can an infinite language have a finite grammar?
Yes — that's the whole point. A finite rule set like generates infinitely many strings through recursion; grammars are always finite by definition.
Is every finite language context-free?
Yes. Just list one production per string; a finite set of such rules generates exactly that finite language, and finite languages are even regular.
If a grammar has a variable that derives only infinite loops (never terminals), can it still be useful?
The variable is non-productive and generates nothing, so any rule mentioning it is effectively dead. Such variables are removed during grammar cleanup before building a Chomsky normal form (figure s04).
Connections
- Context-free grammars (CFG) — productions, derivations, parse trees (parent)
- Regular expressions and DFAs
- Pushdown automata
- Chomsky hierarchy
- Chomsky normal form
- Pumping lemma for CFLs
- Compilers — parsing