GATE Computer ScienceHardGATE CS

A scanner generator is given the rules, in this order: (1) if → IF; (2) [a-z][a-z0-9]* → ID; (3) [0-9]+ → NUM. It uses the longest match and, among rules matching the same length, the earliest rule. White space is skipped. For the input if iffy if1 if 12ab 7 iff what are the numbers of IF, ID and NUM tokens?

  1. A. (2, 3, 2)
  2. B. (3, 3, 2)
  3. C. (2, 4, 2)
  4. D. (1, 5, 2)

Correct answer: C. (2, 4, 2)

if → IF (tie with rule 2, earlier rule wins); iffy, if1 → ID; if → IF; 12ab → NUM(12) then ID(ab); 7 → NUM; iff → ID. So IF = 2, ID = 4 (iffy, if1, ab, iff), NUM = 2 (12, 7).

More Compiler Design questions

Exams that ask this topic