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
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