使用与以下子句相同的关键字的可选子句的 Yacc 扩展

发布于 2024-10-18 15:23:42 字数 534 浏览 2 评论 0原文

我正在开发一个小型 DSL,并且在让 Yacc (Bison) 干净地解析以下符号时遇到问题:

START (RETURN expression WHERE expression)* RETURN (expression)?

这是我到目前为止所拥有的,但我不断遇到转移/减少冲突,我不知道如何修复它们:

start: START conditional_returns returns |
       START returns;

conditional_returns: conditional_returns conditional_return | conditional_return;

conditional_return: RETURN expression WHERE expression;

returns: RETURN expression | RETURN;

我可以看到 RETURN 关键字在不同的子句中重用这一事实导致了问题,但我想了解如何使用这么多规则正确地将其拆分。有任何帮助吗?

I'm developing a small DSL and having problems getting Yacc (Bison) to cleanly parse for the following symbols:

START (RETURN expression WHERE expression)* RETURN (expression)?

This is what I have so far, but I keep getting shift / reduce conflicts and I'm not sure how to fix them:

start: START conditional_returns returns |
       START returns;

conditional_returns: conditional_returns conditional_return | conditional_return;

conditional_return: RETURN expression WHERE expression;

returns: RETURN expression | RETURN;

I can see that the fact the RETURN keyword is reused in a different clause is causing problems, but I'd like to understand how to split this up correctly using this many rules. Any help would be appreciated?

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

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

发布评论

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

评论(1

吹梦到西洲 2024-10-25 15:23:42

就其本身而言,上述规则不会造成任何问题; yacc/bison 可以很好地处理它们,没有移位/减少或减少/减少冲突。如果 start 也是合法的表达式,那么您可能会遇到问题。如果是这种情况,则该语言是不明确的 - 当 start 中有一个 start 时,WHERE 可能与其中任何一个相关联。例如,输入

START RETURN START RETURN expr WHERE expr RETURN expr

可以被解析为

START RETURN ( START RETURN expr ) WHERE expr RETURN expr

或 ,

START RETURN ( START RETURN expr WHERE expr RETURN expr )

具体取决于什么适合您的 DSL,您可以更改语法以强制执行一种含义或另一种含义,或者禁止嵌套 start 表达式(如果它们不这样做)有道理。

By themselves, the above rules don't cause any problems; yacc/bison can handle them just fine with no shift/reduce or reduce/reduce conflicts. Where you might be getting into problems is if start is also a legal expression. If that's the case, the language is ambiguous -- when you have a start within a start, a WHERE might be associated with either one. For example, the input

START RETURN START RETURN expr WHERE expr RETURN expr

could be parsed as either

START RETURN ( START RETURN expr ) WHERE expr RETURN expr

or

START RETURN ( START RETURN expr WHERE expr RETURN expr )

depending on what is right for your DSL, you can change the grammar to enforce one meaning or the other, or to disallow nested start expressions if they don't make sense.

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