Parsing & Syntax Analysis

Answer & solution
Official answer: C, D
Write i for the complete terminal phrase “if b then” and e for “else.” In G1, the string i i a e a has two parses: i [i a e a] attaches the else to the inner if, while [i [i a] e a] attaches it to the outer if. This is the dangling-else ambiguity.
G2 may resemble a matched/unmatched-if grammar, but its M production ends in S, not in an entirely matched nonterminal. Consequently an unmatched if can occur in that else branch. A concrete ambiguous string is i i c e i c e c. Its first parse is i [i c e (i c e c)]: the initial S uses S→iS, and its inner S→M derives i c e (i c e c). Its second parse is i [i c e (i c)] e c: the outer S→M uses M→i M e S, with its then-branch M deriving i c e (i c) and its final S deriving c. The else tokens attach to different if occurrences, yet the terminal string is identical. Thus G2 is ambiguous too.
An LL(1) grammar is unambiguous, so neither grammar can be LL(1). Directly, G1's two if-alternatives have the same FIRST token, and G2's S alternatives both admit strings beginning with if. These create predictive parsing conflicts. Select C,D.