Skip to content
Official PYQ · 2022

GATE CS 2022 Q19 — Parsing (LL, LR)

Explain with AI

Consider the augmented grammar with {+,∗,(,),id}\{ +, {\ast}, (,),\text{id} \} as the set of terminals.

  • S’→SS’ \rightarrow S
  • S→S+R  ∣  RS \rightarrow S + R\; |\; R
  • R→R∗P  ∣  PR \rightarrow R {\ast} P \;| \;P
  • P→(S)  ∣  idP \rightarrow (S)\; |\; \text{id}

If I0I_{0} is the set of two LR(0)\textit{LR}(0) items {[S’→S.],[S→S.+R]}\{ [S’ \rightarrow S.], [S \rightarrow S. + R] \}, then goto(closure(I0),+)\textit{goto(closure}(I_{0}), +) contains exactly ______________ items.

View worked solution

Answer: 5

To find the number of items in goto(closure(I0),+)\text{goto}(\text{closure}(I_0), +), we proceed step-by-step:

Step 1: Compute closure(I0)\text{closure}(I_0)

Given I0={[S’→S.],[S→S.+R]}I_0 = \{ [S’ \rightarrow S.], [S \rightarrow S. + R] \}.

In both items, the dot is not placed before any non-terminal (it is at the end in the first item, and before the terminal ++ in the second). Therefore, no expansion is needed:

closure(I0)=I0={[S’→S.],[S→S.+R]}\text{closure}(I_0) = I_0 = \{ [S’ \rightarrow S.], [S \rightarrow S. + R] \}

Step 2: Compute goto(closure(I0),+)\text{goto}(\text{closure}(I_0), +)

The goto\text{goto} function on terminal ++ shifts the dot over ++ in any item where the dot is immediately before ++. The only such item in closure(I0)\text{closure}(I_0) is [S→S.+R][S \rightarrow S. + R].

Shifting the dot gives the kernel item:

Kernel={[S→S+.R]}\text{Kernel} = \{ [S \rightarrow S + . R] \}

Step 3: Compute the closure of the kernel item

We apply the closure rules to the kernel set:

  1. Start with the kernel item: [S→S+.R][S \rightarrow S + . R]
  2. Since the dot is before the non-terminal RR, we add all RR-productions with the dot at the beginning: [R→.R∗P][R \rightarrow . R * P] [R→.P][R \rightarrow . P]
  3. In [R→.P][R \rightarrow . P], the dot is before the non-terminal PP, so we add all PP-productions with the dot at the beginning: [P→.(S)][P \rightarrow . (S)] [P→.id][P \rightarrow . \text{id}]

Combining all these, the final set of items in goto(closure(I0),+)\text{goto}(\text{closure}(I_0), +) is:

{[S→S+.R],[R→.R∗P],[R→.P],[P→.(S)],[P→.id]}\left\{ \begin{aligned} &[S \rightarrow S + . R], \\ &[R \rightarrow . R * P], \\ &[R \rightarrow . P], \\ &[P \rightarrow . (S)], \\ &[P \rightarrow . \text{id}] \end{aligned} \right\}

Counting the items, we find there are exactly 5 items in the set.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Parsing (LL, LR) questions