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: | henry@spsystems.net (Henry Spencer) |
Newsgroups: | comp.compilers |
Date: | 18 May 2005 12:17:12 -0400 |
Organization: | SP Systems, Toronto, Canada |
References: | 05-05-149 |
Keywords: | analysis, errors |
Posted-Date: | 18 May 2005 12:17:12 EDT |
Thomas Christensen <tc@elvis.dk> wrote:
>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 ?
Most likely it would happen in one of four ways:
+ The scanner/lexer, trying to convert the number to an internal
representation (either that of the type implied by the form of the
constant, or a worst-case temporary internal type), finds it won't fit.
+ The type checker, trying to determine the type of the constant, finds
that the largest possible integer type isn't big enough.
+ The type checker successfully assigns the constant a type, but then
finds that it's not assignment-compatible with the variable, because the
language doesn't have an implicit narrowing conversion.
+ Either the type checker, or some later phase attempting to evaluate
expressions at compile time, notices that the implicit narrowing
conversion would inevitably overflow.
It's also possible for the compiler to accept the code, with the overflow
caught only at run time.
And in sloppy languages like C, it's quite possible for this to be valid
code, or invalid code that the implementation is not required to catch.
--
"Think outside the box -- the box isn't our friend." | Henry Spencer
-- George Herbert | henry@spsystems.net
Return to the
comp.compilers page.
Search the
comp.compilers archives again.