GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Compute nullable FIRST and FOLLOW sets

4 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Propagate FIRST through nullable prefixes and FOLLOW through suffixes and parent dependencies to a fixed point. Different grammars and requested sets/cardinalities; no inferred missing production task.

1 mark · NAT
Report issue

FOLLOW(B) with a nullable suffix

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

1 mark · MCQ
Report issue

Parsing & Syntax Analysis

Question 17, GATE 2017: Grammar: P→xQRS; Q→yz | z; R→w | ε; S→y. Find FOLLOW(Q). A {R}; B {w}; C {w,y}; D {w,$}.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (13 steps)

1. FOLLOW(Q) contains terminal symbols that can occur immediately after a derivation of Q, not the next nonterminal’s name. Its only occurrence is in P→xQRS.

2. R can produce w, placing w immediately after Q. It can also disappear through ε, exposing S, which produces y.

3. S is not nullable, so Q never reaches the end of this production and no end marker is inherited. FOLLOW(Q)={w,y}, C.

Original paper, page 10 · Answer key

2 marks · MCQ
Report issue

FIRST and FOLLOW with nullable indirect productions

Parsing & Syntax Analysis

Question 52, GATE 2012: Grammar: S→aAbB | bAaB | ε; A→S; B→S. S is the start symbol. The partial predictive table has columns a,b,$: S row=[E1,E2,S→ε]; A row=[A→S,A→S,error]; B row=[B→S,B→S,E3]. ε is empty and $ is the input-end marker.
Find FIRST and FOLLOW of A and B.
(A) FIRST(A)=FIRST(B)={a,b,ε}; FOLLOW(A)={a,b}; FOLLOW(B)={a,b,$}.
(B) FIRST(A)={a,b,$}; FIRST(B)={a,b,ε}; FOLLOW(A)={a,b}; FOLLOW(B)={$}.
(C) FIRST(A)=FIRST(B)={a,b,ε}; FOLLOW(A)={a,b}; FOLLOW(B)=∅.
(D) FIRST(A)=FIRST(B)={a,b}; FOLLOW(A)=FOLLOW(B)={a,b}.
Question 52, GATE 2012: Grammar: S→aAbB | bAaB | ε; A→S; B→S. S is the start symbol. The partial predictive table has columns a,b,$: S row=[E1,E2,S→ε]; A row=[A→S,A→S,error]; B row=[B→S,B→S,E3]. ε is empty and $ is the input-end marker.
Find FIRST and FOLLOW of A and B.
(A) FIRST(A)=FIRST(B)={a,b,ε}; FOLLOW(A)={a,b}; FOLLOW(B)={a,b,$}.
(B) FIRST(A)={a,b,$}; FIRST(B)={a,b,ε}; FOLLOW(A)={a,b}; FOLLOW(B)={$}.
(C) FIRST(A)=FIRST(B)={a,b,ε}; FOLLOW(A)={a,b}; FOLLOW(B)=∅.
(D) FIRST(A)=FIRST(B)={a,b}; FOLLOW(A)=FOLLOW(B)={a,b}.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (14 steps)

S can start with a or b or derive ε; A→S and B→S therefore give FIRST(A)=FIRST(B)={a, b,ε}. In S→aAbB and S→bAaB, A is immediately followed by b and a respectively, so FOLLOW(A)={a, b}. Since S is the start symbol, $ belongs to FOLLOW(S). A→S passes FOLLOW(A) into FOLLOW(S), giving FOLLOW(S)={a, b,$}. B occurs at the end of each nonempty S production, so it inherits FOLLOW(S), and FOLLOW(B)={a, b,$}. B→S adds no new symbols. These are option A.

Check: Nullable propagation is essential: A and B derive ε through S even though neither has an explicit ε production. FOLLOW sets require a fixed point because A→S and B→S propagate symbols back to S, which then propagates them to trailing B.

Original paper, page 15 · Answer key

Descriptive
Report issue

Compute FIRST and FOLLOW

Parsing & Syntax Analysis

Question 16(a), GATE 2001: Start symbol E; productions E→aA | (E), A→+E | *E | ε. Terminals a,(,),+,*.
Question 16(a), GATE 2001: Start symbol E; productions E→aA | (E), A→+E | *E | ε. Terminals a,(,),+,*.
Tap to enlarge
Answer & solution

Answer: FIRST(E)={a,(}; FIRST(A)={+,*,ε}; FOLLOW(E)=FOLLOW(A)={),$}. (no official key was published)

E→aA begins with a; E→(E) begins with (, so FIRST(E)={a,(}. A’s alternatives begin with + and *, or are empty, giving FIRST(A)={+,*,ε}. Put end marker $ into FOLLOW(E) because E is the start symbol. E→(E) adds ) to FOLLOW(E). E→aA puts A at the end, so FOLLOW(E) is included in FOLLOW(A). A→+E and A→*E put E at the end, so FOLLOW(A) is included in FOLLOW(E). Iterating these inclusions reaches the fixed point FOLLOW(E)=FOLLOW(A)={),$}. Neither + nor * follows E in this grammar; those operators start A.

Original paper, page 9