Skip to content
Official PYQ · 2018

GATE CS 2018 Q37 — Lexical Analysis

Explain with AI

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 00 or 11 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?

Choose one option
View worked solution

Answer: D

To find the sequence of tokens, we process the input string S=bbaacabcS = \text{bbaacabc} from left to right, matching the longest possible prefix at each step.

Step 1: First Token Match

We analyze the prefixes of S=bbaacabcS = \text{bbaacabc} starting from the left:

  • b matches T2T_2 (since b?=bb? = b, (a∣c)∗=ϵ(a \mid c)^* = \epsilon, ending with bb).
  • bb matches T2T_2 (since b?=bb? = b, (a∣c)∗=ϵ(a \mid c)^* = \epsilon, ending with bb).
  • bba matches T1T_1 (since a?=ϵa? = \epsilon, (b∣c)∗=bb(b \mid c)^* = bb, ending with aa).
  • bbaac matches T3T_3 (since c?=ϵc? = \epsilon, (b∣a)∗=bbaa(b \mid a)^* = bbaa, ending with cc).

No longer prefix matches any of the patterns because:

  • bbaaca (ends in aa) cannot match T1T_1 because (b∣c)∗(b \mid c)^* cannot contain the internal aa's.
  • bbaacab (ends in bb) cannot match T2T_2 because (a∣c)∗(a \mid c)^* cannot contain the internal bb.
  • bbaacabc (ends in cc) cannot match T3T_3 because (b∣a)∗(b \mid a)^* cannot contain the internal cc.

Thus, the longest prefix matched is bbaac, which corresponds to token T3T_3.

Step 2: Second Token Match

The remaining string is S′=abcS' = \text{abc}. We analyze its prefixes:

  • a matches T1T_1 (since a?=ϵa? = \epsilon, (b∣c)∗=ϵ(b \mid c)^* = \epsilon, ending with aa).
  • ab matches T2T_2 (since b?=ϵb? = \epsilon, (a∣c)∗=a(a \mid c)^* = a, ending with bb).
  • abc matches T3T_3 (since c?=ϵc? = \epsilon, (b∣a)∗=ab(b \mid a)^* = ab, ending with cc).

The longest prefix matched is the entire remaining string abc, which corresponds to token T3T_3.

Hence, the sequence of tokens output by the analyzer is T3T3T_3T_3.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Lexical Analysis questions