Question

Consider the grammar given below:

S→AaA→BDB→b∣ϵD→d∣ϵ\begin{aligned} S &\to Aa \\ A &\to BD \\ B &\to b \mid \epsilon \\ D &\to d \mid \epsilon \end{aligned}

Let a,b,d,a, b, d, and $\$ be indexed as follows:

Symbol aa bb dd $\$
Index 3 2 1 0

Compute the FOLLOW\text{FOLLOW} set of the non-terminal BB and write the index values for the symbols in the FOLLOW\text{FOLLOW} set in descending order. (For example, if the FOLLOW\text{FOLLOW} set is {a,b,d,$}\{a, b, d, \$\}, then the answer should be 3210)

Answer: ________.

Official answer and solution
Official answer
31
From
Official GATE 2019 answer key

Solution

Calculate FIRST and FOLLOW sets for the grammar symbols according to standard compiler parsing rules.

The given grammar is:

S→AaS \to Aa
A→BDA \to BD
B→b∣ϵB \to b \mid \epsilon
D→d∣ϵD \to d \mid \epsilon

Step 1: Compute FIRST sets:

  • FIRST(D)={d,ϵ}\text{FIRST}(D) = \{d, \epsilon\}
  • FIRST(B)={b,ϵ}\text{FIRST}(B) = \{b, \epsilon\}
  • FIRST(A)\text{FIRST}(A): Since A→BDA \to BD, FIRST(A)\text{FIRST}(A) includes FIRST(B)∖{ϵ}={b}\text{FIRST}(B) \setminus \{\epsilon\} = \{b\}. Because B⇒∗ϵB \Rightarrow^* \epsilon, FIRST(A)\text{FIRST}(A) also includes FIRST(D)={d,ϵ}\text{FIRST}(D) = \{d, \epsilon\}. Thus FIRST(A)={b,d,ϵ}\text{FIRST}(A) = \{b, d, \epsilon\}.

Step 2: Compute FOLLOW sets:

  • For start symbol SS: FOLLOW(S)={$}\text{FOLLOW}(S) = \{\$\}.
  • For non-terminal AA: In S→AaS \to Aa, AA is followed by terminal aa. Hence FOLLOW(A)={a}\text{FOLLOW}(A) = \{a\}.
  • For non-terminal BB: In A→BDA \to BD, BB is followed by DD.
    • FOLLOW(B)\text{FOLLOW}(B) contains (FIRST(D)∖{ϵ})={d}(\text{FIRST}(D) \setminus \{\epsilon\}) = \{d\}.
    • Since D⇒∗ϵD \Rightarrow^* \epsilon, FOLLOW(B)\text{FOLLOW}(B) also contains everything in FOLLOW(A)={a}\text{FOLLOW}(A) = \{a\}.

Therefore:

FOLLOW(B)={a,d}\text{FOLLOW}(B) = \{a, d\}

The given symbol indices are:

a:3,b:2,d:1,$:0a: 3, \quad b: 2, \quad d: 1, \quad \$: 0

For the symbols in FOLLOW(B)={a,d}\text{FOLLOW}(B) = \{a, d\}, the indices are 33 and 11.
Arranging them in descending order gives 31.

Final answer

31

Common trap

Forgetting that D can derive epsilon, which causes symbol 'a' to be missed from FOLLOW(B).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2019 Computer Science and Information Technology (CS)
Question
29 of 65, printed as CS Q.19
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.