GATE / CS ARCHIVE

GATE CSE 2018 / QUESTION 37

Longest-prefix lexical analysis

Compiler Design · Lexical Analysis · Full GATE CSE 2018 paper

GATE 2018, Q37 2 marks · MCQ
Report issue

Lexical Analysis

Question 37, GATE 2018: Tokens are T1=a?(b|c)*a, T2=b?(a|c)*b, T3=c?(b|a)*c, where ? is optional. Scan bbaacabc using longest-prefix matching. Choices A T1,T2,T3; B T1,T1,T3; C T2,T1,T3; D T3,T3.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Method

At the first position T1 can consume bba: its final a ends the token before the next a. T2 can consume bb but cannot include both leading b symbols in its middle (a|c)*. T3 consumes bbaac; it cannot consume the entire input because its middle cannot contain the earlier c. Remove that five-character prefix and rescan abc, which T3 consumes completely. Longest match is decided afresh at each position, rather than choosing the earliest token pattern that can match anything.

Solution

On bbaacabc, the longest first token is bbaac, matching T3=(optional c)(a|b)*c. T1 matches only bba and T2 only bb at that position. The remaining abc also matches T3 in full, longer than the alternatives. Therefore the output is T3, T3, D.

Original paper, page 15 · Answer key