Goto Action Table Construction
Goto Action Table Construction
Constructing Action
and Goto Tables
MICHAEL WOLLOWSKI
1
12/16/19
LR(1) Items
Represent potential handles and look-ahead symbols
An LR(1) item [A → β • g, a] consists of:
◦ A production A → bg
◦ A placeholder • that indicates the position of the stacktop in
the productions rhs
◦ A specific terminal symbol a as a lookahead symbol.
LR(1) Items
The position of placeholder • distinguishes among the following
three cases:
◦ [A → • bg, a] indicates that an A would be valid and that
recognizing a b next would be one step toward discovering an A.
We call such an item a possibility, because it represents a
possible completion for the input already seen.
◦ [A → b • g, a] indicates that the parser has progressed from
the state [A → • bg, a] by recognizing b. The b is consistent
with recognizing an A. One valid next step would be to
recognize a g. We call such an item partially complete.
◦ [A → bg •, a] indicates that the parser has found bg in a
context where an A followed by an a would be valid. If the
look ahead symbol is a, then the item is a handle and the
parser can reduce bg to A. Such an item is complete.
2
12/16/19
3
12/16/19
Canonical Collection
A Canonical Collection CC of a set of LR(1) items is a
model of all transitions that can occur, beginning at
the start state.
CC = {CC0, CC1, …, CCn}
Each CCi
◦ is a set of LR(1) items
◦ will represents a parser state
Two operations are used to calculate them:
◦ Closure
◦ Goto
Q2
Closure
The closure operation completes a state.
Given a core set of LR(1) items, it adds to that set any
related LR(1) items that they imply.
For example, any set that contains Goal → List may
also contain the productions that derive a List.
Thus, we may add items [List → •List Pair, eof] and
[List → • Pair, eof] to the list containing
[Goal → •List, eof].
Q2
4
12/16/19
Closure
To simplify the task of finding the goal symbol, we require
that the grammar have a unique goal symbol that does not
appear on the right-hand side of any production.
The item [Goal → •List, eof] represents the parser's initial
state for the parentheses grammar.
Every valid parse recognizes Goal followed by eof.
This item forms the core of the first state in CC, labelled cc0.
If the grammar has multiple productions for the goal symbol,
each of them generates an item in the initial core of cc0.
closure(s)
while (s is changing)
for each item [A→β•Cd, a] in s
for each production C → γ
for each b in First(da)
s ← s ∪ {[C →•γ,b]}
return s
5
12/16/19
Goto
To model the transition that the parser would make from a
given state on some grammar symbol, x, the algorithm
computes the set of items that would result from recognizing
an x.
To do so, the algorithm selects the subset of the current set
of LR(1) items where • precedes x and advances the • past
the x in each of them.
Q2
6
12/16/19
goto(s, x)
moved ← {}
for each item i in s
if i is like [A→β•xd, a] then
moved ← moved ∪ { [A→βx•d, a] }
return closure(moved)
7
12/16/19
Algorithm to Build CC
CC0 ← closure({[S’ → •S, eof]})
CC ← { CC0 }
while (new sets still being added to CC)
for each unmarked set CCj in CC
mark CCj as processed
for each X following a • in an item of CCj
temp ← goto(CCj, X)
if (temp not in CC)
then CC ← CC ∪ {temp}
record transition from CCj to temp on X
Either a terminal
or a non-terminal
8
12/16/19
9
12/16/19
10
12/16/19
DFA of ccis
Reduce:
◦ An item of the form [A→β•, a] indicates that the parser has recognized a b
and if the lookahead is a, then the item is a handle.
◦ It generates a reduce item for the production A→β on a in the current state.
11
12/16/19
12
12/16/19
13