Skip to main content

GATE CSE 2018Compiler Design › Lexical Analysis, Compilation Phases & LinkingVerified

GATE CSE 2018 PYQ: A lexical analyzer uses the following patterns to recognize three tokens T 1 , T 2 T_1, T_2 T 1 ​ , T 2 ​ ,…

A lexical analyzer uses the following patterns to recognize three tokens T1,T2T_1, T_2, and T3T_3 over the alphabet {a,b,c}\{a, b, c\}.

  • T1:a?(b∣c)∗aT_1: a?(b \mid c)^\ast a
  • T2:b?(a∣c)∗bT_2: b?(a \mid c)^\ast b
  • T3:c?(b∣a)∗cT_3: c?(b \mid a)^\ast c

Note that ‘x?x?’ means 0 or 1 occurrence of the symbol x.x. Note also that the analyzer outputs the token that matches the longest possible prefix.

If the string bbaacabcbbaacabc is processed by the analyzer, which one of the following is the sequence of tokens it outputs?

  1. A
    T1T2T3T_1T_2T_3
  2. B
    T1T1T3T_1T_1T_3
  3. C
    T2T1T3T_2T_1T_3
  4. D
    T3T3T_3T_3
    Correct answer

Answer

(D) T 3 T 3 T_3T_3 T 3 ​ T 3 ​

Approach

Use maximal munch: at each step choose the token whose regular expression matches the longest prefix of the remaining input.

By the longest-prefix rule, the first token is $T_3$ with lexeme $bbaac$, and the second token is $T_3$ with lexeme $abc$, yielding $T_3T_3$ (option D).

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.