IAE Academy

GATE 2024 CS1 Q.38: solution and answer

GATE 2024 · CS1Q.38 · 2 marks · MCQ

Consider the following grammar , with as the start symbol. The grammar has three incomplete productions denoted by (1), (2), and (3).

The set of terminals is . The FIRST and FOLLOW sets of the different non-terminals are as follows.

, ,

is the set of the three symbols , and the end marker $, and .

Which one of the following options CORRECTLY fills in the incomplete productions?

  1. (A)

    (1) , (2) , (3)

  2. (B)

    (1) , (2) , (3)

  3. (C)

    (1) , (2) , (3)

  4. (D)

    (1) , (2) , (3)

See the question in the paper

Answer and solution

ANSWER

Option A: (1) , (2) , (3) .

Official answer: ASource: Official answer key, GATE 2024 CS1 (IISc Bengaluru)

  1. . The production supplies only , so production (1) must supply both and .

  2. has , which misses . So options B and C are out.

  3. : is nullable, so . This works. It also puts in , as given.

  4. contains neither nor anything else that could add. would add to , and would still have no source. Option D is out.

  5. is the only choice that supplies the in . Production (2) is .

  6. Check (3) . is correct. For , take . Since is nullable, add . From , , and $ is in because is the start symbol. From , . Together these give plus the end marker $, as stated. Also stays : only adds to itself.

  7. Answer: option A.

KEY IDEA

A production for a non-terminal must contribute exactly the missing terminals to its FIRST set, and a nullable non-terminal contributes its FOLLOW set to whatever precedes it. Use FIRST to pick (1) and (2), then verify (3) and the FOLLOW sets.

WATCH OUT FOR

Writing and forgetting that FIRST of a string starting with a nullable must include FIRST() minus and so reaches past it. Also, adding without noticing it puts into FIRST(), which the given set forbids.

From the GATE 2024 CS1 paper. Every question of this paper · Download the full paper · All 13 Parsing questions, with notes

More Parsing questions