Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving dangling else with bison

Tags:

bison

My grammar has these two token declarations:

%token RP
%token ELSE

And these two rules:

Statement  : IF LP Exp RP Statement;

Statement  : IF LP Exp RP Statement ELSE Statement;

From what I understand, a rule's priority is determined by the priority of its last nonterminal. Therefore, the first rule has RP priority and the second rule has ELSE priority which is higher than RP. Below is bison's output:

state 73

   11 Statement: IF LP Exp RP Statement .
   12          | IF LP Exp RP Statement . ELSE Statement

    ELSE  shift, and go to state 76

    ELSE      [reduce using rule 11 (Statement)]
    $default  reduce using rule 11 (Statement)

Shouldn't this conflict be solved by shifting since ELSE has higher priority ?

like image 421
Shmoopy Avatar asked Feb 17 '23 07:02

Shmoopy


2 Answers

No, because %token does not set the precedence (what you call the priority) of a token -- it declares the token to exist with NO precedence. If you want to declare a precedence for the token, you need to use %left, %right or %nonassoc all of which both declare the token AND set its precedence. If you change your code to

%nonassoc RP
%nonassoc ELSE

then RP and ELSE will have have their precedence set, and ELSE will have higher precedence, and the shift/reduce conflict will be resolved by precedence.

like image 96
Chris Dodd Avatar answered Mar 03 '23 02:03

Chris Dodd


you should have a look at this answer, the if/else ambiguity is a pretty common pattern for languages, and it is dealt in Bison's and Yacc's manual, as well as in the dragon books, and on several tutorials on Internet, like this one

Shouldn't this conflict be solved by shifting since ELSE has higher priority ?

as said in the other answer, it indeed is. The conflict is being solved automatically by bison doing an automatic shift, and it tells you about it.. It indeed selects the IF ELSE, and if you add the %expect declaration it won't warn you about that it did something "automatically".

like image 28
zmo Avatar answered Mar 03 '23 02:03

zmo