Similar questions 2 more →
Semantic Analysis & Type Checking

Answer & solution
Official answer: C
Step through it (8 steps)
Bottom-up parsing of aab first reduces the second a by S→a, printing 2. Then Sb reduces to A, printing 3. Finally aA reduces to S, printing 1. The action order is 2, 3, 1, option C.
Reduction order follows a rightmost derivation in reverse, not the textual order in which productions are listed. A valid derivation is S⇒aA⇒aSb⇒aab. Reversing those steps first collapses the second a into S, then Sb into A, then aA into S. Each semantic action is at the end of its production, so it runs at the corresponding reduction and emits 2,3,1. The successful derivation also rules out syntax error.