Re: Checking size of variables

Karsten Nyblad <>
18 May 2005 00:47:58 -0400

          From comp.compilers

Related articles
Checking size of variables (Thomas Christensen) (2005-05-16)
Re: Checking size of variables (Karsten Nyblad) (2005-05-18)
Re: Checking size of variables (Hans-Peter Diettrich) (2005-05-18)
Re: Checking size of variables (2005-05-18)
Re: Checking size of variables (2005-05-18)
Re: Checking size of variables (Peter \Firefly\Lund) (2005-08-07)
| List of all articles for this month |

From: Karsten Nyblad <>
Newsgroups: comp.compilers
Date: 18 May 2005 00:47:58 -0400
Organization: Compilers Central
References: 05-05-149
Keywords: errors
Posted-Date: 18 May 2005 00:47:57 EDT

Thomas Christensen wrote:
> 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 ?

As in so many cases it depends on the languages being compiled, the
goals of the compiler project, the taste of the developers, etc.

Are you implementing a multi target compiler, where the target computers
may implement various integer types in different sizes of variables,
e.g., an integer could be 2 bytes on some platforms, but 4 bytes on
others? You might want to open for support of 8, 16, 32, or unlimited
size of integer variables. Then it might make sense to implement
integers as unlimited size variables in the front end, and postpone
issuing error messages to the code generators.

It depends on the language. E.g. in Cobol you can specify how many
digits you want in your variables, and intermediate calculations cannot
generate an overflow.

Many computer architectures implement floating point numbers with more
digits than is possible in integers, e.g., good old Vaxes implemented 4,
8, and 16 byte floating point formats, but only 4 byte integers. Then
your lexer has to be capable of scanning floating point numbers with
more digits than can be represented in an integers. In XSLT (a language
for transforming XML to XML, see there is no exponent in
floating point numbers. Thus, if you want to enter 1e20, you have to
write 100000000000000000000.0, and your lexer has to be prepared for
many digits in floating point constants.

Then there is constant folding and other optimizations. Again you have
to take the language and the target into account. E.g., in Ada A + 1 -
1 does not mean the same as (A + 1) - 1, because the language allows the
first to be optimized into A even if the expression may generate an
overflow, but the second may not be optimized.

But returning to your original question: I think most compilers will
report some errors in the lexer, some errors during type checking, and
perhaps some errors during code generation. E.g., it might report the
error in the lexer, if the constant cannot be lexed or cannot be stored
in the intermediate representation used between lexer and parser, and
leave all other errors for other parts of the compiler.

Karsten Nyblad
148f3wg02 at sneakemail dot com

Post a followup to this message

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