A lexical analyzer uses the following patterns to recognize three tokens , and over the alphabet .
Note that ‘’ means or occurrence of the symbol Note also that the analyzer outputs the token that matches the longest possible prefix.
If the string is processed by the analyzer, which one of the following is the sequence of tokens it outputs?
View worked solution
Answer: D
To find the sequence of tokens, we process the input string from left to right, matching the longest possible prefix at each step.
Step 1: First Token Match
We analyze the prefixes of starting from the left:
bmatches (since , , ending with ).bbmatches (since , , ending with ).bbamatches (since , , ending with ).bbaacmatches (since , , ending with ).
No longer prefix matches any of the patterns because:
bbaaca(ends in ) cannot match because cannot contain the internal 's.bbaacab(ends in ) cannot match because cannot contain the internal .bbaacabc(ends in ) cannot match because cannot contain the internal .
Thus, the longest prefix matched is bbaac, which corresponds to token .
Step 2: Second Token Match
The remaining string is . We analyze its prefixes:
amatches (since , , ending with ).abmatches (since , , ending with ).abcmatches (since , , ending with ).
The longest prefix matched is the entire remaining string abc, which corresponds to token .
Hence, the sequence of tokens output by the analyzer is .
Free practice · Your answers on this page are not saved.
