|Checking size of variables firstname.lastname@example.org (Thomas Christensen) (2005-05-16)|
|Re: Checking size of variables email@example.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 firstname.lastname@example.org (2005-05-18)|
|Re: Checking size of variables email@example.com (2005-05-18)|
|Re: Checking size of variables firstname.lastname@example.org (Peter \Firefly\Lund) (2005-08-07)|
|Date:||18 May 2005 00:50:50 -0400|
|Organization:||Department of Computer Science, University of Copenhagen|
|Posted-Date:||18 May 2005 00:50:50 EDT|
"Thomas Christensen" <email@example.com> 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.
Return to the
Search the comp.compilers archives again.