Question
Consider two grammars and with the production rules given below:
where are the terminals.
Which of the following option(s) is/are CORRECT?
Official answer and solution
- Official answer
- C and D
- From
- Official GATE 2025 answer key
Solution
Check both grammars for FIRST/FIRST conflicts on terminal if and for dangling-else ambiguity (noting that in , has instead of after else).
- Neither nor is LL(1) (Option C is TRUE):
- In , the two -productions and both start with terminal (FIRST/FIRST conflict).
- In , for non-terminal , , causing a FIRST/FIRST conflict in the LL(1) parsing table.
- Both and are ambiguous (Option D is TRUE):
- is the classic ambiguous dangling-else grammar.
- In the standard unambiguous dangling-else grammar, a matched statement requires both branches of
if-then-elseto be matched (). However, has , which allows to end with an unmatchedifinside itselsebranch! Consequently, the string:has two distinct parse trees in :- Tree 1: , where and .
- Tree 2: , where .
- Since is ambiguous, this also immediately confirms that cannot be LL(1).
Final answer
Options (C), (D)
Common trap
Mistaking for the textbook unambiguous grammar without noticing (instead of ) at the end of .
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2025 Computer Science and Information Technology - Session 2 (CS2)
- Question
- 51 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.