0% found this document useful (0 votes)
23 views

Goto Action Table Construction

The document discusses algorithms for constructing LR(1) parse tables from a grammar. It describes LR(1) items and how the canonical collection of items is built using the closure and goto procedures. This allows producing action and goto tables to parse strings based on the grammar.

Uploaded by

yoursyash310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Goto Action Table Construction

The document discusses algorithms for constructing LR(1) parse tables from a grammar. It describes LR(1) items and how the canonical collection of items is built using the closure and goto procedures. This allows producing action and goto tables to parse strings based on the grammar.

Uploaded by

yoursyash310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

12/16/19

Constructing Action
and Goto Tables
MICHAEL WOLLOWSKI

Mostly based on ”Engineering a Compiler”

Constructing Action and Goto Tables


The compiler writer can build Action and Goto tables by hand.
However, the table-construction algorithm requires scrupulous
bookkeeping.
It is a prime example of the kind of task that should be automated and
relegated to a computer.
In order to understand the behavior of those programs, we will study one
algorithm that can be used to construct LR(1) parse tables.

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

LR(1) Items for Parenthesis Grammar


Here you see the complete set of LR(1) items generated for the
parentheses grammar listed below.

First and Follow Sets for our Grammar


Follow( Goal ) = {eof}
Follow( List ) = {eof,(}
Follow( Pair) = {eof,(, )}

First( Goal ) = {(}


First( List ) = {(}
First( Pair) = {(}

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.

The Closure Procedure


Finds equivalence class of LR(1) items
◦ s : a set of LR(1) items

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

The Closure Procedure: Example


For the parentheses grammar, the initial item is
[Goal → •List, eof]
Applying closure to that set adds the following items:
1. [List→ •List Pair, eof]
2. [List→ •List Pair, (]
3. [List→ •Pair, eof]
4. [List→ •Pair, (]
5. [Pair→ •(Pair), eof]
6. [Pair→ •(Pair), (]
7. [Pair→ •(), eof]
8. [Pair→ •(), (]
This set is cc0

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

The Procedure Goto


Finds state transitions
◦ s : a set of LR(1) items
◦ x : a terminal or non-terminal symbol

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)

The Procedure Goto: Example


Given cc0, we now compute goto(cc0, ()
This set includes the following items:
1. [Pair→ (•Pair), eof]
2. [Pair→ (•Pair), (]
3. [Pair→ (•), eof]
4. [Pair→ (•), (]
5. [Pair→ •(Pair), )]
6. [Pair→ •(), )]

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

CC for Parentheses Grammar

8
12/16/19

CC for Parentheses Grammar

CC for Parentheses Grammar

9
12/16/19

CC for Parentheses Grammar

CC for Parentheses Grammar


The closure sets produced for our grammar:

10
12/16/19

DFA of ccis

Producing the Action and Goto Tables


Each cci becomes a state.
Shift:
◦ An item of the form [A→β•Cg, a] indicates that encountering the terminal
symbol C would be a valid next step toward discovering the nonterminal A.
Either b or g can be e.
◦ It generates a shift item on C in the current state.
◦ The next state for the recognizer is the state generated by computing goto on
the current state with the terminal c.

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

Producing the Action and Goto Tables


Accept:
◦ An item of the form [S’→S•, eof] where S' is the goal symbol indicates the
accepting state for the parser.
◦ This item generates an accept action on eof in the current state.

Producing the Action and Goto Tables


Action and Goto tables for our grammar:

12
12/16/19

Producing the Action and Goto Tables


Notice that:
◦ there are no shift/reduce actions associated with any of the non-terminals in
the grammar.
◦ the CC table tells us transitions outright.
◦ it does not tell us shift, reduce or accept actions.
◦ an application of a rule does not change state per se. As such our CC table
has no state change information for them.
◦ the number associated with each rule is the rule number, not the state
number.

13

You might also like