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 *
215 static struct state *
235 while ((
a = s->
ins) != NULL)
237 while ((
a = s->
outs) != NULL)
310 if (
a->to ==
to &&
a->co ==
co &&
a->type == t)
315 for (
a =
to->
ins;
a != NULL;
a =
a->inchain)
316 if (
a->from ==
from &&
a->co ==
co &&
a->type == t)
355 a->inchainRev = NULL;
360 a->outchainRev = NULL;
431 struct arc *predecessor;
442 if (predecessor == NULL)
462 if (predecessor == NULL)
499 struct state *oldfrom =
a->from;
500 struct arc *predecessor;
502 assert(oldfrom != newfrom);
506 predecessor =
a->outchainRev;
507 if (predecessor == NULL)
510 oldfrom->
outs =
a->outchain;
517 if (
a->outchain != NULL)
519 assert(
a->outchain->outchainRev ==
a);
520 a->outchain->outchainRev = predecessor;
527 a->outchain = newfrom->
outs;
528 a->outchainRev = NULL;
543 struct state *oldto =
a->to;
544 struct arc *predecessor;
550 predecessor =
a->inchainRev;
551 if (predecessor == NULL)
554 oldto->
ins =
a->inchain;
561 if (
a->inchain != NULL)
563 assert(
a->inchain->inchainRev ==
a);
564 a->inchain->inchainRev = predecessor;
571 a->inchain = newto->
ins;
572 a->inchainRev = NULL;
587 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
606 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
631 struct arc **sortarray;
639 sortarray = (
struct arc **)
MALLOC(n *
sizeof(
struct arc *));
640 if (sortarray == NULL)
646 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
655 a->inchain = sortarray[1];
656 a->inchainRev = NULL;
657 for (
i = 1;
i < n - 1;
i++)
660 a->inchain = sortarray[
i + 1];
661 a->inchainRev = sortarray[
i - 1];
665 a->inchainRev = sortarray[
i - 1];
672 const struct arc *aa = *((
const struct arc *
const *)
a);
673 const struct arc *bb = *((
const struct arc *
const *)
b);
698 struct arc **sortarray;
706 sortarray = (
struct arc **)
MALLOC(n *
sizeof(
struct arc *));
707 if (sortarray == NULL)
713 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
722 a->outchain = sortarray[1];
723 a->outchainRev = NULL;
724 for (
i = 1;
i < n - 1;
i++)
727 a->outchain = sortarray[
i + 1];
728 a->outchainRev = sortarray[
i - 1];
732 a->outchainRev = sortarray[
i - 1];
739 const struct arc *aa = *((
const struct arc *
const *)
a);
740 const struct arc *bb = *((
const struct arc *
const *)
b);
766 #define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs) \
767 ((nsrcarcs) < 4 ? 0 : ((nsrcarcs) > 32 || (ndestarcs) > 32))
787 struct state *oldState,
788 struct state *newState)
790 assert(oldState != newState);
792 if (newState->
nins == 0)
797 while ((
a = oldState->
ins) != NULL)
808 while ((
a = oldState->
ins) != NULL)
840 while (oa != NULL && na != NULL)
895 struct state *oldState,
896 struct state *newState)
898 assert(oldState != newState);
901 if (newState->
nins == 0)
906 for (
a = oldState->
ins;
a != NULL;
a =
a->inchain)
915 for (
a = oldState->
ins;
a != NULL;
a =
a->inchain)
944 while (oa != NULL && na != NULL)
989 struct arc **arcarray,
1021 for (
i = 1;
i < arccount;
i++)
1027 arcarray[++
j] = arcarray[
i];
1046 while (
i < arccount && na != NULL)
1048 struct arc *
a = arcarray[
i];
1070 while (
i < arccount)
1073 struct arc *
a = arcarray[
i];
1087 struct state *oldState,
1088 struct state *newState)
1090 assert(oldState != newState);
1092 if (newState->
nouts == 0)
1097 while ((
a = oldState->
outs) != NULL)
1108 while ((
a = oldState->
outs) != NULL)
1138 oa = oldState->
outs;
1139 na = newState->
outs;
1140 while (oa != NULL && na != NULL)
1192 struct state *oldState,
1193 struct state *newState)
1195 assert(oldState != newState);
1198 if (newState->
nouts == 0)
1203 for (
a = oldState->
outs;
a != NULL;
a =
a->outchain)
1212 for (
a = oldState->
outs;
a != NULL;
a =
a->outchain)
1239 oa = oldState->
outs;
1240 na = newState->
outs;
1241 while (oa != NULL && na != NULL)
1295 for (
a = old->
outs;
a != NULL;
a =
a->outchain)
1333 struct state *leftend,
1353 while ((
a = s->
outs) != NULL)
1361 if (to->
nins == 0 && to->
tmp == NULL)
1384 struct state *start,
1448 struct state *start,
1531 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
1551 static struct state *
1557 if (
s1->nouts == 1 &&
s1->outs->type ==
EMPTY)
1560 if (
s2->nins == 1 &&
s2->ins->type ==
EMPTY)
1566 if (
s1->outs == NULL)
1569 for (
a =
s1->outs;
a != NULL;
a =
a->outchain)
1625 int verbose = (f != NULL) ? 1 : 0;
1628 fprintf(f,
"\ninitial cleanup:\n");
1640 fprintf(f,
"\nconstraints:\n");
1647 fprintf(f,
"\nfinal cleanup:\n");
1665 struct state *nexts;
1668 struct state *intermediates;
1678 intermediates = NULL;
1681 nexta =
a->outchain;
1682 if (
a->type ==
'^' ||
a->type ==
BEHIND)
1687 while (intermediates != NULL)
1689 struct state *ns = intermediates->
tmp;
1691 intermediates->
tmp = NULL;
1712 nexta =
a->outchain;
1743 struct state **intermediates)
1754 if (from->
nins == 0)
1765 if (from->
nouts > 1)
1793 for (s = *intermediates; s != NULL; s = s->
tmp)
1804 s->
tmp = *intermediates;
1836 struct state *nexts;
1839 struct state *intermediates;
1849 intermediates = NULL;
1853 if (
a->type ==
'$' ||
a->type ==
AHEAD)
1858 while (intermediates != NULL)
1860 struct state *ns = intermediates->
tmp;
1862 intermediates->
tmp = NULL;
1914 struct state **intermediates)
1954 nexta =
a->outchain;
1964 for (s = *intermediates; s != NULL; s = s->
tmp)
1975 s->
tmp = *intermediates;
2012 #define CA(ct,at) (((ct)<<CHAR_BIT) | (at))
2014 switch (
CA(con->
type,
a->type))
2022 if (con->
co ==
a->co)
2043 if (con->
co ==
a->co)
2049 if (con->
co ==
a->co)
2102 struct state *nexts;
2106 struct arc **inarcsorig;
2107 struct arc **arcarray;
2123 assert(
a != NULL &&
a->outchain == NULL);
2143 assert(
a != NULL &&
a->inchain == NULL);
2208 if (inarcsorig == NULL)
2216 inarcsorig[s->
no] = s->
ins;
2217 totalinarcs += s->
nins;
2226 arcarray = (
struct arc **)
MALLOC(totalinarcs *
sizeof(
struct arc *));
2227 if (arcarray == NULL)
2246 for (
a = inarcsorig[
s2->no];
a != NULL;
a =
a->inchain)
2249 arcarray[arccount++] =
a;
2257 assert(arccount <= totalinarcs);
2266 nskip = s->
nins - prevnins;
2270 inarcsorig[s->
no] =
a;
2284 for (
a = s->
outs;
a != NULL;
a = nexta)
2286 nexta =
a->outchain;
2297 for (s =
nfa->
states; s != NULL; s = nexts)
2323 static struct state *
2326 struct state *lastfound,
2327 struct arc **inarcsorig)
2340 for (
a = inarcsorig[s->
no];
a != NULL;
a =
a->inchain)
2342 if (
a->type ==
EMPTY &&
a->from->tmp == NULL)
2374 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2395 struct state *nexts;
2414 nexta =
a->outchain;
2429 if (
NISERR() || !hasconstraints)
2458 for (s =
nfa->
states; s != NULL; s = nexts)
2511 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2515 struct state *sto =
a->to;
2582 struct state *shead;
2583 struct state *stail;
2584 struct state *sclone;
2585 struct state *nexts;
2606 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2619 }
while (s != sinitial);
2624 shead = refarc->
from;
2632 stail = sinitial->
tmp;
2664 if (sclone->
nouts == 0)
2674 for (
a = shead->
outs;
a != NULL;
a = nexta)
2676 nexta =
a->outchain;
2726 struct state *ssource,
2727 struct state *sclone,
2728 struct state *spredecessor,
2745 donemap = curdonemap;
2746 if (donemap == NULL)
2748 donemap = (
char *)
MALLOC(nstates *
sizeof(
char));
2749 if (donemap == NULL)
2755 if (outerdonemap != NULL)
2763 memcpy(donemap, outerdonemap, nstates *
sizeof(
char));
2768 memset(donemap, 0, nstates *
sizeof(
char));
2769 assert(spredecessor->
no < nstates);
2770 donemap[spredecessor->
no] = 1;
2777 donemap[ssource->
no] = 1;
2799 struct state *sto =
a->to;
2810 struct state *prevclone;
2819 if (donemap[sto->
no] != 0)
2829 if (
a2->to->tmp == sto)
2842 if (refarc &&
a->type == refarc->
type &&
a->co == refarc->
co)
2896 struct state *stoclone;
2899 if (stoclone == NULL)
2905 stoclone->
tmp = sto;
2927 if (curdonemap == NULL)
2931 struct state *stoclone =
a->to;
2936 stoclone->
tmp = NULL;
2960 struct state *nexts;
3010 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3036 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3061 for (aa =
a->to->outs; aa != NULL; aa = aa->
outchain)
3117 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3166 if (haspaths == NULL)
3168 memset(haspaths, 0,
nfa->
nstates *
sizeof(
bool *));
3181 bool *haspath = haspaths[
nfa->
pre->
no];
3195 for (minmatch = 0; minmatch <=
DUPINF + 1; minmatch++)
3197 if (haspath[minmatch])
3201 for (maxmatch = minmatch; maxmatch <
DUPINF + 1; maxmatch++)
3203 if (!haspath[maxmatch + 1])
3206 for (morematch = maxmatch + 1; morematch <=
DUPINF + 1; morematch++)
3208 if (haspath[morematch])
3215 if (haspath != NULL)
3238 if (haspaths[
i] != NULL)
3272 bool result =
false;
3273 bool foundloop =
false;
3293 if (haspath == NULL)
3295 memset(haspath, 0, (
DUPINF + 2) *
sizeof(
bool));
3301 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3316 else if (
a->to == s)
3321 else if (
a->to->tmp != NULL)
3334 if (haspaths[
a->to->no] == NULL)
3347 nexthaspath = haspaths[
a->to->no];
3348 assert(nexthaspath != NULL);
3367 haspath[
i + 1] |= nexthaspath[
i];
3373 if (result && foundloop)
3395 haspaths[s->
no] = haspath;
3425 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3433 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3437 if (
a->to->tmp != NULL)
3443 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3447 if (
a->to->tmp != NULL)
3475 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3480 a->from->tmp =
a->from;
3483 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3487 if (
a->from->tmp != NULL)
3488 a->from->tmp = NULL;
3493 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3497 if (
a->from->tmp != NULL)
3500 a->from->tmp = NULL;
3528 narcs += s->
nouts + 1;
3564 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3611 const struct carc *aa = (
const struct carc *)
a;
3612 const struct carc *bb = (
const struct carc *)
b;
3614 if (aa->
co < bb->
co)
3616 if (aa->
co > bb->
co)
3618 if (aa->
to < bb->
to)
3620 if (aa->
to > bb->
to)
3666 fprintf(f,
", maxmatchall inf");
3677 fprintf(f,
"total of %d states, %d arcs\n", nstates, narcs);
3679 dumpcolors(
nfa->
cm, f);
3690 dumpstate(
struct state *s,
3695 fprintf(f,
"%d%s%c", s->
no, (s->
tmp != NULL) ?
"T" :
"",
3698 fprintf(f,
"\tstate chain bad\n");
3700 fprintf(f,
"\tno out arcs\n");
3703 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3706 fprintf(f,
"\tlink from %d to %d on %d's in-chain\n",
3707 a->from->no,
a->to->no, s->
no);
3716 dumparcs(
struct state *s,
3725 while (
a->outchain != NULL)
3739 }
while (
a != NULL);
3748 dumparc(
struct arc *
a,
3780 fprintf(f,
"%c%d",
a->type, (
int)
a->co);
3785 fprintf(f,
"0x%x/0%lo",
a->type, (
long)
a->co);
3790 for (aa =
a->from->outs; aa != NULL; aa = aa->
outchain)
3802 for (aa =
a->to->ins; aa != NULL; aa = aa->
inchain)
3835 fprintf(f,
", maxmatchall inf");
3841 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 CANCEL_REQUESTED(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]