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的更多相关文章

  1. 4.7.6 Compaction of LR Parsing Tables

    4.7.6 Compaction of LR Parsing Tables A typical programming language grammar with 50 to 100 terminal ...

  2. 4.7.5 Efficient Construction of LALR Parsing Tables

    4.7.5 Efficient Construction of LALR Parsing Tables There are several modifications we can make to A ...

  3. 4.7.4 Constructing LALR Parsing Tables

    4.7.4 Constructing LALR Parsing Tables We now introduce our last parser construction method, the LAL ...

  4. 4.4 Top-Down Parsing

    4.4 Top-Down Parsing Top-down parsing can be viewed as the problem of constructing a parse tree for ...

  5. flex/bison 计算器

    flex %{ #include <stdio.h> #include "mycalc.tab.h" ;} %} %% "+" return ADD ...

  6. (转)Understanding C parsers generated by GNU Bison

    原文链接:https://www.cs.uic.edu/~spopuri/cparser.html Satya Kiran PopuriGraduate StudentUniversity of Il ...

  7. 4.8 Using Ambiguous Grammars

    4.8 Using Ambiguous Grammars It is a fact that every ambiguous grammar fails to be LR and thus is no ...

  8. Lexer and parser generators (ocamllex, ocamlyacc)

    Chapter 12 Lexer and parser generators (ocamllex, ocamlyacc) This chapter describes two program gene ...

  9. mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES

    AutoMySQLBackup备份时,出现mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@' ...

随机推荐

  1. 使用TensorRT加速yolo3

    一.TensorRT支持的模型: TensorRT 直接支持的model有ONNX.Caffe.TensorFlow,其他常见model建议先转化成ONNX.总结如下: 1 ONNX(.onnx) 2 ...

  2. js 技巧 (十)广告JS代码效果大全 【2】

    2.[鼠标感应]     与前面一个代码不同的是,当鼠标移动到广告图片上是可以感应显示另外设置好的广告大图效果,下面就是实现效果所需代码: function bigshow(){     docume ...

  3. day21 03 异常处理

    day21 03 异常处理 1.什么是异常 异常:程序运行时发生错误的信号 错误:语法错误(一般是不能处理的异常) 逻辑错误(可处理的异常) 特点:程序一旦发生错误,就从错误的位置停下来,不再继续执行 ...

  4. json,js中typeof用法详细介绍及NaN、 null 及 undefined 的区别

    JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串 在js使用中的一个函 ...

  5. Bzoj3060 [Poi2012]Tour de Byteotia

    3060: [Poi2012]Tour de Byteotia Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 251  Solved: 161 Des ...

  6. string数据类型操作【四】

    keys *    用于查找所有的key值 exists mykey     #判断该键是否存在,存在返回1,否则返回0. del mykey        删除键(存在就删除返回1,不存在返回为0) ...

  7. sqlserver2008 存储过程使用表参数

    ----首先,我们定义一个表值参数类型,其实就是一个表变量   Create type dbo.tp_Demo_MultiRowsInsert as Table   (   [PName] [Nvar ...

  8. 洛谷 P4181 [USACO18JAN]Rental Service

    P4181 [USACO18JAN]Rental Service 题意翻译 farmer john有N(1≤N≤100,000)头牛,他想赚跟多的钱,所以他准备买牛奶和出租牛.有M(1≤M≤100,0 ...

  9. Ubuntu 16.04 GNOME无法使用拼音输入法问题

    说明:添加好之后重启!不然会出现输入错乱的问题. 参考: http://forum.ubuntu.org.cn/viewtopic.php?t=477765

  10. MySQL主从复制搭建教程收集(待实践)

    先收集一下,后续再搭建测试. https://zhangge.net/4019.html http://www.cnblogs.com/jiangwenju/p/6098974.html http:/ ...