GATE / CS ARCHIVE

GATE CSE 2025 SET 2 / QUESTION 51

GATE CSE 2025 Set 2 Q51

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2025 Set 2 paper

GATE 2025 Set 2, Q51 2 marks · MSQ
Report issue

Parsing & Syntax Analysis

Question 51, GATE 2025: The terminals are if,then,else,a,b,c. Two grammars are:
G1: S→if E then S | if E then S else S | a; E→b.
G2: S→if E then S | M; M→if E then M else S | c; E→b.
Select true statements: A. G1 not LL(1), G2 LL(1); B. G1 LL(1), G2 not LL(1); C. neither LL(1); D. both ambiguous.
Tap to enlarge
Select all that apply
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.

Original paper, page 39 · Answer key