GATE Computer ScienceHardGATE CS

A scanner discards comments, which begin with /* and end at the FIRST */ (comments do not nest). Identifiers are letters, and * and / are separate one-character operator tokens. For the input x /* a /* b */ c */ y how many tokens does the scanner return?

  1. A. 2
  2. B. 4
  3. C. 5
  4. D. 6

Correct answer: C. 5

The comment runs from the first /* to the first */, so it covers /* a /* b */. The remaining text c */ y is scanned normally as c, *, /, y, and with the leading x there are 5 tokens. A scanner with nested comments would return only x and y.

More Compiler Design questions

Exams that ask this topic