shift/reduce conflict with SableCC
I'm at my first experience with SableCC and grammar definition. I have the following grammar (a part of it):
query =
{atop} attroperator |
开发者_如何学Go {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.
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
.
精彩评论