Where do shift / contraction conflicts occur in this Beason code?

I am trying to parse this syntax:

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

I expect to be able to do this as follows:

(, (- (+ 34 1) 8) (/ (+ (* 32 87) 6) 4) (/ 34 8))

This is the code for BISON:

%token NUMBER
%token COMMA
%token OPERATOR
%left OPERATOR
%left COMMA
%%

term: NUMBER | term op term ;
op: OPERATOR | COMMA;
%%

There is a problem:

test.y: conflicts: 2 shift/reduce

How can I solve it?

+3
source share
2 answers

The problem is determining term:

term: NUMBER | term op term ;

When analyzing this issue, the question arises on each issue: should I read another token to find out if I have the first or second form.

The solution may be to determine:

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

Grammar, after its adaptation, is as follows:

%token NUMBER
%token COMMA
%token OPERATOR
%left OPERATOR
%left COMMA
%%

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

compiles without warning using bison (GNU Bison) 2.4.1.

+2
source

, , --verbose example.output, example.y. , :

State 7 conflicts: 2 shift/reduce

()

state 7

    2 term: term . op term
    2     | term op term .

    COMMA     shift, and go to state 4
    OPERATOR  shift, and go to state 5

    COMMA     [reduce using rule 2 (term)]
    OPERATOR  [reduce using rule 2 (term)]
    $default  reduce using rule 2 (term)

    op  go to state 6
+10

Source: https://habr.com/ru/post/1754985/


All Articles