Similar questions 3 more →
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}.](../../assets/questions/CS-2012-Q62-1.webp)
![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}.](../../assets/questions/CS-2012-Q62-2.webp)
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.