
104 An Introduction to Compiler Construction in a Java World
Algorithm 3.10 Computing the LR(1) Collection
Input: a context-free grammar G = (N, T, S, P )
Output: the canonical LR(1) collection of states c = {s
0
, s
1
, . . . , s
n
}
Define an augmented grammar G
0
, which is G with the added non-terminal S
0
and added
production rule S
0
::= S, where S is G’s start symbol. The following steps apply to G
0
.
Enumerate the production rules beginning at 0 for the newly added production.
c ← {s
0
} where s
0
= closure({[S
0
::= ·S, # ]})
repeat
for each s in c, and for each symbol X ∈ T ∪ N do
if goto(s, X) 6= ∅ and goto(s, X) /∈ c then
add goto(s, X) to c.
end if
end for
until ...