I am trying to describe a grammar in bison but I am unsure if it can be done. My intended grammar is this:
%token A B C D SEP
%%
items : /* empty */
| items_nonempty
;
items_nonempty : item
| items_nonempty SEP item
;
item : B
| B SEP D
| B SEP C
| B SEP C SEP D
| A SEP B
| A SEP B SEP D
| A SEP B SEP C
| A SEP B SEP C SEP D
;
"items
" is a (possible empty) sequence of item
elements, separated by a SEP
token.
Each item consists of up to 4 tokens (A B C D
), in that order, separated by SEP
. The A
, C
, and D
tokens in an item are optional.
Note the re-use of the same separator token SEP within each item, and between the items themselves.
I hope the intended grammer is clear. I think it is unambiguous, but I am quite unsure if it is sufficiently restricted to be parseable by bison – unfortunately, my parser knowledge is quite rusty.
Using the grammar as given, bison reports 4 shift/reduce conflicts. Looking at the 'output' I understand where they occur and why; but I am at a loss how (and if) the intended grammar can be written to get rid of the S/R conflicts.
I am unwilling to use an %expect
declaration. Likewise, I am unwilling to have my scanner consume the separator tokens rather than have them pass on to the parser.
Any hints on how to sanitise this grammar would be greatly appreciated.