Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648

"Russ Cox" <rsc@swtch.com>
31 Jan 2006 12:47:25 -0500

          From comp.compilers

Related articles
1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 devriese@cs.tcd.ie (Edsko de Vries) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 torbenm@app-0.diku.dk (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 rsc@swtch.com (Russ Cox) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 jm@bourguet.org (Jean-Marc Bourguet) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 gneuner2@comcast.net (George Neuner) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 rivers@dignus.com (Thomas David Rivers) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 david@tribble.com (David R Tribble) (2006-01-31)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 henry@spsystems.net (2006-01-31)
[3 later articles]
| List of all articles for this month |

From: "Russ Cox" <rsc@swtch.com>
Newsgroups: comp.compilers
Date: 31 Jan 2006 12:47:25 -0500
Organization: Compilers Central
References: 06-01-131
Keywords: lex, arithmetic

> This works fine, with the sole exception of the number "-2147483648".
> The problem is, of course, overflow: -2147483648 is a valid negative
> number (assuming 32-bit numbers), but the integer 2147483648 is _not_ a
> valid positive number. Thus, the above method of dealing with "-" as a
> unary operator breaks down.


I know a few vendor-supplied C compilers that get this wrong,
printing warnings about "positive constant overflows" or something
like that, which is all the more confusing an error when you look at
it and say "but that's *not* a positive constant!".


> The solution is to interpret the "-" as part of the number, and
> generate INT(-2147483648),


I think a better solution is to let the parser do its job and determine
whether the - is a negation or a subtraction, and then once that
is done, you can warn about INT(2147483648) that isn't inside a
negation. I put this into the Plan 9 C compilers recently and it
worked well.


Russ



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.