1 /* nfa - NFA construction routines */
4 * Copyright (c) 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * The United States Government has rights in this work pursuant
11 * to contract no. DE-AC03-76SF00098 between the United States
12 * Department of Energy and the University of California.
14 * Redistribution and use in source and binary forms with or without
15 * modification are permitted provided that: (1) source distributions retain
16 * this entire copyright notice and comment, and (2) distributions including
17 * binaries display the following acknowledgement: ``This product includes
18 * software developed by the University of California, Berkeley and its
19 * contributors'' in the documentation or other materials provided with the
20 * distribution and in all advertising materials mentioning features or use
21 * of this software. Neither the name of the University nor the names of
22 * its contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 /* $Header: /home/daffy/u0/vern/flex/RCS/nfa.c,v 2.17 95/03/04 16:11:42 vern Exp $ */
34 /* declare functions that have forward references */
36 int dupmachine PROTO((int));
37 void mkxtion PROTO((int, int));
40 /* add_accept - add an accepting state to a machine
42 * accepting_number becomes mach's accepting number.
45 void add_accept( mach, accepting_number )
46 int mach, accepting_number;
48 /* Hang the accepting number off an epsilon state. if it is associated
49 * with a state that has a non-epsilon out-transition, then the state
50 * will accept BEFORE it makes that transition, i.e., one character
54 if ( transchar[finalst[mach]] == SYM_EPSILON )
55 accptnum[finalst[mach]] = accepting_number;
59 int astate = mkstate( SYM_EPSILON );
60 accptnum[astate] = accepting_number;
61 (void) link_machines( mach, astate );
66 /* copysingl - make a given number of copies of a singleton machine
70 * newsng = copysingl( singl, num );
72 * newsng - a new singleton composed of num copies of singl
73 * singl - a singleton machine
74 * num - the number of copies of singl to be present in newsng
77 int copysingl( singl, num )
82 copy = mkstate( SYM_EPSILON );
84 for ( i = 1; i <= num; ++i )
85 copy = link_machines( copy, dupmachine( singl ) );
91 /* dumpnfa - debugging routine to write out an nfa */
93 void dumpnfa( state1 )
97 int sym, tsp1, tsp2, anum, ns;
100 _( "\n\n********** beginning dump of nfa with start state %d\n" ),
103 /* We probably should loop starting at firstst[state1] and going to
104 * lastst[state1], but they're not maintained properly when we "or"
105 * all of the rules together. So we use our knowledge that the machine
106 * starts at state 1 and ends at lastnfa.
109 /* for ( ns = firstst[state1]; ns <= lastst[state1]; ++ns ) */
110 for ( ns = 1; ns <= lastnfa; ++ns )
112 fprintf( stderr, _( "state # %4d\t" ), ns );
119 fprintf( stderr, "%3d: %4d, %4d", sym, tsp1, tsp2 );
122 fprintf( stderr, " [%d]", anum );
124 fprintf( stderr, "\n" );
127 fprintf( stderr, _( "********** end of dump\n" ) );
131 /* dupmachine - make a duplicate of a given machine
135 * copy = dupmachine( mach );
137 * copy - holds duplicate of mach
138 * mach - machine to be duplicated
140 * note that the copy of mach is NOT an exact duplicate; rather, all the
141 * transition states values are adjusted so that the copy is self-contained,
142 * as the original should have been.
144 * also note that the original MUST be contiguous, with its low and high
145 * states accessible by the arrays firstst and lastst
148 int dupmachine( mach )
151 int i, init, state_offset;
153 int last = lastst[mach];
155 for ( i = firstst[mach]; i <= last; ++i )
157 state = mkstate( transchar[i] );
159 if ( trans1[i] != NO_TRANSITION )
161 mkxtion( finalst[state], trans1[i] + state - i );
163 if ( transchar[i] == SYM_EPSILON &&
164 trans2[i] != NO_TRANSITION )
165 mkxtion( finalst[state],
166 trans2[i] + state - i );
169 accptnum[state] = accptnum[i];
173 flexfatal( _( "empty machine in dupmachine()" ) );
175 state_offset = state - i + 1;
177 init = mach + state_offset;
178 firstst[init] = firstst[mach] + state_offset;
179 finalst[init] = finalst[mach] + state_offset;
180 lastst[init] = lastst[mach] + state_offset;
186 /* finish_rule - finish up the processing for a rule
188 * An accepting number is added to the given machine. If variable_trail_rule
189 * is true then the rule has trailing context and both the head and trail
190 * are variable size. Otherwise if headcnt or trailcnt is non-zero then
191 * the machine recognizes a pattern with trailing context and headcnt is
192 * the number of characters in the matched part of the pattern, or zero
193 * if the matched part has variable length. trailcnt is the number of
194 * trailing context characters in the pattern, or zero if the trailing
195 * context has variable length.
198 void finish_rule( mach, variable_trail_rule, headcnt, trailcnt )
199 int mach, variable_trail_rule, headcnt, trailcnt;
201 char action_text[MAXLINE];
203 add_accept( mach, num_rules );
205 /* We did this in new_rule(), but it often gets the wrong
206 * number because we do it before we start parsing the current rule.
208 rule_linenum[num_rules] = linenum;
210 /* If this is a continued action, then the line-number has already
211 * been updated, giving us the wrong number.
213 if ( continued_action )
214 --rule_linenum[num_rules];
216 sprintf( action_text, "case %d:\n", num_rules );
217 add_action( action_text );
219 if ( variable_trail_rule )
221 rule_type[num_rules] = RULE_VARIABLE;
223 if ( performance_report > 0 )
225 _( "Variable trailing context rule at line %d\n" ),
226 rule_linenum[num_rules] );
228 variable_trailing_context_rules = true;
233 rule_type[num_rules] = RULE_NORMAL;
235 if ( headcnt > 0 || trailcnt > 0 )
237 /* Do trailing context magic to not match the trailing
240 char *scanner_cp = "yy_c_buf_p = yy_cp";
241 char *scanner_bp = "yy_bp";
244 "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */\n" );
248 sprintf( action_text, "%s = %s + %d;\n",
249 scanner_cp, scanner_bp, headcnt );
250 add_action( action_text );
255 sprintf( action_text, "%s -= %d;\n",
256 scanner_cp, trailcnt );
257 add_action( action_text );
261 "YY_DO_BEFORE_ACTION; /* set up yytext again */\n" );
265 /* Okay, in the action code at this point yytext and yyleng have
266 * their proper final values for this rule, so here's the point
267 * to do any user action. But don't do it for continued actions,
268 * as that'll result in multiple YY_RULE_SETUP's.
270 if ( ! continued_action )
271 add_action( "YY_RULE_SETUP\n" );
273 line_directive_out( (FILE *) 0, 1 );
277 /* link_machines - connect two machines together
281 * new = link_machines( first, last );
283 * new - a machine constructed by connecting first to last
284 * first - the machine whose successor is to be last
285 * last - the machine whose predecessor is to be first
287 * note: this routine concatenates the machine first with the machine
288 * last to produce a machine new which will pattern-match first first
289 * and then last, and will fail if either of the sub-patterns fails.
290 * FIRST is set to new by the operation. last is unmolested.
293 int link_machines( first, last )
299 else if ( last == NIL )
304 mkxtion( finalst[first], last );
305 finalst[first] = finalst[last];
306 lastst[first] = MAX( lastst[first], lastst[last] );
307 firstst[first] = MIN( firstst[first], firstst[last] );
314 /* mark_beginning_as_normal - mark each "beginning" state in a machine
315 * as being a "normal" (i.e., not trailing context-
318 * The "beginning" states are the epsilon closure of the first state
321 void mark_beginning_as_normal( mach )
324 switch ( state_type[mach] )
327 /* Oh, we've already visited here. */
330 case STATE_TRAILING_CONTEXT:
331 state_type[mach] = STATE_NORMAL;
333 if ( transchar[mach] == SYM_EPSILON )
335 if ( trans1[mach] != NO_TRANSITION )
336 mark_beginning_as_normal(
339 if ( trans2[mach] != NO_TRANSITION )
340 mark_beginning_as_normal(
347 _( "bad state type in mark_beginning_as_normal()" ) );
353 /* mkbranch - make a machine that branches to two machines
357 * branch = mkbranch( first, second );
359 * branch - a machine which matches either first's pattern or second's
360 * first, second - machines whose patterns are to be or'ed (the | operator)
362 * Note that first and second are NEITHER destroyed by the operation. Also,
363 * the resulting machine CANNOT be used with any other "mk" operation except
364 * more mkbranch's. Compare with mkor()
367 int mkbranch( first, second )
372 if ( first == NO_TRANSITION )
375 else if ( second == NO_TRANSITION )
378 eps = mkstate( SYM_EPSILON );
380 mkxtion( eps, first );
381 mkxtion( eps, second );
387 /* mkclos - convert a machine into a closure
390 * new = mkclos( state );
392 * new - a new state which matches the closure of "state"
398 return mkopt( mkposcl( state ) );
402 /* mkopt - make a machine optional
406 * new = mkopt( mach );
408 * new - a machine which optionally matches whatever mach matched
409 * mach - the machine to make optional
412 * 1. mach must be the last machine created
413 * 2. mach is destroyed by the call
421 if ( ! SUPER_FREE_EPSILON(finalst[mach]) )
423 eps = mkstate( SYM_EPSILON );
424 mach = link_machines( mach, eps );
427 /* Can't skimp on the following if FREE_EPSILON(mach) is true because
428 * some state interior to "mach" might point back to the beginning
431 eps = mkstate( SYM_EPSILON );
432 mach = link_machines( eps, mach );
434 mkxtion( mach, finalst[mach] );
440 /* mkor - make a machine that matches either one of two machines
444 * new = mkor( first, second );
446 * new - a machine which matches either first's pattern or second's
447 * first, second - machines whose patterns are to be or'ed (the | operator)
449 * note that first and second are both destroyed by the operation
450 * the code is rather convoluted because an attempt is made to minimize
451 * the number of epsilon states needed
454 int mkor( first, second )
462 else if ( second == NIL )
467 /* See comment in mkopt() about why we can't use the first
468 * state of "first" or "second" if they satisfy "FREE_EPSILON".
470 eps = mkstate( SYM_EPSILON );
472 first = link_machines( eps, first );
474 mkxtion( first, second );
476 if ( SUPER_FREE_EPSILON(finalst[first]) &&
477 accptnum[finalst[first]] == NIL )
479 orend = finalst[first];
480 mkxtion( finalst[second], orend );
483 else if ( SUPER_FREE_EPSILON(finalst[second]) &&
484 accptnum[finalst[second]] == NIL )
486 orend = finalst[second];
487 mkxtion( finalst[first], orend );
492 eps = mkstate( SYM_EPSILON );
494 first = link_machines( first, eps );
495 orend = finalst[first];
497 mkxtion( finalst[second], orend );
501 finalst[first] = orend;
506 /* mkposcl - convert a machine into a positive closure
509 * new = mkposcl( state );
511 * new - a machine matching the positive closure of "state"
519 if ( SUPER_FREE_EPSILON(finalst[state]) )
521 mkxtion( finalst[state], state );
527 eps = mkstate( SYM_EPSILON );
528 mkxtion( eps, state );
529 return link_machines( state, eps );
534 /* mkrep - make a replicated machine
537 * new = mkrep( mach, lb, ub );
539 * new - a machine that matches whatever "mach" matched from "lb"
540 * number of times to "ub" number of times
543 * if "ub" is INFINITY then "new" matches "lb" or more occurrences of "mach"
546 int mkrep( mach, lb, ub )
549 int base_mach, tail, copy, i;
551 base_mach = copysingl( mach, lb - 1 );
553 if ( ub == INFINITY )
555 copy = dupmachine( mach );
556 mach = link_machines( mach,
557 link_machines( base_mach, mkclos( copy ) ) );
562 tail = mkstate( SYM_EPSILON );
564 for ( i = lb; i < ub; ++i )
566 copy = dupmachine( mach );
567 tail = mkopt( link_machines( copy, tail ) );
570 mach = link_machines( mach, link_machines( base_mach, tail ) );
577 /* mkstate - create a state with a transition on a given symbol
581 * state = mkstate( sym );
583 * state - a new state matching sym
584 * sym - the symbol the new state is to have an out-transition on
586 * note that this routine makes new states in ascending order through the
587 * state array (and increments LASTNFA accordingly). The routine DUPMACHINE
588 * relies on machines being made in ascending order and that they are
589 * CONTIGUOUS. Change it and you will have to rewrite DUPMACHINE (kludge
590 * that it admittedly is)
596 if ( ++lastnfa >= current_mns )
598 if ( (current_mns += MNS_INCREMENT) >= MAXIMUM_MNS )
600 _( "input rules are too complicated (>= %d NFA states)" ),
605 firstst = reallocate_integer_array( firstst, current_mns );
606 lastst = reallocate_integer_array( lastst, current_mns );
607 finalst = reallocate_integer_array( finalst, current_mns );
608 transchar = reallocate_integer_array( transchar, current_mns );
609 trans1 = reallocate_integer_array( trans1, current_mns );
610 trans2 = reallocate_integer_array( trans2, current_mns );
611 accptnum = reallocate_integer_array( accptnum, current_mns );
613 reallocate_integer_array( assoc_rule, current_mns );
615 reallocate_integer_array( state_type, current_mns );
618 firstst[lastnfa] = lastnfa;
619 finalst[lastnfa] = lastnfa;
620 lastst[lastnfa] = lastnfa;
621 transchar[lastnfa] = sym;
622 trans1[lastnfa] = NO_TRANSITION;
623 trans2[lastnfa] = NO_TRANSITION;
624 accptnum[lastnfa] = NIL;
625 assoc_rule[lastnfa] = num_rules;
626 state_type[lastnfa] = current_state_type;
628 /* Fix up equivalence classes base on this transition. Note that any
629 * character which has its own transition gets its own equivalence
630 * class. Thus only characters which are only in character classes
631 * have a chance at being in the same equivalence class. E.g. "a|b"
632 * puts 'a' and 'b' into two different equivalence classes. "[ab]"
633 * puts them in the same equivalence class (barring other differences
634 * elsewhere in the input).
639 /* We don't have to update the equivalence classes since
640 * that was already done when the ccl was created for the
645 else if ( sym == SYM_EPSILON )
653 /* Map NUL's to csize. */
654 mkechar( sym ? sym : csize, nextecm, ecgroup );
661 /* mkxtion - make a transition from one state to another
665 * mkxtion( statefrom, stateto );
667 * statefrom - the state from which the transition is to be made
668 * stateto - the state to which the transition is to be made
671 void mkxtion( statefrom, stateto )
672 int statefrom, stateto;
674 if ( trans1[statefrom] == NO_TRANSITION )
675 trans1[statefrom] = stateto;
677 else if ( (transchar[statefrom] != SYM_EPSILON) ||
678 (trans2[statefrom] != NO_TRANSITION) )
679 flexfatal( _( "found too many transitions in mkxtion()" ) );
682 { /* second out-transition for an epsilon state */
684 trans2[statefrom] = stateto;
688 /* new_rule - initialize for a new rule */
692 if ( ++num_rules >= current_max_rules )
695 current_max_rules += MAX_RULES_INCREMENT;
696 rule_type = reallocate_integer_array( rule_type,
698 rule_linenum = reallocate_integer_array( rule_linenum,
700 rule_useful = reallocate_integer_array( rule_useful,
704 if ( num_rules > MAX_RULE )
705 lerrif( _( "too many rules (> %d)!" ), MAX_RULE );
707 rule_linenum[num_rules] = linenum;
708 rule_useful[num_rules] = false;