转移/减少与 SableCC 的冲突

发布于 2024-09-08 13:38:54 字数 1062 浏览 6 评论 0原文

我第一次体验 SableCC 和语法定义。 我有以下语法(其中一部分):

query =
           {atop} attroperator |
           {query_par} l_par query r_par |
           {query_and} [q1]:query logic_and [q2]:query  |
           {query_or} [q1]:query logic_or [q2]:query |
           {query_not} logic_not query ;

我有以下错误:

shift/reduce conflict in state [stack: PCommand TLogicNot PQuery *] on
TRPar in {
       [ PQuery = PQuery * TRPar ] (shift),
       [ PQuery = TLogicNot PQuery * ] followed by TRPar (reduce)
}

shift/reduce conflict in state [stack: PCommand TLogicNot PQuery *] on
TLogicAnd in {
       [ PQuery = PQuery * TLogicAnd PQuery ] (shift),
       [ PQuery = TLogicNot PQuery * ] followed by TLogicAnd (reduce)
}

shift/reduce conflict in state [stack: PCommand TLogicNot PQuery *] on
TLogicOr in {
       [ PQuery = PQuery * TLogicOr PQuery ] (shift),
       [ PQuery = TLogicNot PQuery * ] followed by TLogicOr (reduce)
}

我通过将 l_par 和 r_par 添加到所有替代方案中来解决它们,通过 方式,应该增加可读性,但是有没有办法以一种方式做到这一点 优雅的举止?

谢谢。

I'm at my first experience with SableCC and grammar definition.
I have the following grammar (a part of it):

query =
           {atop} attroperator |
           {query_par} l_par query r_par |
           {query_and} [q1]:query logic_and [q2]:query  |
           {query_or} [q1]:query logic_or [q2]:query |
           {query_not} logic_not query ;

I have the following errors:

shift/reduce conflict in state [stack: PCommand TLogicNot PQuery *] on
TRPar in {
       [ PQuery = PQuery * TRPar ] (shift),
       [ PQuery = TLogicNot PQuery * ] followed by TRPar (reduce)
}

shift/reduce conflict in state [stack: PCommand TLogicNot PQuery *] on
TLogicAnd in {
       [ PQuery = PQuery * TLogicAnd PQuery ] (shift),
       [ PQuery = TLogicNot PQuery * ] followed by TLogicAnd (reduce)
}

shift/reduce conflict in state [stack: PCommand TLogicNot PQuery *] on
TLogicOr in {
       [ PQuery = PQuery * TLogicOr PQuery ] (shift),
       [ PQuery = TLogicNot PQuery * ] followed by TLogicOr (reduce)
}

I solved them by adding l_par and r_par to all alternatives which, by the
way, should increase readability but is there a way to do it in an
elegant manner?

Thanks.

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

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

发布评论

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

评论(1

一瞬间的火花 2024-09-15 13:38:54

所以,我已经解决了这个问题。我所做的基本上定义了三个级别的关联性。

query = 
    {query_or} query logic_or term | 
    {query_term} term ;

term =
    {term_and} term logic_and factor |
    {term_factor} factor ;

factor = 
    {atop} attroperator |
    {query_not} logic_not attroperator |
    {query_par} l_par query r_par ;

这是经典的结合性方案 +,* 和一元运算符,例如 - 其中 + =logic_or* =logic_and- =logic_not

So, I've solved the problem. What I've done is basically define three levels of associativity.

query = 
    {query_or} query logic_or term | 
    {query_term} term ;

term =
    {term_and} term logic_and factor |
    {term_factor} factor ;

factor = 
    {atop} attroperator |
    {query_not} logic_not attroperator |
    {query_par} l_par query r_par ;

It's the classic associativity scheme +,* with an unary operator like - where + = logic_or, * = logic_and, - = logic_not.

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