使用Yacc和Lex生成解析树

发布于 2025-01-27 05:05:36 字数 500 浏览 2 评论 0原文

我花了6个小时为LEX和YACC编写了一份编译器。终端报告警告:输入非终端的空规则,没有操作,但我仍然找不到我的代码出了什么问题。

P -> L | LP
L -> S
S -> ID = E | if C the S | if C then S else S
C -> E>E | E<E | E=E 
E -> E+T | E-T | T
T -> F | T*F | T/F
F -> (E)| ID

ACC零件代码

I've spent 6 hours writing a compiler for the productions below with Lex and Yacc. The terminal report warning: empty rule for typed nonterminal, and no action but I still can't find out what's wrong with my code.

P -> L | LP
L -> S
S -> ID = E | if C the S | if C then S else S
C -> E>E | E<E | E=E 
E -> E+T | E-T | T
T -> F | T*F | T/F
F -> (E)| ID

yacc part code

lex part code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2025-02-03 05:05:36

您收到的警告似乎完全准确:

F:      | …

f是非终端;它有一种类型,第一个替代方案是空的,没有动作。如果非终端没有任何动作的生产,则野牛会自动添加操作$$ = $ 1,但不能为空生产而做到这一点,因此它抱怨。

我假设该行中的|是错别字。

The warning you get seems completely accurate:

F:      | …

F is a non-terminal; it has a type, and the first alternative is empty with no action. If a non-terminal has a production with no action, bison will automatically add the action $$ = $1, but it can't do that for an empty production, so it complains.

I assume the | in that line is a typo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文