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)
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)
1504 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
1524static struct state *
1530 if (
s1->nouts == 1 &&
s1->outs->type ==
EMPTY)
1533 if (
s2->nins == 1 &&
s2->ins->type ==
EMPTY)
1539 if (
s1->outs == NULL)
1542 for (
a =
s1->outs;
a != NULL;
a =
a->outchain)
1598 int verbose = (f != NULL) ? 1 : 0;
1601 fprintf(f,
"\ninitial cleanup:\n");
1619 fprintf(f,
"\nconstraints:\n");
1626 fprintf(f,
"\nfinal cleanup:\n");
1644 struct state *nexts;
1647 struct state *intermediates;
1657 intermediates = NULL;
1660 nexta =
a->outchain;
1661 if (
a->type ==
'^' ||
a->type ==
BEHIND)
1666 while (intermediates != NULL)
1668 struct state *ns = intermediates->
tmp;
1670 intermediates->
tmp = NULL;
1691 nexta =
a->outchain;
1722 struct state **intermediates)
1733 if (from->
nins == 0)
1744 if (from->
nouts > 1)
1772 for (s = *intermediates; s != NULL; s = s->
tmp)
1783 s->
tmp = *intermediates;
1815 struct state *nexts;
1818 struct state *intermediates;
1828 intermediates = NULL;
1832 if (
a->type ==
'$' ||
a->type ==
AHEAD)
1837 while (intermediates != NULL)
1839 struct state *ns = intermediates->
tmp;
1841 intermediates->
tmp = NULL;
1893 struct state **intermediates)
1933 nexta =
a->outchain;
1943 for (s = *intermediates; s != NULL; s = s->
tmp)
1954 s->
tmp = *intermediates;
1991#define CA(ct,at) (((ct)<<CHAR_BIT) | (at))
1993 switch (
CA(con->
type,
a->type))
2001 if (con->
co ==
a->co)
2022 if (con->
co ==
a->co)
2028 if (con->
co ==
a->co)
2081 struct state *nexts;
2085 struct arc **inarcsorig;
2086 struct arc **arcarray;
2102 assert(
a != NULL &&
a->outchain == NULL);
2122 assert(
a != NULL &&
a->inchain == NULL);
2187 if (inarcsorig == NULL)
2195 inarcsorig[s->
no] = s->
ins;
2196 totalinarcs += s->
nins;
2205 arcarray = (
struct arc **)
MALLOC(totalinarcs *
sizeof(
struct arc *));
2206 if (arcarray == NULL)
2225 for (
a = inarcsorig[
s2->no];
a != NULL;
a =
a->inchain)
2228 arcarray[arccount++] =
a;
2236 assert(arccount <= totalinarcs);
2245 nskip = s->
nins - prevnins;
2249 inarcsorig[s->
no] =
a;
2263 for (
a = s->
outs;
a != NULL;
a = nexta)
2265 nexta =
a->outchain;
2276 for (s =
nfa->
states; s != NULL; s = nexts)
2302static struct state *
2305 struct state *lastfound,
2306 struct arc **inarcsorig)
2319 for (
a = inarcsorig[s->
no];
a != NULL;
a =
a->inchain)
2321 if (
a->type ==
EMPTY &&
a->from->tmp == NULL)
2353 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2374 struct state *nexts;
2393 nexta =
a->outchain;
2408 if (
NISERR() || !hasconstraints)
2437 for (s =
nfa->
states; s != NULL; s = nexts)
2490 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2494 struct state *sto =
a->to;
2561 struct state *shead;
2562 struct state *stail;
2563 struct state *sclone;
2564 struct state *nexts;
2585 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
2598 }
while (s != sinitial);
2603 shead = refarc->
from;
2611 stail = sinitial->
tmp;
2643 if (sclone->
nouts == 0)
2653 for (
a = shead->
outs;
a != NULL;
a = nexta)
2655 nexta =
a->outchain;
2705 struct state *ssource,
2706 struct state *sclone,
2707 struct state *spredecessor,
2724 donemap = curdonemap;
2725 if (donemap == NULL)
2727 donemap = (
char *)
MALLOC(nstates *
sizeof(
char));
2728 if (donemap == NULL)
2734 if (outerdonemap != NULL)
2742 memcpy(donemap, outerdonemap, nstates *
sizeof(
char));
2747 memset(donemap, 0, nstates *
sizeof(
char));
2748 assert(spredecessor->
no < nstates);
2749 donemap[spredecessor->
no] = 1;
2756 donemap[ssource->
no] = 1;
2778 struct state *sto =
a->to;
2789 struct state *prevclone;
2798 if (donemap[sto->
no] != 0)
2808 if (
a2->to->tmp == sto)
2821 if (refarc &&
a->type == refarc->
type &&
a->co == refarc->
co)
2875 struct state *stoclone;
2878 if (stoclone == NULL)
2884 stoclone->
tmp = sto;
2906 if (curdonemap == NULL)
2910 struct state *stoclone =
a->to;
2915 stoclone->
tmp = NULL;
2947 for (
a = s->
outs;
a != NULL;
a = nexta)
2949 nexta =
a->outchain;
2967 struct state *nexts;
3017 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3043 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3068 for (aa =
a->to->outs; aa != NULL; aa = aa->
outchain)
3124 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3173 if (haspaths == NULL)
3175 memset(haspaths, 0,
nfa->
nstates *
sizeof(
bool *));
3188 bool *haspath = haspaths[
nfa->
pre->
no];
3202 for (minmatch = 0; minmatch <=
DUPINF + 1; minmatch++)
3204 if (haspath[minmatch])
3208 for (maxmatch = minmatch; maxmatch <
DUPINF + 1; maxmatch++)
3210 if (!haspath[maxmatch + 1])
3213 for (morematch = maxmatch + 1; morematch <=
DUPINF + 1; morematch++)
3215 if (haspath[morematch])
3222 if (haspath != NULL)
3245 if (haspaths[
i] != NULL)
3279 bool result =
false;
3280 bool foundloop =
false;
3295 haspath = (
bool *)
MALLOC((
DUPINF + 2) *
sizeof(bool));
3296 if (haspath == NULL)
3298 memset(haspath, 0, (
DUPINF + 2) *
sizeof(
bool));
3304 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3319 else if (
a->to == s)
3324 else if (
a->to->tmp != NULL)
3337 if (haspaths[
a->to->no] == NULL)
3350 nexthaspath = haspaths[
a->to->no];
3351 assert(nexthaspath != NULL);
3370 haspath[
i + 1] |= nexthaspath[
i];
3376 if (result && foundloop)
3398 haspaths[s->
no] = haspath;
3428 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3436 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3440 if (
a->to->tmp != NULL)
3446 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3450 if (
a->to->tmp != NULL)
3478 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3483 a->from->tmp =
a->from;
3486 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3490 if (
a->from->tmp != NULL)
3491 a->from->tmp = NULL;
3496 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3500 if (
a->from->tmp != NULL)
3503 a->from->tmp = NULL;
3531 narcs += s->
nouts + 1;
3567 for (
a = s->
outs;
a != NULL;
a =
a->outchain)
3614 const struct carc *aa = (
const struct carc *)
a;
3615 const struct carc *bb = (
const struct carc *)
b;
3617 if (aa->
co < bb->
co)
3619 if (aa->
co > bb->
co)
3621 if (aa->
to < bb->
to)
3623 if (aa->
to > bb->
to)
3671 fprintf(f,
", maxmatchall inf");
3682 fprintf(f,
"total of %d states, %d arcs\n", nstates, narcs);
3684 dumpcolors(
nfa->
cm, f);
3695dumpstate(
struct state *s,
3700 fprintf(f,
"%d%s%c", s->
no, (s->
tmp != NULL) ?
"T" :
"",
3703 fprintf(f,
"\tstate chain bad\n");
3705 fprintf(f,
"\tno out arcs\n");
3708 for (
a = s->
ins;
a != NULL;
a =
a->inchain)
3711 fprintf(f,
"\tlink from %d to %d on %d's in-chain\n",
3712 a->from->no,
a->to->no, s->
no);
3721dumparcs(
struct state *s,
3730 while (
a->outchain != NULL)
3744 }
while (
a != NULL);
3753dumparc(
struct arc *
a,
3785 fprintf(f,
"%c%d",
a->type, (
int)
a->co);
3793 fprintf(f,
"0x%x/0%lo",
a->type, (
long)
a->co);
3798 for (aa =
a->from->outs; aa != NULL; aa = aa->
outchain)
3810 for (aa =
a->to->ins; aa != NULL; aa = aa->
inchain)
3843 fprintf(f,
", maxmatchall inf");
3849 dumpcstate(st,
cnfa, f);
#define fprintf(file, fmt, msg)
static const FormData_pg_attribute a2
if(TABLE==NULL||TABLE_index==NULL)
#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 struct state * emptyreachable(struct nfa *nfa, struct state *s, struct state *lastfound, struct arc **inarcsorig)
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 int combine(struct nfa *nfa, struct arc *con, struct arc *a)
static void copyouts(struct nfa *nfa, struct state *oldState, struct state *newState)
static struct arc * findarc(struct state *s, int type, color co)
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 struct arc * allocarc(struct nfa *nfa)
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 struct state * single_color_transition(struct state *s1, struct state *s2)
static struct nfa * newnfa(struct vars *v, struct colormap *cm, struct nfa *parent)
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
static int isconstraintarc(struct arc *a)
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 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 int carc_cmp(const void *a, const void *b)
static long optimize(struct nfa *nfa, FILE *f)
static struct state * newfstate(struct nfa *nfa, int flag)
static void removecantmatch(struct nfa *nfa)
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 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 struct state * newstate(struct nfa *nfa)
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 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]