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 are permitted provided
15 * that: (1) source distributions retain this entire copyright notice and
16 * comment, and (2) distributions including binaries display the following
17 * acknowledgement: ``This product includes software developed by the
18 * University of California, Berkeley and its contributors'' in the
19 * documentation or other materials provided with the distribution and in
20 * all advertising materials mentioning features or use of this software.
21 * Neither the name of the University nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * 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 $ */
30 /* $FreeBSD: src/usr.bin/lex/nfa.c,v 1.5 1999/10/27 07:56:46 obrien Exp $ */
35 /* declare functions that have forward references */
37 int dupmachine PROTO((int));
38 void mkxtion PROTO((int, int));
41 /* add_accept - add an accepting state to a machine
43 * accepting_number becomes mach's accepting number.
46 void add_accept( mach, accepting_number )
47 int mach, accepting_number;
49 /* Hang the accepting number off an epsilon state. if it is associated
50 * with a state that has a non-epsilon out-transition, then the state
51 * will accept BEFORE it makes that transition, i.e., one character
55 if ( transchar[finalst[mach]] == SYM_EPSILON )
56 accptnum[finalst[mach]] = accepting_number;
60 int astate = mkstate( SYM_EPSILON );
61 accptnum[astate] = accepting_number;
62 (void) link_machines( mach, astate );
67 /* copysingl - make a given number of copies of a singleton machine
71 * newsng = copysingl( singl, num );
73 * newsng - a new singleton composed of num copies of singl
74 * singl - a singleton machine
75 * num - the number of copies of singl to be present in newsng
78 int copysingl( singl, num )
83 copy = mkstate( SYM_EPSILON );
85 for ( i = 1; i <= num; ++i )
86 copy = link_machines( copy, dupmachine( singl ) );
92 /* dumpnfa - debugging routine to write out an nfa */
94 void dumpnfa( state1 )
98 int sym, tsp1, tsp2, anum, ns;
101 _( "\n\n********** beginning dump of nfa with start state %d\n" ),
104 /* We probably should loop starting at firstst[state1] and going to
105 * lastst[state1], but they're not maintained properly when we "or"
106 * all of the rules together. So we use our knowledge that the machine
107 * starts at state 1 and ends at lastnfa.
110 /* for ( ns = firstst[state1]; ns <= lastst[state1]; ++ns ) */
111 for ( ns = 1; ns <= lastnfa; ++ns )
113 fprintf( stderr, _( "state # %4d\t" ), ns );
120 fprintf( stderr, "%3d: %4d, %4d", sym, tsp1, tsp2 );
123 fprintf( stderr, " [%d]", anum );
125 fprintf( stderr, "\n" );
128 fprintf( stderr, _( "********** end of dump\n" ) );
132 /* dupmachine - make a duplicate of a given machine
136 * copy = dupmachine( mach );
138 * copy - holds duplicate of mach
139 * mach - machine to be duplicated
141 * note that the copy of mach is NOT an exact duplicate; rather, all the
142 * transition states values are adjusted so that the copy is self-contained,
143 * as the original should have been.
145 * also note that the original MUST be contiguous, with its low and high
146 * states accessible by the arrays firstst and lastst
149 int dupmachine( mach )
152 int i, init, state_offset;
154 int last = lastst[mach];
156 for ( i = firstst[mach]; i <= last; ++i )
158 state = mkstate( transchar[i] );
160 if ( trans1[i] != NO_TRANSITION )
162 mkxtion( finalst[state], trans1[i] + state - i );
164 if ( transchar[i] == SYM_EPSILON &&
165 trans2[i] != NO_TRANSITION )
166 mkxtion( finalst[state],
167 trans2[i] + state - i );
170 accptnum[state] = accptnum[i];
174 flexfatal( _( "empty machine in dupmachine()" ) );
176 state_offset = state - i + 1;
178 init = mach + state_offset;
179 firstst[init] = firstst[mach] + state_offset;
180 finalst[init] = finalst[mach] + state_offset;
181 lastst[init] = lastst[mach] + state_offset;
187 /* finish_rule - finish up the processing for a rule
189 * An accepting number is added to the given machine. If variable_trail_rule
190 * is true then the rule has trailing context and both the head and trail
191 * are variable size. Otherwise if headcnt or trailcnt is non-zero then
192 * the machine recognizes a pattern with trailing context and headcnt is
193 * the number of characters in the matched part of the pattern, or zero
194 * if the matched part has variable length. trailcnt is the number of
195 * trailing context characters in the pattern, or zero if the trailing
196 * context has variable length.
199 void finish_rule( mach, variable_trail_rule, headcnt, trailcnt )
200 int mach, variable_trail_rule, headcnt, trailcnt;
202 char action_text[MAXLINE];
204 add_accept( mach, num_rules );
206 /* We did this in new_rule(), but it often gets the wrong
207 * number because we do it before we start parsing the current rule.
209 rule_linenum[num_rules] = linenum;
211 /* If this is a continued action, then the line-number has already
212 * been updated, giving us the wrong number.
214 if ( continued_action )
215 --rule_linenum[num_rules];
217 sprintf( action_text, "case %d:\n", num_rules );
218 add_action( action_text );
220 if ( variable_trail_rule )
222 rule_type[num_rules] = RULE_VARIABLE;
224 if ( performance_report > 0 )
226 _( "Variable trailing context rule at line %d\n" ),
227 rule_linenum[num_rules] );
229 variable_trailing_context_rules = true;
234 rule_type[num_rules] = RULE_NORMAL;
236 if ( headcnt > 0 || trailcnt > 0 )
238 /* Do trailing context magic to not match the trailing
241 char *scanner_cp = "yy_c_buf_p = yy_cp";
242 char *scanner_bp = "yy_bp";
245 "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */\n" );
249 sprintf( action_text, "%s = %s + %d;\n",
250 scanner_cp, scanner_bp, headcnt );
251 add_action( action_text );
256 sprintf( action_text, "%s -= %d;\n",
257 scanner_cp, trailcnt );
258 add_action( action_text );
262 "YY_DO_BEFORE_ACTION; /* set up yytext again */\n" );
266 /* Okay, in the action code at this point yytext and yyleng have
267 * their proper final values for this rule, so here's the point
268 * to do any user action. But don't do it for continued actions,
269 * as that'll result in multiple YY_RULE_SETUP's.
271 if ( ! continued_action )
272 add_action( "YY_RULE_SETUP\n" );
274 line_directive_out( (FILE *) 0, 1 );
278 /* link_machines - connect two machines together
282 * new = link_machines( first, last );
284 * new - a machine constructed by connecting first to last
285 * first - the machine whose successor is to be last
286 * last - the machine whose predecessor is to be first
288 * note: this routine concatenates the machine first with the machine
289 * last to produce a machine new which will pattern-match first first
290 * and then last, and will fail if either of the sub-patterns fails.
291 * FIRST is set to new by the operation. last is unmolested.
294 int link_machines( first, last )
300 else if ( last == NIL )
305 mkxtion( finalst[first], last );
306 finalst[first] = finalst[last];
307 lastst[first] = MAX( lastst[first], lastst[last] );
308 firstst[first] = MIN( firstst[first], firstst[last] );
315 /* mark_beginning_as_normal - mark each "beginning" state in a machine
316 * as being a "normal" (i.e., not trailing context-
319 * The "beginning" states are the epsilon closure of the first state
322 void mark_beginning_as_normal( mach )
325 switch ( state_type[mach] )
328 /* Oh, we've already visited here. */
331 case STATE_TRAILING_CONTEXT:
332 state_type[mach] = STATE_NORMAL;
334 if ( transchar[mach] == SYM_EPSILON )
336 if ( trans1[mach] != NO_TRANSITION )
337 mark_beginning_as_normal(
340 if ( trans2[mach] != NO_TRANSITION )
341 mark_beginning_as_normal(
348 _( "bad state type in mark_beginning_as_normal()" ) );
354 /* mkbranch - make a machine that branches to two machines
358 * branch = mkbranch( first, second );
360 * branch - a machine which matches either first's pattern or second's
361 * first, second - machines whose patterns are to be or'ed (the | operator)
363 * Note that first and second are NEITHER destroyed by the operation. Also,
364 * the resulting machine CANNOT be used with any other "mk" operation except
365 * more mkbranch's. Compare with mkor()
368 int mkbranch( first, second )
373 if ( first == NO_TRANSITION )
376 else if ( second == NO_TRANSITION )
379 eps = mkstate( SYM_EPSILON );
381 mkxtion( eps, first );
382 mkxtion( eps, second );
388 /* mkclos - convert a machine into a closure
391 * new = mkclos( state );
393 * new - a new state which matches the closure of "state"
399 return mkopt( mkposcl( state ) );
403 /* mkopt - make a machine optional
407 * new = mkopt( mach );
409 * new - a machine which optionally matches whatever mach matched
410 * mach - the machine to make optional
413 * 1. mach must be the last machine created
414 * 2. mach is destroyed by the call
422 if ( ! SUPER_FREE_EPSILON(finalst[mach]) )
424 eps = mkstate( SYM_EPSILON );
425 mach = link_machines( mach, eps );
428 /* Can't skimp on the following if FREE_EPSILON(mach) is true because
429 * some state interior to "mach" might point back to the beginning
432 eps = mkstate( SYM_EPSILON );
433 mach = link_machines( eps, mach );
435 mkxtion( mach, finalst[mach] );
441 /* mkor - make a machine that matches either one of two machines
445 * new = mkor( first, second );
447 * new - a machine which matches either first's pattern or second's
448 * first, second - machines whose patterns are to be or'ed (the | operator)
450 * note that first and second are both destroyed by the operation
451 * the code is rather convoluted because an attempt is made to minimize
452 * the number of epsilon states needed
455 int mkor( first, second )
463 else if ( second == NIL )
468 /* See comment in mkopt() about why we can't use the first
469 * state of "first" or "second" if they satisfy "FREE_EPSILON".
471 eps = mkstate( SYM_EPSILON );
473 first = link_machines( eps, first );
475 mkxtion( first, second );
477 if ( SUPER_FREE_EPSILON(finalst[first]) &&
478 accptnum[finalst[first]] == NIL )
480 orend = finalst[first];
481 mkxtion( finalst[second], orend );
484 else if ( SUPER_FREE_EPSILON(finalst[second]) &&
485 accptnum[finalst[second]] == NIL )
487 orend = finalst[second];
488 mkxtion( finalst[first], orend );
493 eps = mkstate( SYM_EPSILON );
495 first = link_machines( first, eps );
496 orend = finalst[first];
498 mkxtion( finalst[second], orend );
502 finalst[first] = orend;
507 /* mkposcl - convert a machine into a positive closure
510 * new = mkposcl( state );
512 * new - a machine matching the positive closure of "state"
520 if ( SUPER_FREE_EPSILON(finalst[state]) )
522 mkxtion( finalst[state], state );
528 eps = mkstate( SYM_EPSILON );
529 mkxtion( eps, state );
530 return link_machines( state, eps );
535 /* mkrep - make a replicated machine
538 * new = mkrep( mach, lb, ub );
540 * new - a machine that matches whatever "mach" matched from "lb"
541 * number of times to "ub" number of times
544 * if "ub" is INFINITY then "new" matches "lb" or more occurrences of "mach"
547 int mkrep( mach, lb, ub )
550 int base_mach, tail, copy, i;
552 base_mach = copysingl( mach, lb - 1 );
554 if ( ub == INFINITY )
556 copy = dupmachine( mach );
557 mach = link_machines( mach,
558 link_machines( base_mach, mkclos( copy ) ) );
563 tail = mkstate( SYM_EPSILON );
565 for ( i = lb; i < ub; ++i )
567 copy = dupmachine( mach );
568 tail = mkopt( link_machines( copy, tail ) );
571 mach = link_machines( mach, link_machines( base_mach, tail ) );
578 /* mkstate - create a state with a transition on a given symbol
582 * state = mkstate( sym );
584 * state - a new state matching sym
585 * sym - the symbol the new state is to have an out-transition on
587 * note that this routine makes new states in ascending order through the
588 * state array (and increments LASTNFA accordingly). The routine DUPMACHINE
589 * relies on machines being made in ascending order and that they are
590 * CONTIGUOUS. Change it and you will have to rewrite DUPMACHINE (kludge
591 * that it admittedly is)
597 if ( ++lastnfa >= current_mns )
599 if ( (current_mns += MNS_INCREMENT) >= MAXIMUM_MNS )
601 _( "input rules are too complicated (>= %d NFA states)" ),
606 firstst = reallocate_integer_array( firstst, current_mns );
607 lastst = reallocate_integer_array( lastst, current_mns );
608 finalst = reallocate_integer_array( finalst, current_mns );
609 transchar = reallocate_integer_array( transchar, current_mns );
610 trans1 = reallocate_integer_array( trans1, current_mns );
611 trans2 = reallocate_integer_array( trans2, current_mns );
612 accptnum = reallocate_integer_array( accptnum, current_mns );
614 reallocate_integer_array( assoc_rule, current_mns );
616 reallocate_integer_array( state_type, current_mns );
619 firstst[lastnfa] = lastnfa;
620 finalst[lastnfa] = lastnfa;
621 lastst[lastnfa] = lastnfa;
622 transchar[lastnfa] = sym;
623 trans1[lastnfa] = NO_TRANSITION;
624 trans2[lastnfa] = NO_TRANSITION;
625 accptnum[lastnfa] = NIL;
626 assoc_rule[lastnfa] = num_rules;
627 state_type[lastnfa] = current_state_type;
629 /* Fix up equivalence classes base on this transition. Note that any
630 * character which has its own transition gets its own equivalence
631 * class. Thus only characters which are only in character classes
632 * have a chance at being in the same equivalence class. E.g. "a|b"
633 * puts 'a' and 'b' into two different equivalence classes. "[ab]"
634 * puts them in the same equivalence class (barring other differences
635 * elsewhere in the input).
640 /* We don't have to update the equivalence classes since
641 * that was already done when the ccl was created for the
646 else if ( sym == SYM_EPSILON )
654 /* Map NUL's to csize. */
655 mkechar( sym ? sym : csize, nextecm, ecgroup );
662 /* mkxtion - make a transition from one state to another
666 * mkxtion( statefrom, stateto );
668 * statefrom - the state from which the transition is to be made
669 * stateto - the state to which the transition is to be made
672 void mkxtion( statefrom, stateto )
673 int statefrom, stateto;
675 if ( trans1[statefrom] == NO_TRANSITION )
676 trans1[statefrom] = stateto;
678 else if ( (transchar[statefrom] != SYM_EPSILON) ||
679 (trans2[statefrom] != NO_TRANSITION) )
680 flexfatal( _( "found too many transitions in mkxtion()" ) );
683 { /* second out-transition for an epsilon state */
685 trans2[statefrom] = stateto;
689 /* new_rule - initialize for a new rule */
693 if ( ++num_rules >= current_max_rules )
696 current_max_rules += MAX_RULES_INCREMENT;
697 rule_type = reallocate_integer_array( rule_type,
699 rule_linenum = reallocate_integer_array( rule_linenum,
701 rule_useful = reallocate_integer_array( rule_useful,
705 if ( num_rules > MAX_RULE )
706 lerrif( _( "too many rules (> %d)!" ), MAX_RULE );
708 rule_linenum[num_rules] = linenum;
709 rule_useful[num_rules] = false;