IAE Academy

GATE 2024 CS2 Q.40: solution and answer

GATE 2024 · CS2Q.40 · 2 marks · MCQ

Consider the following context-free grammar where the start symbol is and the set of terminals is .



The following is a partially-filled LL(1) parsing table (rows S, A, B; columns a, b, c, d, $).

Which one of the following options represents the CORRECT combination for the numbered cells in the parsing table?

Note: In the options, "blank" denotes that the corresponding cell is empty.

GATE 2024 CS2 question 40: LL(1) parsing table with columns a, b, c, d, and rows S, A, B.Tap to open full size
Figure from the GATE 2024 CS2 paper. It is part of the question.
Figure description

LL(1) parsing table with columns a, b, c, d, and rows S, A, B. Row S: a = S -> AaAb; b = S -> BbBa; c = (1); d = (2); = empty. Row A: a = A -> epsilon; b = (3); c = A -> cS; d = empty; = empty. Row B: a = (4); b = B -> epsilon; c = empty; d = B -> dS; = empty.

  1. (A)

    (1) S -> AaAb (2) S -> BbBa (3) A -> epsilon (4) B -> epsilon

  2. (B)

    (1) S -> BbBa (2) S -> AaAb (3) A -> epsilon (4) B -> epsilon

  3. (C)

    (1) S -> AaAb (2) S -> BbBa (3) blank (4) blank

  4. (D)

    (1) S -> BbBa (2) S -> AaAb (3) blank (4) blank

Answer and solution

ANSWER

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

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

  1. Rule for an LL(1) table: put in column if , or if and .

  2. : can start with or vanish, so we get and then . Hence .

  3. : can start with or vanish, so we get and then . Hence .

  4. Cell (1) is . Since , it holds . Cell (2) is . Since , it holds .

  5. : in the first is followed by and the second by . So . ( occurs nowhere else on a right-hand side.)

  6. Cell (3) is . Since , we place there. Column already shows the same entry.

  7. : in the first is followed by and the second by . So .

  8. Cell (4) is . Since , we place there. The given entry agrees.

  9. So (1) , (2) , (3) , (4) , which is option A.

KEY IDEA

An LL(1) table entry for goes under every terminal in , and, when can derive , also under every terminal in . Here , and .

WATCH OUT FOR

Using only FIRST sets and leaving the -entries blank (options C and D). The -production must also be entered under every terminal of FOLLOW. Another slip is swapping the two productions, forgetting that a nullable leading symbol makes or select the production that begins with or respectively.

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

More Parsing questions