Question

Consider the following context-free grammar where the set of terminals is {a,b,c,d,f}\{a, b, c, d, f\}.

S→daT∣RfS \rightarrow daT \mid Rf
T→aS∣baT∣ϵT \rightarrow aS \mid baT \mid \epsilon
R→caTR∣ϵR \rightarrow caTR \mid \epsilon

The following is a partially-filled LL(1) parsing table.

LL(1) parsing table with numbered target cells
abcdf$
S(1)S -> daT(2)
TT -> aST -> baT(3)T -> eps(4)
RR -> caTRR -> eps

Which one of the following choices represents the correct combination for the numbered cells in the parsing table ("blank" denotes that the corresponding cell is empty)?

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2021 answer key

Solution

Compute the FIRST and FOLLOW sets for the non-terminals S, T, and R, and identify the entries for the numbered cells in the LL(1) parsing table.

The grammar productions are:

  • S→daT∣RfS \rightarrow daT \mid Rf
  • T→aS∣baT∣ϵT \rightarrow aS \mid baT \mid \epsilon
  • R→caTR∣ϵR \rightarrow caTR \mid \epsilon

Compute FIRST sets:

  • FIRST(R)={c,ϵ}\text{FIRST}(R) = \{c, \epsilon\}
  • FIRST(T)={a,b,ϵ}\text{FIRST}(T) = \{a, b, \epsilon\}
  • FIRST(S)={d}∪(FIRST(R)∖{ϵ})∪{f}={c,d,f}\text{FIRST}(S) = \{d\} \cup (\text{FIRST}(R) \setminus \{\epsilon\}) \cup \{f\} = \{c, d, f\}

For non-terminal SS, production S→RfS \rightarrow Rf has FIRST(Rf)={c,f}\text{FIRST}(Rf) = \{c, f\}. Thus, S→RfS \rightarrow Rf is entered in cell [S,c][S, c] (cell 1) and in cell [S,f][S, f] (cell 2).

Compute FOLLOW(T)\text{FOLLOW}(T):

  • From S→daTS \rightarrow daT, FOLLOW(S)⊆FOLLOW(T)\text{FOLLOW}(S) \subseteq \text{FOLLOW}(T). Since SS is start symbol, $∈FOLLOW(S)\$ \in \text{FOLLOW}(S), so $∈FOLLOW(T)\$ \in \text{FOLLOW}(T).
  • From R→caTRR \rightarrow caTR, FIRST(R)∖{ϵ}={c}⊆FOLLOW(T)\text{FIRST}(R) \setminus \{\epsilon\} = \{c\} \subseteq \text{FOLLOW}(T), and FOLLOW(R)⊆FOLLOW(T)\text{FOLLOW}(R) \subseteq \text{FOLLOW}(T).
  • From S→RfS \rightarrow Rf, f∈FOLLOW(R)f \in \text{FOLLOW}(R), which implies f∈FOLLOW(T)f \in \text{FOLLOW}(T).
    Thus FOLLOW(T)={c,f,$}\text{FOLLOW}(T) = \{c, f, \$\}. Production T→ϵT \rightarrow \epsilon is placed under all terminals in FOLLOW(T)\text{FOLLOW}(T), so it appears in cell [T,c][T, c] (cell 3), in cell [T,f][T, f] (already filled in the given table) and in cell [T,$][T, \$] (cell 4).

Therefore, the entries are:
(1) S→RfS \rightarrow Rf, (2) S→RfS \rightarrow Rf, (3) T→ϵT \rightarrow \epsilon, (4) T→ϵT \rightarrow \epsilon, matching option A.

Final answer

A

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

Source

Paper
GATE 2021 Computer Science and Information Technology (CS, Set-1)
Question
41 of 65, printed as CS Q.31
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.