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 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
36 /* declare functions that have forward references */
38 int dupmachine PROTO((int));
39 void mkxtion PROTO((int, int));
42 /* add_accept - add an accepting state to a machine
44 * accepting_number becomes mach's accepting number.
47 void add_accept( mach, accepting_number )
48 int mach, accepting_number;
50 /* Hang the accepting number off an epsilon state. if it is associated
51 * with a state that has a non-epsilon out-transition, then the state
52 * will accept BEFORE it makes that transition, i.e., one character
56 if ( transchar[finalst[mach]] == SYM_EPSILON )
57 accptnum[finalst[mach]] = accepting_number;
61 int astate = mkstate( SYM_EPSILON );
62 accptnum[astate] = accepting_number;
63 (void) link_machines( mach, astate );
68 /* copysingl - make a given number of copies of a singleton machine
72 * newsng = copysingl( singl, num );
74 * newsng - a new singleton composed of num copies of singl
75 * singl - a singleton machine
76 * num - the number of copies of singl to be present in newsng
79 int copysingl( singl, num )
84 copy = mkstate( SYM_EPSILON );
86 for ( i = 1; i <= num; ++i )
87 copy = link_machines( copy, dupmachine( singl ) );
93 /* dumpnfa - debugging routine to write out an nfa */
95 void dumpnfa( state1 )
99 int sym, tsp1, tsp2, anum, ns;
102 _( "\n\n********** beginning dump of nfa with start state %d\n" ),
105 /* We probably should loop starting at firstst[state1] and going to
106 * lastst[state1], but they're not maintained properly when we "or"
107 * all of the rules together. So we use our knowledge that the machine
108 * starts at state 1 and ends at lastnfa.
111 /* for ( ns = firstst[state1]; ns <= lastst[state1]; ++ns ) */
112 for ( ns = 1; ns <= lastnfa; ++ns )
114 fprintf( stderr, _( "state # %4d\t" ), ns );
121 fprintf( stderr, "%3d: %4d, %4d", sym, tsp1, tsp2 );
124 fprintf( stderr, " [%d]", anum );
126 fprintf( stderr, "\n" );
129 fprintf( stderr, _( "********** end of dump\n" ) );
133 /* dupmachine - make a duplicate of a given machine
137 * copy = dupmachine( mach );
139 * copy - holds duplicate of mach
140 * mach - machine to be duplicated
142 * note that the copy of mach is NOT an exact duplicate; rather, all the
143 * transition states values are adjusted so that the copy is self-contained,
144 * as the original should have been.
146 * also note that the original MUST be contiguous, with its low and high
147 * states accessible by the arrays firstst and lastst
150 int dupmachine( mach )
153 int i, init, state_offset;
155 int last = lastst[mach];
157 for ( i = firstst[mach]; i <= last; ++i )
159 state = mkstate( transchar[i] );
161 if ( trans1[i] != NO_TRANSITION )
163 mkxtion( finalst[state], trans1[i] + state - i );
165 if ( transchar[i] == SYM_EPSILON &&
166 trans2[i] != NO_TRANSITION )
167 mkxtion( finalst[state],
168 trans2[i] + state - i );
171 accptnum[state] = accptnum[i];
175 flexfatal( _( "empty machine in dupmachine()" ) );
177 state_offset = state - i + 1;
179 init = mach + state_offset;
180 firstst[init] = firstst[mach] + state_offset;
181 finalst[init] = finalst[mach] + state_offset;
182 lastst[init] = lastst[mach] + state_offset;
188 /* finish_rule - finish up the processing for a rule
190 * An accepting number is added to the given machine. If variable_trail_rule
191 * is true then the rule has trailing context and both the head and trail
192 * are variable size. Otherwise if headcnt or trailcnt is non-zero then
193 * the machine recognizes a pattern with trailing context and headcnt is
194 * the number of characters in the matched part of the pattern, or zero
195 * if the matched part has variable length. trailcnt is the number of
196 * trailing context characters in the pattern, or zero if the trailing
197 * context has variable length.
200 void finish_rule( mach, variable_trail_rule, headcnt, trailcnt )
201 int mach, variable_trail_rule, headcnt, trailcnt;
203 char action_text[MAXLINE];
205 add_accept( mach, num_rules );
207 /* We did this in new_rule(), but it often gets the wrong
208 * number because we do it before we start parsing the current rule.
210 rule_linenum[num_rules] = linenum;
212 /* If this is a continued action, then the line-number has already
213 * been updated, giving us the wrong number.
215 if ( continued_action )
216 --rule_linenum[num_rules];
218 sprintf( action_text, "case %d:\n", num_rules );
219 add_action( action_text );
221 if ( variable_trail_rule )
223 rule_type[num_rules] = RULE_VARIABLE;
225 if ( performance_report > 0 )
227 _( "Variable trailing context rule at line %d\n" ),
228 rule_linenum[num_rules] );
230 variable_trailing_context_rules = true;
235 rule_type[num_rules] = RULE_NORMAL;
237 if ( headcnt > 0 || trailcnt > 0 )
239 /* Do trailing context magic to not match the trailing
242 char *scanner_cp = "yy_c_buf_p = yy_cp";
243 char *scanner_bp = "yy_bp";
246 "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */\n" );
250 sprintf( action_text, "%s = %s + %d;\n",
251 scanner_cp, scanner_bp, headcnt );
252 add_action( action_text );
257 sprintf( action_text, "%s -= %d;\n",
258 scanner_cp, trailcnt );
259 add_action( action_text );
263 "YY_DO_BEFORE_ACTION; /* set up yytext again */\n" );
267 /* Okay, in the action code at this point yytext and yyleng have
268 * their proper final values for this rule, so here's the point
269 * to do any user action. But don't do it for continued actions,
270 * as that'll result in multiple YY_RULE_SETUP's.
272 if ( ! continued_action )
273 add_action( "YY_RULE_SETUP\n" );
275 line_directive_out( (FILE *) 0, 1 );
279 /* link_machines - connect two machines together
283 * new = link_machines( first, last );
285 * new - a machine constructed by connecting first to last
286 * first - the machine whose successor is to be last
287 * last - the machine whose predecessor is to be first
289 * note: this routine concatenates the machine first with the machine
290 * last to produce a machine new which will pattern-match first first
291 * and then last, and will fail if either of the sub-patterns fails.
292 * FIRST is set to new by the operation. last is unmolested.
295 int link_machines( first, last )
301 else if ( last == NIL )
306 mkxtion( finalst[first], last );
307 finalst[first] = finalst[last];
308 lastst[first] = MAX( lastst[first], lastst[last] );
309 firstst[first] = MIN( firstst[first], firstst[last] );
316 /* mark_beginning_as_normal - mark each "beginning" state in a machine
317 * as being a "normal" (i.e., not trailing context-
320 * The "beginning" states are the epsilon closure of the first state
323 void mark_beginning_as_normal( mach )
326 switch ( state_type[mach] )
329 /* Oh, we've already visited here. */
332 case STATE_TRAILING_CONTEXT:
333 state_type[mach] = STATE_NORMAL;
335 if ( transchar[mach] == SYM_EPSILON )
337 if ( trans1[mach] != NO_TRANSITION )
338 mark_beginning_as_normal(
341 if ( trans2[mach] != NO_TRANSITION )
342 mark_beginning_as_normal(
349 _( "bad state type in mark_beginning_as_normal()" ) );
355 /* mkbranch - make a machine that branches to two machines
359 * branch = mkbranch( first, second );
361 * branch - a machine which matches either first's pattern or second's
362 * first, second - machines whose patterns are to be or'ed (the | operator)
364 * Note that first and second are NEITHER destroyed by the operation. Also,
365 * the resulting machine CANNOT be used with any other "mk" operation except
366 * more mkbranch's. Compare with mkor()
369 int mkbranch( first, second )
374 if ( first == NO_TRANSITION )
377 else if ( second == NO_TRANSITION )
380 eps = mkstate( SYM_EPSILON );
382 mkxtion( eps, first );
383 mkxtion( eps, second );
389 /* mkclos - convert a machine into a closure
392 * new = mkclos( state );
394 * new - a new state which matches the closure of "state"
400 return mkopt( mkposcl( state ) );
404 /* mkopt - make a machine optional
408 * new = mkopt( mach );
410 * new - a machine which optionally matches whatever mach matched
411 * mach - the machine to make optional
414 * 1. mach must be the last machine created
415 * 2. mach is destroyed by the call
423 if ( ! SUPER_FREE_EPSILON(finalst[mach]) )
425 eps = mkstate( SYM_EPSILON );
426 mach = link_machines( mach, eps );
429 /* Can't skimp on the following if FREE_EPSILON(mach) is true because
430 * some state interior to "mach" might point back to the beginning
433 eps = mkstate( SYM_EPSILON );
434 mach = link_machines( eps, mach );
436 mkxtion( mach, finalst[mach] );
442 /* mkor - make a machine that matches either one of two machines
446 * new = mkor( first, second );
448 * new - a machine which matches either first's pattern or second's
449 * first, second - machines whose patterns are to be or'ed (the | operator)
451 * note that first and second are both destroyed by the operation
452 * the code is rather convoluted because an attempt is made to minimize
453 * the number of epsilon states needed
456 int mkor( first, second )
464 else if ( second == NIL )
469 /* See comment in mkopt() about why we can't use the first
470 * state of "first" or "second" if they satisfy "FREE_EPSILON".
472 eps = mkstate( SYM_EPSILON );
474 first = link_machines( eps, first );
476 mkxtion( first, second );
478 if ( SUPER_FREE_EPSILON(finalst[first]) &&
479 accptnum[finalst[first]] == NIL )
481 orend = finalst[first];
482 mkxtion( finalst[second], orend );
485 else if ( SUPER_FREE_EPSILON(finalst[second]) &&
486 accptnum[finalst[second]] == NIL )
488 orend = finalst[second];
489 mkxtion( finalst[first], orend );
494 eps = mkstate( SYM_EPSILON );
496 first = link_machines( first, eps );
497 orend = finalst[first];
499 mkxtion( finalst[second], orend );
503 finalst[first] = orend;
508 /* mkposcl - convert a machine into a positive closure
511 * new = mkposcl( state );
513 * new - a machine matching the positive closure of "state"
521 if ( SUPER_FREE_EPSILON(finalst[state]) )
523 mkxtion( finalst[state], state );
529 eps = mkstate( SYM_EPSILON );
530 mkxtion( eps, state );
531 return link_machines( state, eps );
536 /* mkrep - make a replicated machine
539 * new = mkrep( mach, lb, ub );
541 * new - a machine that matches whatever "mach" matched from "lb"
542 * number of times to "ub" number of times
545 * if "ub" is INFINITY then "new" matches "lb" or more occurrences of "mach"
548 int mkrep( mach, lb, ub )
551 int base_mach, tail, copy, i;
553 base_mach = copysingl( mach, lb - 1 );
555 if ( ub == INFINITY )
557 copy = dupmachine( mach );
558 mach = link_machines( mach,
559 link_machines( base_mach, mkclos( copy ) ) );
564 tail = mkstate( SYM_EPSILON );
566 for ( i = lb; i < ub; ++i )
568 copy = dupmachine( mach );
569 tail = mkopt( link_machines( copy, tail ) );
572 mach = link_machines( mach, link_machines( base_mach, tail ) );
579 /* mkstate - create a state with a transition on a given symbol
583 * state = mkstate( sym );
585 * state - a new state matching sym
586 * sym - the symbol the new state is to have an out-transition on
588 * note that this routine makes new states in ascending order through the
589 * state array (and increments LASTNFA accordingly). The routine DUPMACHINE
590 * relies on machines being made in ascending order and that they are
591 * CONTIGUOUS. Change it and you will have to rewrite DUPMACHINE (kludge
592 * that it admittedly is)
598 if ( ++lastnfa >= current_mns )
600 if ( (current_mns += MNS_INCREMENT) >= MAXIMUM_MNS )
602 _( "input rules are too complicated (>= %d NFA states)" ),
607 firstst = reallocate_integer_array( firstst, current_mns );
608 lastst = reallocate_integer_array( lastst, current_mns );
609 finalst = reallocate_integer_array( finalst, current_mns );
610 transchar = reallocate_integer_array( transchar, current_mns );
611 trans1 = reallocate_integer_array( trans1, current_mns );
612 trans2 = reallocate_integer_array( trans2, current_mns );
613 accptnum = reallocate_integer_array( accptnum, current_mns );
615 reallocate_integer_array( assoc_rule, current_mns );
617 reallocate_integer_array( state_type, current_mns );
620 firstst[lastnfa] = lastnfa;
621 finalst[lastnfa] = lastnfa;
622 lastst[lastnfa] = lastnfa;
623 transchar[lastnfa] = sym;
624 trans1[lastnfa] = NO_TRANSITION;
625 trans2[lastnfa] = NO_TRANSITION;
626 accptnum[lastnfa] = NIL;
627 assoc_rule[lastnfa] = num_rules;
628 state_type[lastnfa] = current_state_type;
630 /* Fix up equivalence classes base on this transition. Note that any
631 * character which has its own transition gets its own equivalence
632 * class. Thus only characters which are only in character classes
633 * have a chance at being in the same equivalence class. E.g. "a|b"
634 * puts 'a' and 'b' into two different equivalence classes. "[ab]"
635 * puts them in the same equivalence class (barring other differences
636 * elsewhere in the input).
641 /* We don't have to update the equivalence classes since
642 * that was already done when the ccl was created for the
647 else if ( sym == SYM_EPSILON )
655 /* Map NUL's to csize. */
656 mkechar( sym ? sym : csize, nextecm, ecgroup );
663 /* mkxtion - make a transition from one state to another
667 * mkxtion( statefrom, stateto );
669 * statefrom - the state from which the transition is to be made
670 * stateto - the state to which the transition is to be made
673 void mkxtion( statefrom, stateto )
674 int statefrom, stateto;
676 if ( trans1[statefrom] == NO_TRANSITION )
677 trans1[statefrom] = stateto;
679 else if ( (transchar[statefrom] != SYM_EPSILON) ||
680 (trans2[statefrom] != NO_TRANSITION) )
681 flexfatal( _( "found too many transitions in mkxtion()" ) );
684 { /* second out-transition for an epsilon state */
686 trans2[statefrom] = stateto;
690 /* new_rule - initialize for a new rule */
694 if ( ++num_rules >= current_max_rules )
697 current_max_rules += MAX_RULES_INCREMENT;
698 rule_type = reallocate_integer_array( rule_type,
700 rule_linenum = reallocate_integer_array( rule_linenum,
702 rule_useful = reallocate_integer_array( rule_useful,
706 if ( num_rules > MAX_RULE )
707 lerrif( _( "too many rules (> %d)!" ), MAX_RULE );
709 rule_linenum[num_rules] = linenum;
710 rule_useful[num_rules] = false;