[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: shift reduce conflict



Emanuele Ianni <dierre <at> gmail.com> writes:

> 
> Hi guys! 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} query |
> 		{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 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 way?
> 
> Thanks.
> 
> Emanuele
> 

Guys, solved the problem finally:

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.