码迷,mamicode.com
首页 > 其他好文 > 详细

4.7.3 Canonical LR(1) Parsing Tables

时间:2015-09-04 14:08:49      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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)

4.7.3 Canonical LR(1) Parsing Tables

标签:

原文地址:http://www.cnblogs.com/cuishengli/p/4781591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!