4.7.3 Canonical LR(1) Parsing Tables

We now give the rules for constructing the LR(1) ACTION and GOTO functions from the sets of LR(1) items. These functions are represented by a table, as before. The only difference is in the values of the entries.

Algorithm 4.56 : Construction of canonical-LR parsing tables.

INPUT : An augmented grammar G’.

OUTPUT : The canonical-LR parsing table functions ACTION and GOTO for G’ .

METHOD :

1. Construct C’ = {I0, I1, … , In}, the collection of sets of LR(1) items for G’ .

2. State i of the parser is constructed from Ii. The parsing action for state i is determined as follows.

(a) If [A→α@aβ, b] is in Ii and GOTO(Ii,a) = Ij, then set ACTION[I, a] to “shift j.” Here a must be a terminal.

(b) If [A →α@, a] is in Ii , A ≠ S’, then set ACTION[I, a] to “reduce A ->α@”

(c) If [S’→ S@, $] is in Ii, then set ACTION[I, $] to “accept.”

If any conflicting actions result from the above rules, we say the grammar is not LR(1). The algorithm fails to produce a parser in this case.

3. The goto transitions for state i are constructed for all nonterminals A using the rule: If GOTO(Ii, A) = Ij , then GOTO[I, A] = j.

4. All entries not defined by rules (2) and (3) are made “error.”

5. The initial state of the parser is the one constructed from the set of items containing [S’→ @S, $].

The table formed from the parsing action and goto functions produced by

Algorithm 4.56 is called the canonical LR(1) parsing table. An LR parser using this table is called a canonical-LR(1) parser. If the parsing action function has no multiply defined entries, then the given grammar is called an LR(1) grammar. As before, we omit the “(1)” if it is understood.

Example 4.57 : The canonical parsing table for grammar (4.55) is shown in

Fig. 4.42. Productions 1, 2, and 3 are S →CC , C→cC , and C→d, respectively. □

Every SLR(1) grammar is an LR(1) grammar, but for an SLR(1) grammar the canonical LR parser may have more states than the SLR parser for the same grammar. The grammar of the previous examples is SLR and has an SLR parser with seven states, compared with the ten of Fig. 4.42.

STATE

ACTION

GOTO

c

d

$

S

C

0

s3

s4

 

1

2

1

 

 

acc

 

 

2

s6

s7

 

 

5

3

s3

s4

 

 

8

4

r3

r3

 

 

 

5

 

 

r1

 

 

6

s6

s7

 

 

9

7

 

 

r3

 

 

8

r2

r2

 

 

 

9

 

 

r2

 

 

Figure 4.42: Canonical parsing table for grammar (4.55)