2010-07-06 48 views
2

我在SableCC和语法定义的第一次经历。 我有以下的语法(的一部分):转移/减少与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所有替代它,由 方式,应该增加可读性,但有没有办法以优雅的方式来做到这一点?

谢谢。

回答

3

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

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