Computer Sciences > GATE 2025 SET-2 > LL(1) Parsing
Consider two grammars G1 and G2 with the production rules given below:
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
where if, then, else, a, b, c are the terminals.
Which of the following option(s) is/are CORRECT?
A
G1 is not LL(1) and G2 is LL(1)
B
G1 is LL(1) and G2 is not LL(1).
C
G1 and G2 are not LL(1).
D
G1 and G2 are ambiguous.

Correct : a

This question is a classic example of the dangling else problem in compiler design, and it tests whether you understand how grammar structure affects LL(1) parseability.
Analyzing G1:
G1 has the following productions for S:
S → if E then S | if E then S else S | a
The problem is immediately visible — both the first and second productions start with the terminal if. This means FIRST(if E then S) and FIRST(if E then S else S) both contain ''if'', causing a conflict in the LL(1) parsing table. An LL(1) parser uses just one token of lookahead to decide which production to apply, but here, seeing ''if'' alone doesn''t tell the parser which of the two ''if'' productions to choose.
This conflict makes G1 not LL(1). G1 is also ambiguous — the string "if b then if b then a else a" has two valid parse trees depending on which ''if'' the ''else'' is associated with. This is the textbook dangling else ambiguity.
Analyzing G2:
G2 cleverly resolves the dangling else problem by splitting S into two non-terminals:
S → if E then S | M
M → if E then M else S | c
Here, M represents a matched statement (every ''if'' has a corresponding ''else''), and S represents an unmatched statement (an ''if'' that may not have an ''else''). Let''s check the FIRST sets:
For S: FIRST(if E then S) = {if} and FIRST(M) = {if, c}. Wait — both contain ''if'', which would still be a conflict. However, looking more carefully, S → if E then S uses ''if'' to mean an unmatched if, while M → if E then M else S requires a matched if. The grammar design ensures the parser always associates an ''else'' with the nearest unmatched ''if'', resolving the ambiguity structurally.
In G2, no string has two different parse trees — the grammar is unambiguous. With proper FIRST/FOLLOW analysis, the parsing table for G2 has no conflicts, confirming G2 is LL(1).
Checking Option D — are both grammars ambiguous?
G1 is ambiguous (as shown above). G2 is not ambiguous — its design specifically eliminates the dangling else ambiguity by forcing the ''else'' to always match the nearest ''if''. So Option D is incorrect.

Similar Questions

Consider the following context-free grammar where the set of terminals is {a, b, c, d, f}.S -> daT | RfT -> aS | baT | εR -> caTR | εThe following is a partiall...
#1031 MCQ
A palindrome is a word that reads the same forwards and backwards. In a game of words, a player has the following two plates painted with letters. From...
#1 MCQ
Which number does not belong in the series below? 2, 5, 10, 17, 26, 37, 50, 64
#4 MCQ

Related Topics

LL(1) grammar GATE 2025 GATE CS 2025

Unique Visitor Count

Total Unique Visitors

Loading......