Question

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

S′→SS' \to S
S→S+R∣RS \to S + R \mid R
R→R∗P∣PR \to R * P \mid P
P→(S)∣idP \to ( S ) \mid \text{id}

If I0I_0 is the set of two LR(0) items {[S′→S⋅],[S→S⋅+R]}\{[S' \to S\cdot], [S \to S\cdot + R]\}, then goto(closure(I0),+)\text{goto}(\text{closure}(I_0), +) contains exactly __________ items.

Official answer and solution
Official answer
5
From
Official GATE 2022 answer key

Solution

Compute the closure of the transition from I0I_0 on terminal symbol '+' using standard LR(0) item closure rules.

Given I0={[S′→S⋅],[S→S⋅+R]}I_0 = \{[S' \to S\cdot], [S \to S\cdot + R]\}.

To compute goto(closure(I0),+)\text{goto}(\text{closure}(I_0), +):

  1. Since the dot is followed by end-of-string in [S′→S⋅][S' \to S\cdot] and by the terminal '+' in [S→S⋅+R][S \to S\cdot + R], no non-terminals follow the dot. Thus closure(I0)=I0\text{closure}(I_0) = I_0.
  2. On symbol '+', we shift the dot over '+' in the item [S→S⋅+R][S \to S\cdot + R] to obtain the kernel of the new state:
J={[S→S+⋅R]}J = \{[S \to S + \cdot R]\}
  1. Now compute closure(J)\text{closure}(J):
    • The dot is immediately before non-terminal RR. We add all productions for RR with the dot at the beginning:
      [R→⋅R∗P],[R→⋅P][R \to \cdot R * P], \quad [R \to \cdot P]
    • In [R→⋅P][R \to \cdot P], the dot is before non-terminal PP. We add all productions for PP with the dot at the beginning:
      [P→⋅(S)],[P→⋅id][P \to \cdot ( S )], \quad [P \to \cdot \text{id}]
    • In [R→⋅R∗P][R \to \cdot R * P], the dot is before RR, which is already added.
    • In [P→⋅(S)][P \to \cdot ( S )] and [P→⋅id][P \to \cdot \text{id}], the dots are before terminals '(' and 'id', so no further items are added.

Collecting all items in the state:

  1. [S→S+⋅R][S \to S + \cdot R]
  2. [R→⋅R∗P][R \to \cdot R * P]
  3. [R→⋅P][R \to \cdot P]
  4. [P→⋅(S)][P \to \cdot ( S )]
  5. [P→⋅id][P \to \cdot \text{id}]

There are exactly 5 items.

Final answer

5

Common trap

Forgetting that [R→⋅P][R \to \cdot P] recursively adds the productions for PP.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2022 Computer Science and Information Technology (CS)
Question
29 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.