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

torbenm@app-0.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
31 Jan 2006 12:47:16 -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)
[4 later articles]
| List of all articles for this month |
From: torbenm@app-0.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
Newsgroups: comp.compilers
Date: 31 Jan 2006 12:47:16 -0500
Organization: Department of Computer Science, University of Copenhagen
References: 06-01-131
Keywords: lex, arithmetic
Posted-Date: 31 Jan 2006 12:47:16 EST

"Edsko de Vries" <devriese@cs.tcd.ie> 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..


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.


                Torben



Post a followup to this message

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