No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / lex / nfa.c
blobd1df43fde6857139374fc3c3546fc9e1f230f36a
1 /* nfa - NFA construction routines */
3 /*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Vern Paxson.
9 *
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 /* $NetBSD: nfa.c,v 1.10 1998/01/05 05:15:56 perry Exp $ */
31 #include "flexdef.h"
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
51 * too soon.
54 if ( transchar[finalst[mach]] == SYM_EPSILON )
55 accptnum[finalst[mach]] = accepting_number;
57 else
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
68 * synopsis
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 )
78 int singl, num;
80 int copy, i;
82 copy = mkstate( SYM_EPSILON );
84 for ( i = 1; i <= num; ++i )
85 copy = link_machines( copy, dupmachine( singl ) );
87 return copy;
91 /* dumpnfa - debugging routine to write out an nfa */
93 void dumpnfa( state1 )
94 int state1;
97 int sym, tsp1, tsp2, anum, ns;
99 fprintf( stderr,
100 _( "\n\n********** beginning dump of nfa with start state %d\n" ),
101 state1 );
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 );
114 sym = transchar[ns];
115 tsp1 = trans1[ns];
116 tsp2 = trans2[ns];
117 anum = accptnum[ns];
119 fprintf( stderr, "%3d: %4d, %4d", sym, tsp1, tsp2 );
121 if ( anum != NIL )
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
133 * synopsis
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 )
149 int mach;
151 int i, init, state_offset;
152 int state = 0;
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];
172 if ( state == 0 )
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;
182 return init;
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 snprintf(action_text, sizeof(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 )
224 fprintf( stderr,
225 _( "Variable trailing context rule at line %d\n" ),
226 rule_linenum[num_rules] );
228 variable_trailing_context_rules = true;
231 else
233 rule_type[num_rules] = RULE_NORMAL;
235 if ( headcnt > 0 || trailcnt > 0 )
237 /* Do trailing context magic to not match the trailing
238 * characters.
240 char *scanner_cp = "yy_c_buf_p = yy_cp";
241 char *scanner_bp = "yy_bp";
243 add_action(
244 "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */\n" );
246 if ( headcnt > 0 )
248 snprintf(action_text, sizeof(action_text),
249 "%s = %s + %d;\n", scanner_cp, scanner_bp,
250 headcnt);
251 add_action( action_text );
254 else
256 snprintf(action_text, sizeof(action_text),
257 "%s -= %d;\n", scanner_cp, trailcnt);
258 add_action( action_text );
261 add_action(
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
280 * synopsis
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 )
295 int first, last;
297 if ( first == NIL )
298 return last;
300 else if ( last == NIL )
301 return first;
303 else
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] );
310 return first;
315 /* mark_beginning_as_normal - mark each "beginning" state in a machine
316 * as being a "normal" (i.e., not trailing context-
317 * associated) states
319 * The "beginning" states are the epsilon closure of the first state
322 void mark_beginning_as_normal( mach )
323 register int mach;
325 switch ( state_type[mach] )
327 case STATE_NORMAL:
328 /* Oh, we've already visited here. */
329 return;
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(
338 trans1[mach] );
340 if ( trans2[mach] != NO_TRANSITION )
341 mark_beginning_as_normal(
342 trans2[mach] );
344 break;
346 default:
347 flexerror(
348 _( "bad state type in mark_beginning_as_normal()" ) );
349 break;
354 /* mkbranch - make a machine that branches to two machines
356 * synopsis
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 )
369 int first, second;
371 int eps;
373 if ( first == NO_TRANSITION )
374 return second;
376 else if ( second == NO_TRANSITION )
377 return first;
379 eps = mkstate( SYM_EPSILON );
381 mkxtion( eps, first );
382 mkxtion( eps, second );
384 return eps;
388 /* mkclos - convert a machine into a closure
390 * synopsis
391 * new = mkclos( state );
393 * new - a new state which matches the closure of "state"
396 int mkclos( state )
397 int state;
399 return mkopt( mkposcl( state ) );
403 /* mkopt - make a machine optional
405 * synopsis
407 * new = mkopt( mach );
409 * new - a machine which optionally matches whatever mach matched
410 * mach - the machine to make optional
412 * notes:
413 * 1. mach must be the last machine created
414 * 2. mach is destroyed by the call
417 int mkopt( mach )
418 int mach;
420 int eps;
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
430 * for a closure.
432 eps = mkstate( SYM_EPSILON );
433 mach = link_machines( eps, mach );
435 mkxtion( mach, finalst[mach] );
437 return mach;
441 /* mkor - make a machine that matches either one of two machines
443 * synopsis
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 )
456 int first, second;
458 int eps, orend;
460 if ( first == NIL )
461 return second;
463 else if ( second == NIL )
464 return first;
466 else
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 );
491 else
493 eps = mkstate( SYM_EPSILON );
495 first = link_machines( first, eps );
496 orend = finalst[first];
498 mkxtion( finalst[second], orend );
502 finalst[first] = orend;
503 return first;
507 /* mkposcl - convert a machine into a positive closure
509 * synopsis
510 * new = mkposcl( state );
512 * new - a machine matching the positive closure of "state"
515 int mkposcl( state )
516 int state;
518 int eps;
520 if ( SUPER_FREE_EPSILON(finalst[state]) )
522 mkxtion( finalst[state], state );
523 return state;
526 else
528 eps = mkstate( SYM_EPSILON );
529 mkxtion( eps, state );
530 return link_machines( state, eps );
535 /* mkrep - make a replicated machine
537 * synopsis
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
543 * note
544 * if "ub" is INFINITY then "new" matches "lb" or more occurrences of "mach"
547 int mkrep( mach, lb, ub )
548 int 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 ) ) );
561 else
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 ) );
574 return mach;
578 /* mkstate - create a state with a transition on a given symbol
580 * synopsis
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)
594 int mkstate( sym )
595 int sym;
597 if ( ++lastnfa >= current_mns )
599 if ( (current_mns += MNS_INCREMENT) >= MAXIMUM_MNS )
600 lerrif(
601 _( "input rules are too complicated (>= %d NFA states)" ),
602 current_mns );
604 ++num_reallocs;
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 );
613 assoc_rule =
614 reallocate_integer_array( assoc_rule, current_mns );
615 state_type =
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).
638 if ( sym < 0 )
640 /* We don't have to update the equivalence classes since
641 * that was already done when the ccl was created for the
642 * first time.
646 else if ( sym == SYM_EPSILON )
647 ++numeps;
649 else
651 check_char( sym );
653 if ( useecs )
654 /* Map NUL's to csize. */
655 mkechar( sym ? sym : csize, nextecm, ecgroup );
658 return lastnfa;
662 /* mkxtion - make a transition from one state to another
664 * synopsis
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()" ) );
682 else
683 { /* second out-transition for an epsilon state */
684 ++eps2;
685 trans2[statefrom] = stateto;
689 /* new_rule - initialize for a new rule */
691 void new_rule()
693 if ( ++num_rules >= current_max_rules )
695 ++num_reallocs;
696 current_max_rules += MAX_RULES_INCREMENT;
697 rule_type = reallocate_integer_array( rule_type,
698 current_max_rules );
699 rule_linenum = reallocate_integer_array( rule_linenum,
700 current_max_rules );
701 rule_useful = reallocate_integer_array( rule_useful,
702 current_max_rules );
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;