void gen_next_match(void) { /* NOTE - changes in here should be reflected in gen_next_state() and * gen_NUL_trans(). */ 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][%s]) > 0 )", char_map ); indent_up(); if ( num_backing_up > 0 ) { indent_puts( "{" ); /* } for vi */ gen_backing_up(); outc( '\n' ); } indent_puts( "++yy_cp;" ); if ( num_backing_up > 0 ) /* { for vi */ indent_puts( "}" ); indent_down(); outc( '\n' ); indent_puts( "yy_current_state = -yy_current_state;" ); } else if ( fullspd ) { indent_puts( "{" ); /* } for vi */ indent_puts( "yyconst struct yy_trans_info *yy_trans_info;\n" ); indent_puts( "YY_CHAR yy_c;\n" ); indent_put2s( "for ( yy_c = %s;", char_map ); indent_puts( " (yy_trans_info = &yy_current_state[(unsigned int) yy_c])->" ); indent_puts( "yy_verify == yy_c;" ); indent_put2s( " yy_c = %s )", char_map_2 ); indent_up(); if ( num_backing_up > 0 ) indent_puts( "{" ); /* } for vi */ indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" ); if ( num_backing_up > 0 ) { outc( '\n' ); gen_backing_up(); /* { for vi */ indent_puts( "}" ); } indent_down(); /* { for vi */ indent_puts( "}" ); } else { /* compressed */ indent_puts( "do" ); indent_up(); indent_puts( "{" ); /* } for vi */ gen_next_state( false ); indent_puts( "++yy_cp;" ); /* { for vi */ indent_puts( "}" ); indent_down(); do_indent(); if ( interactive ) out_dec( "while ( yy_base[yy_current_state] != %d );\n", jambase ); else out_dec( "while ( yy_current_state != %d );\n", jamstate ); if ( ! reject && ! interactive ) { /* Do the guaranteed-needed backing up to figure out * the match. */ indent_puts( "yy_cp = yy_last_accepting_cpos;" ); indent_puts( "yy_current_state = yy_last_accepting_state;" ); } } }
void gen_machine(char *machine_name) { struct state *s, *snext; struct transition *t, *tnext; struct task *task, *tasknext; if (states == NULL) fprintf(stderr, "%s: no states declared in machine %s\n", __func__, machine_name); /* dump forward declarations of all the states */ for (s=states; s; s=s->next) { if(s->action == ACTION_RUN || s->action == ACTION_PJMP) gen_runfunc_decl(s->function_or_machine); gen_state_decl(s->name); } /* delcare the machine start point */ fprintf(out_file, "\nstruct PINT_state_machine_s %s = {\n", machine_name); fprintf(out_file, "\t.name = \"%s\",\n", machine_name); fprintf(out_file, "\t.first_state = &ST_%s\n", states->name); fprintf(out_file, "};\n\n"); /* generate all output */ for (s=states; s; s=s->next) { gen_state_start(s->name, machine_name); gen_state_action(s->action, s->function_or_machine, s->name); if(s->action == ACTION_PJMP) { gen_pjtbl(s->name); /* if there are tasks (the action must be a pjmp) so we output * stuff for the tasks */ for (task=s->task; task; task=task->next) { gen_return_code(task->return_code); gen_next_state(TRANS_PJMP, task->task_name); } } gen_trtbl(s->name); for (t=s->transition; t; t=t->next) { gen_return_code(t->return_code); gen_next_state(t->type, t->next_state); } gen_state_end(); } /* purge for next machine */ for (s=states; s; s=snext) { if(s->task) { for (task=s->task; task; task=tasknext) { tasknext = task->next; free(task->return_code); free(task); } } for (t=s->transition; t; t=tnext) { tnext = t->next; free(t->return_code); free(t); } snext = s->next; free(s->name); free(s); } states = NULL; }
void make_tables(void) { int i; int did_eof_rule = false; skelout(); /* First, take care of YY_DO_BEFORE_ACTION depending on yymore * being used. */ set_indent( 1 ); if ( yymore_used && ! yytext_is_array ) { indent_puts( "yytext_ptr -= yy_more_len; \\" ); indent_puts( "yyleng = (int) (yy_cp - yytext_ptr); \\" ); } else indent_puts( "yyleng = (int) (yy_cp - yy_bp); \\" ); /* Now also deal with copying yytext_ptr to yytext if needed. */ skelout(); if ( yytext_is_array ) { if ( yymore_used ) indent_puts( "if ( yyleng + yy_more_offset >= YYLMAX ) \\" ); else indent_puts( "if ( yyleng >= YYLMAX ) \\" ); indent_up(); indent_puts( "YY_FATAL_ERROR( \"token too large, exceeds YYLMAX\" ); \\" ); indent_down(); if ( yymore_used ) { indent_puts( "yy_flex_strncpy( &yytext[yy_more_offset], yytext_ptr, yyleng + 1 ); \\" ); indent_puts( "yyleng += yy_more_offset; \\" ); indent_puts( "yy_prev_more_offset = yy_more_offset; \\" ); indent_puts( "yy_more_offset = 0; \\" ); } else { indent_puts( "yy_flex_strncpy( yytext, yytext_ptr, yyleng + 1 ); \\" ); } } set_indent( 0 ); skelout(); out_dec( "#define YY_NUM_RULES %d\n", num_rules ); out_dec( "#define YY_END_OF_BUFFER %d\n", num_rules + 1 ); if ( fullspd ) { /* Need to define the transet type as a size large * enough to hold the biggest offset. */ int total_table_size = tblend + numecs + 1; char *trans_offset_type = (total_table_size >= MAX_SHORT || long_align) ? "long" : "short"; set_indent( 0 ); indent_puts( "struct yy_trans_info" ); indent_up(); indent_puts( "{" ); /* } for vi */ if ( long_align ) indent_puts( "long yy_verify;" ); else indent_puts( "short yy_verify;" ); /* In cases where its sister yy_verify *is* a "yes, there is * a transition", yy_nxt is the offset (in records) to the * next state. In most cases where there is no transition, * the value of yy_nxt is irrelevant. If yy_nxt is the -1th * record of a state, though, then yy_nxt is the action number * for that state. */ indent_put2s( "%s yy_nxt;", trans_offset_type ); indent_puts( "};" ); indent_down(); } if ( fullspd ) genctbl(); else if ( fulltbl ) genftbl(); else gentabs(); /* Definitions for backing up. We don't need them if REJECT * is being used because then we use an alternative backin-up * technique instead. */ if ( num_backing_up > 0 && ! reject ) { if ( ! C_plus_plus ) { indent_puts( "static yy_state_type yy_last_accepting_state;" ); indent_puts( "static char *yy_last_accepting_cpos;\n" ); } } if ( nultrans ) { out_str_dec( C_state_decl, "yy_NUL_trans", lastdfa + 1 ); for ( i = 1; i <= lastdfa; ++i ) { if ( fullspd ) out_dec( " &yy_transition[%d],\n", base[i] ); else mkdata( nultrans[i] ); } dataend(); } if ( ddebug ) { /* Spit out table mapping rules to line numbers. */ if ( ! C_plus_plus ) { indent_puts( "extern int yy_flex_debug;" ); indent_puts( "int yy_flex_debug = 1;\n" ); } out_str_dec( long_align ? C_long_decl : C_short_decl, "yy_rule_linenum", num_rules ); for ( i = 1; i < num_rules; ++i ) mkdata( rule_linenum[i] ); dataend(); } if ( reject ) { /* Declare state buffer variables. */ if ( ! C_plus_plus ) { outn( "static yy_state_type yy_state_buf[YY_BUF_SIZE + 2], *yy_state_ptr;" ); outn( "static char *yy_full_match;" ); outn( "static int yy_lp;" ); } if ( variable_trailing_context_rules ) { if ( ! C_plus_plus ) { outn( "static int yy_looking_for_trail_begin = 0;" ); outn( "static int yy_full_lp;" ); outn( "static int *yy_full_state;" ); } out_hex( "#define YY_TRAILING_MASK 0x%x\n", (unsigned int) YY_TRAILING_MASK ); out_hex( "#define YY_TRAILING_HEAD_MASK 0x%x\n", (unsigned int) YY_TRAILING_HEAD_MASK ); } outn( "#define REJECT \\" ); outn( "{ \\" ); /* } for vi */ outn( "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */ \\" ); outn( "yy_cp = yy_full_match; /* restore poss. backed-over text */ \\" ); if ( variable_trailing_context_rules ) { outn( "yy_lp = yy_full_lp; /* restore orig. accepting pos. */ \\" ); outn( "yy_state_ptr = yy_full_state; /* restore orig. state */ \\" ); outn( "yy_current_state = *yy_state_ptr; /* restore curr. state */ \\" ); } outn( "++yy_lp; \\" ); outn( "goto find_rule; \\" ); /* { for vi */ outn( "}" ); } else { outn( "/* The intent behind this definition is that it'll catch" ); outn( " * any uses of REJECT which flex missed." ); outn( " */" ); outn( "#define REJECT reject_used_but_not_detected" ); } if ( yymore_used ) { if ( ! C_plus_plus ) { if ( yytext_is_array ) { indent_puts( "static int yy_more_offset = 0;" ); indent_puts( "static int yy_prev_more_offset = 0;" ); } else { indent_puts( "static int yy_more_flag = 0;" ); indent_puts( "static int yy_more_len = 0;" ); } } if ( yytext_is_array ) { indent_puts( "#define yymore() (yy_more_offset = yy_flex_strlen( yytext ))" ); indent_puts( "#define YY_NEED_STRLEN" ); indent_puts( "#define YY_MORE_ADJ 0" ); indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET \\" ); indent_up(); indent_puts( "{ \\" ); indent_puts( "yy_more_offset = yy_prev_more_offset; \\" ); indent_puts( "yyleng -= yy_more_offset; \\" ); indent_puts( "}" ); indent_down(); } else { indent_puts( "#define yymore() (yy_more_flag = 1)" ); indent_puts( "#define YY_MORE_ADJ yy_more_len" ); indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET" ); } } else { indent_puts( "#define yymore() yymore_used_but_not_detected" ); indent_puts( "#define YY_MORE_ADJ 0" ); indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET" ); } if ( ! C_plus_plus ) { if ( yytext_is_array ) { outn( "#ifndef YYLMAX" ); outn( "#define YYLMAX 8192" ); outn( "#endif\n" ); outn( "char yytext[YYLMAX];" ); outn( "char *yytext_ptr;" ); } else outn( "char *yytext;" ); } out( &action_array[defs1_offset] ); line_directive_out( stdout, 0 ); skelout(); if ( ! C_plus_plus ) { if ( use_read ) { outn( "\tif ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \\" ); outn( "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );" ); } else { outn( "\tif ( yy_current_buffer->yy_is_interactive ) \\" ); outn( "\t\t{ \\" ); outn( "\t\tint c = '*', n; \\" ); outn( "\t\tfor ( n = 0; n < max_size && \\" ); outn( "\t\t\t (c = getc( yyin )) != EOF && c != '\\n'; ++n ) \\" ); outn( "\t\t\tbuf[n] = (char) c; \\" ); outn( "\t\tif ( c == '\\n' ) \\" ); outn( "\t\t\tbuf[n++] = (char) c; \\" ); outn( "\t\tif ( c == EOF && ferror( yyin ) ) \\" ); outn( "\t\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" ); \\" ); outn( "\t\tresult = n; \\" ); outn( "\t\t} \\" ); outn( "\telse if ( ((result = fread( buf, 1, max_size, yyin )) == 0) \\" ); outn( "\t\t && ferror( yyin ) ) \\" ); outn( "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );" ); } } skelout(); indent_puts( "#define YY_RULE_SETUP \\" ); indent_up(); if ( bol_needed ) { indent_puts( "if ( yyleng > 0 ) \\" ); indent_up(); indent_puts( "yy_current_buffer->yy_at_bol = \\" ); indent_puts( "\t\t(yytext[yyleng - 1] == '\\n'); \\" ); indent_down(); } indent_puts( "YY_USER_ACTION" ); indent_down(); skelout(); /* Copy prolog to output file. */ out( &action_array[prolog_offset] ); line_directive_out( stdout, 0 ); skelout(); set_indent( 2 ); if ( yymore_used && ! yytext_is_array ) { indent_puts( "yy_more_len = 0;" ); indent_puts( "if ( yy_more_flag )" ); indent_up(); indent_puts( "{" ); indent_puts( "yy_more_len = yy_c_buf_p - yytext_ptr;" ); indent_puts( "yy_more_flag = 0;" ); indent_puts( "}" ); indent_down(); } skelout(); gen_start_state(); /* Note, don't use any indentation. */ outn( "yy_match:" ); gen_next_match(); skelout(); set_indent( 2 ); gen_find_action(); skelout(); if ( do_yylineno ) { indent_puts( "if ( yy_act != YY_END_OF_BUFFER )" ); indent_up(); indent_puts( "{" ); indent_puts( "int yyl;" ); indent_puts( "for ( yyl = 0; yyl < yyleng; ++yyl )" ); indent_up(); indent_puts( "if ( yytext[yyl] == '\\n' )" ); indent_up(); indent_puts( "++yylineno;" ); indent_down(); indent_down(); indent_puts( "}" ); indent_down(); } skelout(); if ( ddebug ) { indent_puts( "if ( yy_flex_debug )" ); indent_up(); indent_puts( "{" ); indent_puts( "if ( yy_act == 0 )" ); indent_up(); indent_puts( C_plus_plus ? "cerr << \"--scanner backing up\\n\";" : "fprintf( stderr, \"--scanner backing up\\n\" );" ); indent_down(); do_indent(); out_dec( "else if ( yy_act < %d )\n", num_rules ); indent_up(); if ( C_plus_plus ) { indent_puts( "cerr << \"--accepting rule at line \" << yy_rule_linenum[yy_act] <<" ); indent_puts( " \"(\\\"\" << yytext << \"\\\")\\n\";" ); } else { indent_puts( "fprintf( stderr, \"--accepting rule at line %d (\\\"%s\\\")\\n\"," ); indent_puts( " yy_rule_linenum[yy_act], yytext );" ); } indent_down(); do_indent(); out_dec( "else if ( yy_act == %d )\n", num_rules ); indent_up(); if ( C_plus_plus ) { indent_puts( "cerr << \"--accepting default rule (\\\"\" << yytext << \"\\\")\\n\";" ); } else { indent_puts( "fprintf( stderr, \"--accepting default rule (\\\"%s\\\")\\n\"," ); indent_puts( " yytext );" ); } indent_down(); do_indent(); out_dec( "else if ( yy_act == %d )\n", num_rules + 1 ); indent_up(); indent_puts( C_plus_plus ? "cerr << \"--(end of buffer or a NUL)\\n\";" : "fprintf( stderr, \"--(end of buffer or a NUL)\\n\" );" ); indent_down(); do_indent(); outn( "else" ); indent_up(); if ( C_plus_plus ) { indent_puts( "cerr << \"--EOF (start condition \" << YY_START << \")\\n\";" ); } else { indent_puts( "fprintf( stderr, \"--EOF (start condition %d)\\n\", YY_START );" ); } indent_down(); indent_puts( "}" ); indent_down(); } /* Copy actions to output file. */ skelout(); indent_up(); gen_bu_action(); out( &action_array[action_offset] ); line_directive_out( stdout, 0 ); /* generate cases for any missing EOF rules */ for ( i = 1; i <= lastsc; ++i ) if ( ! sceof[i] ) { do_indent(); out_str( "case YY_STATE_EOF(%s):\n", scname[i] ); did_eof_rule = true; } if ( did_eof_rule ) { indent_up(); indent_puts( "yyterminate();" ); indent_down(); } /* Generate code for handling NUL's, if needed. */ /* First, deal with backing up and setting up yy_cp if the scanner * finds that it should JAM on the NUL. */ skelout(); set_indent( 4 ); if ( fullspd || fulltbl ) indent_puts( "yy_cp = yy_c_buf_p;" ); else { /* compressed table */ if ( ! reject && ! interactive ) { /* Do the guaranteed-needed backing up to figure * out the match. */ indent_puts( "yy_cp = yy_last_accepting_cpos;" ); indent_puts( "yy_current_state = yy_last_accepting_state;" ); } else /* Still need to initialize yy_cp, though * yy_current_state was set up by * yy_get_previous_state(). */ indent_puts( "yy_cp = yy_c_buf_p;" ); } /* Generate code for yy_get_previous_state(). */ set_indent( 1 ); skelout(); gen_start_state(); set_indent( 2 ); skelout(); gen_next_state( true ); set_indent( 1 ); skelout(); gen_NUL_trans(); skelout(); if ( do_yylineno ) { /* update yylineno inside of unput() */ indent_puts( "if ( c == '\\n' )" ); indent_up(); indent_puts( "--yylineno;" ); indent_down(); } skelout(); /* Update BOL and yylineno inside of input(). */ if ( bol_needed ) { indent_puts( "yy_current_buffer->yy_at_bol = (c == '\\n');" ); if ( do_yylineno ) { indent_puts( "if ( yy_current_buffer->yy_at_bol )" ); indent_up(); indent_puts( "++yylineno;" ); indent_down(); } } else if ( do_yylineno ) { indent_puts( "if ( c == '\\n' )" ); indent_up(); indent_puts( "++yylineno;" ); indent_down(); } skelout(); /* Copy remainder of input to output. */ line_directive_out( stdout, 1 ); if ( sectnum == 3 ) (void) flexscan(); /* copy remainder of input to output */ }