A lexical analyzer uses the following patterns to recognize three tokens T₁,…

2021

A lexical analyzer uses the following patterns to recognize three tokens T₁, T₂ and T₃ over the alphabet {a, b, c}.

T₁ : a?(b | c)*a
T₂ : b?(a | c)*b
T₃ : c?(b | a)*c

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

  1. A.

    T₁ T₂ T₃

  2. B.

    T₁ T₁ T₃

  3. C.

    T₂ T₁ T₃

  4. D.

    T₃ T₃

  5. E.

    Question not attempted

Attempted by 82 students.

Show answer & explanation

Correct answer: D

Check longest prefix:

  • *T₃: c?(b|a)c → matches strings ending with c

    • Prefix "bbaac" (valid)

  • Try longer like "bbaacabc" (contains extra c inside → invalid)

Other options:

  • T₁ gives max length 3 ("bba")

  • T₂ gives max length 1 ("b")

Longest match = "bbaac" → T₃

🔹 Remaining string: "abc"

Now match again:

  • *T₃: c?(b|a)c

    • "abc" (ends with c, middle only a/b)

  • *T₂: b?(a|c)b

    • "ab" but shorter

Longest match = "abc" → T₃

Explore the full course: Bpsc