From: Vern Paxson Date: Mon, 29 Nov 1993 16:40:41 +0000 (+0000) Subject: Fixed to buffer section 1 definitions X-Git-Tag: flex-2-5-5b~325 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cbe0e6b465562d465a7fbc15e62402a96d694db1;p=flex Fixed to buffer section 1 definitions --- diff --git a/flex.skl b/flex.skl index efcbe3d..adf5d88 100644 --- a/flex.skl +++ b/flex.skl @@ -60,56 +60,9 @@ #define YY_PROTO(proto) () #endif -%% section 1 definitions go here - -/* Amount of stuff to slurp up with each read. */ -#ifndef YY_READ_BUF_SIZE -#define YY_READ_BUF_SIZE 8192 -#endif - /* Returned upon end-of-file. */ -#define YY_END_TOK 0 - -/* Copy whatever the last rule matched to the standard output. */ - -#ifndef ECHO -%- Standard (non-C++) definition -/* This used to be an fputs(), but since the string might contain NUL's, - * we now use fwrite(). - */ -#define ECHO (void) fwrite( yytext, yyleng, 1, yyout ) -%+ C++ definition -#define ECHO LexerOutput( yytext, yyleng ) -%* -#endif - -/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, - * is returned in "result". - */ -#ifndef YY_INPUT -#define YY_INPUT(buf,result,max_size) \ -%% fread()/read() definition of YY_INPUT goes here unless we're doing C++ -%+ C++ definition - if ( (result = LexerInput( (char *) buf, max_size )) < 0 ) \ - YY_FATAL_ERROR( "input in flex scanner failed" ); -%* -#endif - -/* No semi-colon after return; correct usage is to write "yyterminate();" - - * we don't want an extra ';' after the "return" because that will cause - * some compilers to complain about unreachable statements. - */ -#ifndef yyterminate -#define yyterminate() return YY_NULL -#endif - #define YY_NULL 0 -/* Report a fatal error. */ -#ifndef YY_FATAL_ERROR -#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) -#endif - /* 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. @@ -129,28 +82,8 @@ */ #define YY_NEW_FILE yyrestart( yyin ) -/* Default declaration of generated scanner - a define so the user can - * easily add parameters. - */ -#ifndef YY_DECL -%- Standard (non-C++) definition -#define YY_DECL int yylex YY_PROTO(( void )) -%+ C++ definition -#define YY_DECL int yyFlexLexer::yylex() -%* -#endif - -/* Code executed at the end of each rule. */ -#ifndef YY_BREAK -#define YY_BREAK break; -#endif - #define YY_END_OF_BUFFER_CHAR 0 -#ifndef YY_BUF_SIZE -#define YY_BUF_SIZE (YY_READ_BUF_SIZE * 2) /* size of default input buffer */ -#endif - typedef struct yy_buffer_state *YY_BUFFER_STATE; extern int yyleng; @@ -262,8 +195,6 @@ static int yy_n_chars; /* number of characters read into yy_ch_buf */ int yyleng; -%% yyin/yyout and (if -l option) yylineno definition & initialization goes here - /* Points to current character in buffer. */ static char *yy_c_buf_p = (char *) 0; static int yy_init = 1; /* whether we need to initialize */ @@ -285,7 +216,7 @@ void yy_init_buffer YY_PROTO(( YY_BUFFER_STATE b, FILE *file )); #define yy_new_buffer yy_create_buffer -%% declarations of yytext/yytext_ptr (and C++ include, if used) go here +%% yytext/yyin/yyout/yy_state_type/yylineno etc. def's & init go here %- Standard (non-C++) definition #ifdef __cplusplus @@ -307,13 +238,80 @@ static void yy_fatal_error YY_PROTO(( const char msg[] )); */ #define YY_DO_BEFORE_ACTION \ yytext_ptr = yy_bp; \ -%% code to fiddle yytext and yyleng for yymore() goes here +%% code to fiddle yytext and yyleng for yymore() goes here yy_hold_char = *yy_cp; \ *yy_cp = '\0'; \ %% code to copy yytext_ptr to yytext[] goes here, if %array yy_c_buf_p = yy_cp; -%% data tables for the DFA go here +%% data tables for the DFA and the user's section 1 definitions go here + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#define YY_READ_BUF_SIZE 8192 +#endif + +/* Copy whatever the last rule matched to the standard output. */ + +#ifndef ECHO +%- Standard (non-C++) definition +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO (void) fwrite( yytext, yyleng, 1, yyout ) +%+ C++ definition +#define ECHO LexerOutput( yytext, yyleng ) +%* +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ +%% fread()/read() definition of YY_INPUT goes here unless we're doing C++ +%+ C++ definition + if ( (result = LexerInput( (char *) buf, max_size )) < 0 ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); +%* +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +%- Standard (non-C++) definition +#define YY_DECL int yylex YY_PROTO(( void )) +%+ C++ definition +#define YY_DECL int yyFlexLexer::yylex() +%* +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK break; +#endif + +#ifndef YY_BUF_SIZE +#define YY_BUF_SIZE (YY_READ_BUF_SIZE * 2) /* size of default input buffer */ +#endif YY_DECL { diff --git a/flexdef.h b/flexdef.h index d03c782..3b5cfd8 100644 --- a/flexdef.h +++ b/flexdef.h @@ -366,12 +366,12 @@ extern int yymore_really_used, reject_really_used; * * action_array - array to hold the rule actions * action_size - size of action_array - * prolog - pointer to where the prolog starts in action_array + * defs1_offset - index where the user's section 1 definitions start + * in action_array + * prolog_offset - index where the prolog starts in action_array * action_offset - index where the non-prolog starts in action_array * action_index - index where the next action should go, with respect - * to "action" - * action - pointer to where non-prolog starts; equal to - * &action_array[action_offset] + * to "action_array" */ extern int datapos, dataline, linenum; @@ -383,8 +383,9 @@ extern char **input_files; extern int num_input_files; extern char *program_name; -extern char *action_array, *prolog, *action; -extern int action_size, action_offset, action_index; +extern char *action_array; +extern int action_size; +extern int defs1_offset, prolog_offset, action_offset, action_index; /* Variables for stack of states having only one out-transition: @@ -738,6 +739,11 @@ extern void lerrsf PROTO((char[], char[])); /* Spit out a "# line" statement. */ extern void line_directive_out PROTO((FILE*)); +/* Mark the current position in the action array as the end of the section 1 + * user defs. + */ +extern void mark_defs1 PROTO((void)); + /* Mark the current position in the action array as the end of the prolog. */ extern void mark_prolog PROTO((void)); @@ -750,7 +756,7 @@ extern void mkdata PROTO((int)); /* generate a data statement */ extern int myctoi PROTO((char [])); /* Return a printable version of the given character, which might be - * 8-bit + * 8-bit. */ extern char *readable_form PROTO((int)); diff --git a/gen.c b/gen.c index 1c47e0a..5d527e0 100644 --- a/gen.c +++ b/gen.c @@ -1221,10 +1221,38 @@ void make_tables() indent_puts( "#define YY_MORE_ADJ 0" ); } + fputs( &action_array[defs1_offset], stdout ); + + skelout(); + + if ( ! C_plus_plus ) + { + if ( use_read ) + { + printf( +"\tif ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \\\n" ); + printf( + "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );\n" ); + } + + else + { + printf( + "\tif ( yy_current_buffer->is_interactive ) \\\n" ); + printf( + "\t\tresult = (buf[0] = getc( yyin )) == EOF ? 0 : 1; \\\n" ); + printf( +"\telse if ( ((result = fread( (char *) buf, 1, max_size, yyin )) == 0)\\\n" ); + printf( "\t\t && ferror( yyin ) ) \\\n" ); + printf( + "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );\n" ); + } + } + skelout(); /* Copy prolog to output file. */ - fputs( prolog, stdout ); + fputs( &action_array[prolog_offset], stdout ); skelout(); @@ -1323,7 +1351,7 @@ void make_tables() skelout(); indent_up(); gen_bu_action(); - fputs( action, stdout ); + fputs( &action_array[action_offset], stdout ); /* generate cases for any missing EOF rules */ for ( i = 1; i <= lastsc; ++i ) diff --git a/main.c b/main.c index aa03217..cee89ad 100644 --- a/main.c +++ b/main.c @@ -58,8 +58,8 @@ int yymore_really_used, reject_really_used; int datapos, dataline, linenum; FILE *skelfile = NULL; int skel_ind = 0; -char *action_array, *prolog, *action; -int action_size, action_offset, action_index; +char *action_array; +int action_size, defs1_offset, prolog_offset, action_offset, action_index; char *infilename = NULL; int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE]; int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp; @@ -442,9 +442,9 @@ char **argv; /* Initialize dynamic array for holding the rule actions. */ action_size = 2048; /* default size of action array in bytes */ - prolog = action = action_array = - allocate_character_array( action_size ); - action_offset = action_index = 0; + action_array = allocate_character_array( action_size ); + defs1_offset = prolog_offset = action_offset = action_index = 0; + action_array[0] = '\0'; program_name = argv[0]; @@ -806,6 +806,8 @@ void readin() { skelout(); + line_directive_out( (FILE *) 0 ); + if ( yyparse() ) { pinpoint_message( "fatal parse error" ); @@ -837,34 +839,6 @@ void readin() if ( ddebug ) puts( "\n#define FLEX_DEBUG" ); - skelout(); - - if ( ! C_plus_plus ) - { - if ( use_read ) - { - printf( -"\tif ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \\\n" ); - printf( - "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );\n" ); - } - - else - { - printf( - "\tif ( yy_current_buffer->is_interactive ) \\\n" ); - printf( - "\t\tresult = (buf[0] = getc( yyin )) == EOF ? 0 : 1; \\\n" ); - printf( -"\telse if ( ((result = fread( (char *) buf, 1, max_size, yyin )) == 0)\\\n" ); - printf( "\t\t && ferror( yyin ) ) \\\n" ); - printf( - "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );\n" ); - } - } - - skelout(); - if ( lex_compat ) { printf( "FILE *yyin = stdin, *yyout = stdout;\n" ); @@ -874,26 +848,10 @@ void readin() else printf( "FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;\n" ); - skelout(); - if ( C_plus_plus ) printf( "\n#include \"FlexLexer.h\"\n" ); - line_directive_out( stdout ); - - if ( useecs ) - numecs = cre8ecs( nextecm, ecgroup, csize ); else - numecs = csize; - - /* Now map the equivalence class for NUL to its expected place. */ - ecgroup[0] = ecgroup[csize]; - NUL_ec = abs( ecgroup[0] ); - - if ( useecs ) - ccl2ecl(); - - if ( ! C_plus_plus ) { if ( yytext_is_array ) { @@ -913,8 +871,19 @@ void readin() puts( "#define yytext_ptr yytext" ); } } - } + if ( useecs ) + numecs = cre8ecs( nextecm, ecgroup, csize ); + else + numecs = csize; + + /* Now map the equivalence class for NUL to its expected place. */ + ecgroup[0] = ecgroup[csize]; + NUL_ec = abs( ecgroup[0] ); + + if ( useecs ) + ccl2ecl(); + } /* set_up_initial_allocations - allocate memory for internal tables */ diff --git a/misc.c b/misc.c index cb82151..94b41f2 100644 --- a/misc.c +++ b/misc.c @@ -43,16 +43,14 @@ char *new_text; { int len = strlen( new_text ); - while ( len + action_index + action_offset >= action_size - 10 - /* slop */ ) + while ( len + action_index >= action_size - 10 /* slop */ ) { action_size *= 2; - prolog = action_array = + action_array = reallocate_character_array( action_array, action_size ); - action = &action_array[action_offset]; } - strcpy( &action[action_index], new_text ); + strcpy( &action_array[action_index], new_text ); action_index += len; } @@ -408,17 +406,27 @@ FILE *output_file; } +/* mark_defs1 - mark the current position in the action array as + * representing where the user's section 1 definitions end + * and the prolog begins + */ +void mark_defs1() + { + defs1_offset = 0; + action_array[action_index++] = '\0'; + action_offset = prolog_offset = action_index; + action_array[action_index] = '\0'; + } + + /* mark_prolog - mark the current position in the action array as - * representing the action prolog + * representing the end of the action prolog */ void mark_prolog() { - prolog = action_array; action_array[action_index++] = '\0'; action_offset = action_index; - action = &action_array[action_offset]; - action_index = 0; - action[action_index] = '\0'; + action_array[action_index] = '\0'; } diff --git a/scan.l b/scan.l index f19f87e..a555624 100644 --- a/scan.l +++ b/scan.l @@ -35,7 +35,6 @@ #define ACTION_ECHO add_action( yytext ) #define MARK_END_OF_PROLOG mark_prolog(); -#undef YY_DECL #define YY_DECL \ int flexscan() @@ -89,12 +88,12 @@ CCL_CHAR ([^\\\n\]]|{ESCSEQ}) ^{WS} indented_code = true; BEGIN(CODEBLOCK); -^"/*" ECHO; BEGIN(C_COMMENT); +^"/*" ACTION_ECHO; BEGIN(C_COMMENT); ^"%s"{NAME}? return SCDECL; ^"%x"{NAME}? return XSCDECL; ^"%{".*{NL} { ++linenum; - line_directive_out( stdout ); + line_directive_out( (FILE *) 0 ); indented_code = false; BEGIN(CODEBLOCK); } @@ -103,8 +102,9 @@ CCL_CHAR ([^\\\n\]]|{ESCSEQ}) ^"%%".* { sectnum = 2; - line_directive_out( stdout ); bracelevel = 0; + mark_defs1(); + line_directive_out( (FILE *) 0 ); BEGIN(SECT2PROLOG); return SECTEND; } @@ -149,20 +149,20 @@ CCL_CHAR ([^\\\n\]]|{ESCSEQ}) {OPTWS}{NL} ++linenum; return '\n'; -"*/" ECHO; BEGIN(INITIAL); -"*/".*{NL} ++linenum; ECHO; BEGIN(INITIAL); -[^*\n]+ ECHO; -"*" ECHO; -{NL} ++linenum; ECHO; +"*/" ACTION_ECHO; BEGIN(INITIAL); +"*/".*{NL} ++linenum; ACTION_ECHO; BEGIN(INITIAL); +[^*\n]+ ACTION_ECHO; +"*" ACTION_ECHO; +{NL} ++linenum; ACTION_ECHO; ^"%}".*{NL} ++linenum; BEGIN(INITIAL); -"reject" ECHO; CHECK_REJECT(yytext); -"yymore" ECHO; CHECK_YYMORE(yytext); -{NAME}|{NOT_NAME}|. ECHO; +"reject" ACTION_ECHO; CHECK_REJECT(yytext); +"yymore" ACTION_ECHO; CHECK_YYMORE(yytext); +{NAME}|{NOT_NAME}|. ACTION_ECHO; {NL} { ++linenum; - ECHO; + ACTION_ECHO; if ( indented_code ) BEGIN(INITIAL); } @@ -223,7 +223,7 @@ CCL_CHAR ([^\\\n\]]|{ESCSEQ}) if ( bracelevel <= 0 ) { /* not in %{ ... %} */ yyless( 0 ); /* put it all back */ - MARK_END_OF_PROLOG; + mark_prolog(); BEGIN(SECT2); } else @@ -234,7 +234,7 @@ CCL_CHAR ([^\\\n\]]|{ESCSEQ}) {NL} ++linenum; ACTION_ECHO; <> { - MARK_END_OF_PROLOG; + mark_prolog(); sectnum = 0; yyterminate(); /* to stop the parser */ }