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'@' ...
随机推荐
- vue开发--生成token并保存到本地存储中
首先回顾一下token:token认证是RESTFUL.api的一个很重要的部分,通过token认证和token设置,后端会有一个接口传给前台: http://localhost/yiiserver/ ...
- Caused by: java.io.IOException: 您的主机中的软件中止了一个已建立的连接。
异常详情 2017-07-16 10:55:26,218 ERROR [500.jsp] - java.io.IOException: 你的主机中的软件中止了一个已建立的连接. org.apache. ...
- 关于.NET玩爬虫这些事 【初码干货】
这几天在微信群里又聊到.NET可以救中国但是案例太少不深的问题,我说.NET玩爬虫简直就是宇宙第一,于是大神朱永光说,你为何不来写一篇总结一下? 那么今天就全面的来总结一下,在.NET生态下,如何玩爬 ...
- linux 下CPU数量、核心数量、是否支持超线程的判断
判断依据:1.具有相同core id的cpu是同一个core的超线程.2.具有相同physical id的cpu是同一颗cpu封装的线程或者cores. 英文版:1.Physical id and c ...
- hihoCoder#1077 RMQ问题再临-线段树
原题地址 终于做到线段树的题了,因为建树.更新.查询都是递归操作,所以其实挺好写的. 用数组存的树,记得MAX_NODE开成两倍叶节点数大小,否则RE啊..不要问我是怎么知道的. 代码: #inclu ...
- hdu 1075 字典树
#include<stdio.h> #include<iostream> struct node { int num,i; node *a[27]; char s[20];// ...
- Quartz.net框架使用
概述:Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了 ...
- CF676E:The Last Fight Between Human and AI
人类和电脑在一个多项式上进行博弈,多项式的最高次项已知,一开始系数都不确定.电脑先开始操作,每次操作可以确定某次项的系数,这个系数可以是任意实数.给出一个博弈中间状态,最后如果这个多项式被x-K整除就 ...
- Linux下汇编语言学习笔记21 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- 斯特林(Stirling)公式 求大数阶乘的位数
我们知道整数n的位数的计算方法为:log10(n)+1n!=10^m故n!的位数为 m = log10(n!)+1 lgN!=lg1+lg2+lg3+lg4+lg5+................. ...