IAE Academy

GATE 2026 CS2 Q.35: solution and answer

GATE 2026 · CS2Q.35 · 1 mark · NAT

A lexical analyzer uses the following token definitions

For the string given below,

x1 23mm 78 y 7z zz5 14A 8H AaYcD

the number of tokens (excluding ) that will be produced by the lexical analyzer is ______. (answer in integer)

See the question in the paper

Answer and solution

ANSWER

The lexical analyzer produces 13 tokens, excluding white space.

Official answer: 13Source: Official answer key, GATE 2026 CS2 (IIT Guwahati)

  1. The analyzer always takes the longest match from the current position. An must start with a letter and may then continue with letters or digits. A is a run of digits only.

  2. White space is skipped, because is excluded from the count. Split the string at the blanks: x1, 23mm, 78, y, 7z, zz5, 14A, 8H, AaYcD.

  3. x1 begins with a letter, so the whole of it is one . Tokens: 1.

  4. 23mm begins with a digit, so it cannot be an . The longest is 23, because m is not a digit. Then mm is one . Tokens: 2.

  5. 78 is one . Tokens: 1.

  6. y is one . Tokens: 1.

  7. 7z: the longest is 7, then z is an . Tokens: 2.

  8. zz5 begins with a letter, and a digit may follow inside an , so it is one . Tokens: 1.

  9. 14A: 14 is a , then A is an . Tokens: 2.

  10. 8H: 8 is a , then H is an . Tokens: 2.

  11. AaYcD is all letters, so it is one . Tokens: 1.

  12. Total: .

KEY IDEA

A lexer takes the longest match at each position. An identifier must start with a letter, while a number is digits only, so a lexeme that starts with a digit splits where the digits end.

WATCH OUT FOR

Treating a lexeme such as 23mm or 7z as a single token. An cannot start with a digit, so each of these splits into a followed by an . Another slip is splitting zz5 into two tokens, when a digit is allowed inside an after its first letter.

From the GATE 2026 CS2 paper. Every question of this paper · Download the full paper · All 8 Lexical analysis questions, with notes

More Lexical analysis questions