help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

how to solve this reduce/reduce conflict?


From: Lukas Arsalan
Subject: how to solve this reduce/reduce conflict?
Date: Wed, 21 Sep 2022 21:31:20 +0000 (UTC)

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]