GATE / CS ARCHIVE

GATE CSE 1996 / QUESTION 11

Eliminate nullable and unit rules

Theory of Computation · Context-Free Grammars & Languages · Full GATE CSE 1996 paper

GATE 1996, Q11 5 marks · Descriptive
Report issue

Context-Free Grammars & Languages

Question 11, GATE 1996: Transform S→ABAC, A→aA|ε, B→bB|ε, C→d into an equivalent CFG with no ε-productions and no unit productions.
Tap to enlarge
Answer & solution

Answer: S→ABAC|BAC|AAC|ABC|AC|BC|d; A→aA|a; B→bB|b; C→d. (no official key was published)

The nullable nonterminals are A and B. C is not nullable because it must produce d; S is consequently not nullable. In S→ABAC, independently retain or omit each of the two occurrences of A and the one occurrence of B. The distinct right sides are ABAC, BAC, AAC, ABC, AC, BC and C. The side AC arises twice because either A can be the sole retained A, but duplicate productions need only be written once.

Removing A→ε and B→ε requires adding the terminating alternatives A→a and B→b: their old recursive rules could previously terminate by using the nullable final nonterminal. Keep A→aA|a and B→bB|b, so these variables now generate positive rather than arbitrary nonnegative repetitions. Keep C→d.

The only unit production introduced is S→C. Substitute C's nonunit right side to obtain S→d, then delete S→C. The resulting grammar is S→ABAC|BAC|AAC|ABC|AC|BC|d, A→aA|a, B→bB|b, C→d.

Every rule now has a nonempty right side; none has a sole nonterminal. The original language is {ai bj ak d:i,j,k≥0}. The seven start alternatives cover exactly which of the three repetition blocks are empty, with the all-empty case producing d. Thus the transformation preserves the language, including the shortest word d.

Original paper, page 8