From 0892f3f0b30ea489a905e0d0513c0317cf1be528 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Tue, 7 Dec 1993 11:10:49 +0000 Subject: [PATCH] Fixed nasty 8-bit bugs --- flex.skl | 7 +++++++ gen.c | 35 ++++++++++++++++++----------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/flex.skl b/flex.skl index 336fdec..244a930 100644 --- a/flex.skl +++ b/flex.skl @@ -65,6 +65,13 @@ class istream; /* Returned upon end-of-file. */ #define YY_NULL 0 +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + /* Enter a start condition. This macro really ought to take a parameter, * but we do it the disgusting crufty way forced on us by the ()-less * definition of BEGIN. diff --git a/gen.c b/gen.c index c12849c..e25160d 100644 --- a/gen.c +++ b/gen.c @@ -48,6 +48,7 @@ static int indent_level = 0; /* each level is 8 spaces */ /* *Everything* is done in terms of arrays starting at 1, so provide * a null entry for the zero element of all C arrays. */ +static char C_int_decl[] = "static const int %s[%d] =\n { 0,\n"; static char C_short_decl[] = "static const short int %s[%d] =\n { 0,\n"; static char C_long_decl[] = "static const long int %s[%d] =\n { 0,\n"; static char C_state_decl[] = @@ -225,15 +226,10 @@ void genctbl() void genecs() { Char clower(); - static char C_char_decl[] = "static const %s %s[%d] =\n { 0,\n"; - /* } so vi doesn't get confused */ register int i, j; int numrows; - if ( numecs < csize ) - printf( C_char_decl, "YY_CHAR", "yy_ec", csize ); - else - printf( C_char_decl, "short", "yy_ec", csize ); + printf( C_int_decl, "yy_ec", csize ); for ( i = 1; i < csize; ++i ) { @@ -477,14 +473,18 @@ void gen_next_match() /* NOTE - changes in here should be reflected in gen_next_state() and * gen_NUL_trans(). */ - char *char_map = useecs ? "yy_ec[(unsigned int) *yy_cp]" : "*yy_cp"; - char *char_map_2 = - useecs ? "yy_ec[(unsigned int) *++yy_cp]" : "*++yy_cp"; + char *char_map = useecs ? + "yy_ec[YY_SC_TO_UI(*yy_cp)]" : + "YY_SC_TO_UI(*yy_cp)"; + + char *char_map_2 = useecs ? + "yy_ec[YY_SC_TO_UI(*++yy_cp)]" : + "YY_SC_TO_UI(*++yy_cp)"; if ( fulltbl ) { indent_put2s( -"while ( (yy_current_state = yy_nxt[yy_current_state][(unsigned int)%s]) > 0 )", + "while ( (yy_current_state = yy_nxt[yy_current_state][%s]) > 0 )", char_map ); indent_up(); @@ -586,16 +586,17 @@ int worry_about_NULs; { if ( useecs ) (void) sprintf( char_map, - "(*yy_cp ? yy_ec[(unsigned int) *yy_cp] : %d)", + "(*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : %d)", NUL_ec ); else (void) sprintf( char_map, - "(*yy_cp ? *yy_cp : %d)", NUL_ec ); + "(*yy_cp ? YY_SC_TO_UI(*yy_cp) : %d)", NUL_ec ); } else - yy_strcpy( char_map, - useecs ? "yy_ec[(unsigned int) *yy_cp]" : "*yy_cp" ); + yy_strcpy( char_map, useecs ? + "yy_ec[YY_SC_TO_UI(*yy_cp)]" : + "YY_SC_TO_UI(*yy_cp)" ); if ( worry_about_NULs && nultrans ) { @@ -610,12 +611,12 @@ int worry_about_NULs; if ( fulltbl ) indent_put2s( - "yy_current_state = yy_nxt[yy_current_state][(unsigned int) %s];", + "yy_current_state = yy_nxt[yy_current_state][%s];", char_map ); else if ( fullspd ) indent_put2s( - "yy_current_state += yy_current_state[(unsigned int) %s].yy_nxt;", + "yy_current_state += yy_current_state[%s].yy_nxt;", char_map ); else @@ -904,7 +905,7 @@ void gentabs() if ( trace ) fputs( "\n\nMeta-Equivalence Classes:\n", stderr ); - printf( C_char_decl, "yy_meta", numecs + 1 ); + printf( C_int_decl, "yy_meta", numecs + 1 ); for ( i = 1; i <= numecs; ++i ) { -- 2.40.0