parsing - Shift/reduce conflicts in bison -
i'm new bison
, i'm having trouble shift/reduce conflicts... i'm trying load file array data[]
:
struct _data { char name[50]; char surname[50]; int year; } data[1000];
here part of bison code:
%token id num nl eof %% file : list eof ; list : record | list record ; record : name surname year nl { count++; } | nl { count++; } | /*empty*/ ; name : id { strcpy(data[count].name, yytext); } ; surname: id { strcpy(data[count].surname, yytext); } ; year : num { data[count].year= atoi(yytext); } ; %%
i error:
conflicts: 5 shift/reduce
any idea went wrong?
you can use -v
option bison
produce .output
file containing lot more information can diagnose shift/reduce conflicts. in particular, show every parser state, including list of items, , indicate states have conflicts.
but in case, problem pretty simple. stripped essentials have:
list: record | list record ; record: | /* nothing */ ;
ignoring definition of something
is, problem list
can consist of number of records
, 1 after another, , record
can empty. means nothing can parsed number of empty records
, totally ambiguous. 2 consecutive somethings
in input separated 0, 1, 2, 42, or 273 empty records
. since parser can't know whether start parsing new something
(shift) or emit empty record
(reduce), complains there shift/reduce conflict.
in case solution pretty simple. can see non-empty something
must end nl
; presumably intent file
consists of number of records
, each on own line. can rewrite:
file: list eof ; list: record | list nl record ; record: name surname year | /* empty */ ;
now record
, empty or not, must followed either eof
or nl
. cannot directly followed record
.
Comments
Post a Comment