/* Copyright (c) 1990 The Regents of the University of California. */ /* All rights reserved. */
/* This code is derived from software contributed to Berkeley by */ /* Vern Paxson. */
/* The United States Government has rights in this work pursuant */ /* to contract no. DE-AC03-76SF00098 between the United States */ /* Department of Energy and the University of California. */
/* Redistribution and use in source and binary forms, with or without */ /* modification, are permitted provided that the following conditions */ /* are met: */
/* 1. Redistributions of source code must retain the above copyright */ /* notice, this list of conditions and the following disclaimer. */ /* 2. Redistributions in binary form must reproduce the above copyright */ /* notice, this list of conditions and the following disclaimer in the */ /* documentation and/or other materials provided with the distribution. */
/* Neither the name of the University nor the names of its contributors */ /* may be used to endorse or promote products derived from this software */ /* without specific prior written permission. */
/* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */ /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ /* PURPOSE. */
#include"flexdef.h" #include"tables.h"
/* declare functions that have forward references */
void dump_associated_rules PROTO ((FILE *, int)); void dump_transitions PROTO ((FILE *, int[])); void sympartition PROTO ((int[], int, int[], int[])); int symfollowset PROTO ((int[], int, int, int[]));
/* check_for_backing_up - check a DFA state for backing up * *synopsis *voidcheck_for_backing_up(intds,intstate[numecs]); * *dsisthenumberofthestatetocheckandstate[]isitsout-transitions, *indexedbyequivalenceclass.
*/
void check_for_backing_up (ds, state) int ds; int state[];
{ if ((reject && !dfaacc[ds].dfaacc_set) || (!reject && !dfaacc[ds].dfaacc_state)) { /* state is non-accepting */
++num_backing_up;
if (backing_up_report) {
fprintf (backing_up_file,
_("State #%d is non-accepting -\n"), ds);
/* identify the state */
dump_associated_rules (backing_up_file, ds);
/* Now identify it further using the out- and *jam-transitions.
*/
dump_transitions (backing_up_file, state);
putc ('\n', backing_up_file);
}
}
}
/* check_trailing_context - check to see if NFA state set constitutes *"dangerous"trailingcontext * *synopsis *voidcheck_trailing_context(intnfa_states[num_states+1],intnum_states, *intaccset[nacc+1],intnacc); * *NOTES *Trailingcontextis"dangerous"ifboththeheadandthetrailing *partareofvariablesize\and/there'saDFAstatewhichcontains *bothanacceptingstatefortheheadpartoftheruleandNFAstates *whichoccurafterthebeginningofthetrailingcontext. * *Whensucharuleismatched,it'simpossibletotellifhavingbeen *intheDFAstateindicatesthebeginningofthetrailingcontextor *further-alongscanningofthepattern.Inthesecases,awarning *messageisissued. * *nfa_states[1..num_states]isthelistofNFAstatesintheDFA. *accset[1..nacc]isthelistofacceptingnumbersfortheDFAstate.
*/
void check_trailing_context (nfa_states, num_states, accset, nacc) int *nfa_states, num_states; int *accset; int nacc;
{ int i, j;
for (i = 1; i <= num_states; ++i) { int ns = nfa_states[i]; int type = state_type[ns]; int ar = assoc_rule[ns];
if (type == STATE_NORMAL || rule_type[ar] != RULE_VARIABLE) { /* do nothing */
}
/* dump_associated_rules - list the rules associated with a DFA state * *GoesthroughthesetofNFAstatesassociatedwiththeDFAand *extractsthefirstMAX_ASSOC_RULESuniquerules,sortsthem, *andwritesareporttothegivenfile.
*/
void dump_associated_rules (file, ds)
FILE *file; int ds;
{ int i, j; int num_associated_rules = 0; int rule_set[MAX_ASSOC_RULES + 1]; int *dset = dss[ds]; int size = dfasiz[ds];
for (i = 1; i <= size; ++i) { int rule_num = rule_linenum[assoc_rule[dset[i]]];
for (j = 1; j <= num_associated_rules; ++j) if (rule_num == rule_set[j]) break;
if (j > num_associated_rules) { /* new rule */ if (num_associated_rules < MAX_ASSOC_RULES)
rule_set[++num_associated_rules] =
rule_num;
}
}
fprintf (file, _(" associated rule line numbers:"));
for (i = 1; i <= num_associated_rules; ++i) { if (i % 8 == 1)
putc ('\n', file);
fprintf (file, "\t%d", rule_set[i]);
}
putc ('\n', file);
}
/* dump_transitions - list the transitions associated with a DFA state * *synopsis *dump_transitions(FILE*file,intstate[numecs]); * *Goesthroughthesetofout-transitionsandliststheminhuman-readable *form(i.e.,notasequivalenceclasses);alsolistsjamtransitions *(i.e.,allthosewhicharenotout-transitions,plusEOF).Thedump *isdonetothegivenfile.
*/
void dump_transitions (file, state)
FILE *file; int state[];
{ int i, ec; int out_char_set[CSIZE];
for (i = 0; i < csize; ++i) {
ec = ABS (ecgroup[i]);
out_char_set[i] = state[ec];
}
fprintf (file, _(" out-transitions: "));
list_character_set (file, out_char_set);
/* now invert the members of the set to get the jam transitions */ for (i = 0; i < csize; ++i)
out_char_set[i] = !out_char_set[i];
fprintf (file, _("\n jam-transitions: EOF "));
list_character_set (file, out_char_set);
putc ('\n', file);
}
/* epsclosure - construct the epsilon closure of a set of ndfa states * *synopsis *int*epsclosure(intt[num_states],int*numstates_addr, *intaccset[num_rules+1],int*nacc_addr, *int*hashval_addr); * *NOTES *Theepsilonclosureisthesetofallstatesreachablebyanarbitrary *numberofepsilontransitions,whichthemselvesdonothaveepsilon *transitionsgoingout,unionedwiththesetofstateswhichhavenon-null *acceptingnumbers.tisanarrayofsizenumstatesofnfastatenumbers. *Uponreturn,tholdstheepsilonclosureand*numstates_addrisupdated. *accsetholdsalistoftheacceptingnumbers,andthesizeofaccsetis *givenby*nacc_addr.tmaybesubjectedtoreallocationifitisnot *largeenoughtoholdtheepsilonclosure. * *hashvalisthehashvalueforthedfacorrespondingtothestateset.
*/
int *epsclosure (t, ns_addr, accset, nacc_addr, hv_addr) int *t, *ns_addr, accset[], *nacc_addr, *hv_addr;
{ int stkpos, ns, tsp; int numstates = *ns_addr, nacc, hashval, transsym, nfaccnum; int stkend, nstate; staticint did_stk_init = false, *stk;
/* The state could be marked if we've already pushed it onto *thestack.
*/ if (!IS_MARKED (ns)) {
PUT_ON_STACK (ns);
CHECK_ACCEPT (ns);
hashval += ns;
}
}
if (nultrans)
nultrans =
reallocate_integer_array (nultrans,
current_max_dfas);
}
/* ntod - convert an ndfa to a dfa * *Createsthedfacorrespondingtothendfawe'veconstructed.The *dfastartsoutinstate#1.
*/
void ntod ()
{ int *accset, ds, nacc, newds; int sym, hashval, numstates, dsize; int num_full_table_rows=0; /* used only for -f */ int *nset, *dset; int targptr, totaltrans, i, comstate, comfreq, targ; int symlist[CSIZE + 1]; int num_start_states; int todo_head, todo_next;
/* Note that the following are indexed by *equivalence classes* *andnotbycharacters.Sinceequivalenceclassesareindexed *beginningwith1,evenifthescanneracceptsNUL's,this *meansthat(sinceeverycharacterispotentiallyinitsown *equivalenceclass)thesearraysmusthaveroomforindices *from1toCSIZE,sotheirsizemustbeCSIZE+1.
*/ int duplist[CSIZE + 1], state[CSIZE + 1]; int targfreq[CSIZE + 1] = {0}, targstate[CSIZE + 1];
/* accset needs to be large enough to hold all of the rules present *intheinput,*plus*theirYY_TRAILING_HEAD_MASKvariants.
*/
accset = allocate_integer_array ((num_rules + 1) * 2);
nset = allocate_integer_array (current_max_dfa_size);
/* The "todo" queue is represented by the head, which is the DFA *statecurrentlybeingprocessed,andthe"next",whichisthe *nextDFAstatenumberavailable(notinuse).Wedependonthe *factthatsnstods()returnsDFA's\inincreasingorder/,andthus *needonlyknowtheboundsofthedfastobeprocessed.
*/
todo_head = todo_next = 0;
for (i = 0; i <= csize; ++i) {
duplist[i] = NIL;
symlist[i] = false;
}
for (i = 0; i <= num_rules; ++i)
accset[i] = NIL;
if (trace) {
dumpnfa (scset[1]);
fputs (_("\n\nDFA Dump:\n\n"), stderr);
}
inittbl ();
/* Check to see whether we should build a separate table for *transitionsonNULcharacters.Wedon'tdothisforfull-speed *(-F)scanners,sinceforthemwedon'thaveasimplestate *numberlyingaroundwithwhichtoindexthetable.Wealso *don'tbotherdoingitforscannersunless(1)NULisinitsown *equivalenceclass(indicatedbyapositivevalueof *ecgroup[NUL]),(2)NUL'sequivalenceclassisthelast *equivalenceclass,and(3)thenumberofequivalenceclassesis *thesameasthenumberofcharacters.Thislattercasecomes *aboutwhenuseecsisfalseorwhenit'struebuteverycharacter *stillmanagestolandinitsownclass(unlikely,butit's *cheaptocheckfor).Ifallthesethingsaretruethenthe *charactercodeneededtorepresentNUL'sequivalenceclassfor *indexingthetablesisgoingtotakeonemorebitthanthe *numberofcharacters,andthereforewewon'tbeassuredof *beingabletofititintoaYY_CHARvariable.Thisrulesout *storingthetransitionsinacompressedtable,sincethecode *forinterpretingthemusesaYY_CHARvariable(perhapsit *shouldjustuseaninteger,though;thisisworthpondering... *###). * *Finally,forfulltables,wewantthenumberofentriesinthe *tabletobeapoweroftwosothearrayreferencesgofast(it *willjusttakeashifttocomputethemajorindex).If *encodingNUL'stransitionsinthetablewillspoilthis,we *giveititsowntable(notethatthiswillbethecaseifwe're *notusingequivalenceclasses).
*/
/* Note that the test for ecgroup[0] == numecs below accomplishes *both(1)and(2)above
*/ if (!fullspd && ecgroup[0] == numecs) { /* NUL is alone in its equivalence class, which is the *lastone.
*/ int use_NUL_table = (numecs == csize);
if (fulltbl && !use_NUL_table) { /* We still may want to use the table if numecs *isapowerof2.
*/ int power_of_two;
elseif (fulltbl) { if (nultrans) /* We won't be including NUL's transitions in the *table,sobuilditforentriesfrom0..numecs-1.
*/
num_full_table_rows = numecs;
else /* Take into account the fact that we'll be including *theNULentriesinthetransitiontable.Buildit *from0..numecs.
*/
num_full_table_rows = numecs + 1;
/* Unless -Ca, declare it "short" because it's a real *long-shotthatthatwon'tbelargeenough.
*/ if (gentables)
out_str_dec
("static yyconst %s yy_nxt[][%d] =\n {\n",
long_align ? "flex_int32_t" : "flex_int16_t",
num_full_table_rows); else {
out_dec ("#undef YY_NXT_LOLEN\n#define YY_NXT_LOLEN (%d)\n", num_full_table_rows);
out_str ("static yyconst %s *yy_nxt =0;\n",
long_align ? "flex_int32_t" : "flex_int16_t");
}
if (gentables)
outn (" {");
/* Generate 0 entries for state #0. */ for (i = 0; i < num_full_table_rows; ++i) {
mk2data (0);
yynxt_data[yynxt_curr++] = 0;
}
dataflush (); if (gentables)
outn (" },\n");
}
/* Create the first states. */
num_start_states = lastsc * 2;
for (i = 1; i <= num_start_states; ++i) {
numstates = 1;
/* For each start condition, make one state for the case when *we'reatthebeginningoftheline(the'^'operator)and *oneforthecasewhenwe'renot.
*/ if (i % 2 == 1)
nset[numstates] = scset[(i / 2) + 1]; else
nset[numstates] =
mkbranch (scbol[i / 2], scset[i / 2]);
/* Each time we hit here, it's another td_hilen, so we realloc. */
yynxt_tbl->td_hilen++;
yynxt_tbl->td_data = yynxt_data =
(flex_int32_t *) realloc (yynxt_data,
yynxt_tbl->td_hilen *
yynxt_tbl->td_lolen * sizeof (flex_int32_t));
for (i = 1; i < num_full_table_rows; ++i) { /* Jams are marked by negative of state *number.
*/
mk2data (state[i] ? state[i] : -ds);
yynxt_data[yynxt_curr++] =
state[i] ? state[i] : -ds;
}
elseif (ds == end_of_buffer_state) /* Special case this state to make sure it does what *it'ssupposedto,i.e.,jamonend-of-buffer.
*/
stack1 (ds, 0, 0, JAMSTATE);
else { /* normal, compressed state */
/* Determine which destination state is the most *common,andhowmanytransitionstoitthereare.
*/
comfreq = 0;
comstate = 0;
for (i = 1; i <= targptr; ++i) if (targfreq[i] > comfreq) {
comfreq = targfreq[i];
comstate = targstate[i];
}
/* Create tables for all the states with only one *out-transition.
*/ while (onesp > 0) {
mk1tbl (onestate[onesp], onesym[onesp],
onenext[onesp], onedef[onesp]);
--onesp;
}
/* snstods - converts a set of ndfa states into a dfa state * *synopsis *is_new_state=snstods(intsns[numstates],intnumstates, *intaccset[num_rules+1],intnacc, *inthashval,int*newds_addr); * *Onreturn,thedfastatenumberisinnewds.
*/
int snstods (sns, numstates, accset, nacc, hashval, newds_addr) int sns[], numstates, accset[], nacc, hashval, *newds_addr;
{ int didsort = 0; int i, j; int newds, *oldsns;
for (i = 1; i <= lastdfa; ++i) if (hashval == dhash[i]) { if (numstates == dfasiz[i]) {
oldsns = dss[i];
if (!didsort) { /* We sort the states in sns so we *cancompareittooldsnsquickly.
*/
qsort (&sns [1], numstates, sizeof (sns [1]), intcmp);
didsort = 1;
}
for (j = 1; j <= numstates; ++j) if (sns[j] != oldsns[j]) break;
if (nacc == 0) { if (reject)
dfaacc[newds].dfaacc_set = (int *) 0; else
dfaacc[newds].dfaacc_state = 0;
accsiz[newds] = 0;
}
elseif (reject) { /* We sort the accepting set in increasing order so the *disambiguatingrulethatthefirstrulelistedisconsidered *matchintheeventoftieswillwork.
*/
/* Save the accepting set for later */ for (i = 1; i <= nacc; ++i) {
dfaacc[newds].dfaacc_set[i] = accset[i];
if (accset[i] <= num_rules) /* Who knows, perhaps a REJECT can yield *thisrule.
*/
rule_useful[accset[i]] = true;
}
accsiz[newds] = nacc;
}
else { /* Find lowest numbered rule so the disambiguating rule *willwork.
*/
j = num_rules + 1;
for (i = 1; i <= nacc; ++i) if (accset[i] < j)
j = accset[i];
dfaacc[newds].dfaacc_state = j;
if (j <= num_rules)
rule_useful[j] = true;
}
*newds_addr = newds;
return1;
}
/* symfollowset - follow the symbol transitions one step * *synopsis *numstates=symfollowset(intds[current_max_dfa_size],intdsize, *inttranssym,intnset[current_max_dfa_size]);
*/
int symfollowset (ds, dsize, transsym, nset) int ds[], dsize, transsym, nset[];
{ int ns, tsp, sym, i, j, lenccl, ch, numstates, ccllist;
numstates = 0;
for (i = 1; i <= dsize; ++i) { /* for each nfa state ns in the state set of ds */
ns = ds[i];
sym = transchar[ns];
tsp = trans1[ns];
if (sym < 0) { /* it's a character class */
sym = -sym;
ccllist = cclmap[sym];
lenccl = ccllen[sym];
if (cclng[sym]) { for (j = 0; j < lenccl; ++j) { /* Loop through negated character *class.
*/
ch = ccltbl[ccllist + j];
if (ch == 0)
ch = NUL_ec;
if (ch > transsym) /* Transsym isn't in negated *ccl.
*/ break;
/* sympartition - partition characters with same out-transitions * *synopsis *sympartition(intds[current_max_dfa_size],intnumstates, *intsymlist[numecs],intduplist[numecs]);
*/
void sympartition (ds, numstates, symlist, duplist) int ds[], numstates; int symlist[], duplist[];
{ int tch, i, j, k, ns, dupfwd[CSIZE + 1], lenccl, cclp, ich;
/* Partitioning is done by creating equivalence classes for those *characterswhichhaveout-transitionsfromthegivenstate.Thus *wearereallycreatingequivalenceclassesofequivalenceclasses.
*/
for (i = 1; i <= numecs; ++i) { /* initialize equivalence class list */
duplist[i] = i - 1;
dupfwd[i] = i + 1;
}
duplist[1] = NIL;
dupfwd[numecs] = NIL;
for (i = 1; i <= numstates; ++i) {
ns = ds[i];
tch = transchar[ns];
if (tch != SYM_EPSILON) { if (tch < -lastccl || tch >= csize) {
flexfatal (_
("bad transition character detected in sympartition()"));
}
if (tch >= 0) { /* character transition */ int ec = ecgroup[tch];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.