0

On the grammar of a .y (yacc -- bison) file I've defined the following rules:

C : E | D | F | A

A : B | B '[' C ']' ;

(this is something like B with optional [C]) when I compile the .y file in the appropriate way using a proper lexer file I get the following shift\reduce conflict:

State 48
74 A: B .
75 | B . '[' C ']'
'[' shift, and go to state 91
'[' [reduce using rule 74 (A)]
$default reduce using rule 74 (A)

My problem is the following if it has predecessor '[' I want it to shift and not reduce. The problem is I cannot find the solution to this error, while searching for hours on the documentation. How can I fix this (please be code-specific).

Community
  • 1
  • 1
ysig
  • 447
  • 4
  • 18
  • 1
    You have not supplied enough information to reproduce your error. When I create a bison file from your example I find no problems. Show more code that reproduces your error please. – Brian Tompsett - 汤莱恩 Mar 25 '16 at 22:15

2 Answers2

1

The problem is that there is some context in which an A might be followed by a [. Without seeing more of the grammar, it is impossible to be more precise.

However, it is worth noting that bison/yacc will do exactly what you want: resolve the conflict in favour of the shift. So aside from the warning, everything should be just fine.

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

If you try to expand the A rule, you get two possible states

State 1:

A -> B

State 2:

A -> B
A -> BC

All possible states for A are the following

A -> B (State 1)
A -> B (State 2)
A -> BC

Bison can not determine if you are in the State 1 or 2 for B input.

You can replace the A rule by (assuming B and C are tokens):

A: B | B C
bipe15
  • 111
  • 1
  • 8