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'@' ...
随机推荐
- Go:map
一.map的创建方式 func main() { // map创建方式1 // 声明后再make var stu1 map[int]string stu1 = make(map[int]string, ...
- Linux中find命令用法大全
Linux 查找命令是Linux系统中最重要和最常用的命令之一.查找用于根据与参数匹配的文件指定的条件来搜索和查找文件和目录列表的命令.查找可以在各种条件下使用,您可以通过权限,用户,组,文件类型,日 ...
- 15Spring AOP基础
为什么需要AOP? 先来看一段代码: package com.cn.spring.aop.helloworld; //加减乘除的接口类 public interface ArithmeticCalcu ...
- 基础知识:if条件、while循环、for循环 相关练习
1.实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! while True: name = input('请输入用户名:') psw = inpu ...
- LeetCode 167. Two Sum II – Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- NOIP2012提高组D1T3 开车旅行
n<=100000个山,每个山有高度,从一个山到另一个山代价为高度差,有A和B两人一起开车,A每次选前进方向的次近山,B选最近,保证山高度不同且如果代价相同的山低的代价算小,每次旅行先A走,然后 ...
- Linux下汇编语言学习笔记16 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Visual C++ 网络编程 笔记
第一章 网络分层模型 OSI模型应用层:服务于应用程序的协议,比如用于域名解析的DNS协议,用于下载界面内容的HTTP协议表示层:处理不同硬件和操作系统之间的差异,确保应用层之间顺利通信 and 加密 ...
- P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
https://www.luogu.org/problem/show?pid=2910 题目描述 Farmer John is on a boat seeking fabled treasure on ...
- Ubuntu 16.04设置rc.local开机启动命令/脚本的方法(通过update-rc.d管理Ubuntu开机启动程序/服务)
注意:rc.local脚本里面启动的用户默认为root权限. 一.rc.local脚本 rc.local脚本是一个Ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令.该脚本位于/et ...