0

If I have a grammar where a certain expression can match two productions, I will obviously have a reduce/reduce conflict with yacc. Specifically, say I have two productions (FirstProduction and SecondProduction) where both of them could be TOKEN END.

Then yacc will not be able to know what to reduce TOKEN END to (FirstProduction or SecondProduction). However, I want to make it so that yacc prioritises FirstProduction in this situation. How can I achieve that?

Note that both FirstProduction and SecondProduction could be a great deal of things and that Body is the only place in the grammar where these conflict.

Also, I do know that in these situations, yacc will choose the first production that was declared in the grammar. However, I want to avoid having any reduce/reduce warnings.

David Gomes
  • 5,644
  • 16
  • 60
  • 103
  • 1
    You mean `Body: FirstProduction | SecondProduction`, right? – rici Mar 21 '16 at 22:25
  • I edited it to reflect that Body can be a list of FirstProductions or a list of SecondProductions (a list of 0 or more instances of First or Second productions). This way, I did not mean what you said, but instead what I said (albeit edited), for sure. – David Gomes Mar 21 '16 at 22:26

2 Answers2

1

Bison has no way to explicitly mark one production as preferred over another; the only such mechanism is precedence relations, which resolve shift/reduce conflicts. As you say, the file order provides an implicit priority. You can suppress the warning with an %expect declaration; unfortunately, that only lets you tell bison how many conflicts to expect, and not which conflicts.

rici
  • 234,347
  • 28
  • 237
  • 341
1

You can refactor the grammar to not allow the second list to start with something that could be part of the first list:

Body: FirstProductionList SecondProductionList
    | FirstProductionList
    ;

FirstProductionList: FirstProductionList FirstProduction
                   | /* empty */
                   ;

SecondProductionList: SecondProductionList SecondProduction
                    | NonFirstProduction
                    ;

NonFirstProduction is any production that is unique to SecondProduction, and marks the transition from reducing FirstProdutions to SecondProductions

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226