Related articles |
---|
Checking size of variables tc@elvis.dk (Thomas Christensen) (2005-05-16) |
Re: Checking size of variables 148f3wg02@sneakemail.com (Karsten Nyblad) (2005-05-18) |
Re: Checking size of variables DrDiettrich@compuserve.de (Hans-Peter Diettrich) (2005-05-18) |
Re: Checking size of variables torbenm@diku.dk (2005-05-18) |
Re: Checking size of variables henry@spsystems.net (2005-05-18) |
Re: Checking size of variables firefly@diku.dk (Peter \Firefly\Lund) (2005-08-07) |
From: | torbenm@diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=) |
Newsgroups: | comp.compilers |
Date: | 18 May 2005 00:50:50 -0400 |
Organization: | Department of Computer Science, University of Copenhagen |
References: | 05-05-149 |
Keywords: | errors |
Posted-Date: | 18 May 2005 00:50:50 EDT |
"Thomas Christensen" <tc@elvis.dk> writes:
> Where does a compiler _usually_ perform checks on the size of
> integers and strings ?
>
> For example. If a user declares a variable as an "int" in the source
> code, and assigns it some 50 digit number (or any number, exceeding
> the size of an int).
>
> Is this sort of error detected by the lexer or by the type checker, or ?
It depends on the language. Some languages allow integers of
different size, so at best you can only complain about integers
exceeding the maximum size during lexing or parsing. It is only
during type-checking or maybe even not until runtime that you can see
if an actual value exceeds the size if the variable into which it is
stored. Some languages/compilers allow floating point constants to be
expressed as large integer constants, so an integer constant of 50
digits could be used where a floating point number is expected (of
course, some precision will be lost).
Other languages (like C) treat integers as modulo the wordsize, so a
50-digit constant would theoretically be valid, but only the last 32
or 64 bits would be stored.
I guess the safest way of handling constants is to leave them as
strings until code generation time, where you must decide on how to
represent it on the machine in question and give an error if you can
not. Or, even better, have unbounded integers as a standard datatype,
so conversion to bounded integers or floating point numbers is done by
an explicit or implicit typecast in the program.
Torben
Return to the
comp.compilers page.
Search the
comp.compilers archives again.