4.7.3 Canonical LR(1) Parsing Tables
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的更多相关文章
- 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 ...
- 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 ...
- 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 Top-Down Parsing
4.4 Top-Down Parsing Top-down parsing can be viewed as the problem of constructing a parse tree for ...
- flex/bison 计算器
flex %{ #include <stdio.h> #include "mycalc.tab.h" ;} %} %% "+" return ADD ...
- (转)Understanding C parsers generated by GNU Bison
原文链接:https://www.cs.uic.edu/~spopuri/cparser.html Satya Kiran PopuriGraduate StudentUniversity of Il ...
- 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 ...
- Lexer and parser generators (ocamllex, ocamlyacc)
Chapter 12 Lexer and parser generators (ocamllex, ocamlyacc) This chapter describes two program gene ...
- 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'@' ...
随机推荐
- php - namespace篇
之前没有系统学习过PHP语言,直接上手TP框架了,所以认为namespace和use是TP框架的一部分,最近学习语言模块的时候遇到了这个问题,所以汇总了一下. PHP中命名空间可以解决两类问题: 用户 ...
- 指定PING的网卡
struct ifreq ifr; // 绑定在eth0上 memset( &ifr, , sizeof( struct ifreq ) ); snprintf( ifr.ifr_name, ...
- STM32——GPIO口的八种工作模式
GPIO的输入工作模式1——输入浮空模式: GPIO_Mode_IN_FLOATING =0x04 工作原理:配置完相应寄存器为此工作模式后,高低电平信号通过1处的IO口输入进去,由于寄存器配置了的缘 ...
- this关键字的由来及使用
Student.java /* * 学生类 * * 起名字我们要求做到见名知意. * * 如果有局部变量名和成员变量名相同,在局部使用的时候,采用的是就近原则. * *我们有没有办法吧局部变量的nam ...
- 本地==〉Github(push)
[概述] Git中的项目是本地的,为了可以协同工作.需要将项目推送到GitHub服务器上. [步骤] 1) 第一步:创建项目 2) 第二步:在github上创建一个同名的空项目 ①选择Your rep ...
- stm32 调试时卡在LDR R0, =SystemInit
网上找到的可能的原因 堆栈空间默认的太小 默认startup_stm32f10x_hd.s中 Stack_Size EQU 0x00000400,如果改大之后,可能调试就可以正常运行. 出现最多的情况 ...
- Webdriver测试脚本2(控制浏览器)
Webdriver提供了操作浏览器的一些方法,例如控制浏览器的大小.操作浏览器前进和后退等. 控制浏览器窗口大小 有时候我们希望能以某种浏览器尺寸打开,让访问的页面在这种尺寸下运行.例如可以将浏览器设 ...
- HDU 2897 经典巴什博弈
从n个石子中每次取p~q个,求先手能否获胜 可以先列举一部分数据,然后观察可得总是在p+q中循环,所以只要用n对p+q取模就好了 #include <cstdio> #include &l ...
- codevs1792 分解质因数
题目描述 Description 编写一个把整数N分解为质因数乘积的程序. 输入描述 Input Description 输入一个整数 N 输出描述 Output Description 输出 分解质 ...
- hdu - 1068 Girls and Boys (二分图最大独立集+拆点)
http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) ...