Question:

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: Jul 7, 2026
Show Solution
collegedunia
Verified By Collegedunia

Correct Answer: 13

Solution and Explanation

Step 1: The lexer uses maximal munch, so at each position it matches the longest possible token according to the given definitions: \(id \to letter\,(letter\,|\,digit)^*\) and \(number \to digit^+\).
Step 2: Break the input by whitespace and tokenize each chunk: \(x1\) starts with a letter, so the entire chunk matches one \(id\) token.
Step 3: \(23mm\) starts with digits, so \(23\) matches a \(number\) token, and the remaining \(mm\) matches a separate \(id\) token - 2 tokens.
Step 4: \(78\) is all digits, so it is a single \(number\) token.
Step 5: \(y\) is a single \(id\) token.
Step 6: \(7z\) splits into \(number\) \(7\) and \(id\) \(z\) - 2 tokens.
Step 7: \(zz5\) starts with a letter, so the whole chunk (letters followed by a digit) matches one \(id\) token.
Step 8: \(14A\) splits into \(number\) \(14\) and \(id\) \(A\) - 2 tokens.
Step 9: \(8H\) splits into \(number\) \(8\) and \(id\) \(H\) - 2 tokens.
Step 10: \(AaYcD\) starts with a letter and contains only letters, so it matches one \(id\) token.
Step 11: Adding up: \(1 + 2 + 1 + 1 + 2 + 1 + 2 + 2 + 1 = 13\) tokens excluding whitespace.
Final Answer: \[\boxed{13}\]
Was this answer helpful?
0
0

Top GATE CS Computer Science and IT Engineering Questions

View More Questions

Top GATE CS Compiler Design Questions

View More Questions