diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2020-09-14 11:01:31 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2020-09-14 11:01:31 +0300 |
commit | cde8fd84a060bcae8a5960897ff2e2de576b2e08 (patch) | |
tree | 35b446917752ade64aedcb416675581c112400d3 /support | |
parent | 696533f3f4a3b2dd36042eac5e1dce6f9bcec129 (diff) | |
download | egawk-cde8fd84a060bcae8a5960897ff2e2de576b2e08.tar.gz egawk-cde8fd84a060bcae8a5960897ff2e2de576b2e08.tar.bz2 egawk-cde8fd84a060bcae8a5960897ff2e2de576b2e08.zip |
Update dfa.c.
Diffstat (limited to 'support')
-rw-r--r-- | support/ChangeLog | 4 | ||||
-rw-r--r-- | support/dfa.c | 167 |
2 files changed, 97 insertions, 74 deletions
diff --git a/support/ChangeLog b/support/ChangeLog index fc3500a9..0b14f6a6 100644 --- a/support/ChangeLog +++ b/support/ChangeLog @@ -1,3 +1,7 @@ +2020-09-14 Arnold D. Robbins <arnold@skeeve.com> + + * dfa.c: Synced from GNULIB. + 2020-07-29 Arnold D. Robbins <arnold@skeeve.com> * dfa.h: Synced from GNULIB. diff --git a/support/dfa.c b/support/dfa.c index 3327f627..8a012ad6 100644 --- a/support/dfa.c +++ b/support/dfa.c @@ -63,10 +63,10 @@ isasciidigit (char c) #ifndef FALLTHROUGH # if 201710L < __STDC_VERSION__ # define FALLTHROUGH [[__fallthrough__]] -# elif __GNUC__ < 7 -# define FALLTHROUGH ((void) 0) -# else +# elif (__GNUC__ >= 7) || (__clang_major__ >= 10) # define FALLTHROUGH __attribute__ ((__fallthrough__)) +# else +# define FALLTHROUGH ((void) 0) # endif #endif @@ -481,10 +481,12 @@ struct dfa idx_t depth; /* Depth required of an evaluation stack used for depth-first traversal of the parse tree. */ - idx_t nleaves; /* Number of leaves on the parse tree. */ + idx_t nleaves; /* Number of non-EMPTY leaves + in the parse tree. */ idx_t nregexps; /* Count of parallel regexps being built with dfaparse. */ bool fast; /* The DFA is fast. */ + bool epsilon; /* Does a token match only the empty string? */ token utf8_anychar_classes[9]; /* To lower ANYCHAR in UTF-8 locales. */ mbstate_t mbs; /* Multibyte conversion state. */ @@ -1614,18 +1616,31 @@ addtok_mb (struct dfa *dfa, token t, char mbprop) dfa->parse.depth--; break; + case EMPTY: + dfa->epsilon = true; + goto increment_depth; + case BACKREF: dfa->fast = false; + goto increment_nleaves; + + case BEGLINE: + case ENDLINE: + case BEGWORD: + case ENDWORD: + case LIMWORD: + case NOTLIMWORD: + dfa->epsilon = true; FALLTHROUGH; default: + increment_nleaves: dfa->nleaves++; - FALLTHROUGH; - case EMPTY: + increment_depth: dfa->parse.depth++; + if (dfa->depth < dfa->parse.depth) + dfa->depth = dfa->parse.depth; break; } - if (dfa->parse.depth > dfa->depth) - dfa->depth = dfa->parse.depth; } static void addtok_wc (struct dfa *dfa, wint_t wc); @@ -2146,6 +2161,8 @@ merge (position_set const *s1, position_set const *s2, position_set *m) merge_constrained (s1, s2, -1, m); } +/* Merge into DST all the elements of SRC, possibly destroying + the contents of the temporary M. */ static void merge2 (position_set *dst, position_set const *src, position_set *m) { @@ -2290,24 +2307,26 @@ state_index (struct dfa *d, position_set const *s, int context) return i; } -/* Find the epsilon closure of a set of positions. If any position of the set +/* Find the epsilon closure of D's set of positions. If any position of the set contains a symbol that matches the empty string in some context, replace that position with the elements of its follow labeled with an appropriate constraint. Repeat exhaustively until no funny positions are left. - S->elems must be large enough to hold the result. */ + S->elems must be large enough to hold the result. BACKWARD is D's + backward set; use and update it too. */ static void -epsclosure (struct dfa const *d) +epsclosure (struct dfa const *d, position_set *backward) { position_set tmp; alloc_position_set (&tmp, d->nleaves); for (idx_t i = 0; i < d->tindex; i++) - if (d->follows[i].nelem > 0 && d->tokens[i] >= NOTCHAR - && d->tokens[i] != BACKREF && d->tokens[i] != ANYCHAR - && d->tokens[i] != MBCSET && d->tokens[i] < CSET) + if (0 < d->follows[i].nelem) { unsigned int constraint; switch (d->tokens[i]) { + default: + continue; + case BEGLINE: constraint = BEGLINE_CONSTRAINT; break; @@ -2326,16 +2345,19 @@ epsclosure (struct dfa const *d) case NOTLIMWORD: constraint = NOTLIMWORD_CONSTRAINT; break; - default: + case EMPTY: constraint = NO_CONSTRAINT; break; } delete (i, &d->follows[i]); - for (idx_t j = 0; j < d->tindex; j++) - if (i != j && d->follows[j].nelem > 0) - replace (&d->follows[j], i, &d->follows[i], constraint, &tmp); + for (idx_t j = 0; j < backward[i].nelem; j++) + replace (&d->follows[backward[i].elems[j].index], i, &d->follows[i], + constraint, &tmp); + for (idx_t j = 0; j < d->follows[i].nelem; j++) + replace (&backward[d->follows[i].elems[j].index], i, &backward[i], + NO_CONSTRAINT, &tmp); } free (tmp.elems); } @@ -2405,8 +2427,6 @@ merge_nfa_state (struct dfa *d, idx_t tindex, char *flags, position_set *follows = d->follows; idx_t nelem = 0; - d->constraints[tindex] = 0; - for (idx_t i = 0; i < follows[tindex].nelem; i++) { idx_t sindex = follows[tindex].elems[i].index; @@ -2471,34 +2491,27 @@ compare (const void *a, const void *b) static void reorder_tokens (struct dfa *d) { - idx_t nleaves; - ptrdiff_t *map; - token *tokens; - position_set *follows; - int *constraints; - char *multibyte_prop; - - nleaves = 0; - - map = xnmalloc (d->tindex, sizeof *map); - + idx_t nleaves = 0; + ptrdiff_t *map = xnmalloc (d->tindex, sizeof *map); map[0] = nleaves++; - for (idx_t i = 1; i < d->tindex; i++) map[i] = -1; - tokens = xnmalloc (d->nleaves, sizeof *tokens); - follows = xnmalloc (d->nleaves, sizeof *follows); - constraints = xnmalloc (d->nleaves, sizeof *constraints); + token *tokens = xnmalloc (d->nleaves, sizeof *tokens); + position_set *follows = xnmalloc (d->nleaves, sizeof *follows); + int *constraints = xnmalloc (d->nleaves, sizeof *constraints); + char *multibyte_prop = (d->localeinfo.multibyte + ? xnmalloc (d->nleaves, sizeof *multibyte_prop) + : NULL); - if (d->localeinfo.multibyte) - multibyte_prop = xnmalloc (d->nleaves, sizeof *multibyte_prop); - else - multibyte_prop = NULL; + for (idx_t i = 0; i < d->tindex; i++) + for (idx_t j = 0; j < d->follows[i].nelem; j++) + if (map[d->follows[i].elems[j].index] < 0) + map[d->follows[i].elems[j].index] = nleaves++; for (idx_t i = 0; i < d->tindex; i++) { - if (map[i] == -1) + if (map[i] < 0) { free (d->follows[i].elems); d->follows[i].elems = NULL; @@ -2514,12 +2527,7 @@ reorder_tokens (struct dfa *d) multibyte_prop[map[i]] = d->multibyte_prop[i]; for (idx_t j = 0; j < d->follows[i].nelem; j++) - { - if (map[d->follows[i].elems[j].index] == -1) - map[d->follows[i].elems[j].index] = nleaves++; - - d->follows[i].elems[j].index = map[d->follows[i].elems[j].index]; - } + d->follows[i].elems[j].index = map[d->follows[i].elems[j].index]; qsort (d->follows[i].elems, d->follows[i].nelem, sizeof *d->follows[i].elems, compare); @@ -2570,7 +2578,7 @@ dfaoptimize (struct dfa *d) position_set *merged = &merged0; alloc_position_set (merged, d->nleaves); - d->constraints = xnmalloc (d->tindex, sizeof *d->constraints); + d->constraints = xcalloc (d->tindex, sizeof *d->constraints); for (idx_t i = 0; i < d->tindex; i++) if (flags[i] & OPT_QUEUED) @@ -2659,10 +2667,11 @@ dfaanalyze (struct dfa *d, bool searchflag) position_set merged; /* Result of merging sets. */ addtok (d, CAT); + idx_t tindex = d->tindex; #ifdef DEBUG fprintf (stderr, "dfaanalyze:\n"); - for (idx_t i = 0; i < d->tindex; i++) + for (idx_t i = 0; i < tindex; i++) { fprintf (stderr, " %td:", i); prtok (d->tokens[i]); @@ -2672,9 +2681,11 @@ dfaanalyze (struct dfa *d, bool searchflag) d->searchflag = searchflag; alloc_position_set (&merged, d->nleaves); - d->follows = xcalloc (d->tindex, sizeof *d->follows); + d->follows = xcalloc (tindex, sizeof *d->follows); + position_set *backward + = d->epsilon ? xcalloc (tindex, sizeof *backward) : NULL; - for (idx_t i = 0; i < d->tindex; i++) + for (idx_t i = 0; i < tindex; i++) { switch (d->tokens[i]) { @@ -2694,12 +2705,8 @@ dfaanalyze (struct dfa *d, bool searchflag) { tmp.elems = firstpos - stk[-1].nfirstpos; tmp.nelem = stk[-1].nfirstpos; - position *p = lastpos - stk[-1].nlastpos; - for (idx_t j = 0; j < stk[-1].nlastpos; j++) - { - merge (&tmp, &d->follows[p[j].index], &merged); - copy (&merged, &d->follows[p[j].index]); - } + for (position *p = lastpos - stk[-1].nlastpos; p < lastpos; p++) + merge2 (&d->follows[p->index], &tmp, &merged); } FALLTHROUGH; case QMARK: @@ -2709,17 +2716,27 @@ dfaanalyze (struct dfa *d, bool searchflag) break; case CAT: + /* Every element in the lastpos of the first argument is in + the backward set of every element in the firstpos of the + second argument. */ + if (backward) + { + tmp.nelem = stk[-2].nlastpos; + tmp.elems = lastpos - stk[-1].nlastpos - stk[-2].nlastpos; + for (position *p = firstpos - stk[-1].nfirstpos; + p < firstpos; p++) + merge2 (&backward[p->index], &tmp, &merged); + } + /* Every element in the firstpos of the second argument is in the follow of every element in the lastpos of the first argument. */ { tmp.nelem = stk[-1].nfirstpos; tmp.elems = firstpos - stk[-1].nfirstpos; - position *p = lastpos - stk[-1].nlastpos - stk[-2].nlastpos; - for (idx_t j = 0; j < stk[-2].nlastpos; j++) - { - merge (&tmp, &d->follows[p[j].index], &merged); - copy (&merged, &d->follows[p[j].index]); - } + for (position *plim = lastpos - stk[-1].nlastpos, + *p = plim - stk[-2].nlastpos; + p < plim; p++) + merge2 (&d->follows[p->index], &tmp, &merged); } /* The firstpos of a CAT node is the firstpos of the first argument, @@ -2800,14 +2817,21 @@ dfaanalyze (struct dfa *d, bool searchflag) #endif } - /* For each follow set that is the follow set of a real position, replace - it with its epsilon closure. */ - epsclosure (d); + if (backward) + { + /* For each follow set that is the follow set of a real position, + replace it with its epsilon closure. */ + epsclosure (d, backward); + + for (idx_t i = 0; i < tindex; i++) + free (backward[i].elems); + free (backward); + } dfaoptimize (d); #ifdef DEBUG - for (idx_t i = 0; i < d->tindex; i++) + for (idx_t i = 0; i < tindex; i++) if (d->tokens[i] == BEG || d->tokens[i] < NOTCHAR || d->tokens[i] == BACKREF || d->tokens[i] == ANYCHAR || d->tokens[i] == MBCSET || d->tokens[i] >= CSET) @@ -2831,12 +2855,10 @@ dfaanalyze (struct dfa *d, bool searchflag) append (pos, &tmp); - d->separates = xnmalloc (d->tindex, sizeof *d->separates); + d->separates = xcalloc (tindex, sizeof *d->separates); - for (idx_t i = 0; i < d->tindex; i++) + for (idx_t i = 0; i < tindex; i++) { - d->separates[i] = 0; - if (prev_newline_dependent (d->constraints[i])) d->separates[i] |= CTX_NEWLINE; if (prev_letter_dependent (d->constraints[i])) @@ -3134,10 +3156,7 @@ build_state (state_num s, struct dfa *d, unsigned char uc) mergeit &= d->multibyte_prop[group.elems[j].index]; } if (mergeit) - { - merge (&d->states[0].elems, &group, &tmp); - copy (&tmp, &group); - } + merge2 (&group, &d->states[0].elems, &tmp); } /* Find out if the new state will want any context information, |