2012-06-06 16:10:32 +02:00
|
|
|
/* $NetBSD: nfa.c,v 1.1.1.1 2009/10/26 00:26:28 christos Exp $ */
|
|
|
|
|
2005-07-11 14:54:10 +02:00
|
|
|
/* nfa - NFA construction routines */
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
/* Copyright (c) 1990 The Regents of the University of California. */
|
|
|
|
/* All rights reserved. */
|
|
|
|
|
|
|
|
/* This code is derived from software contributed to Berkeley by */
|
|
|
|
/* Vern Paxson. */
|
|
|
|
|
|
|
|
/* The United States Government has rights in this work pursuant */
|
|
|
|
/* to contract no. DE-AC03-76SF00098 between the United States */
|
|
|
|
/* Department of Energy and the University of California. */
|
|
|
|
|
|
|
|
/* This file is part of flex. */
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
/* Redistribution and use in source and binary forms, with or without */
|
|
|
|
/* modification, are permitted provided that the following conditions */
|
|
|
|
/* are met: */
|
|
|
|
|
|
|
|
/* 1. Redistributions of source code must retain the above copyright */
|
|
|
|
/* notice, this list of conditions and the following disclaimer. */
|
|
|
|
/* 2. Redistributions in binary form must reproduce the above copyright */
|
|
|
|
/* notice, this list of conditions and the following disclaimer in the */
|
|
|
|
/* documentation and/or other materials provided with the distribution. */
|
|
|
|
|
|
|
|
/* Neither the name of the University nor the names of its contributors */
|
|
|
|
/* may be used to endorse or promote products derived from this software */
|
|
|
|
/* without specific prior written permission. */
|
|
|
|
|
|
|
|
/* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
|
|
|
|
/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
|
|
|
|
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
|
|
|
|
/* PURPOSE. */
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
#include "flexdef.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* declare functions that have forward references */
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int dupmachine PROTO ((int));
|
|
|
|
void mkxtion PROTO ((int, int));
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* add_accept - add an accepting state to a machine
|
|
|
|
*
|
|
|
|
* accepting_number becomes mach's accepting number.
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
void add_accept (mach, accepting_number)
|
|
|
|
int mach, accepting_number;
|
|
|
|
{
|
2005-07-11 14:54:10 +02:00
|
|
|
/* Hang the accepting number off an epsilon state. if it is associated
|
|
|
|
* with a state that has a non-epsilon out-transition, then the state
|
|
|
|
* will accept BEFORE it makes that transition, i.e., one character
|
|
|
|
* too soon.
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (transchar[finalst[mach]] == SYM_EPSILON)
|
2005-07-11 14:54:10 +02:00
|
|
|
accptnum[finalst[mach]] = accepting_number;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else {
|
|
|
|
int astate = mkstate (SYM_EPSILON);
|
|
|
|
|
2005-07-11 14:54:10 +02:00
|
|
|
accptnum[astate] = accepting_number;
|
2012-06-06 16:10:32 +02:00
|
|
|
(void) link_machines (mach, astate);
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* copysingl - make a given number of copies of a singleton machine
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
*
|
|
|
|
* newsng = copysingl( singl, num );
|
|
|
|
*
|
|
|
|
* newsng - a new singleton composed of num copies of singl
|
|
|
|
* singl - a singleton machine
|
|
|
|
* num - the number of copies of singl to be present in newsng
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int copysingl (singl, num)
|
|
|
|
int singl, num;
|
|
|
|
{
|
|
|
|
int copy, i;
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
copy = mkstate (SYM_EPSILON);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
for (i = 1; i <= num; ++i)
|
|
|
|
copy = link_machines (copy, dupmachine (singl));
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
return copy;
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* dumpnfa - debugging routine to write out an nfa */
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
void dumpnfa (state1)
|
|
|
|
int state1;
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
{
|
|
|
|
int sym, tsp1, tsp2, anum, ns;
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
fprintf (stderr,
|
|
|
|
_
|
|
|
|
("\n\n********** beginning dump of nfa with start state %d\n"),
|
|
|
|
state1);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
/* We probably should loop starting at firstst[state1] and going to
|
|
|
|
* lastst[state1], but they're not maintained properly when we "or"
|
|
|
|
* all of the rules together. So we use our knowledge that the machine
|
|
|
|
* starts at state 1 and ends at lastnfa.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* for ( ns = firstst[state1]; ns <= lastst[state1]; ++ns ) */
|
2012-06-06 16:10:32 +02:00
|
|
|
for (ns = 1; ns <= lastnfa; ++ns) {
|
|
|
|
fprintf (stderr, _("state # %4d\t"), ns);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
sym = transchar[ns];
|
|
|
|
tsp1 = trans1[ns];
|
|
|
|
tsp2 = trans2[ns];
|
|
|
|
anum = accptnum[ns];
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
fprintf (stderr, "%3d: %4d, %4d", sym, tsp1, tsp2);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (anum != NIL)
|
|
|
|
fprintf (stderr, " [%d]", anum);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
fprintf (stderr, "\n");
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
fprintf (stderr, _("********** end of dump\n"));
|
|
|
|
}
|
|
|
|
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
/* dupmachine - make a duplicate of a given machine
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
*
|
|
|
|
* copy = dupmachine( mach );
|
|
|
|
*
|
|
|
|
* copy - holds duplicate of mach
|
|
|
|
* mach - machine to be duplicated
|
|
|
|
*
|
|
|
|
* note that the copy of mach is NOT an exact duplicate; rather, all the
|
|
|
|
* transition states values are adjusted so that the copy is self-contained,
|
|
|
|
* as the original should have been.
|
|
|
|
*
|
|
|
|
* also note that the original MUST be contiguous, with its low and high
|
|
|
|
* states accessible by the arrays firstst and lastst
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int dupmachine (mach)
|
|
|
|
int mach;
|
|
|
|
{
|
|
|
|
int i, init, state_offset;
|
|
|
|
int state = 0;
|
|
|
|
int last = lastst[mach];
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
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);
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
accptnum[state] = accptnum[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == 0)
|
|
|
|
flexfatal (_("empty machine in dupmachine()"));
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
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;
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* finish_rule - finish up the processing for a rule
|
|
|
|
*
|
|
|
|
* An accepting number is added to the given machine. If variable_trail_rule
|
|
|
|
* is true then the rule has trailing context and both the head and trail
|
|
|
|
* are variable size. Otherwise if headcnt or trailcnt is non-zero then
|
|
|
|
* the machine recognizes a pattern with trailing context and headcnt is
|
|
|
|
* the number of characters in the matched part of the pattern, or zero
|
|
|
|
* if the matched part has variable length. trailcnt is the number of
|
|
|
|
* trailing context characters in the pattern, or zero if the trailing
|
|
|
|
* context has variable length.
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
void finish_rule (mach, variable_trail_rule, headcnt, trailcnt,
|
|
|
|
pcont_act)
|
|
|
|
int mach, variable_trail_rule, headcnt, trailcnt, pcont_act;
|
|
|
|
{
|
|
|
|
char action_text[MAXLINE];
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
add_accept (mach, num_rules);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
/* We did this in new_rule(), but it often gets the wrong
|
|
|
|
* number because we do it before we start parsing the current rule.
|
|
|
|
*/
|
|
|
|
rule_linenum[num_rules] = linenum;
|
|
|
|
|
|
|
|
/* If this is a continued action, then the line-number has already
|
|
|
|
* been updated, giving us the wrong number.
|
|
|
|
*/
|
2012-06-06 16:10:32 +02:00
|
|
|
if (continued_action)
|
2005-07-11 14:54:10 +02:00
|
|
|
--rule_linenum[num_rules];
|
|
|
|
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
/* If the previous rule was continued action, then we inherit the
|
|
|
|
* previous newline flag, possibly overriding the current one.
|
|
|
|
*/
|
|
|
|
if (pcont_act && rule_has_nl[num_rules - 1])
|
|
|
|
rule_has_nl[num_rules] = true;
|
|
|
|
|
|
|
|
snprintf (action_text, sizeof(action_text), "case %d:\n", num_rules);
|
|
|
|
add_action (action_text);
|
|
|
|
if (rule_has_nl[num_rules]) {
|
|
|
|
snprintf (action_text, sizeof(action_text), "/* rule %d can match eol */\n",
|
|
|
|
num_rules);
|
|
|
|
add_action (action_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (variable_trail_rule) {
|
2005-07-11 14:54:10 +02:00
|
|
|
rule_type[num_rules] = RULE_VARIABLE;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (performance_report > 0)
|
|
|
|
fprintf (stderr,
|
|
|
|
_
|
|
|
|
("Variable trailing context rule at line %d\n"),
|
|
|
|
rule_linenum[num_rules]);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
variable_trailing_context_rules = true;
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else {
|
2005-07-11 14:54:10 +02:00
|
|
|
rule_type[num_rules] = RULE_NORMAL;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (headcnt > 0 || trailcnt > 0) {
|
2005-07-11 14:54:10 +02:00
|
|
|
/* Do trailing context magic to not match the trailing
|
|
|
|
* characters.
|
|
|
|
*/
|
2012-06-06 16:10:32 +02:00
|
|
|
char *scanner_cp = "YY_G(yy_c_buf_p) = yy_cp";
|
|
|
|
char *scanner_bp = "yy_bp";
|
|
|
|
|
|
|
|
add_action
|
|
|
|
("*yy_cp = YY_G(yy_hold_char); /* undo effects of setting up yytext */\n");
|
|
|
|
|
|
|
|
if (headcnt > 0) {
|
|
|
|
snprintf (action_text, sizeof(action_text), "%s = %s + %d;\n",
|
|
|
|
scanner_cp, scanner_bp, headcnt);
|
|
|
|
add_action (action_text);
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
|
|
|
|
else {
|
|
|
|
snprintf (action_text, sizeof(action_text), "%s -= %d;\n",
|
|
|
|
scanner_cp, trailcnt);
|
|
|
|
add_action (action_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_action
|
|
|
|
("YY_DO_BEFORE_ACTION; /* set up yytext again */\n");
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
/* Okay, in the action code at this point yytext and yyleng have
|
|
|
|
* their proper final values for this rule, so here's the point
|
|
|
|
* to do any user action. But don't do it for continued actions,
|
|
|
|
* as that'll result in multiple YY_RULE_SETUP's.
|
|
|
|
*/
|
2012-06-06 16:10:32 +02:00
|
|
|
if (!continued_action)
|
|
|
|
add_action ("YY_RULE_SETUP\n");
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
line_directive_out ((FILE *) 0, 1);
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* link_machines - connect two machines together
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
*
|
|
|
|
* new = link_machines( first, last );
|
|
|
|
*
|
|
|
|
* new - a machine constructed by connecting first to last
|
|
|
|
* first - the machine whose successor is to be last
|
|
|
|
* last - the machine whose predecessor is to be first
|
|
|
|
*
|
|
|
|
* note: this routine concatenates the machine first with the machine
|
|
|
|
* last to produce a machine new which will pattern-match first first
|
|
|
|
* and then last, and will fail if either of the sub-patterns fails.
|
|
|
|
* FIRST is set to new by the operation. last is unmolested.
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int link_machines (first, last)
|
|
|
|
int first, last;
|
|
|
|
{
|
|
|
|
if (first == NIL)
|
2005-07-11 14:54:10 +02:00
|
|
|
return last;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else if (last == NIL)
|
2005-07-11 14:54:10 +02:00
|
|
|
return first;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else {
|
|
|
|
mkxtion (finalst[first], last);
|
2005-07-11 14:54:10 +02:00
|
|
|
finalst[first] = finalst[last];
|
2012-06-06 16:10:32 +02:00
|
|
|
lastst[first] = MAX (lastst[first], lastst[last]);
|
|
|
|
firstst[first] = MIN (firstst[first], firstst[last]);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mark_beginning_as_normal - mark each "beginning" state in a machine
|
|
|
|
* as being a "normal" (i.e., not trailing context-
|
|
|
|
* associated) states
|
|
|
|
*
|
|
|
|
* The "beginning" states are the epsilon closure of the first state
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
void mark_beginning_as_normal (mach)
|
|
|
|
register int mach;
|
|
|
|
{
|
|
|
|
switch (state_type[mach]) {
|
|
|
|
case STATE_NORMAL:
|
|
|
|
/* Oh, we've already visited here. */
|
|
|
|
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]);
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
flexerror (_
|
|
|
|
("bad state type in mark_beginning_as_normal()"));
|
|
|
|
break;
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mkbranch - make a machine that branches to two machines
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
*
|
|
|
|
* branch = mkbranch( first, second );
|
|
|
|
*
|
|
|
|
* branch - a machine which matches either first's pattern or second's
|
|
|
|
* first, second - machines whose patterns are to be or'ed (the | operator)
|
|
|
|
*
|
|
|
|
* Note that first and second are NEITHER destroyed by the operation. Also,
|
|
|
|
* the resulting machine CANNOT be used with any other "mk" operation except
|
|
|
|
* more mkbranch's. Compare with mkor()
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int mkbranch (first, second)
|
|
|
|
int first, second;
|
|
|
|
{
|
|
|
|
int eps;
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (first == NO_TRANSITION)
|
2005-07-11 14:54:10 +02:00
|
|
|
return second;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else if (second == NO_TRANSITION)
|
2005-07-11 14:54:10 +02:00
|
|
|
return first;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
eps = mkstate (SYM_EPSILON);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
mkxtion (eps, first);
|
|
|
|
mkxtion (eps, second);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
return eps;
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mkclos - convert a machine into a closure
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
* new = mkclos( state );
|
|
|
|
*
|
|
|
|
* new - a new state which matches the closure of "state"
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int mkclos (state)
|
|
|
|
int state;
|
|
|
|
{
|
|
|
|
return mkopt (mkposcl (state));
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mkopt - make a machine optional
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
*
|
|
|
|
* new = mkopt( mach );
|
|
|
|
*
|
|
|
|
* new - a machine which optionally matches whatever mach matched
|
|
|
|
* mach - the machine to make optional
|
|
|
|
*
|
|
|
|
* notes:
|
|
|
|
* 1. mach must be the last machine created
|
|
|
|
* 2. mach is destroyed by the call
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int mkopt (mach)
|
|
|
|
int mach;
|
|
|
|
{
|
|
|
|
int eps;
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (!SUPER_FREE_EPSILON (finalst[mach])) {
|
|
|
|
eps = mkstate (SYM_EPSILON);
|
|
|
|
mach = link_machines (mach, eps);
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
/* Can't skimp on the following if FREE_EPSILON(mach) is true because
|
|
|
|
* some state interior to "mach" might point back to the beginning
|
|
|
|
* for a closure.
|
|
|
|
*/
|
2012-06-06 16:10:32 +02:00
|
|
|
eps = mkstate (SYM_EPSILON);
|
|
|
|
mach = link_machines (eps, mach);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
mkxtion (mach, finalst[mach]);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
return mach;
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mkor - make a machine that matches either one of two machines
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
*
|
|
|
|
* new = mkor( first, second );
|
|
|
|
*
|
|
|
|
* new - a machine which matches either first's pattern or second's
|
|
|
|
* first, second - machines whose patterns are to be or'ed (the | operator)
|
|
|
|
*
|
|
|
|
* note that first and second are both destroyed by the operation
|
|
|
|
* the code is rather convoluted because an attempt is made to minimize
|
|
|
|
* the number of epsilon states needed
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int mkor (first, second)
|
|
|
|
int first, second;
|
|
|
|
{
|
|
|
|
int eps, orend;
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (first == NIL)
|
2005-07-11 14:54:10 +02:00
|
|
|
return second;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else if (second == NIL)
|
2005-07-11 14:54:10 +02:00
|
|
|
return first;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else {
|
2005-07-11 14:54:10 +02:00
|
|
|
/* See comment in mkopt() about why we can't use the first
|
|
|
|
* state of "first" or "second" if they satisfy "FREE_EPSILON".
|
|
|
|
*/
|
2012-06-06 16:10:32 +02:00
|
|
|
eps = mkstate (SYM_EPSILON);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
first = link_machines (eps, first);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
mkxtion (first, second);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (SUPER_FREE_EPSILON (finalst[first]) &&
|
|
|
|
accptnum[finalst[first]] == NIL) {
|
2005-07-11 14:54:10 +02:00
|
|
|
orend = finalst[first];
|
2012-06-06 16:10:32 +02:00
|
|
|
mkxtion (finalst[second], orend);
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else if (SUPER_FREE_EPSILON (finalst[second]) &&
|
|
|
|
accptnum[finalst[second]] == NIL) {
|
2005-07-11 14:54:10 +02:00
|
|
|
orend = finalst[second];
|
2012-06-06 16:10:32 +02:00
|
|
|
mkxtion (finalst[first], orend);
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else {
|
|
|
|
eps = mkstate (SYM_EPSILON);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
first = link_machines (first, eps);
|
2005-07-11 14:54:10 +02:00
|
|
|
orend = finalst[first];
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
mkxtion (finalst[second], orend);
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
finalst[first] = orend;
|
|
|
|
return first;
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mkposcl - convert a machine into a positive closure
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
* new = mkposcl( state );
|
|
|
|
*
|
|
|
|
* new - a machine matching the positive closure of "state"
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int mkposcl (state)
|
|
|
|
int state;
|
|
|
|
{
|
|
|
|
int eps;
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (SUPER_FREE_EPSILON (finalst[state])) {
|
|
|
|
mkxtion (finalst[state], state);
|
2005-07-11 14:54:10 +02:00
|
|
|
return state;
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else {
|
|
|
|
eps = mkstate (SYM_EPSILON);
|
|
|
|
mkxtion (eps, state);
|
|
|
|
return link_machines (state, eps);
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mkrep - make a replicated machine
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
* new = mkrep( mach, lb, ub );
|
|
|
|
*
|
|
|
|
* new - a machine that matches whatever "mach" matched from "lb"
|
|
|
|
* number of times to "ub" number of times
|
|
|
|
*
|
|
|
|
* note
|
2012-06-06 16:10:32 +02:00
|
|
|
* if "ub" is INFINITE_REPEAT then "new" matches "lb" or more occurrences of "mach"
|
2005-07-11 14:54:10 +02:00
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
int mkrep (mach, lb, ub)
|
|
|
|
int mach, lb, ub;
|
|
|
|
{
|
|
|
|
int base_mach, tail, copy, i;
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
base_mach = copysingl (mach, lb - 1);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (ub == INFINITE_REPEAT) {
|
|
|
|
copy = dupmachine (mach);
|
|
|
|
mach = link_machines (mach,
|
|
|
|
link_machines (base_mach,
|
|
|
|
mkclos (copy)));
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else {
|
|
|
|
tail = mkstate (SYM_EPSILON);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
for (i = lb; i < ub; ++i) {
|
|
|
|
copy = dupmachine (mach);
|
|
|
|
tail = mkopt (link_machines (copy, tail));
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
mach =
|
|
|
|
link_machines (mach,
|
|
|
|
link_machines (base_mach, tail));
|
2005-07-11 14:54:10 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
return mach;
|
|
|
|
}
|
|
|
|
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
/* mkstate - create a state with a transition on a given symbol
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
*
|
|
|
|
* state = mkstate( sym );
|
|
|
|
*
|
|
|
|
* state - a new state matching sym
|
|
|
|
* sym - the symbol the new state is to have an out-transition on
|
|
|
|
*
|
|
|
|
* note that this routine makes new states in ascending order through the
|
|
|
|
* state array (and increments LASTNFA accordingly). The routine DUPMACHINE
|
|
|
|
* relies on machines being made in ascending order and that they are
|
|
|
|
* CONTIGUOUS. Change it and you will have to rewrite DUPMACHINE (kludge
|
|
|
|
* that it admittedly is)
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
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);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
++num_reallocs;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
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);
|
2005-07-11 14:54:10 +02:00
|
|
|
assoc_rule =
|
2012-06-06 16:10:32 +02:00
|
|
|
reallocate_integer_array (assoc_rule, current_mns);
|
2005-07-11 14:54:10 +02:00
|
|
|
state_type =
|
2012-06-06 16:10:32 +02:00
|
|
|
reallocate_integer_array (state_type, current_mns);
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Fix up equivalence classes base on this transition. Note that any
|
|
|
|
* character which has its own transition gets its own equivalence
|
|
|
|
* class. Thus only characters which are only in character classes
|
|
|
|
* have a chance at being in the same equivalence class. E.g. "a|b"
|
|
|
|
* puts 'a' and 'b' into two different equivalence classes. "[ab]"
|
|
|
|
* puts them in the same equivalence class (barring other differences
|
|
|
|
* elsewhere in the input).
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (sym < 0) {
|
2005-07-11 14:54:10 +02:00
|
|
|
/* We don't have to update the equivalence classes since
|
|
|
|
* that was already done when the ccl was created for the
|
|
|
|
* first time.
|
|
|
|
*/
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else if (sym == SYM_EPSILON)
|
2005-07-11 14:54:10 +02:00
|
|
|
++numeps;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else {
|
|
|
|
check_char (sym);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (useecs)
|
2005-07-11 14:54:10 +02:00
|
|
|
/* Map NUL's to csize. */
|
2012-06-06 16:10:32 +02:00
|
|
|
mkechar (sym ? sym : csize, nextecm, ecgroup);
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
return lastnfa;
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* mkxtion - make a transition from one state to another
|
|
|
|
*
|
|
|
|
* synopsis
|
|
|
|
*
|
|
|
|
* mkxtion( statefrom, stateto );
|
|
|
|
*
|
|
|
|
* statefrom - the state from which the transition is to be made
|
|
|
|
* stateto - the state to which the transition is to be made
|
|
|
|
*/
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
void mkxtion (statefrom, stateto)
|
|
|
|
int statefrom, stateto;
|
|
|
|
{
|
|
|
|
if (trans1[statefrom] == NO_TRANSITION)
|
2005-07-11 14:54:10 +02:00
|
|
|
trans1[statefrom] = stateto;
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else if ((transchar[statefrom] != SYM_EPSILON) ||
|
|
|
|
(trans2[statefrom] != NO_TRANSITION))
|
|
|
|
flexfatal (_("found too many transitions in mkxtion()"));
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
else { /* second out-transition for an epsilon state */
|
2005-07-11 14:54:10 +02:00
|
|
|
++eps2;
|
|
|
|
trans2[statefrom] = stateto;
|
|
|
|
}
|
2012-06-06 16:10:32 +02:00
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
/* new_rule - initialize for a new rule */
|
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
void new_rule ()
|
|
|
|
{
|
|
|
|
if (++num_rules >= current_max_rules) {
|
2005-07-11 14:54:10 +02:00
|
|
|
++num_reallocs;
|
|
|
|
current_max_rules += MAX_RULES_INCREMENT;
|
2012-06-06 16:10:32 +02:00
|
|
|
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);
|
|
|
|
rule_has_nl = reallocate_bool_array (rule_has_nl,
|
|
|
|
current_max_rules);
|
|
|
|
}
|
2005-07-11 14:54:10 +02:00
|
|
|
|
2012-06-06 16:10:32 +02:00
|
|
|
if (num_rules > MAX_RULE)
|
|
|
|
lerrif (_("too many rules (> %d)!"), MAX_RULE);
|
2005-07-11 14:54:10 +02:00
|
|
|
|
|
|
|
rule_linenum[num_rules] = linenum;
|
|
|
|
rule_useful[num_rules] = false;
|
2012-06-06 16:10:32 +02:00
|
|
|
rule_has_nl[num_rules] = false;
|
|
|
|
}
|