Question

Consider the following grammar:

P→xQRSP \to x Q R S
Q→yz∣zQ \to y z \mid z
R→w∣ϵR \to w \mid \epsilon
S→yS \to y

What is FOLLOW(Q)\text{FOLLOW}(Q)?

Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2017 answer key

Solution

Compute FOLLOW(Q)\text{FOLLOW}(Q) using the standard First and Follow set construction rules for context-free grammars.

Given grammar productions:

P→xQRSP \to x Q R S
Q→yz∣zQ \to y z \mid z
R→w∣ϵR \to w \mid \epsilon
S→yS \to y

Non-terminal QQ appears on the right-hand side of P→xQRSP \to x Q R S.
By the definition of FOLLOW sets, FOLLOW(Q)\text{FOLLOW}(Q) contains everything in FIRST(RS)\text{FIRST}(R S) except ϵ\epsilon.

Compute FIRST(R)\text{FIRST}(R) and FIRST(S)\text{FIRST}(S):

FIRST(R)={w,ϵ}\text{FIRST}(R) = \{w, \epsilon\}
FIRST(S)={y}\text{FIRST}(S) = \{y\}

Since ϵ∈FIRST(R)\epsilon \in \text{FIRST}(R):

FIRST(RS)=(FIRST(R)∖{ϵ})∪FIRST(S)={w}∪{y}={w,y}\text{FIRST}(R S) = (\text{FIRST}(R) \setminus \{\epsilon\}) \cup \text{FIRST}(S) = \{w\} \cup \{y\} = \{w, y\}

Since FIRST(S)\text{FIRST}(S) does not contain ϵ\epsilon, FIRST(RS)\text{FIRST}(R S) does not derive ϵ\epsilon. Therefore:

FOLLOW(Q)={w,y}\text{FOLLOW}(Q) = \{w, y\}

This matches Option C.

Final answer

C

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

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 1
Question
17 of 65, printed as CS Q.17
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.