GATE / CS ARCHIVE

GATE CSE 2019 / QUESTION 19

FOLLOW(B) with a nullable suffix

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2019 paper

GATE 2019, Q19 1 mark · NAT
Report issue

Parsing & Syntax Analysis

Question 19, GATE 2019: The grammar is S→Aa, A→BD, B→b|ε, D→d|ε. Symbols a,b,d,$ have indices 3,2,1,0 respectively. Find FOLLOW(B), then concatenate its symbol indices in descending order.
Tap to enlarge
Answer & solution

Official answer: 31

Step through it (13 steps)

Method: use the rule for a nonterminal followed by a possibly nullable suffix.

1. FOLLOW(B) contains terminals that can appear immediately after B in a sentential form, not the terminals that B itself derives. In A→BD, the suffix following B is D.
2. FIRST(D)={d,ε}. Add its non-epsilon terminal d to FOLLOW(B), because D may produce d immediately after B.
3. D can also disappear via D→ε. In that case whatever follows A also follows B. From S→Aa, the terminal a follows A, so FOLLOW(A)={a}; add a to FOLLOW(B).
4. There are no other occurrences contributing more symbols. In particular, $ follows the start symbol S, but the required terminal a after A prevents $ from propagating to A or B. The symbol b is in FIRST(B), not FOLLOW(B), and ε is never a FOLLOW-set member.
5. Thus FOLLOW(B)={a,d}. Their indices are 3 and 1, already in descending order, so concatenate them as 31.

Result: 31. The language generated from S is {a,ba,da,bda}, which also illustrates that a B occurrence is followed by d or a.

Original paper, page 7 · Answer key