Skip to content
GATEiit

GATE CSE 2018 Q47: Token sequence from longest-match lexer on bbaacabc

Syllabus topics: Lexical analysis, parsing and syntax-directed translation; Regular expressions and finite automata

Question

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

T1: a?(b∣c)∗a

T2: b?(a∣c)∗b

T3: 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?

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation

Answer: D (official GATE 2018 final answer key)

Concept used: Maximal munch: at each step the lexer picks the longest prefix of the remaining input that matches any token pattern.

Concept notes

Loading notes…

First step: From the start of bbaacabc, find for each pattern the longest prefix it matches (note T3 allows any run of a/b before a c), pick the longest, and repeat on the rest.

Full step-by-step solution

Loading…

Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.

More questions on Lexical analysis, parsing and syntax-directed translation

Every PYQ on this topic →

Share this question

WhatsAppDownload image

Source: official GATE 2018 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key