I have written following grammar:
%union{
string *s;
float num;
}
%token div_token mod_token sqrt_token it_token abs_token
%token <num> num_token
%token <s> Stampa
%type <num> E
%left '+' '-'
%left '*' '/' div_token mod_token
%left UMINUS
%left abs_token sqrt_token
%%
Program: Program Line '\n' { }
| Line '\n' { }
;
Line: Stampa {cout<<*$1<<endl;}
| E {cout<<$1<<endl; broj = $1;}
| it_token {cout<<broj<<endl;}
;
E: E '+' E {$$ = $1 + $3;}
| E '-' E {$$ = $1 - $3;}
| E '*' E {$$ = $1 * $3;}
| E '/' E {if($3!=0)
$$ = $1 / $3;
else
yyerror("Deljenje nulom");
}
| mod_token E E {$$ = (float)((int)$2 % (int)$3);}
| div_token E E {$$ = (float)((int)($2 / $3));}
| sqrt_token E { $$ = sqrt($2); }
| '(' E ')' {$$=$2;}
| abs_token E { $$ = abs($2);}
| '-' E %prec UMINUS {$$=-$2;}
| num_token {$$ = $1;}
;
Now, bison found 8 reduce/reduce conflicts. When I delete line
| '-' E %prec UMINUS {$$=-$2;}
there are none. I think priorities and associative property are well defined. Can someone tell me how to resolve conflicts?
This should fix the problem:
This fixes a couple of issues. You probably meant:
And it is more clear to write the following:
You can see the conflicts with bison option
-v
which produces axyz.output
:The operator reductions on
div_token
andmod_token
are suspect. The ambiguity of the grammar is caused by these operators applied to two expressionsE
.EDIT
Perhaps you are looking to keep the prefix div and mod operators. If so, you need to disambiguate the grammar. One possible solution is:
and add the type of
F
: