bison - 使用野牛时 yacc 嵌入操作的问题
问题描述
我正在尝试从 Brian Kernighan 和 Rob Pike 于 1984 年出版的“The Unix Programming Environment”一书中实现编译器。这本书假定使用 yacc,但我使用的是具有 bison 版本 2.3 的 Mac。本书第 276 页描述了代码问题。
我从使用嵌入式操作(yacc lingo)的语法/操作中收到警告,我认为这与野牛中的中间规则操作相同。
下面是生成警告的语法(行号来自列表):
158: defn: FUNC procname { $2->type=FUNCTION; indef=1; }
159: '(' ')' stmt { code(procret); define($2); indef=0; }
160: | PROC procname { $2->type=PROCEDURE; indef=1; }
161: '(' ')' stmt { code(procret); define($2); indef=0; }
162: ;
163:
164:
165: procname: VAR
166: | FUNCTION
167: | PROCEDURE
168: ;
以下是来自 Bison 的警告:
7 rules never reduced
hoc.y: warning: 4 useless nonterminals and 7 useless rules
hoc.y:158.1-4: warning: useless nonterminal: defn
hoc.y:158.29-59: warning: useless nonterminal: @1
hoc.y:160.29-60: warning: useless nonterminal: @2
hoc.y:47.17-24: warning: useless nonterminal: procname
hoc.y:158.29-59: warning: useless rule: @1: /* empty */
hoc.y:158.7-159.67: warning: useless rule: defn: FUNC procname @1 '(' ')' stmt
hoc.y:160.29-60: warning: useless rule: @2: /* empty */
hoc.y:160.7-161.67: warning: useless rule: defn: PROC procname @2 '(' ')' stmt
hoc.y:165.11-13: warning: useless rule: procname: VAR
hoc.y:166.7-14: warning: useless rule: procname: FUNCTION
hoc.y:167.7-15: warning: useless rule: procname: PROCEDURE
yacc而不是bison可以接受语法/动作吗?如果是这样,野牛有“yacc 模式”吗?如果不是,应该如何重写语法/动作以使野牛可以接受?谢谢
解决方案
这些中等规则的行为在野牛中是完全可以接受的。那不是你的问题。
如评论中所示,此错误消息的最可能原因是您的语法缺少包含 的产生式defn
,这是 的第三个产生式list
:
list: /* nothing */
| list '\n'
| list defn '\n'
| list asgn '\n' { code2(xpop, STOP); return 1; }
| list stmt '\n' { code(STOP); return 1; }
| list expr '\n' { code2(printtop, STOP); return 1; }
| list error '\n' { yyerrok; }
;
您可以使用Wayback Machine aka web.archive.org下载包含 UPE 书中所有代码的 tarball 。我从hoc 的 Wikipedia 条目中获得了该链接,其中还包含指向其他 hoc 实现的链接。
推荐阅读
- c - 是否总是需要返回声明?
- html - CSS Grid - 底部的空白以及如何删除它
- php - 用于从字符串中提取 id 和 name 的正则表达式
- javascript - 在 jquery 插件中使用链式选择器和方法作为选项,对此感到困惑,$(this) 范围
- python-3.x - 调整图像大小时出现 Tkinter 错误
- c# - 将 Angular/Asp.Net Core 2.1 应用程序部署到 AWS:500 错误
- c# - 如何将 Autofac 与 Serilog 包装类一起使用
- haskell - 为什么此约束在这些单独的实例中具有不同的行为?
- vb.net - 调整图片框大小并保持纵横比 (1:1) vb.net
- swift - iOS - 使用泛型重用相同的 TableViewController