A lexical analyzer uses the following token definitions
For the string given below,
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:
x1→ id (1 token)23→ number (1 token)mm→ id (1 token)78→ number (1 token)y→ id (1 token)7→ number (1 token)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.zz5→ id (1 token)14→ number (1 token)A→ id (1 token)8→ number (1 token)H→ id (1 token)AaYcD→ id (1 token)
Summing these up, the total number of non-whitespace tokens is:
Free practice · Your answers on this page are not saved.
