Question:medium

A lexical analyzer uses the following token definitions
ο‚· π‘™π‘’π‘‘π‘‘π‘’π‘Ÿβ†’[π΄βˆ’π‘π‘Žβˆ’π‘§]
ο‚· 𝑑𝑖𝑔𝑖𝑑→[0 βˆ’9]
ο‚· π‘–π‘‘β†’π‘™π‘’π‘‘π‘‘π‘’π‘Ÿ (π‘™π‘’π‘‘π‘‘π‘’π‘Ÿ | 𝑑𝑖𝑔𝑖𝑑)*
ο‚· π‘›π‘’π‘šπ‘π‘’π‘Ÿβ†’π‘‘π‘–π‘”π‘–π‘‘+
ο‚· 𝑀𝑠→(π‘π‘™π‘Žπ‘›π‘˜ | π‘‘π‘Žπ‘ | 𝑛𝑒𝑀𝑙𝑖𝑛𝑒)+
For the string given below,
π‘₯1 23π‘šπ‘š 78 𝑦 7𝑧 𝑧𝑧5 14𝐴 8𝐻 π΄π‘Žπ‘Œπ‘π·
the number of tokens (excluding 𝑀𝑠) that will be produced by the lexical analyzer
is __________. (answer in integer)

Show Hint

An id token can absorb trailing digits after its first letter, but a number token made of leading digits cannot absorb a following letter, so a new token starts wherever digits are followed by a letter.
Updated On: Aug 3, 2026
Show Solution

Correct Answer: 13

Solution and Explanation

Since \(id\) must begin with a letter and \(number\) must consist purely of digits, a chunk of non-whitespace characters splits into a new token every time the character class changes from digit to letter, but not the other way around (because once an \(id\) starts with a letter, it can absorb trailing digits too). Applying this rule chunk by chunk: \(x1\) begins with a letter so digits after it are absorbed into the same \(id\), giving 1 token. \(23mm\) begins with digits, which form their own \(number\) token, and the letters that follow cannot be absorbed backward, so they form a second, separate \(id\) token, giving 2 tokens. \(78\) is purely numeric, 1 token. \(y\) is a lone letter, 1 token. \(7z\) again splits into a leading \(number\) and a trailing \(id\), 2 tokens. \(zz5\) begins with letters, so the trailing digit gets absorbed into the same \(id\), giving just 1 token. \(14A\) and \(8H\) each split into a leading \(number\) and trailing \(id\), contributing 2 tokens apiece. Finally \(AaYcD\) is entirely letters, forming 1 token. Summing across all nine whitespace-separated chunks: \(1+2+1+1+2+1+2+2+1 = 13\) tokens are produced in total, excluding whitespace.
Was this answer helpful?
0


Questions Asked in GATE CS exam