GATE Computer ScienceHardGATE CS (PYQ pattern)

A scanner has two token rules, R1 = a b* and R2 = b c. It always takes the longest prefix matching some rule and never backtracks over a token it has chosen. For which inputs does it report an error even though the input can be split into a sequence of tokens? I. abbc II. bcab III. abc

  1. A. I only
  2. B. I, II and III
  3. C. II and III only
  4. D. I and III only

Correct answer: D. I and III only

I: the scanner takes abb (R1) and is left with c, which no rule matches; yet ab | bc is a valid split. II: bc then ab: success. III: it takes ab and is stuck at c, though a | bc works. Maximal munch is greedy, so a valid tokenisation can exist where it fails.

More Compiler Design questions

Exams that ask this topic