Question

A lexical analyzer uses the following token definitions

  • letter→[A−Za−z]letter \rightarrow [A-Za-z]
  • digit→[0−9]digit \rightarrow [0-9]
  • id→letter(letter∣digit)∗id \rightarrow letter (letter \mid digit)^*
  • number→digit+number \rightarrow digit^+
  • ws→(blank∣tab∣newline)+ws \rightarrow (blank \mid tab \mid newline)^+

For the string given below,
x1 23mm 78 y 7z zz5 14A 8H AaYcD
the number of tokens (excluding wsws) that will be produced by the lexical analyzer is __________. (answer in integer)

Official answer and solution
Official answer
13
From
Official GATE 2026 answer key

Solution

Apply the longest-match (maximal munch) rule to each whitespace-separated chunk.

By the maximal-munch rule, a chunk starting with a digit matches numbernumber up to the first letter, and the remaining suffix starting with a letter matches idid:

  1. x1 →\rightarrow <id, x1> (1 token)
  2. 23mm →\rightarrow <number, 23>, <id, mm> (2 tokens)
  3. 78 →\rightarrow <number, 78> (1 token)
  4. y →\rightarrow <id, y> (1 token)
  5. 7z →\rightarrow <number, 7>, <id, z> (2 tokens)
  6. zz5 →\rightarrow <id, zz5> (1 token)
  7. 14A →\rightarrow <number, 14>, <id, A> (2 tokens)
  8. 8H →\rightarrow <number, 8>, <id, H> (2 tokens)
  9. AaYcD →\rightarrow <id, AaYcD> (1 token)

Total tokens (excluding wsws) =1+2+1+1+2+1+2+2+1=13= 1 + 2 + 1 + 1 + 2 + 1 + 2 + 2 + 1 = 13.

Final answer

13

Common trap

Counting whitespace-separated words (9) without splitting 23mm, 7z, 14A, and 8H into <number> followed by <id>.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2026 Computer Science and Information Technology - Session 2 (CS2)
Question
35 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.