how to solve this reduce/reduce conflict?
Lukas Arsalan <[email protected]> Wed, 21 Sep 2022 21:31:20 +0000 (UTC)
| Newsgroups | gmane.comp.parsers.bison.general |
|---|---|
| Message-ID | <[email protected]> |
Usually -2^2 is considered to be -4, because: the minus is interpreted as a unary operator with lower precedence, than ^ (power)... E.g.: http://www.isthe.com/chongo/tech/comp/calc/
_but_:
I would like to have a parser,
[1] that binds the sign of a number stronger than a ^ (power), and
[2] that binds the unary inversion-operator of an expression weaker than a ^ (power).
My parse works, but the bison manual says, that i shalt fix the conflict, because it is somehow worrisome.
https://www.gnu.org/software/bison/manual/html_node/Reduce_002fReduce.html
example:
[1] -2^2=(-2)^2=4
[2] a=2 ; -a^2=-(a^2)=-(2^2)=-4
In this yy-file is a reduce/reduce conflict:
%left "+" "-";
%right "^";
exp:
"-" "num" { $$ = -*new Float($2); std::cout << "NUMinv" << $$ << std::endl; }
| "num" { $$ = new Float($1); std::cout << "num" << $$ << std::endl; }
| "+" exp { $$ = $2; std::cout << "noninv" << $$ << std::endl; }
| "-" exp { $$ = -*$2; std::cout << "inv" << $$ << std::endl; }
| exp "+" exp { $$ = *$1 + $3; std::cout << "add" << $$ << std::endl; }
| exp "-" exp { $$ = *$1 - $3; std::cout << "sub" << $$ << std::endl; }
| exp "^" exp { $$ = *$1 ^ $3; std::cout << "pow" << $$ << std::endl; };
bison -Wcounterexamples says:
Example: "-" "num" •
First reduce derivation
exp
↳ 4: "-" "num" •
Second reduce derivation
exp
↳ 7: "-" exp
↳ 5: "num" •
How can i solve that without a preprocessor, that wraps negative numbers (e. g.: "-4*(-2+-3)" --> "(-4)*((-2)+(-3))") with brackets?
-Arne