CSC376 · 2081 · 5 marks
Define core item. Compute the LR(1) item sets for the following grammar. S→AAS \rightarrow AAS→AAA→aA∣bA \rightarrow aA \mid bA→aA∣b
Define core item. Compute the LR(1) item sets for the following grammar. S→AAS \rightarrow AAS→AAA→aA∣bA \rightarrow aA \mid bA→aA∣b[5]
LR(1) Item Sets for S → AA, A → aA | b
Given Data
Grammar:
- $S \rightarrow AA$
- $A \rightarrow aA \mid b$
Definition of Core Item
The core of an LR(1) item $[A \rightarrow \alpha.\beta, a]$ is its first component $A \rightarrow \alpha.\beta$, i.e., the underlying LR(0) item with the look-ahead removed. Two LR(1) item sets have the same core if their sets of LR(0) items (ignoring look-aheads) are identical.
Step 1: Augmented Grammar
$$S' \rightarrow S,\quad S \rightarrow AA,\quad A \rightarrow aA,\quad A \rightarrow b$$
Step 2: FIRST Sets
$$\text{FIRST}(S) = {a,b},\qquad \text{FIRST}(A) = {a,b}$$
Step 3: Canonical Collection
I₀ = Closure([S' → .S, $]):
[S' → .S, $]
[S → .AA, $]
[A → .aA, a/b]
[A → .b, a/b]
I₁ = GOTO(I₀, S):
[S' → S., $] (accept)
I₂ = GOTO(I₀, A):
[S → A.A, $]
[A → .aA, $]
[A → .b, $]
I₃ = GOTO(I₀, a):
[A → a.A, a/b]
[A → .aA, a/b]
[A → .b, a/b]
I₄ = GOTO(I₀, b):
[A → b., a/b] (reduce A → b)
I₅ = GOTO(I₂, A):
[S → AA., $] (reduce S → AA)
I₆ = GOTO(I₂, a):
[A → a.A, $]
[A → .aA, $]
[A → .b, $]
I₇ = GOTO(I₂, b):
[A → b., $] (reduce A → b)
I₈ = GOTO(I₃, A):
[A → aA., a/b] (reduce A → aA)
- GOTO(I₃, a) = I₃ (same items, self-loop)
- GOTO(I₃, b) = I₄
I₉ = GOTO(I₆, A):
[A → aA., $] (reduce A → aA)
- GOTO(I₆, a) = I₆ (self-loop)
- GOTO(I₆, b) = I₇
Summary of Transitions
| State | On S | On A | On a | On b |
|---|---|---|---|---|
| I₀ | I₁ | I₂ | I₃ | I₄ |
| I₂ | I₅ | I₆ | I₇ | |
| I₃ | I₈ | I₃ | I₄ | |
| I₆ | I₉ | I₆ | I₇ |
Total: 10 canonical LR(1) item sets (I₀ through I₉).
Note: Unlike LALR, states I₄/I₇ and I₈/I₉ have the same core but different look-aheads, so they remain distinct in the canonical LR(1) collection.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.