Re: how to solve this reduce/reduce conflict?

Akim Demaille <[email protected]> Thu, 22 Sep 2022 07:08:23 +0200
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
Hi,

> Le 21 sept. 2022 à 23:31, Lukas Arsalan <[email protected]> a écrit :
> 
> exp:
>    "-" "num"            { $$ = -*new Float($2); std::cout << "NUMinv" << $$ << std::endl; }
> |  "num"                { $$ = new Float($1); std::cout << "num" << $$ << std::endl; }
> |  "-" exp              { $$ = -*$2; std::cout << "inv" << $$ << std::endl; }

This snippet is clearly ambiguous, since it allows two different parses of -1, which -Wcex nicely showed.

If I were you, I would handle this in the scanner.  IOW, the scanner should be extended to support signed literals, and process that initial `-`.  So the grammar would no longer include `exp: "num"`.

Your actions look quite badly typed.  And `std::endl` should seldom be used, `'\n'` is enough.

Cheers!