GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 11(b)

Find the shortest ambiguous word

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

GATE 1997, Q11(b) 2 marks · Descriptive
Report issue

Context-Free Grammars & Languages

Question 11(b), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
Find a shortest word having two distinct parse trees.
Question 11(b), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
Find a shortest word having two distinct parse trees.
Question 11(b), GATE 1997: Use the GFG grammar with start symbol S:
S → bSe | PQR
P → bPc | ε
Q → cQd | ε
R → dRe | ε.
Terminals are b,c,d,e. Generated words are b^i c^j d^k e^m with nonnegative integer exponents. GateForum instead prints S→bSc and mistakenly gives nonpositive exponents; those literal variants must be distinguished.
Find a shortest word having two distinct parse trees.
Tap to enlarge
Answer & solution

Answer: bcde (length 4) (no official key was published)

Bcde has two different parses. The difference is whether the initial b and final e are supplied by the outer S rule or by P and R:

Parse 1: S⇒PQR, with P⇒bPc⇒bc, Q⇒ε, R⇒dRe⇒de. Concatenation gives bcde.
Parse 2: S⇒bSe⇒bPQRe, with P⇒ε, Q⇒cQd⇒cd, R⇒ε. This also gives bcde.

These are distinct parse trees because the first root uses S→PQR and the second root uses S→bSe. Merely changing the order of independent rewrites would not demonstrate ambiguity; here the production at the root really differs.

To prove shortestness, fix a word's exponents. Every parse is uniquely determined by t, the number of outer S→bSe applications, since p=i−t, q=j−i+t and r=m−t determine the unique chains in P,Q,R. Two parses require two possible t values. If consecutive valid choices are t and t+1, the lower choice has p≥1 and r≥1. Those two recursive chains alone contribute at least two b/c symbols and two d/e symbols, so the total length is at least four. The displayed word attains that bound.

Original paper, page 7