123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709 |
- #include "flexdef.h"
- int dupmachine PROTO((int));
- void mkxtion PROTO((int, int));
- void add_accept( mach, accepting_number )
- int mach, accepting_number;
- {
-
- if ( transchar[finalst[mach]] == SYM_EPSILON )
- accptnum[finalst[mach]] = accepting_number;
- else
- {
- int astate = mkstate( SYM_EPSILON );
- accptnum[astate] = accepting_number;
- (void) link_machines( mach, astate );
- }
- }
- int copysingl( singl, num )
- int singl, num;
- {
- int copy, i;
- copy = mkstate( SYM_EPSILON );
- for ( i = 1; i <= num; ++i )
- copy = link_machines( copy, dupmachine( singl ) );
- return copy;
- }
- void dumpnfa( state1 )
- int state1;
- {
- int sym, tsp1, tsp2, anum, ns;
- fprintf( stderr,
- _( "\n\n********** beginning dump of nfa with start state %d\n" ),
- state1 );
-
-
- for ( ns = 1; ns <= lastnfa; ++ns )
- {
- fprintf( stderr, _( "state # %4d\t" ), ns );
- sym = transchar[ns];
- tsp1 = trans1[ns];
- tsp2 = trans2[ns];
- anum = accptnum[ns];
- fprintf( stderr, "%3d: %4d, %4d", sym, tsp1, tsp2 );
- if ( anum != NIL )
- fprintf( stderr, " [%d]", anum );
- fprintf( stderr, "\n" );
- }
- fprintf( stderr, _( "********** end of dump\n" ) );
- }
- int dupmachine( mach )
- int mach;
- {
- int i, init, state_offset;
- int state = 0;
- int last = lastst[mach];
- for ( i = firstst[mach]; i <= last; ++i )
- {
- state = mkstate( transchar[i] );
- if ( trans1[i] != NO_TRANSITION )
- {
- mkxtion( finalst[state], trans1[i] + state - i );
- if ( transchar[i] == SYM_EPSILON &&
- trans2[i] != NO_TRANSITION )
- mkxtion( finalst[state],
- trans2[i] + state - i );
- }
- accptnum[state] = accptnum[i];
- }
- if ( state == 0 )
- flexfatal( _( "empty machine in dupmachine()" ) );
- state_offset = state - i + 1;
- init = mach + state_offset;
- firstst[init] = firstst[mach] + state_offset;
- finalst[init] = finalst[mach] + state_offset;
- lastst[init] = lastst[mach] + state_offset;
- return init;
- }
- void finish_rule( mach, variable_trail_rule, headcnt, trailcnt )
- int mach, variable_trail_rule, headcnt, trailcnt;
- {
- char action_text[MAXLINE];
- add_accept( mach, num_rules );
-
- rule_linenum[num_rules] = linenum;
-
- if ( continued_action )
- --rule_linenum[num_rules];
- sprintf( action_text, "case %d:\n", num_rules );
- add_action( action_text );
- if ( variable_trail_rule )
- {
- rule_type[num_rules] = RULE_VARIABLE;
- if ( performance_report > 0 )
- fprintf( stderr,
- _( "Variable trailing context rule at line %d\n" ),
- rule_linenum[num_rules] );
- variable_trailing_context_rules = true;
- }
- else
- {
- rule_type[num_rules] = RULE_NORMAL;
- if ( headcnt > 0 || trailcnt > 0 )
- {
-
- char *scanner_cp = "yy_c_buf_p = yy_cp";
- char *scanner_bp = "yy_bp";
- add_action(
- "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */\n" );
- if ( headcnt > 0 )
- {
- sprintf( action_text, "%s = %s + %d;\n",
- scanner_cp, scanner_bp, headcnt );
- add_action( action_text );
- }
- else
- {
- sprintf( action_text, "%s -= %d;\n",
- scanner_cp, trailcnt );
- add_action( action_text );
- }
- add_action(
- "YY_DO_BEFORE_ACTION; /* set up yytext again */\n" );
- }
- }
-
- if ( ! continued_action )
- add_action( "YY_RULE_SETUP\n" );
- line_directive_out( (FILE *) 0, 1 );
- }
- int link_machines( first, last )
- int first, last;
- {
- if ( first == NIL )
- return last;
- else if ( last == NIL )
- return first;
- else
- {
- mkxtion( finalst[first], last );
- finalst[first] = finalst[last];
- lastst[first] = MAX( lastst[first], lastst[last] );
- firstst[first] = MIN( firstst[first], firstst[last] );
- return first;
- }
- }
- void mark_beginning_as_normal( mach )
- int mach;
- {
- switch ( state_type[mach] )
- {
- case STATE_NORMAL:
-
- return;
- case STATE_TRAILING_CONTEXT:
- state_type[mach] = STATE_NORMAL;
- if ( transchar[mach] == SYM_EPSILON )
- {
- if ( trans1[mach] != NO_TRANSITION )
- mark_beginning_as_normal(
- trans1[mach] );
- if ( trans2[mach] != NO_TRANSITION )
- mark_beginning_as_normal(
- trans2[mach] );
- }
- break;
- default:
- flexerror(
- _( "bad state type in mark_beginning_as_normal()" ) );
- break;
- }
- }
- int mkbranch( first, second )
- int first, second;
- {
- int eps;
- if ( first == NO_TRANSITION )
- return second;
- else if ( second == NO_TRANSITION )
- return first;
- eps = mkstate( SYM_EPSILON );
- mkxtion( eps, first );
- mkxtion( eps, second );
- return eps;
- }
- int mkclos( state )
- int state;
- {
- return mkopt( mkposcl( state ) );
- }
- int mkopt( mach )
- int mach;
- {
- int eps;
- if ( ! SUPER_FREE_EPSILON(finalst[mach]) )
- {
- eps = mkstate( SYM_EPSILON );
- mach = link_machines( mach, eps );
- }
-
- eps = mkstate( SYM_EPSILON );
- mach = link_machines( eps, mach );
- mkxtion( mach, finalst[mach] );
- return mach;
- }
- int mkor( first, second )
- int first, second;
- {
- int eps, orend;
- if ( first == NIL )
- return second;
- else if ( second == NIL )
- return first;
- else
- {
-
- eps = mkstate( SYM_EPSILON );
- first = link_machines( eps, first );
- mkxtion( first, second );
- if ( SUPER_FREE_EPSILON(finalst[first]) &&
- accptnum[finalst[first]] == NIL )
- {
- orend = finalst[first];
- mkxtion( finalst[second], orend );
- }
- else if ( SUPER_FREE_EPSILON(finalst[second]) &&
- accptnum[finalst[second]] == NIL )
- {
- orend = finalst[second];
- mkxtion( finalst[first], orend );
- }
- else
- {
- eps = mkstate( SYM_EPSILON );
- first = link_machines( first, eps );
- orend = finalst[first];
- mkxtion( finalst[second], orend );
- }
- }
- finalst[first] = orend;
- return first;
- }
- int mkposcl( state )
- int state;
- {
- int eps;
- if ( SUPER_FREE_EPSILON(finalst[state]) )
- {
- mkxtion( finalst[state], state );
- return state;
- }
- else
- {
- eps = mkstate( SYM_EPSILON );
- mkxtion( eps, state );
- return link_machines( state, eps );
- }
- }
- int mkrep( mach, lb, ub )
- int mach, lb, ub;
- {
- int base_mach, tail, copy, i;
- base_mach = copysingl( mach, lb - 1 );
- if ( ub == INFINITY )
- {
- copy = dupmachine( mach );
- mach = link_machines( mach,
- link_machines( base_mach, mkclos( copy ) ) );
- }
- else
- {
- tail = mkstate( SYM_EPSILON );
- for ( i = lb; i < ub; ++i )
- {
- copy = dupmachine( mach );
- tail = mkopt( link_machines( copy, tail ) );
- }
- mach = link_machines( mach, link_machines( base_mach, tail ) );
- }
- return mach;
- }
- int mkstate( sym )
- int sym;
- {
- if ( ++lastnfa >= current_mns )
- {
- if ( (current_mns += MNS_INCREMENT) >= maximum_mns )
- lerrif(
- _( "input rules are too complicated (>= %d NFA states)" ),
- current_mns );
- ++num_reallocs;
- firstst = reallocate_integer_array( firstst, current_mns );
- lastst = reallocate_integer_array( lastst, current_mns );
- finalst = reallocate_integer_array( finalst, current_mns );
- transchar = reallocate_integer_array( transchar, current_mns );
- trans1 = reallocate_integer_array( trans1, current_mns );
- trans2 = reallocate_integer_array( trans2, current_mns );
- accptnum = reallocate_integer_array( accptnum, current_mns );
- assoc_rule =
- reallocate_integer_array( assoc_rule, current_mns );
- state_type =
- reallocate_integer_array( state_type, current_mns );
- }
- firstst[lastnfa] = lastnfa;
- finalst[lastnfa] = lastnfa;
- lastst[lastnfa] = lastnfa;
- transchar[lastnfa] = sym;
- trans1[lastnfa] = NO_TRANSITION;
- trans2[lastnfa] = NO_TRANSITION;
- accptnum[lastnfa] = NIL;
- assoc_rule[lastnfa] = num_rules;
- state_type[lastnfa] = current_state_type;
-
- if ( sym < 0 )
- {
-
- }
- else if ( sym == SYM_EPSILON )
- ++numeps;
- else
- {
- check_char( sym );
- if ( useecs )
-
- mkechar( sym ? sym : csize, nextecm, ecgroup );
- }
- return lastnfa;
- }
- void mkxtion( statefrom, stateto )
- int statefrom, stateto;
- {
- if ( trans1[statefrom] == NO_TRANSITION )
- trans1[statefrom] = stateto;
- else if ( (transchar[statefrom] != SYM_EPSILON) ||
- (trans2[statefrom] != NO_TRANSITION) )
- flexfatal( _( "found too many transitions in mkxtion()" ) );
- else
- {
- ++eps2;
- trans2[statefrom] = stateto;
- }
- }
- void new_rule()
- {
- if ( ++num_rules >= current_max_rules )
- {
- ++num_reallocs;
- current_max_rules += MAX_RULES_INCREMENT;
- rule_type = reallocate_integer_array( rule_type,
- current_max_rules );
- rule_linenum = reallocate_integer_array( rule_linenum,
- current_max_rules );
- rule_useful = reallocate_integer_array( rule_useful,
- current_max_rules );
- }
- if ( num_rules > MAX_RULE )
- lerrif( _( "too many rules (> %d)!" ), MAX_RULE );
- rule_linenum[num_rules] = linenum;
- rule_useful[num_rules] = false;
- }
|