Where are the shift/reduce conflicts in this Bison code coming from?

后端 未结 3 505
旧时难觅i
旧时难觅i 2021-01-14 14:39

I\'m trying to parse this syntax:

34 + 1 − 8, 32 * 87 + 6 / 4, 34 / 8

I\'m expecting to ground it like this:

(, (- (+ 34 1)         


        
3条回答
  •  一生所求
    2021-01-14 15:38

    The problem is with your definition of term:

    term: NUMBER | term op term ;
    

    When parsing this, at each number, the question is: should I read another token to know if I have the first, or the second form.

    A solution could be to define:

    term: NUMBER reminder;
    reminder: /* empty */ | op term;
    

    The grammar, once adapted, looks like the following:

    %token NUMBER
    %token COMMA
    %token OPERATOR
    %left OPERATOR
    %left COMMA
    %%
    
    term: NUMBER reminder;
    reminder: /* empty */ | op term;
    op: OPERATOR | COMMA;
    %%
    

    compiles without warnings with bison (GNU Bison) 2.4.1.

提交回复
热议问题