📄 dfa.c
字号:
prtok(d->tokens[d->follows[i].elems[j].index]); } putc('\n', stderr);#endif copy(&d->follows[i], &merged); epsclosure(&merged, d); if (d->follows[i].nelem < merged.nelem) REALLOC(d->follows[i].elems, position, merged.nelem); copy(&merged, &d->follows[i]); } /* Get the epsilon closure of the firstpos of the regexp. The result will be the set of positions of state 0. */ merged.nelem = 0; for (i = 0; i < nfirstpos[-1]; ++i) insert(firstpos[i], &merged); epsclosure(&merged, d); /* Check if any of the positions of state 0 will want newline context. */ wants_newline = 0; for (i = 0; i < merged.nelem; ++i) if (PREV_NEWLINE_DEPENDENT(merged.elems[i].constraint)) wants_newline = 1; /* Build the initial state. */ d->salloc = 1; d->sindex = 0; MALLOC(d->states, dfa_state, d->salloc); state_index(d, &merged, wants_newline, 0); free(o_nullable); free(o_nfirst); free(o_firstpos); free(o_nlast); free(o_lastpos); free(nalloc); free(merged.elems);}/* Find, for each character, the transition out of state s of d, and store it in the appropriate slot of trans. We divide the positions of s into groups (positions can appear in more than one group). Each group is labeled with a set of characters that every position in the group matches (taking into account, if necessary, preceding context information of s). For each group, find the union of the its elements' follows. This set is the set of positions of the new state. For each character in the group's label, set the transition on this character to be to a state corresponding to the set's positions, and its associated backward context information, if necessary. If we are building a searching matcher, we include the positions of state 0 in every state. The collection of groups is constructed by building an equivalence-class partition of the positions of s. For each position, find the set of characters C that it matches. Eliminate any characters from C that fail on grounds of backward context. Search through the groups, looking for a group whose label L has nonempty intersection with C. If L - C is nonempty, create a new group labeled L - C and having the same positions as the current group, and set L to the intersection of L and C. Insert the position in this group, set C = C - L, and resume scanning. If after comparing with every group there are characters remaining in C, create a new group labeled with the characters of C and insert this position in that group. */voiddfastate (int s, struct dfa *d, int trans[]){ position_set grps[NOTCHAR]; /* As many as will ever be needed. */ charclass labels[NOTCHAR]; /* Labels corresponding to the groups. */ int ngrps = 0; /* Number of groups actually used. */ position pos; /* Current position being considered. */ charclass matches; /* Set of matching characters. */ int matchesf; /* True if matches is nonempty. */ charclass intersect; /* Intersection with some label set. */ int intersectf; /* True if intersect is nonempty. */ charclass leftovers; /* Stuff in the label that didn't match. */ int leftoversf; /* True if leftovers is nonempty. */ static charclass letters; /* Set of characters considered letters. */ static charclass newline; /* Set of characters that aren't newline. */ position_set follows; /* Union of the follows of some group. */ position_set tmp; /* Temporary space for merging sets. */ int state; /* New state. */ int wants_newline; /* New state wants to know newline context. */ int state_newline; /* New state on a newline transition. */ int wants_letter; /* New state wants to know letter context. */ int state_letter; /* New state on a letter transition. */ static int initialized; /* Flag for static initialization. */#ifdef MBS_SUPPORT int next_isnt_1st_byte = 0; /* Flag If we can't add state0. */#endif int i, j, k; /* Initialize the set of letters, if necessary. */ if (! initialized) { initialized = 1; for (i = 0; i < NOTCHAR; ++i) if (IS_WORD_CONSTITUENT(i)) setbit(i, letters); setbit(eolbyte, newline); } zeroset(matches); for (i = 0; i < d->states[s].elems.nelem; ++i) { pos = d->states[s].elems.elems[i]; if (d->tokens[pos.index] >= 0 && d->tokens[pos.index] < NOTCHAR) setbit(d->tokens[pos.index], matches); else if (d->tokens[pos.index] >= CSET) copyset(d->charclasses[d->tokens[pos.index] - CSET], matches);#ifdef MBS_SUPPORT else if (d->tokens[pos.index] == ANYCHAR || d->tokens[pos.index] == MBCSET) /* MB_CUR_MAX > 1 */ { /* ANYCHAR and MBCSET must match with a single character, so we must put it to d->states[s].mbps, which contains the positions which can match with a single character not a byte. */ if (d->states[s].mbps.nelem == 0) { MALLOC(d->states[s].mbps.elems, position, d->states[s].elems.nelem); } insert(pos, &(d->states[s].mbps)); continue; }#endif /* MBS_SUPPORT */ else continue; /* Some characters may need to be eliminated from matches because they fail in the current context. */ if (pos.constraint != 0xFF) { if (! MATCHES_NEWLINE_CONTEXT(pos.constraint, d->states[s].newline, 1)) clrbit(eolbyte, matches); if (! MATCHES_NEWLINE_CONTEXT(pos.constraint, d->states[s].newline, 0)) for (j = 0; j < CHARCLASS_INTS; ++j) matches[j] &= newline[j]; if (! MATCHES_LETTER_CONTEXT(pos.constraint, d->states[s].letter, 1)) for (j = 0; j < CHARCLASS_INTS; ++j) matches[j] &= ~letters[j]; if (! MATCHES_LETTER_CONTEXT(pos.constraint, d->states[s].letter, 0)) for (j = 0; j < CHARCLASS_INTS; ++j) matches[j] &= letters[j]; /* If there are no characters left, there's no point in going on. */ for (j = 0; j < CHARCLASS_INTS && !matches[j]; ++j) continue; if (j == CHARCLASS_INTS) continue; } for (j = 0; j < ngrps; ++j) { /* If matches contains a single character only, and the current group's label doesn't contain that character, go on to the next group. */ if (d->tokens[pos.index] >= 0 && d->tokens[pos.index] < NOTCHAR && !tstbit(d->tokens[pos.index], labels[j])) continue; /* Check if this group's label has a nonempty intersection with matches. */ intersectf = 0; for (k = 0; k < CHARCLASS_INTS; ++k) (intersect[k] = matches[k] & labels[j][k]) ? (intersectf = 1) : 0; if (! intersectf) continue; /* It does; now find the set differences both ways. */ leftoversf = matchesf = 0; for (k = 0; k < CHARCLASS_INTS; ++k) { /* Even an optimizing compiler can't know this for sure. */ int match = matches[k], label = labels[j][k]; (leftovers[k] = ~match & label) ? (leftoversf = 1) : 0; (matches[k] = match & ~label) ? (matchesf = 1) : 0; } /* If there were leftovers, create a new group labeled with them. */ if (leftoversf) { copyset(leftovers, labels[ngrps]); copyset(intersect, labels[j]); MALLOC(grps[ngrps].elems, position, d->nleaves); copy(&grps[j], &grps[ngrps]); ++ngrps; } /* Put the position in the current group. Note that there is no reason to call insert() here. */ grps[j].elems[grps[j].nelem++] = pos; /* If every character matching the current position has been accounted for, we're done. */ if (! matchesf) break; } /* If we've passed the last group, and there are still characters unaccounted for, then we'll have to create a new group. */ if (j == ngrps) { copyset(matches, labels[ngrps]); zeroset(matches); MALLOC(grps[ngrps].elems, position, d->nleaves); grps[ngrps].nelem = 1; grps[ngrps].elems[0] = pos; ++ngrps; } } MALLOC(follows.elems, position, d->nleaves); MALLOC(tmp.elems, position, d->nleaves); /* If we are a searching matcher, the default transition is to a state containing the positions of state 0, otherwise the default transition is to fail miserably. */ if (d->searchflag) { wants_newline = 0; wants_letter = 0; for (i = 0; i < d->states[0].elems.nelem; ++i) { if (PREV_NEWLINE_DEPENDENT(d->states[0].elems.elems[i].constraint)) wants_newline = 1; if (PREV_LETTER_DEPENDENT(d->states[0].elems.elems[i].constraint)) wants_letter = 1; } copy(&d->states[0].elems, &follows); state = state_index(d, &follows, 0, 0); if (wants_newline) state_newline = state_index(d, &follows, 1, 0); else state_newline = state; if (wants_letter) state_letter = state_index(d, &follows, 0, 1); else state_letter = state; for (i = 0; i < NOTCHAR; ++i) trans[i] = (IS_WORD_CONSTITUENT(i)) ? state_letter : state; trans[eolbyte] = state_newline; } else for (i = 0; i < NOTCHAR; ++i) trans[i] = -1; for (i = 0; i < ngrps; ++i) { follows.nelem = 0; /* Find the union of the follows of the positions of the group. This is a hideously inefficient loop. Fix it someday. */ for (j = 0; j < grps[i].nelem; ++j) for (k = 0; k < d->follows[grps[i].elems[j].index].nelem; ++k) insert(d->follows[grps[i].elems[j].index].elems[k], &follows);#ifdef MBS_SUPPORT if (MB_CUR_MAX > 1) { /* If a token in follows.elems is not 1st byte of a multibyte character, or the states of follows must accept the bytes which are not 1st byte of the multibyte character. Then, if a state of follows encounter a byte, it must not be a 1st byte of a multibyte character nor singlebyte character. We cansel to add state[0].follows to next state, because state[0] must accept 1st-byte For example, we assume <sb a> is a certain singlebyte character, <mb A> is a certain multibyte character, and the codepoint of <sb a> equals the 2nd byte of the codepoint of <mb A>. When state[0] accepts <sb a>, state[i] transit to state[i+1] by accepting accepts 1st byte of <mb A>, and state[i+1] accepts 2nd byte of <mb A>, if state[i+1] encounter the codepoint of <sb a>, it must not be <sb a> but 2nd byte of <mb A>, so we can not add state[0]. */ next_isnt_1st_byte = 0; for (j = 0; j < follows.nelem; ++j) { if (!(d->multibyte_prop[follows.elems[j].index] & 1)) { next_isnt_1st_byte = 1; break; } } }#endif /* If we are building a searching matcher, throw in the positions of state 0 as well. */#ifdef MBS_SUPPORT if (d->searchflag && (MB_CUR_MAX == 1 || !next_isnt_1st_byte))#else if (d->searchflag)#endif for (j = 0; j < d->states[0].elems.nelem; ++j) insert(d->states[0].elems.elems[j], &follows); /* Find out if the new state will want any context information. */ wants_newline = 0; if (tstbit(eolbyte, labels[i])) for (j = 0; j < follows.nelem; ++j) if (PREV_NEWLINE_DEPENDENT(follows.elems[j].constraint)) wants_newline = 1; wants_letter = 0; for (j = 0; j < CHARCLASS_INTS; ++j) if (labels[i][j] & letters[j]) break; if (j < CHARCLASS_INTS) for (j = 0; j < follows.nelem; ++j) if (PREV_LETTER_DEPENDENT(follows.elems[j].constraint)) wants_letter = 1; /* Find the state(s) corresponding to the union of the follows. */ state = state_index(d, &follows, 0, 0); if (wants_newline) state_newline = state_index(d, &follows, 1, 0); else state_newline = state; if (wants_letter) state_letter = state_index(d, &follows, 0, 1); else state_letter = state; /* Set the transitions for each character in the current label. */ for (j = 0; j < CHARCLASS_INTS; ++j) for (k = 0; k < INTBITS; ++k) if (labels[i][j] & 1 << k) { int c = j * INTBITS + k; if (c == eolbyte) trans[c] = state_newline; else if (IS_WORD_CONSTITUENT(c)) trans[c] = state_letter; else if (c < NOTCHAR) trans[c] = state; } } for (i = 0; i < ngrps; ++i) free(grps[i].elems); free(follows.elems); free(tmp.elems);}/* Some routines for manipulating a compiled dfa's transition tables. Each state may or may not have a transition table; if it does, and it is a non-accepting state, then d->trans[state] points to its table. If it is an accepting state then d->fails[state] points to its table. If it has no table at all, then d->trans[state] is NULL. TODO: Improve this comment, get rid of the unnecessary redundancy. */static voidbuild_state (int s, struct dfa *d){ int *trans; /* The new transition table. */ int i; /* Set an upper limit on the number of transition tables that will ever exist at once. 1024 is arbitrary. The idea is that the frequently used transition tables will be quickly rebuilt, whereas the ones that were only needed once or twice will be cleared away. */ if (d->trcount >= 1024) { for (i = 0; i < d->tralloc; ++i) if (d->trans[i]) { free((ptr_t) d->trans[i]); d->trans[i] = NULL; } else if (d->fails[i]) { free((ptr_t) d->fails[i]); d->fails[i] = NULL; } d->trcount = 0; } ++d->trcount; /* Set up the success bits for this state. */ d->success[s] = 0; if (ACCEPTS_IN_CONTEXT(d->states[s].newline, 1, d->states[s].letter, 0, s, *d)) d->success[s] |= 4; if (ACCEPTS_IN_CONTEXT(d->states[s].newline, 0, d->states[s].letter, 1, s, *d)) d->success[s] |= 2; if (ACCEPTS_IN_CONTEXT(d->states[s].newline, 0, d->states[s].letter, 0, s, *d)) d->success[s] |= 1; MALLOC(trans, int, NOTCHAR); dfastate(s, d, trans); /* Now go through the new transition table, and make sure that the trans and fail arrays are allocated large enough to hold a pointer for the largest state mentioned in the table. */ for (i = 0; i < NOTCHAR; ++i) if (trans[i] >= d->tralloc) { int oldalloc = d->tralloc; while (trans[i] >= d->tralloc) d->tralloc *= 2; REALLOC(d->realtrans, int *, d->tralloc + 1); d->trans = d->realtrans + 1; REALLOC(d->fails, int *, d->tralloc); REALLOC(d->success, int, d->tralloc); while (oldalloc < d->tralloc) { d->trans[oldalloc] = NULL; d->fails[oldalloc++] = NULL; } } /* Newline is a sentinel. */ trans[eolbyte] = -1; if (ACCEPTING(s, *d)) d->fails[s] = trans; else d->trans[s] = trans;}static voidbuild_state_zero (struct dfa *d){ d->tralloc = 1; d->trcount = 0; CALLOC(d->realtrans, int *, d->tralloc + 1); d->trans = d->realtrans + 1; CALLOC(d->fails, int *, d->tralloc); MALLOC(d->success, int, d->tralloc); build_state(0, d);}#ifdef MBS_SUPPORT/* Multibyte character handling sub-routins for dfaexec. *//* Initial state may encounter the byte which is not a singlebyte character nor 1st byte of a multibyte character. But it is incorrect for initial state to accept such a byte. For example, in sjis encoding the regular expression like "\\" accepts the codepoint 0x5c, but should not accept the 2nd byte of the codepoint 0x815c. Then Initial state must skip the bytes whi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -