使用与以下子句相同的关键字的可选子句的 Yacc 扩展
我正在开发一个小型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就其本身而言,上述规则不会造成任何问题; yacc/bison 可以很好地处理它们,没有移位/减少或减少/减少冲突。如果
start
也是合法的表达式
,那么您可能会遇到问题。如果是这种情况,则该语言是不明确的 - 当start
中有一个start
时,WHERE 可能与其中任何一个相关联。例如,输入可以被解析为
或 ,
具体取决于什么适合您的 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 legalexpression
. If that's the case, the language is ambiguous -- when you have astart
within astart
, a WHERE might be associated with either one. For example, the inputcould be parsed as either
or
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.