Question
A lexical analyzer uses the following patterns to recognize three tokens T1, T2, and T3 over the alphabet .
Note that ‘x?’ means 0 or 1 occurrence of the symbol x. 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?
Official answer and solution
- Official answer
- D
- From
- Official GATE 2018 answer key
Solution
Trace the longest prefix matching mechanism on the input string against regular expressions , , and .
Given regular expression patterns:
- (ends with , preceded by or , optional initial )
- (ends with , preceded by or , optional initial )
- (ends with , preceded by or , optional initial )
Input string:
Step 1: Match longest prefix starting at position 0 (bbaacabc):
- : Can match
bba(length 3, since , , then ). Next character is , which cannot extend . - : Can match
bb(length 2, , , then ). - : Can match
bbaac(length 5, , , then ).
Comparing lengths, the longest prefix isbbaacof length 5, outputting token .
Step 2: Match remaining substring abc:
- : Ends with , not , cannot match
abc. - : Ends with , not , cannot match
abc. - : For
abc, , , then . Matches entire stringabc(length 3), outputting token .
Thus, the lexical analyzer outputs the sequence , matching option D.
D
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2018 Computer Science and Information Technology (CS)
- Question
- 47 of 65, printed as CS Q.37
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.