IAE Academy

GATE 2023 CS Q.19: solution and answer

GATE 2023 · CSQ.19 · 1 mark · MCQ

Consider the following definition of a lexical token id for an identifier in a programming language, using extended regular expressions:

letter → [A-Za-z]
digit → [0-9]
id → letter (letter | digit)*

Which one of the following Non-deterministic Finite-state Automata with ε-transitions accepts the set of valid identifiers? (A double-circle denotes a final state)

GATE 2023 CS question 19: Four NFAs with ε-transitions, each starting from a start state (arrow in).Tap to open full size
Figure from the GATE 2023 CS paper. It is part of the question.
Figure description

Four NFAs with ε-transitions, each starting from a start state (arrow in). (A) Start state q0 has a 'letter' transition to a double-circle final state q1, and also an ε-transition from q0 to q1 (curved arrow below). q1 has a self-loop labelled 'letter' and 'digit'. (B) Start q0 --letter--> double-circle q1. q1 has an ε-transition to a double-circle state q2 and another ε-transition to a double-circle state q3. q2 has a self-loop labelled 'letter'; q3 has a self-loop labelled 'digit'. (C) Start q0 --letter--> double-circle q1. q1 --ε--> q2 --letter--> q3 --ε--> q5, and q1 --ε--> q4 --digit--> q6 --ε--> q5, where q5 is a non-final state with an ε-transition back to q1. (D) Start q0 --letter--> double-circle q1 --letter--> q2 --digit--> q3, and q3 has an ε-transition back to q1.

  1. (A)

    Figure — NFA (A) as described

  2. (B)

    Figure — NFA (B) as described

  3. (C)

    Figure — NFA (C) as described

  4. (D)

    Figure — NFA (D) as described

Answer and solution

ANSWER

The NFA in option (C) accepts exactly the valid identifiers, so the answer is (C).

Official answer: CSource: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. The target language is : one letter first, then any mix of letters and digits, in any order, of any length (including none).

  2. (A): the start state has an -move to the final state , so the empty string is accepted. Since , (A) is wrong.

  3. (B): after the first letter, the machine takes an -move to one of two separate final states. One loops only on letter, the other only on digit, so it accepts or but never a mix. The string letter digit letter is rejected although it is in , so (B) is wrong.

  4. (D): after the first letter the machine reaches the final state , and from there it can only read letter then digit before an -move returns it to . It accepts . The string letter digit, which is in , is rejected, so (D) is wrong.

  5. (C): the first letter leads to the final state . From two -branches start, one reading letter and one reading digit. Both end in a non-final state that has an -move back to .

  6. So each trip around the loop through reads exactly one letter or one digit, in any order and any number of times, and is final. This is the standard construction for placed after the first letter, so the machine accepts exactly .

  7. Hence the answer is (C).

KEY IDEA

A letter, then a loop of choices (letter | digit) that returns to a final state, is how is built. Check each option for , for a mixed string like letter digit letter, and for a single letter followed by a digit.

WATCH OUT FOR

Choosing (B) because it has both a letter loop and a digit loop. The two loops sit on separate final states, so a letter and a digit can never be mixed after the first letter.

From the GATE 2023 CS paper. Every question of this paper · Download the full paper · All 18 Finite automata questions, with notes

More Finite automata questions