39 #define NISERR() VISERR(nfa->v)
40 #define NERR(e) VERR(nfa->v, (e))
114 for (sb =
nfa->
lastsb; sb != NULL; sb = sbnext)
121 for (ab =
nfa->
lastab; ab != NULL; ab = abnext)
136 static struct state *
211 static struct state *
231 while ((
a = s->
ins) != NULL)
233 while ((
a = s->
outs) != NULL)
302 if (
a->to ==
to &&
a->co ==
co &&
a->type == t)
307 for (
a =
to->
ins;
a != NULL;
a =
a->inchain)
308 if (
a->from ==
from &&
a->co ==
co &&
a->type == t)
347 a->inchainRev = NULL;
352 a->outchainRev = NULL;
423 struct arc *predecessor;
434 if (predecessor == NULL)
454 if (predecessor == NULL)
491 struct state *oldfrom =
a->from;
492 struct arc *predecessor;
494 assert(oldfrom != newfrom);
498 predecessor =
a->outchainRev;
499 if (predecessor == NULL)
502 oldfrom->
outs =
a->outchain;
509 if (
a->outchain != NULL)
511 assert(
a->outchain->outchainRev ==
a);
512 a->outchain->outchainRev = predecessor;
519 a->outchain = newfrom->
outs;
520 a->outchainRev = NULL;
535 struct state *oldto =
a->to;
536 struct arc *predecessor;
542 predecessor =
a->inchainRev;
543 if (predecessor == NULL)
546 oldto->
ins =
a->inchain;
553 if (
a->inchain != NULL)
555 assert(
a->inchain->inchainRev ==
a);
556 a->inchain->inchainRev = predecessor;
563 a->inchain = newto->
ins;
564 a->inchainRev = NULL;
579 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
598 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
623 struct arc **sortarray;
631 sortarray = (
struct arc **)
MALLOC(n *
sizeof(
struct arc *));
632 if (sortarray == NULL)
638 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
647 a->inchain = sortarray[1];
648 a->inchainRev = NULL;
649 for (
i = 1;
i < n - 1;
i++)
652 a->inchain = sortarray[
i + 1];
653 a->inchainRev = sortarray[
i - 1];
657 a->inchainRev = sortarray[
i - 1];
664 const struct arc *aa = *((
const struct arc *
const *)
a);
665 const struct arc *bb = *((
const struct arc *
const *)
b);
690 struct arc **sortarray;
698 sortarray = (
struct arc **)
MALLOC(n *
sizeof(
struct arc *));
699 if (sortarray == NULL)
705 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
714 a->outchain = sortarray[1];
715 a->outchainRev = NULL;
716 for (
i = 1;
i < n - 1;
i++)
719 a->outchain = sortarray[
i + 1];
720 a->outchainRev = sortarray[
i - 1];
724 a->outchainRev = sortarray[
i - 1];
731 const struct arc *aa = *((
const struct arc *
const *)
a);
732 const struct arc *bb = *((
const struct arc *
const *)
b);
758 #define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs) \
759 ((nsrcarcs) < 4 ? 0 : ((nsrcarcs) > 32 || (ndestarcs) > 32))
779 struct state *oldState,
780 struct state *newState)
782 assert(oldState != newState);
784 if (newState->
nins == 0)
789 while ((
a = oldState->
ins) != NULL)
800 while ((
a = oldState->
ins) != NULL)
828 while (oa != NULL && na != NULL)
883 struct state *oldState,
884 struct state *newState)
886 assert(oldState != newState);
889 if (newState->
nins == 0)
894 for (
a = oldState->
ins;
a != NULL;
a =
a->inchain)
903 for (
a = oldState->
ins;
a != NULL;
a =
a->inchain)
928 while (oa != NULL && na != NULL)
973 struct arc **arcarray,
1001 for (
i = 1;
i < arccount;
i++)
1007 arcarray[++
j] = arcarray[
i];
1026 while (
i < arccount && na != NULL)
1028 struct arc *
a = arcarray[
i];
1050 while (
i < arccount)
1053 struct arc *
a = arcarray[
i];
1067 struct state *oldState,
1068 struct state *newState)
1070 assert(oldState != newState);
1072 if (newState->
nouts == 0)
1077 while ((
a = oldState->
outs) != NULL)
1088 while ((
a = oldState->
outs) != NULL)
1114 oa = oldState->
outs;
1115 na = newState->
outs;
1116 while (oa != NULL && na != NULL)
1168 struct state *oldState,
1169 struct state *newState)
1171 assert(oldState != newState);
1174 if (newState->
nouts == 0)
1179 for (
a = oldState->
outs;
a != NULL;
a =
a->outchain)
1188 for (
a = oldState->
outs;
a != NULL;
a =
a->outchain)
1211 oa = oldState->
outs;
1212 na = newState->
outs;
1213 while (oa != NULL && na != NULL)
1267 for (
a = old->
outs;
a != NULL;
a =
a->outchain)
1305 struct state *leftend,
1325 while ((
a = s->
outs) != NULL)
1333 if (to->
nins == 0 && to->
tmp == NULL)
1503 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
1523 static struct state *
1529 if (
s1->nouts == 1 &&
s1->outs->type ==
EMPTY)
1532 if (
s2->nins == 1 &&
s2->ins->type ==
EMPTY)
1538 if (
s1->outs == NULL)
1541 for (
a =
s1->outs;
a != NULL;
a =
a->outchain)
1597 int verbose = (f != NULL) ? 1 : 0;
1600 fprintf(f,
"\ninitial cleanup:\n");
1612 fprintf(f,
"\nconstraints:\n");
1619 fprintf(f,
"\nfinal cleanup:\n");
1637 struct state *nexts;
1640 struct state *intermediates;
1650 intermediates = NULL;
1653 nexta =
a->outchain;
1654 if (
a->type ==
'^' ||
a->type ==
BEHIND)
1659 while (intermediates != NULL)
1661 struct state *ns = intermediates->
tmp;
1663 intermediates->
tmp = NULL;
1684 nexta =
a->outchain;
1715 struct state **intermediates)
1726 if (from->
nins == 0)
1737 if (from->
nouts > 1)
1765 for (s = *intermediates; s != NULL; s = s->
tmp)
1776 s->
tmp = *intermediates;
1808 struct state *nexts;
1811 struct state *intermediates;
1821 intermediates = NULL;
1825 if (
a->type ==
'$' ||
a->type ==
AHEAD)
1830 while (intermediates != NULL)
1832 struct state *ns = intermediates->
tmp;
1834 intermediates->
tmp = NULL;
1886 struct state **intermediates)
1926 nexta =
a->outchain;
1936 for (s = *intermediates; s != NULL; s = s->
tmp)
1947 s->
tmp = *intermediates;
1984 #define CA(ct,at) (((ct)<<CHAR_BIT) | (at))
1986 switch (
CA(con->
type,
a->type))
1994 if (con->
co ==
a->co)
2015 if (con->
co ==
a->co)
2021 if (con->
co ==
a->co)
2074 struct state *nexts;
2078 struct arc **inarcsorig;
2079 struct arc **arcarray;
2095 assert(
a != NULL &&
a->outchain == NULL);
2115 assert(
a != NULL &&
a->inchain == NULL);
2180 if (inarcsorig == NULL)
2188 inarcsorig[s->
no] = s->
ins;
2189 totalinarcs += s->
nins;
2198 arcarray = (
struct arc **)
MALLOC(totalinarcs *
sizeof(
struct arc *));
2199 if (arcarray == NULL)
2218 for (
a = inarcsorig[
s2->no];
a != NULL;
a =
a->inchain)
2221 arcarray[arccount++] =
a;
2229 assert(arccount <= totalinarcs);
2238 nskip = s->
nins - prevnins;
2242 inarcsorig[s->
no] =
a;
2256 for (
a = s->
outs;
a != NULL;
a = nexta)
2258 nexta =
a->outchain;
2269 for (s =
nfa->
states; s != NULL; s = nexts)
2295 static struct state *
2298 struct state *lastfound,
2299 struct arc **inarcsorig)
2312 for (
a = inarcsorig[s->
no];
a != NULL;
a =
a->inchain)
2314 if (
a->type ==
EMPTY &&
a->from->tmp == NULL)
2346 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2367 struct state *nexts;
2386 nexta =
a->outchain;
2401 if (
NISERR() || !hasconstraints)
2430 for (s =
nfa->
states; s != NULL; s = nexts)
2483 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2487 struct state *sto =
a->to;
2554 struct state *shead;
2555 struct state *stail;
2556 struct state *sclone;
2557 struct state *nexts;
2578 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2591 }
while (s != sinitial);
2596 shead = refarc->
from;
2604 stail = sinitial->
tmp;
2636 if (sclone->
nouts == 0)
2646 for (
a = shead->
outs;
a != NULL;
a = nexta)
2648 nexta =
a->outchain;
2698 struct state *ssource,
2699 struct state *sclone,
2700 struct state *spredecessor,
2717 donemap = curdonemap;
2718 if (donemap == NULL)
2720 donemap = (
char *)
MALLOC(nstates *
sizeof(
char));
2721 if (donemap == NULL)
2727 if (outerdonemap != NULL)
2735 memcpy(donemap, outerdonemap, nstates *
sizeof(
char));
2740 memset(donemap, 0, nstates *
sizeof(
char));
2741 assert(spredecessor->
no < nstates);
2742 donemap[spredecessor->
no] = 1;
2749 donemap[ssource->
no] = 1;
2771 struct state *sto =
a->to;
2782 struct state *prevclone;
2791 if (donemap[sto->
no] != 0)
2801 if (
a2->to->tmp == sto)
2814 if (refarc &&
a->type == refarc->
type &&
a->co == refarc->
co)
2868 struct state *stoclone;
2871 if (stoclone == NULL)
2877 stoclone->
tmp = sto;
2899 if (curdonemap == NULL)
2903 struct state *stoclone =
a->to;
2908 stoclone->
tmp = NULL;
2932 struct state *nexts;
2982 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3008 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3033 for (aa =
a->to->outs; aa != NULL; aa = aa->
outchain)
3089 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3138 if (haspaths == NULL)
3140 memset(haspaths, 0,
nfa->
nstates *
sizeof(
bool *));
3153 bool *haspath = haspaths[
nfa->
pre->
no];
3167 for (minmatch = 0; minmatch <=
DUPINF + 1; minmatch++)
3169 if (haspath[minmatch])
3173 for (maxmatch = minmatch; maxmatch <
DUPINF + 1; maxmatch++)
3175 if (!haspath[maxmatch + 1])
3178 for (morematch = maxmatch + 1; morematch <=
DUPINF + 1; morematch++)
3180 if (haspath[morematch])
3187 if (haspath != NULL)
3210 if (haspaths[
i] != NULL)
3244 bool result =
false;
3245 bool foundloop =
false;
3261 if (haspath == NULL)
3263 memset(haspath, 0, (
DUPINF + 2) *
sizeof(
bool));
3269 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3284 else if (
a->to == s)
3289 else if (
a->to->tmp != NULL)
3302 if (haspaths[
a->to->no] == NULL)
3315 nexthaspath = haspaths[
a->to->no];
3316 assert(nexthaspath != NULL);
3335 haspath[
i + 1] |= nexthaspath[
i];
3341 if (result && foundloop)
3363 haspaths[s->
no] = haspath;
3393 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3401 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3405 if (
a->to->tmp != NULL)
3411 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3415 if (
a->to->tmp != NULL)
3443 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3448 a->from->tmp =
a->from;
3451 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3455 if (
a->from->tmp != NULL)
3456 a->from->tmp = NULL;
3461 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3465 if (
a->from->tmp != NULL)
3468 a->from->tmp = NULL;
3496 narcs += s->
nouts + 1;
3532 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3579 const struct carc *aa = (
const struct carc *)
a;
3580 const struct carc *bb = (
const struct carc *)
b;
3582 if (aa->
co < bb->
co)
3584 if (aa->
co > bb->
co)
3586 if (aa->
to < bb->
to)
3588 if (aa->
to > bb->
to)
3634 fprintf(f,
", maxmatchall inf");
3645 fprintf(f,
"total of %d states, %d arcs\n", nstates, narcs);
3647 dumpcolors(
nfa->
cm, f);
3658 dumpstate(
struct state *s,
3663 fprintf(f,
"%d%s%c", s->
no, (s->
tmp != NULL) ?
"T" :
"",
3666 fprintf(f,
"\tstate chain bad\n");
3668 fprintf(f,
"\tno out arcs\n");
3671 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3674 fprintf(f,
"\tlink from %d to %d on %d's in-chain\n",
3675 a->from->no,
a->to->no, s->
no);
3684 dumparcs(
struct state *s,
3693 while (
a->outchain != NULL)
3707 }
while (
a != NULL);
3716 dumparc(
struct arc *
a,
3748 fprintf(f,
"%c%d",
a->type, (
int)
a->co);
3753 fprintf(f,
"0x%x/0%lo",
a->type, (
long)
a->co);
3758 for (aa =
a->from->outs; aa != NULL; aa = aa->
outchain)
3770 for (aa =
a->to->ins; aa != NULL; aa = aa->
inchain)
3803 fprintf(f,
", maxmatchall inf");
3809 dumpcstate(st,
cnfa, f);
static const FormData_pg_attribute a2
if(TABLE==NULL||TABLE_index==NULL)
static void const char fflush(stdout)
#define qsort(a, b, c, d)
static void colorchain(struct colormap *cm, struct arc *a)
static void uncolorchain(struct colormap *cm, struct arc *a)
static color maxcolor(struct colormap *cm)
static color pseudocolor(struct colormap *cm)
static void rainbow(struct nfa *nfa, struct colormap *cm, int type, color but, struct state *from, struct state *to)
static void mergeins(struct nfa *nfa, struct state *s, struct arc **arcarray, int arccount)
static void cleartraverse(struct nfa *nfa, struct state *s)
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
static void clonesuccessorstates(struct nfa *nfa, struct state *ssource, struct state *sclone, struct state *spredecessor, struct arc *refarc, char *curdonemap, char *outerdonemap, int nstates)
static struct state * emptyreachable(struct nfa *nfa, struct state *s, struct state *lastfound, struct arc **inarcsorig)
static int combine(struct nfa *nfa, struct arc *con, struct arc *a)
static void copyouts(struct nfa *nfa, struct state *oldState, struct state *newState)
static int sortins_cmp(const void *a, const void *b)
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs)
static void cleanup(struct nfa *nfa)
static int findconstraintloop(struct nfa *nfa, struct state *s)
static void compact(struct nfa *nfa, struct cnfa *cnfa)
static void specialcolors(struct nfa *nfa)
static void dupnfa(struct nfa *nfa, struct state *start, struct state *stop, struct state *from, struct state *to)
static long analyze(struct nfa *nfa)
static void duptraverse(struct nfa *nfa, struct state *s, struct state *stmp)
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
static bool checkmatchall_recurse(struct nfa *nfa, struct state *s, bool **haspaths)
static void freenfa(struct nfa *nfa)
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
static int isconstraintarc(struct arc *a)
static struct arc * allocarc(struct nfa *nfa)
static struct state * single_color_transition(struct state *s1, struct state *s2)
static void fixempties(struct nfa *nfa, FILE *f)
static void copyins(struct nfa *nfa, struct state *oldState, struct state *newState)
static int push(struct nfa *nfa, struct arc *con, struct state **intermediates)
static void moveouts(struct nfa *nfa, struct state *oldState, struct state *newState)
static int hasconstraintout(struct state *s)
static void freearc(struct nfa *nfa, struct arc *victim)
static void checkmatchall(struct nfa *nfa)
static struct state * newfstate(struct nfa *nfa, int flag)
static void sortins(struct nfa *nfa, struct state *s)
static void cloneouts(struct nfa *nfa, struct state *old, struct state *from, struct state *to, int type)
static struct nfa * newnfa(struct vars *v, struct colormap *cm, struct nfa *parent)
static int carc_cmp(const void *a, const void *b)
static long optimize(struct nfa *nfa, FILE *f)
static void sortouts(struct nfa *nfa, struct state *s)
static void deltraverse(struct nfa *nfa, struct state *leftend, struct state *s)
static void dropstate(struct nfa *nfa, struct state *s)
static void removetraverse(struct nfa *nfa, struct state *s)
static void pushfwd(struct nfa *nfa, FILE *f)
static int hasnonemptyout(struct state *s)
static void pullback(struct nfa *nfa, FILE *f)
static void markcanreach(struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
static struct state * newstate(struct nfa *nfa)
static void delsub(struct nfa *nfa, struct state *lp, struct state *rp)
static void moveins(struct nfa *nfa, struct state *oldState, struct state *newState)
static bool check_in_colors_match(struct state *s, color co1, color co2)
static void removeconstraints(struct nfa *nfa, struct state *start, struct state *stop)
static int sortouts_cmp(const void *a, const void *b)
static void carcsort(struct carc *first, size_t n)
static int pull(struct nfa *nfa, struct arc *con, struct state **intermediates)
static void changearctarget(struct arc *a, struct state *newto)
static struct arc * findarc(struct state *s, int type, color co)
static void changearcsource(struct arc *a, struct state *newfrom)
static void freestate(struct nfa *nfa, struct state *s)
static void dumpnfa(struct nfa *nfa, FILE *f)
static void markreachable(struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
static void fixconstraintloops(struct nfa *nfa, FILE *f)
static bool check_out_colors_match(struct state *s, color co1, color co2)
static void breakconstraintloop(struct nfa *nfa, struct state *sinitial)
static void freecnfa(struct cnfa *cnfa)
#define STACK_TOO_DEEP(re)
#define REG_MAX_COMPILE_SPACE
#define STATEBATCHSIZE(n)
struct arc a[FLEXIBLE_ARRAY_MEMBER]
struct statebatch * lastsb
struct state * freestates
struct state s[FLEXIBLE_ARRAY_MEMBER]