Question

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

T1:a?(b∣c)∗aT2:b?(a∣c)∗bT3:c?(b∣a)∗c\begin{aligned} T_1 &: a? (b \mid c)^* a \\ T_2 &: b? (a \mid c)^* b \\ T_3 &: c? (b \mid a)^* c \end{aligned}

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 bbaacabcbbaacabc is processed by the analyzer, which one of the following is the sequence of tokens it outputs?

Choose one option
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 T1T_1, T2T_2, and T3T_3.

Given regular expression patterns:

  • T1:a?(b∣c)∗aT_1: a? (b \mid c)^* a (ends with aa, preceded by bb or cc, optional initial aa)
  • T2:b?(a∣c)∗bT_2: b? (a \mid c)^* b (ends with bb, preceded by aa or cc, optional initial bb)
  • T3:c?(b∣a)∗cT_3: c? (b \mid a)^* c (ends with cc, preceded by bb or aa, optional initial cc)

Input string: bbaacabcbbaacabc

Step 1: Match longest prefix starting at position 0 (bbaacabc):

  • T1T_1: Can match bba (length 3, since a?=εa? = \varepsilon, (b∣c)∗=bb(b \mid c)^* = bb, then aa). Next character is aa, which cannot extend T1T_1.
  • T2T_2: Can match bb (length 2, b?=bb? = b, (a∣c)∗=ε(a \mid c)^* = \varepsilon, then bb).
  • T3T_3: Can match bbaac (length 5, c?=εc? = \varepsilon, (b∣a)∗=bbaa(b \mid a)^* = bbaa, then cc).
    Comparing lengths, the longest prefix is bbaac of length 5, outputting token T3T_3.

Step 2: Match remaining substring abc:

  • T1T_1: Ends with cc, not aa, cannot match abc.
  • T2T_2: Ends with cc, not bb, cannot match abc.
  • T3T_3: For abc, c?=εc? = \varepsilon, (b∣a)∗=ab(b \mid a)^* = ab, then cc. Matches entire string abc (length 3), outputting token T3T_3.

Thus, the lexical analyzer outputs the sequence T3T3T_3 T_3, matching option D.

Final answer

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.