GateAI
Lexical Analysis

GATE CSE 2026 Set 2 · Q35

NAT1 markMedium

Time

A lexical analyzer uses the following token definitions • letter→[A−Za−z]letter \rightarrow [A - Z a - z] • digit→[0−9]digit \rightarrow [0 - 9] • id→letter (letter∣digit)∗id \rightarrow letter \text{ } (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 AaYcDx1\ \ 23mm\ 78\ y\ 7z\ zz5\ 14A\ 8H\ AaYcD the number of tokens (excluding ws) that will be produced by the lexical analyzer is __________. (answer in integer)

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 13

Lexical Analysis

A lexical analyzer breaks an input stream into tokens using the Longest Match (Maximal Munch) rule:

Analyzing each segment of the input string separated by whitespace (ws):

  1. x1 →\rightarrow Starts with a letter, consumes x1 as id (1 token).

  2. 23mm →\rightarrow Starts with digits, maximal match for number is 23 (1 token). The remaining mm starts with a letter, matching id (1 token)   ⟹  2\implies 2 tokens.

  3. 78 →\rightarrow Matches number (1 token).

  4. y →\rightarrow Matches id (1 token).

  5. 7z→\rightarrow7 matches number (1 token), z matches id (1 token)   ⟹  2\implies 2 tokens.

  6. zz5 →\rightarrow Starts with a letter, matches id (1 token).

  7. 14A→\rightarrow14 matches number (1 token), A matches id (1 token)   ⟹  2\implies 2 tokens.

  8. 8H→\rightarrow8 matches number (1 token), H matches id (1 token)   ⟹  2\implies 2 tokens.

  9. AaYcD →\rightarrow Matches id (1 token).

Total count of tokens (excluding whitespace):

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

Thus, the total number of tokens produced is 13.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Lexical Analysis previous year question is Q35 of the GATE CSE 2026 Set 2 paper. Find more GATE CSE PYQs on Lexical Analysis in Compiler Design, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Lexical Analysis PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Lexical Analysis in GATE CSE

Lexical Analysis has 7 GATE CSE PYQs across 6 papers from 2011 to 2026. This is the only 1-mark NAT question among them, and it is the only one in GATE CSE 2026 Set 2.

How GATE tests Lexical Analysis

More from GATE CSE 2026 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2026 Set 2 · Q35 · NAT

A lexical analyzer uses the following token definitions • letter → [A - Z a - z] • digit → [0 - 9] • id → letter (letter digit)* • number → digit+ • ws → (blank tab newline)+ For the string given below, x1 23mm 78 y 7z zz5 14A 8H AaYcD the number of tokens (excluding ws) that will be produced by the lexical analyzer is … (answer in integer)

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.