Skip to content
Official PYQ · 2026

GATE CS 2026 Set 2 Q25 — Lexical Analysis

Explain with AI

A lexical analyzer uses the following token definitions

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

For the string given below,
x123 mm78 y 7z zz5 14A8H AaYcD x1 \quad 23 \mathrm{~mm} \quad 78 \quad \text{ y } \quad 7 z \quad \text{ zz5 } \quad 14 A \quad 8 H \quad \text{ AaYcD }
the number of tokens (excluding ws) that will be produced by the lexical analyzer is ____\_\_\_\_. (answer in integer)

View worked solution

Answer: 13:13

To find the number of tokens produced by the lexical analyzer (excluding whitespace), we analyze the input string based on the given token definitions:

  • id: Starts with a letter, followed by zero or more letters/digits.
  • number: Consists of one or more digits only.

Let's process the input string sequence by sequence using the principle of longest match:

  1. x1 → id (1 token)
  2. 23 → number (1 token)
  3. mm → id (1 token)
  4. 78 → number (1 token)
  5. y → id (1 token)
  6. 7 → number (1 token)
  7. z → id (1 token)
    Note: Even if written as "7z" without space, "7" is matched as a number and "z" as an identifier because an identifier cannot start with a digit.
  8. zz5 → id (1 token)
  9. 14 → number (1 token)
  10. A → id (1 token)
  11. 8 → number (1 token)
  12. H → id (1 token)
  13. AaYcD → id (1 token)

Summing these up, the total number of non-whitespace tokens is:

1+1+1+1+1+1+1+1+1+1+1+1+1=131 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 13

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Lexical Analysis questions