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

"David R Tribble" <david@tribble.com>
31 Jan 2006 21:22:45 -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)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 gah@ugcs.caltech.edu (glen herrmannsfeldt) (2006-02-02)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 DrDiettrich@compuserve.de (Hans-Peter Diettrich) (2006-02-02)
Re: 1 - 1, 1 -1, 1-1, 1 - -1 and -2147483648 david@tribble.com (David R Tribble) (2006-02-03)
| List of all articles for this month |

From: "David R Tribble" <david@tribble.com>
Newsgroups: comp.compilers
Date: 31 Jan 2006 21:22:45 -0500
Organization: http://groups.google.com
References: 06-01-13106-01-134
Keywords: lex, arithmetic

Edsko de Vries writes:
>> our current definition for integer constant looks something like
>> INT ([1-9][0-9]*)|0
>> In particular, note that it does not allow for an (optional) "+" or "-"
>> at the start of the integer. In other words, "-" is treated as a unary
>> operator, rather than as part of the number.
>>
>> 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.
>>
>> At the moment, I have added a new rule to the lexer that matches
>> this exact number, which solves the problem - but it's a hack..
>


Torben Ęgidius Mogensen wrote:
> One solution is to let the lexer keep the number as a string and
> handle unary minus applied to a constant in the parser (at this time
> converting the string to a number). Or you can use a bignum package
> in the compiler, only converting to machine integers when you generate
> code.


Another method is to convert all but the last digit into an integer,
so that <2147483648> becomes <214748364, 8>. Then a simple
range comparison can be made to determine if the token is too
big for a valid integer value. This method avoids overflow.


It's interesting to note that the C and C++ grammars do not
treat leading signs as part of a constant but as a separate
unary operator. Other languages such as COBOL do treat
leading signs as part of a literal constant, but these kinds of
languages also typically require additional spaces to separate
operands from operators.


-drt


Post a followup to this message

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