GATE / CS ARCHIVE

SIMILAR QUESTIONS / THEORY OF COMPUTATION

Count binary and terminal CNF productions

3 questions solved the same way, newest first. Back to Theory of Computation

How these are solved

For a nonempty length-n yield in CNF, count n terminal productions and n−1 binary productions. Symbolic versus numerical requested counts are distinguished from strict repeats.

2 marks · NAT
Report issue

Count productions in a CNF derivation

Context-Free Grammars & Languages

Question 59, GATE 2024: A context-free grammar in Chomsky Normal Form has terminals {a,b,c} and 10 variables including start symbol S. It derives w=a³⁰b³⁰c³⁰. How many rule applications occur in a derivation S⇒*w?
Tap to enlarge
Answer & solution

Official answer: 179

Approach: In Chomsky Normal Form, a nonempty derivation uses binary-variable rules A→BC and terminal rules A→a. Count each kind using the final word length.
1. The word contains 30+30+30=90 terminal symbols. Each requires exactly one terminal-producing rule application, so there are 90 such applications.
2. Initially there is one variable occurrence. Each binary rule replaces one variable with two and therefore increases the total number of current symbol occurrences by one. Terminal rules replace one symbol with one and do not change that total.
3. To grow from one symbol to a final string of 90 symbols requires exactly 89 binary-rule applications. Equivalently, the variable branching tree is full binary with 90 leaves and 89 internal branching nodes.
4. Total rule applications =90+89=179.
Answer: 179. The ten available variable names can be reused and do not bound the number of variable occurrences. Derivation order does not change these counts; the CNF epsilon exception is irrelevant to this nonempty word.

Original paper, page 34 · Answer key

5 marks · Descriptive
Report issue

Asked again GATE 1992, Q2(xviii) (same question)

Length of a strict CNF derivation

Context-Free Grammars & Languages

Question 20, GATE 1994: A CFG has only productions A→BC and A→a. For a generated terminal word of length ℓ, how many production applications occur in its derivation? GateForum omits the symbol after “length”; GFG supplies ℓ.
Tap to enlarge
Answer & solution

Answer: 2ℓ−1 production applications for ℓ≥1. (no official key was published)

Each terminal of the final word is introduced by exactly one A→a production: no other production introduces terminals, and no production subsequently removes them. Hence there are exactly ℓ terminal-producing steps. Start with one nonterminal. Each A→BC application increases the number of nonterminals by one, whereas each A→a decreases it by one. If b is the number of binary applications, the final nonterminal count is 1+b−ℓ=0, so b=ℓ−1. The total number of applications is b+ℓ=2ℓ−1.

The same result follows by viewing the nonterminal portion of the parse tree as a full binary tree: ℓ terminal-producing leaves require ℓ−1 binary internal nodes. The count is independent of which nonterminal is expanded first, of leftmost versus rightmost derivation, and of ambiguity of the grammar. For ℓ=1 there is just the single terminal production, agreeing with the formula.

This is a count of steps, not a count of sentential forms: including both the initial symbol and final word gives 2ℓ sentential forms. Under the strict production forms stated here ε cannot be generated. If a different CNF convention permits S→ε, its ε derivation is a separate one-step exception, not an application of the formula at ℓ=0.

Original paper, page 6

Report issue

Asked again GATE 1994, Q20 (same question)

Length of a CNF derivation

Context-Free Grammars & Languages

Question 2(xviii), GATE 1992: For a nonempty word w of length ℓ in a Chomsky-normal-form grammar, how many production applications derive it?
Question 2(xviii), GATE 1992: For a nonempty word w of length ℓ in a Chomsky-normal-form grammar, how many production applications derive it?
Tap to enlarge
Select all that apply
Answer & solution

Answer: C: 2ℓ−1 (no official key was published)

A CNF parse tree uses binary nonterminal productions A→BC and terminal productions A→a. There are ℓ terminal-producing leaves. A full binary tree with ℓ leaves has ℓ−1 binary internal nodes, so the total applications are ℓ+(ℓ−1)=2ℓ−1. The order of expanding variables does not change that count. The special ε-production sometimes allowed for the start symbol is outside this nonempty-word case; substituting ℓ=0 into the formula would not count its derivation correctly.

Original paper, page 4