GATE Computer ScienceVery hardGATE CSE

A lexer uses longest match, with ties broken in favour of the rule listed first. Rules in order: IF = "if"; ELSE = "else"; ID = [a-z][a-z0-9]*; NUM = [0-9]+; LE = "<="; NE = "<>"; LT = "<"; EQ = "="; blanks are skipped. For the input iffy if x1<=7<>y9 else2 else<3 how many tokens are produced and how many of them are ID tokens?

  1. A. 11 tokens, 3 IDs
  2. B. 10 tokens, 4 IDs
  3. C. 12 tokens, 5 IDs
  4. D. 11 tokens, 4 IDs

Correct answer: D. 11 tokens, 4 IDs

Tokens: ID(iffy) IF(if) ID(x1) LE(<=) NUM(7) NE(<>) ID(y9) ID(else2) ELSE(else) LT(<) NUM(3). "iffy" and "else2" are longer ID matches than the keywords, while "if" and "else" tie and go to the earlier keyword rules.

More Compiler Design questions

Exams that ask this topic