Question

Consider the following grammar GG

S→F∣HF→p∣cH→d∣c\begin{aligned} S & \to F \mid H \\ F & \to p \mid c \\ H & \to d \mid c \end{aligned}

where S,F,S, F, and HH are non-terminal symbols, p,d,p, d, and cc are terminal symbols. Which of the following statement(s) is/are correct?
S1. LL(1) can parse all strings that are generated using grammar GG
S2. LR(1) can parse all strings that are generated using grammar GG

Choose one option
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 GG is:

S→F∣HF→p∣cH→d∣c\begin{aligned} S & \to F \mid H \\ F & \to p \mid c \\ H & \to d \mid c \end{aligned}

Consider the terminal string c. It has two distinct leftmost derivations:

  1. S  ⟹  F  ⟹  cS \implies F \implies c
  2. S  ⟹  H  ⟹  cS \implies H \implies c
    Since a string in L(G)L(G) has two distinct parse trees (and two distinct leftmost derivations), grammar GG is ambiguous.

It is a fundamental theorem of formal language theory that no ambiguous grammar can be LL(kk) or LR(kk) for any k≥1k \ge 1:

  • LL(1) parsing table will have multiple entries in M[S,c]M[S, c] (S→FS \to F and S→HS \to H).
  • LR(1) parser will have a reduce-reduce conflict on lookahead $ (or lookahead c).

Therefore, neither LL(1) nor LR(1) can parse all strings using grammar GG (Option D).

Final answer

D

Common trap

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.