Question
Consider the following grammar
where and are non-terminal symbols, and are terminal symbols. Which of the following statement(s) is/are correct?
S1. LL(1) can parse all strings that are generated using grammar
S2. LR(1) can parse all strings that are generated using grammar
Official answer and solution
- Official answer
- D
- From
- Official GATE 2015 answer key
Solution
Check whether the grammar is ambiguous, which immediately determines LL(1) and LR(1) compatibility.
The given grammar is:
Consider the terminal string c. It has two distinct leftmost derivations:
Since a string in has two distinct parse trees (and two distinct leftmost derivations), grammar is ambiguous.
It is a fundamental theorem of formal language theory that no ambiguous grammar can be LL() or LR() for any :
- LL(1) parsing table will have multiple entries in ( and ).
- LR(1) parser will have a reduce-reduce conflict on lookahead
$(or lookaheadc).
Therefore, neither LL(1) nor LR(1) can parse all strings using grammar (Option D).
D
Assuming LR(1) can parse any grammar with 1 lookahead, forgetting that ambiguity is fatal for both LL and LR parsers.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2015 Computer Science and Information Technology (CS) Session 3
- Question
- 41 of 65, printed as CS Q.31
- 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.