Size limit of enum type?

S

Stephan Schulz

Hi,

in my standard scanner/parser package I am using a typedef enum to
enumerate possible tokens, with each token type corresponding to a
power of two, somewhat like the following:

typedef enum
{
NoToken = 0,
Ident = 1,
PosInt = 2,
String = 4
}TokenType;

The nice thing is that one can denote sets of tokens by binary or,
e.g. if you allow Ident or String in your grammar you can write
something like accept(input_stream, Ident|String).

Whether this was a wise choice is now moot. The code has been running
for something like 6-7 years and is, in practice, portable to all
machines I have ever tried it on (which includes big- and little
Endian 32 and 64 bit Unixes and even the OS from Hell^WRedmont). It is
used in lots of different parsers and cannot be easily replaced (see
http://www.eprover.org, if you are interested in my project).

I suspect you guess my problem by now. I have reached a stage where I
need more that 32 different token types...and on most current machines
int is 32 bit. I was wondering if the current C standard says anything
about the maximum size of numbers in enums, especially since long long
now is a standard type. Are enums restricted to int or unsigend int,
or will an enum automatically be as big as necessary (as long as a
suitable int type exists)?

Of course my alternative it to just typedef TokenType to long long and
use #defines. But I'd only do that if absolutely necessary.

Thanks for any help!

Bye,

Stephan
 
J

Jack Klein

Hi,

in my standard scanner/parser package I am using a typedef enum to
enumerate possible tokens, with each token type corresponding to a
power of two, somewhat like the following:

typedef enum
{
NoToken = 0,
Ident = 1,
PosInt = 2,
String = 4
}TokenType;

The nice thing is that one can denote sets of tokens by binary or,
e.g. if you allow Ident or String in your grammar you can write
something like accept(input_stream, Ident|String).

Whether this was a wise choice is now moot. The code has been running
for something like 6-7 years and is, in practice, portable to all
machines I have ever tried it on (which includes big- and little
Endian 32 and 64 bit Unixes and even the OS from Hell^WRedmont). It is
used in lots of different parsers and cannot be easily replaced (see
http://www.eprover.org, if you are interested in my project).

I suspect you guess my problem by now. I have reached a stage where I
need more that 32 different token types...and on most current machines
int is 32 bit. I was wondering if the current C standard says anything
about the maximum size of numbers in enums, especially since long long
now is a standard type. Are enums restricted to int or unsigend int,
or will an enum automatically be as big as necessary (as long as a
suitable int type exists)?

Of course my alternative it to just typedef TokenType to long long and
use #defines. But I'd only do that if absolutely necessary.

Thanks for any help!

Bye,

Stephan

Some compilers might allow this as an extension, but it is neither
guaranteed nor portable. All versions of the C standard contain this
wording: "The expression that defines the value of an enumeration
constant shall be an integer constant expression that has a value
representable as an int."

I have proposed a more flexible use of enumerations for the next
revision of the C and C++ standards, both of them some years off. It
turns out that there is a very extensive proposal for expanding their
use in C++, far beyond what I proposed or what would be welcome in C,
but there was little interest in any change from C committee members.
 
S

Stephan Schulz

On Mon, 4 Oct 2004 23:28:33 +0000 (UTC),
(e-mail address removed)-muenchen.de (Stephan Schulz) wrote in
comp.lang.c:

[enum range > int?]
Some compilers might allow this as an extension, but it is neither
guaranteed nor portable. All versions of the C standard contain this
wording: "The expression that defines the value of an enumeration
constant shall be an integer constant expression that has a value
representable as an int."

Thanks a lot. My copy of Harbison/Steele is 500km away...

It's typedef time ;-(

Bye,

Stephan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top