Similar questions 7 more →
Parsing & Syntax Analysis
![Question 40, GATE 2024: Grammar: S→AaAb | BbBa, A→cS | ε, B→dS | ε. Missing table cells are (1) M[S,c], (2) M[S,d], (3) M[A,b], (4) M[B,a]. Options: A AaAb,BbBa,ε,ε; B BbBa,AaAb,ε,ε; C AaAb,BbBa,blank,blank; D BbBa,AaAb,blank,blank (right sides for the relevant row nonterminal).](../../assets/questions/CS-2024-S2-Q40-1.webp)
Answer & solution
Official answer: A
Step through it (2 parts)
Approach: Use FIRST for nonempty alternatives and FOLLOW for epsilon alternatives.
1. Derivation. FIRST(AaAb)={c, a}, while FIRST(BbBa)={d, b}. Thus the S-row c cell chooses S→AaAb and the d cell S→BbBa. A occurs before a and b in the first production, so FOLLOW(A)={a, b}; its epsilon production is entered for both lookaheads. Similarly FOLLOW(B)={b, a}, so B→epsilon goes in both a, b cells. Hence cells(1)–(4) are S→AaAb, S→BbBa, A→epsilon, B→epsilon, option A.
2. Explanation and checks. A can disappear, so AaAb can start with a; if A does not disappear it begins with c. The same reasoning yields b or d for BbBa. Each occurrence of A in the S production is followed immediately by a terminal, once a and once b, so both terminals enter FOLLOW(A). The two B occurrences similarly supply b and a. There is no need to put an epsilon production under every lookahead: only FOLLOW lookaheads receive it.