#include <stdio.h> #include"defs.h" #include"fsa.h" #include"hash.h" #include"externals.h" /* the following three definitions are for use in extracting edge-label *andstateincompactlystoredpairs.
*/ #define SHIFT 24 #define MASKR (unsigned)0xffffff #define MASKL (unsigned)0xff000000
short_hash_init(&ht, FALSE, 0, 0, 0);
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
nssi = fsaptr->num_initial;
for (i = 0; i < nssi; i++)
*(++ht_ptre) = fsaptr->initial[i + 1]; /* now perform epsilon closure of initial subset */
ptr = ht_ptrb - 1;
if (eps_trans) while (++ptr <= ht_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr == 0) {
csi = *(tableptr + 1);
if (csi == 0) {
tableptr += 2; continue;
}
if (csi > *ht_ptre) { /* We have a new state for the image subset to be added to the end
*/
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
if (ptr > ht_ptr)
ptr = ht_ptr - 1;
}
}
}
tableptr += 2;
}
}
im = short_hash_locate(&ht, ht_ptre - ht_ptrb + 1); /* Each state in 'det' will be represented as a subset of the set of states *of*fsaptr.Theinitialstatecontainstheinitialstates *of*fsaptr. *Asubsetisanaccept-stateifitcontainsanacceptstateof*fsaptr *Thesubsetswillbestoredasvariable-lengthrecordsinthehash-table, *alwaysinincreasingorder.
*/
if (im != 1) {
fprintf(stderr, "Hash-initialisation problem in nfa_determinize.\n"); return0;
}
if ((tempfile = fopen(tempfilename, "w")) == 0) {
fprintf(stderr, "Error: cannot open file %s\n", tempfilename); return0;
}
if (dense_op)
tmalloc(fsarow, int, ngens) else tmalloc(fsarow, int, 2 * ngens + 1)
cstate = 0;
if (dense_op)
len = ngens; /* The length of the fsarow output. */
nt = 0; /* Number of transitions in det */
while (++cstate <= ht.num_recs) {
if (kbm_print_level >= 3) {
if ((cstate <= 1000 && cstate % 100 == 0) ||
(cstate <= 10000 && cstate % 1000 == 0) ||
(cstate <= 100000 && cstate % 5000 == 0) || cstate % 50000 == 0)
printf(" #cstate = %d; number of states = %d.\n", cstate,
ht.num_recs);
}
cs_ptr = short_hash_rec(&ht, cstate);
cs_ptre = short_hash_rec(&ht, cstate) + short_hash_rec_len(&ht, cstate) - 1;
if (!dense_op)
len = 0;
for (g1 = 1; g1 <= ngens; g1++) { /* Calculate action of generator g1 on state cstate - to get the image, *wesimplyapplyittoeachstateinthesubsetofstatesrepresenting *cstate.
*/
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
ptr = cs_ptr - 1; while (++ptr <= cs_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr == g1) {
csi = *(tableptr + 1);
if (csi == 0) {
tableptr += 2; continue;
}
if (ht_ptrb > ht_ptre || csi > *ht_ptre) { /* We have a new state for the image subset to be added to the end
*/
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
}
}
}
tableptr += 2;
}
} /* now perform epsilon closure of image subset */
ptr = ht_ptrb - 1;
if (eps_trans) while (++ptr <= ht_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr == 0) {
csi = *(tableptr + 1);
if (csi == 0) {
tableptr += 2; continue;
}
if (csi > *ht_ptre) { /* We have a new state for the image subset to be added to the
* end */
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
if (ptr > ht_ptr)
ptr = ht_ptr - 1;
}
}
}
tableptr += 2;
}
}
im = short_hash_locate(&ht, ht_ptre - ht_ptrb + 1);
if (dense_op)
fsarow[g1 - 1] = im; else if (im > 0) {
fsarow[++len] = g1;
fsarow[++len] = im;
}
if (im > 0)
nt++;
}
if (!dense_op)
fsarow[0] = len++;
fwrite((void *)fsarow, sizeof(int), (size_t)len, tempfile);
}
fclose(tempfile);
/* Locate the accept states. A state is an accept state if and only if *thesubsetrepresentingitcontainsanacceptstateof*fsaptr. *Also,ifsubsetsistrue,recordthestatesintheintlistfield *ofdet->states.
*/
tmalloc(det->is_accepting, boolean, ns + 1);
for (i = 1; i <= ns; i++)
det->is_accepting[i] = FALSE;
if (subsets)
tmalloc(det->states->intlist, int *, ns + 1);
ct = 0;
for (cstate = 1; cstate <= ns; cstate++) {
len = short_hash_rec_len(&ht, cstate);
cs_ptr = short_hash_rec(&ht, cstate);
cs_ptre = short_hash_rec(&ht, cstate) + len - 1; /* See if the set contains an accept-state */
ptr = cs_ptr - 1; while (++ptr <= cs_ptre)
if (fsaptr->is_accepting[*ptr]) {
det->is_accepting[cstate] = TRUE;
ct++; break;
}
if (subsets) {
tmalloc(det->states->intlist[cstate], int, len + 1);
det->states->intlist[cstate][0] = len;
n = 0;
ptr = cs_ptr - 1; while (++ptr <= cs_ptre)
det->states->intlist[cstate][++n] = *ptr;
}
}
det->num_accepting = ct;
if (ct == 1 || ct != ns) {
tmalloc(det->accepting, int, ct + 1);
ct = 0;
for (i = 1; i <= ns; i++)
if (det->is_accepting[i])
det->accepting[++ct] = i;
}
tfree(fsaptr->is_accepting);
tfree(det->is_accepting);
short_hash_clear(&ht);
tfree(fsarow);
if (destroy)
fsa_clear(fsaptr);
/* Now read the transition table back in */
tempfile = fopen(tempfilename, "r");
compressed_transitions_read(det, tempfile);
fclose(tempfile);
hash_init(&ht, FALSE, 0, 0, 0);
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
nssi = fsaptr->num_initial;
for (i = 0; i < nssi; i++)
*(++ht_ptre) = fsaptr->initial[i + 1]; /* now perform epsilon closure of initial subset */
ptr = ht_ptrb - 1;
if (eps_trans) while (++ptr <= ht_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr == 0) {
csi = *(tableptr + 1);
if (csi == 0) {
tableptr += 2; continue;
}
if (csi > *ht_ptre) { /* We have a new state for the image subset to be added to the end
*/
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
if (ptr > ht_ptr)
ptr = ht_ptr - 1;
}
}
}
tableptr += 2;
}
}
im = hash_locate(&ht, ht_ptre - ht_ptrb + 1); /* Each state in 'det' will be represented as a subset of the set of states *of*fsaptr.Theinitialstatecontainstheinitialstates *of*fsaptr. *Asubsetisanaccept-stateifitcontainsanacceptstateof*fsaptr *Thesubsetswillbestoredasvariable-lengthrecordsinthehash-table, *alwaysinincreasingorder.
*/
if (im != 1) {
fprintf(stderr, "Hash-initialisation problem in nfa_determinize.\n"); return0;
}
if ((tempfile = fopen(tempfilename, "w")) == 0) {
fprintf(stderr, "Error: cannot open file %s\n", tempfilename); return0;
}
if (dense_op)
tmalloc(fsarow, int, ngens) else tmalloc(fsarow, int, 2 * ngens + 1)
cstate = 0;
if (dense_op)
len = ngens; /* The length of the fsarow output. */
nt = 0; /* Number of transitions in det */
while (++cstate <= ht.num_recs) {
if (kbm_print_level >= 3) {
if ((cstate <= 1000 && cstate % 100 == 0) ||
(cstate <= 10000 && cstate % 1000 == 0) ||
(cstate <= 100000 && cstate % 5000 == 0) || cstate % 50000 == 0)
printf(" #cstate = %d; number of states = %d.\n", cstate,
ht.num_recs);
}
cs_ptr = hash_rec(&ht, cstate);
cs_ptre = hash_rec(&ht, cstate) + hash_rec_len(&ht, cstate) - 1;
if (!dense_op)
len = 0;
for (g1 = 1; g1 <= ngens; g1++) { /* Calculate action of generator g1 on state cstate - to get the image, *wesimplyapplyittoeachstateinthesubsetofstatesrepresenting *cstate.
*/
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
ptr = cs_ptr - 1; while (++ptr <= cs_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr == g1) {
csi = *(tableptr + 1);
if (csi == 0) {
tableptr += 2; continue;
}
if (ht_ptrb > ht_ptre || csi > *ht_ptre) { /* We have a new state for the image subset to be added to the end
*/
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
}
}
}
tableptr += 2;
}
} /* now perform epsilon closure of image subset */
ptr = ht_ptrb - 1;
if (eps_trans) while (++ptr <= ht_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr == 0) {
csi = *(tableptr + 1);
if (csi == 0) {
tableptr += 2; continue;
}
if (csi > *ht_ptre) { /* We have a new state for the image subset to be added to the
* end */
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
if (ptr > ht_ptr)
ptr = ht_ptr - 1;
}
}
}
tableptr += 2;
}
}
im = hash_locate(&ht, ht_ptre - ht_ptrb + 1);
if (dense_op)
fsarow[g1 - 1] = im; else if (im > 0) {
fsarow[++len] = g1;
fsarow[++len] = im;
}
if (im > 0)
nt++;
}
if (!dense_op)
fsarow[0] = len++;
fwrite((void *)fsarow, sizeof(int), (size_t)len, tempfile);
}
fclose(tempfile);
/* Locate the accept states. A state is an accept state if and only if *thesubsetrepresentingitcontainsanacceptstateof*fsaptr. *Also,ifsubsetsistrue,recordthestatesintheintlistfield *ofdet->states.
*/
tmalloc(det->is_accepting, boolean, ns + 1);
for (i = 1; i <= ns; i++)
det->is_accepting[i] = FALSE;
if (subsets)
tmalloc(det->states->intlist, int *, ns + 1);
ct = 0;
for (cstate = 1; cstate <= ns; cstate++) {
len = hash_rec_len(&ht, cstate);
cs_ptr = hash_rec(&ht, cstate);
cs_ptre = hash_rec(&ht, cstate) + len - 1; /* See if the set contains an accept-state */
ptr = cs_ptr - 1; while (++ptr <= cs_ptre)
if (fsaptr->is_accepting[*ptr]) {
det->is_accepting[cstate] = TRUE;
ct++; break;
}
if (subsets) {
tmalloc(det->states->intlist[cstate], int, len + 1);
det->states->intlist[cstate][0] = len;
n = 0;
ptr = cs_ptr - 1; while (++ptr <= cs_ptre)
det->states->intlist[cstate][++n] = *ptr;
}
}
det->num_accepting = ct;
if (ct == 1 || ct != ns) {
tmalloc(det->accepting, int, ct + 1);
ct = 0;
for (i = 1; i <= ns; i++)
if (det->is_accepting[i])
det->accepting[++ct] = i;
}
tfree(fsaptr->is_accepting);
tfree(det->is_accepting);
hash_clear(&ht);
tfree(fsarow);
if (destroy)
fsa_clear(fsaptr);
/* Now read the transition table back in */
tempfile = fopen(tempfilename, "r");
compressed_transitions_read(det, tempfile);
fclose(tempfile);
unlink(tempfilename);
return det;
}
/* Version of nfa_determinize for transition tables stored compactly. *FORTHEMOMENTWEASSUMENOEPSILONTRANSITIONS *Thefsa*fsaptrmustbeanfsawithcompacttable. *Thereturnedfsaacceptsthesamelanguagebutisdeterministic.
*/
fsa *nfa_cdeterminize(fsa *fsaptr, storage_type op_table_type, boolean destroy,
char *tempfilename)
{
if (kbm_print_level >= 3)
printf(" #Calling nfa_determinize.\n");
if (fsaptr->states->size < MAXUSHORT) return nfa_cdeterminize_short(fsaptr, op_table_type, destroy, tempfilename); else return nfa_cdeterminize_int(fsaptr, op_table_type, destroy, tempfilename);
}
short_hash_init(&ht, FALSE, 0, 0, 0);
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
nssi = fsaptr->num_initial;
for (i = 0; i < nssi; i++)
*(++ht_ptre) = fsaptr->initial[i + 1];
im = short_hash_locate(&ht, ht_ptre - ht_ptrb + 1); /* Each state in 'det' will be represented as a subset of the set of states *of*fsaptr.Theinitialstatecontainstheinitialstates *of*fsaptr. *Asubsetisanaccept-stateifitcontainsanacceptstateof*fsaptr *Thesubsetswillbestoredasvariable-lengthrecordsinthehash-table, *alwaysinincreasingorder.
*/
if (im != 1) {
fprintf(stderr, "Hash-initialisation problem in nfa_determinize.\n"); return0;
}
if ((tempfile = fopen(tempfilename, "w")) == 0) {
fprintf(stderr, "Error: cannot open file %s\n", tempfilename); return0;
}
if (dense_op)
tmalloc(fsarow, int, ngens) else tmalloc(fsarow, int, 2 * ngens + 1)
cstate = 0;
if (dense_op)
len = ngens; /* The length of the fsarow output. */
nt = 0; /* Number of transitions in det */
while (++cstate <= ht.num_recs) {
if (kbm_print_level >= 3) {
if ((cstate <= 1000 && cstate % 100 == 0) ||
(cstate <= 10000 && cstate % 1000 == 0) ||
(cstate <= 100000 && cstate % 5000 == 0) || cstate % 50000 == 0)
printf(" #cstate = %d; number of states = %d.\n", cstate,
ht.num_recs);
}
cs_ptr = short_hash_rec(&ht, cstate);
cs_ptre = short_hash_rec(&ht, cstate) + short_hash_rec_len(&ht, cstate) - 1;
if (!dense_op)
len = 0;
for (g1 = 1; g1 <= ngens; g1++) { /* Calculate action of generator g1 on state cstate - to get the image, *wesimplyapplyittoeachstateinthesubsetofstatesrepresenting *cstate.
*/
g1shift = ((unsigned)g1) << SHIFT;
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
ptr = cs_ptr - 1; while (++ptr <= cs_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if ((*tableptr & MASKL) == g1shift) {
csi = *tableptr & MASKR;
if (csi == 0) continue;
if (ht_ptrb > ht_ptre || csi > *ht_ptre) { /* We have a new state for the image subset to be added to the end
*/
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
}
}
}
tableptr++;
}
}
im = short_hash_locate(&ht, ht_ptre - ht_ptrb + 1);
if (dense_op)
fsarow[g1 - 1] = im; else if (im > 0) {
fsarow[++len] = g1;
fsarow[++len] = im;
}
if (im > 0)
nt++;
}
if (!dense_op)
fsarow[0] = len++;
fwrite((void *)fsarow, sizeof(int), (size_t)len, tempfile);
}
fclose(tempfile);
/* Locate the accept states. A state is an accept state if and only if *thesubsetrepresentingitcontainsanacceptstateof*fsaptr.
*/
tmalloc(det->is_accepting, boolean, ns + 1);
for (i = 1; i <= ns; i++)
det->is_accepting[i] = FALSE;
ct = 0;
for (cstate = 1; cstate <= ns; cstate++) {
cs_ptr = short_hash_rec(&ht, cstate);
cs_ptre = short_hash_rec(&ht, cstate) + short_hash_rec_len(&ht, cstate) - 1; /* See if the set contains an accept-state */
ptr = cs_ptr - 1; while (++ptr <= cs_ptre)
if (fsaptr->is_accepting[*ptr]) {
det->is_accepting[cstate] = TRUE;
ct++; break;
}
}
det->num_accepting = ct;
if (ct == 1 || ct != ns) {
tmalloc(det->accepting, int, ct + 1);
ct = 0;
for (i = 1; i <= ns; i++)
if (det->is_accepting[i])
det->accepting[++ct] = i;
}
tfree(fsaptr->is_accepting);
tfree(det->is_accepting);
short_hash_clear(&ht);
tfree(fsarow);
if (destroy)
fsa_clear(fsaptr);
/* Now read the transition table back in */
tempfile = fopen(tempfilename, "r");
compressed_transitions_read(det, tempfile);
fclose(tempfile);
unlink(tempfilename);
return det;
}
static fsa *nfa_cdeterminize_int(fsa *fsaptr, storage_type op_table_type,
boolean destroy, char *tempfilename)
{
int ngens, nssi, ns, *fsarow, nt, cstate, csi, im, i, g1, len = 0, ct; unsigned int **table, *tableptr, *tableptre, g1shift;
int *ht_ptr, *ht_chptr, *ht_ptrb, *ht_ptre, *cs_ptr, *cs_ptre, *ptr;
boolean dense_op;
hash_table ht;
fsa *det;
FILE *tempfile;
if (kbm_print_level >= 3)
printf(" #Calling nfa_determinize_short.\n");
hash_init(&ht, FALSE, 0, 0, 0);
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
nssi = fsaptr->num_initial;
for (i = 0; i < nssi; i++)
*(++ht_ptre) = fsaptr->initial[i + 1];
im = hash_locate(&ht, ht_ptre - ht_ptrb + 1); /* Each state in 'det' will be represented as a subset of the set of states *of*fsaptr.Theinitialstatecontainstheinitialstates *of*fsaptr. *Asubsetisanaccept-stateifitcontainsanacceptstateof*fsaptr *Thesubsetswillbestoredasvariable-lengthrecordsinthehash-table, *alwaysinincreasingorder.
*/
if (im != 1) {
fprintf(stderr, "Hash-initialisation problem in nfa_determinize.\n"); return0;
}
if ((tempfile = fopen(tempfilename, "w")) == 0) {
fprintf(stderr, "Error: cannot open file %s\n", tempfilename); return0;
}
if (dense_op)
tmalloc(fsarow, int, ngens) else tmalloc(fsarow, int, 2 * ngens + 1)
cstate = 0;
if (dense_op)
len = ngens; /* The length of the fsarow output. */
nt = 0; /* Number of transitions in det */
while (++cstate <= ht.num_recs) {
if (kbm_print_level >= 3) {
if ((cstate <= 1000 && cstate % 100 == 0) ||
(cstate <= 10000 && cstate % 1000 == 0) ||
(cstate <= 100000 && cstate % 5000 == 0) || cstate % 50000 == 0)
printf(" #cstate = %d; number of states = %d.\n", cstate,
ht.num_recs);
}
cs_ptr = hash_rec(&ht, cstate);
cs_ptre = hash_rec(&ht, cstate) + hash_rec_len(&ht, cstate) - 1;
if (!dense_op)
len = 0;
for (g1 = 1; g1 <= ngens; g1++) { /* Calculate action of generator g1 on state cstate - to get the image, *wesimplyapplyittoeachstateinthesubsetofstatesrepresenting *cstate.
*/
g1shift = ((unsigned)g1) << SHIFT;
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
ptr = cs_ptr - 1; while (++ptr <= cs_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if ((*tableptr & MASKL) == g1shift) {
csi = *tableptr & MASKR;
if (csi == 0) continue;
if (ht_ptrb > ht_ptre || csi > *ht_ptre) { /* We have a new state for the image subset to be added to the end
*/
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
}
}
}
tableptr++;
}
}
im = hash_locate(&ht, ht_ptre - ht_ptrb + 1);
if (dense_op)
fsarow[g1 - 1] = im; else if (im > 0) {
fsarow[++len] = g1;
fsarow[++len] = im;
}
if (im > 0)
nt++;
}
if (!dense_op)
fsarow[0] = len++;
fwrite((void *)fsarow, sizeof(int), (size_t)len, tempfile);
}
fclose(tempfile);
/* Locate the accept states. A state is an accept state if and only if *thesubsetrepresentingitcontainsanacceptstateof*fsaptr.
*/
tmalloc(det->is_accepting, boolean, ns + 1);
for (i = 1; i <= ns; i++)
det->is_accepting[i] = FALSE;
ct = 0;
for (cstate = 1; cstate <= ns; cstate++) {
cs_ptr = hash_rec(&ht, cstate);
cs_ptre = hash_rec(&ht, cstate) + hash_rec_len(&ht, cstate) - 1; /* See if the set contains an accept-state */
ptr = cs_ptr - 1; while (++ptr <= cs_ptre)
if (fsaptr->is_accepting[*ptr]) {
det->is_accepting[cstate] = TRUE;
ct++; break;
}
}
det->num_accepting = ct;
if (ct == 1 || ct != ns) {
tmalloc(det->accepting, int, ct + 1);
ct = 0;
for (i = 1; i <= ns; i++)
if (det->is_accepting[i])
det->accepting[++ct] = i;
}
tfree(fsaptr->is_accepting);
tfree(det->is_accepting);
hash_clear(&ht);
tfree(fsarow);
if (destroy)
fsa_clear(fsaptr);
/* Now read the transition table back in */
tempfile = fopen(tempfilename, "r");
compressed_transitions_read(det, tempfile);
fclose(tempfile);
unlink(tempfilename);
return det;
}
/* *fsaptr must be a 2-variable (non-deterministic) fsa. *Forthemomentweassumeithasnoepsilontransitions. *Thereturnedfsaisdeterministicandacceptsawordw_1iff(w_1,w_2)is *acceptedby*fsaptr,forsomew_2(withtheusualpaddingconventions). *Infact,nfa_existscallsnfa_exists_intornfa_exists_short, *thelatterusingtheshorthashtablefunctions.
*/
fsa *nfa_exists(fsa *fsaptr, storage_type op_table_type, boolean destroy,
char *tempfilename)
{
if (kbm_print_level >= 3)
printf(" #Calling nfa_exists.\n");
if (fsaptr->states->size < MAXUSHORT) return nfa_exists_short(fsaptr, op_table_type, destroy, tempfilename); else return nfa_exists_int(fsaptr, op_table_type, destroy, tempfilename);
}
short_hash_init(&ht, FALSE, 0, 0, 0);
ht_ptr = ht.current_ptr;
k = fsaptr->num_initial;
for (i = 1; i <= k; i++)
ht_ptr[i - 1] = fsaptr->initial[i];
im = short_hash_locate(&ht, k); /* Each state in 'exists' will be represented as a subset of the set of states *of*fsaptr.Theinitialstateconsistsoftheinitialstates *of*fsaptr.Asubsetisanaccept-stateifitcontainsanacceptstateof **fsaptr,orifonecangettoanaccept-statebyapplyingelements *($,x)where$isthepaddingsymbol. *Thesubsetswillbestoredasvariable-lengthrecordsinthehash-table, *alwaysinincreasingorder.
*/
if (im != 1) {
fprintf(stderr, "Hash-initialisation problem in nfa_exists.\n"); return0;
}
if ((tempfile = fopen(tempfilename, "w")) == 0) {
fprintf(stderr, "Error: cannot open file %s\n", tempfilename); return0;
}
if (dense_op)
tmalloc(fsarow, int, ngens) else tmalloc(fsarow, int, 2 * ngens + 1)
cstate = 0;
if (dense_op)
len = ngens; /* The length of the fsarow output. */
nt = 0; /* Number of transitions in exists */
while (++cstate <= ht.num_recs) {
if (kbm_print_level >= 3) {
if ((cstate <= 1000 && cstate % 100 == 0) ||
(cstate <= 10000 && cstate % 1000 == 0) ||
(cstate <= 100000 && cstate % 5000 == 0) || cstate % 50000 == 0)
printf(" #cstate = %d; number of states = %d.\n", cstate,
ht.num_recs);
}
cs_ptr = short_hash_rec(&ht, cstate);
cs_ptre = short_hash_rec(&ht, cstate) + short_hash_rec_len(&ht, cstate) - 1;
if (!dense_op)
len = 0;
for (g1 = 1; g1 <= ngens; g1++) { /* Calculate action of generator g1 on state cstate - to get the image, *wehavetoapply(g1,g2)toeachelementinthesubsetcorrespondingto *cstate,andthisforeachgeneratorg2ofthebase-alphabet(including *thepaddingsymbol).
*/
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
ptr = cs_ptr - 1;
es = (g1 - 1) * (ngens + 1) + 1;
ef = g1 * (ngens + 1); /* As g2 ranges from 1 to ngens+1 in the pair (g1,g2), for fixed g1, the *correspondingedgenumberinthefsarangesfromestoef.
*/ while (++ptr <= cs_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr >= es && *tableptr <= ef) {
csi = *(tableptr + 1);
if (csi == 0) continue;
if (ht_ptrb > ht_ptre || csi > *ht_ptre) { /* We have a new state for the image subset to be added to the end
*/
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
}
}
}
tableptr += 2;
}
}
im = short_hash_locate(&ht, ht_ptre - ht_ptrb + 1);
if (dense_op)
fsarow[g1 - 1] = im; else if (im > 0) {
fsarow[++len] = g1;
fsarow[++len] = im;
}
if (im > 0)
nt++;
}
if (!dense_op)
fsarow[0] = len++;
fwrite((void *)fsarow, sizeof(int), (size_t)len, tempfile);
}
fclose(tempfile);
/* Locate the accept states. This is slightly cumbersome, since a state *isanacceptstateifeitherthecorrespondingsubsetcontainsan *acceptstateof*fsaptr,orwecangetfromsomesuchstatetoan *acceptstatebyapplyingelements($,x). *Wewillneedtousethearrayexists->is_accepting.
*/
tmalloc(exists->is_accepting, boolean, ns + 1);
for (i = 1; i <= ns; i++)
exists->is_accepting[i] = FALSE;
ct = 0;
for (cstate = ns; cstate >= 1; cstate--) { /* We work backwards through the states, since we wish to add on additional *elementsattheendofthelistinthehash-table-thisdestroys *laterentries,butthatdoesn'tmatter,sincewehavealreadydealt *withthem.
*/
cs_ptr = short_hash_rec(&ht, cstate);
cs_ptre = short_hash_rec(&ht, cstate) + short_hash_rec_len(&ht, cstate) - 1; /* First see if the set itself contains an accept-state */
ptr = cs_ptr - 1; while (++ptr <= cs_ptre)
if (fsaptr->is_accepting[*ptr]) {
exists->is_accepting[cstate] = TRUE;
ct++; break;
}
if (exists->is_accepting[cstate]) continue; /* Next apply generators ($,x) and see if we get anything new, and *ifitisanacceptstate. *Anythingnewisaddedtotheendofthelist-wedon'tneedto *botherabouthavingtheminincreasingorderthistime.
*/
es = ngens * (ngens + 1) + 1;
ef = (ngens + 1) * (ngens + 1) - 1;
ptr = cs_ptr - 1; while (++ptr <= cs_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr >= es && *tableptr <= ef) {
csi = *(tableptr + 1);
if (csi == 0) continue;
if (fsaptr->is_accepting[csi]) {
exists->is_accepting[cstate] = TRUE;
ct++; break;
} /* see if csi is new */
ht_chptr = cs_ptr - 1;
got = FALSE; while (++ht_chptr <= cs_ptre)
if (csi == *ht_chptr) {
got = TRUE; break;
}
if (!got) /* add csi to the end */
*(++cs_ptre) = csi;
}
tableptr += 2;
}
if (exists->is_accepting[cstate]) break;
}
}
exists->num_accepting = ct;
if (ct == 1 || ct != ns) {
tmalloc(exists->accepting, int, ct + 1);
ct = 0;
for (i = 1; i <= ns; i++)
if (exists->is_accepting[i])
exists->accepting[++ct] = i;
}
tfree(fsaptr->is_accepting);
tfree(exists->is_accepting);
short_hash_clear(&ht);
tfree(fsarow);
if (destroy)
fsa_clear(fsaptr);
/* Now read the transition table back in */
tempfile = fopen(tempfilename, "r");
compressed_transitions_read(exists, tempfile);
fclose(tempfile);
hash_init(&ht, FALSE, 0, 0, 0);
ht_ptr = ht.current_ptr;
k = fsaptr->num_initial;
for (i = 1; i <= k; i++)
ht_ptr[i - 1] = fsaptr->initial[i];
im = hash_locate(&ht, k); /* Each state in 'exists' will be represented as a subset of the set of states *of*fsaptr.Theinitialstateconsistsoftheinitialstates *of*fsaptr.Asubsetisanaccept-stateifitcontainsanacceptstateof **fsaptr,orifonecangettoanaccept-statebyapplyingelements *($,x)where$isthepaddingsymbol. *Thesubsetswillbestoredasvariable-lengthrecordsinthehash-table, *alwaysinincreasingorder.
*/
if (im != 1) {
fprintf(stderr, "Hash-initialisation problem in nfa_exists.\n"); return0;
}
if ((tempfile = fopen(tempfilename, "w")) == 0) {
fprintf(stderr, "Error: cannot open file %s\n", tempfilename); return0;
}
if (dense_op)
tmalloc(fsarow, int, ngens) else tmalloc(fsarow, int, 2 * ngens + 1)
cstate = 0;
if (dense_op)
len = ngens; /* The length of the fsarow output. */
nt = 0; /* Number of transitions in exists */
while (++cstate <= ht.num_recs) {
if (kbm_print_level >= 3) {
if ((cstate <= 1000 && cstate % 100 == 0) ||
(cstate <= 10000 && cstate % 1000 == 0) ||
(cstate <= 100000 && cstate % 5000 == 0) || cstate % 50000 == 0)
printf(" #cstate = %d; number of states = %d.\n", cstate,
ht.num_recs);
}
cs_ptr = hash_rec(&ht, cstate);
cs_ptre = hash_rec(&ht, cstate) + hash_rec_len(&ht, cstate) - 1;
if (!dense_op)
len = 0;
for (g1 = 1; g1 <= ngens; g1++) { /* Calculate action of generator g1 on state cstate - to get the image, *wehavetoapply(g1,g2)toeachelementinthesubsetcorrespondingto *cstate,andthisforeachgeneratorg2ofthebase-alphabet(including *thepaddingsymbol).
*/
ht_ptrb = ht.current_ptr;
ht_ptre = ht_ptrb - 1;
ptr = cs_ptr - 1;
es = (g1 - 1) * (ngens + 1) + 1;
ef = g1 * (ngens + 1); /* As g2 ranges from 1 to ngens+1 in the pair (g1,g2), for fixed g1, the *correspondingedgenumberinthefsarangesfromestoef.
*/ while (++ptr <= cs_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr >= es && *tableptr <= ef) {
csi = *(tableptr + 1);
if (csi == 0) continue;
if (ht_ptrb > ht_ptre || csi > *ht_ptre) { /* We have a new state for the image subset to be added to the end
*/
*(++ht_ptre) = csi;
} else {
ht_chptr = ht_ptrb; while (*ht_chptr < csi)
ht_chptr++;
if (csi < *ht_chptr) { /* we have a new state for the image subset to be added in the
* middle */
ht_ptr = ++ht_ptre; while (ht_ptr > ht_chptr) {
*ht_ptr = *(ht_ptr - 1);
ht_ptr--;
}
*ht_ptr = csi;
}
}
}
tableptr += 2;
}
}
im = hash_locate(&ht, ht_ptre - ht_ptrb + 1);
if (dense_op)
fsarow[g1 - 1] = im; else if (im > 0) {
fsarow[++len] = g1;
fsarow[++len] = im;
}
if (im > 0)
nt++;
}
if (!dense_op)
fsarow[0] = len++;
fwrite((void *)fsarow, sizeof(int), (size_t)len, tempfile);
}
fclose(tempfile);
/* Locate the accept states. This is slightly cumbersome, since a state *isanacceptstateifeitherthecorrespondingsubsetcontainsan *acceptstateof*fsaptr,orwecangetfromsomesuchstatetoan *acceptstatebyapplyingelements($,x). *Wewillneedtousethearrayexists->is_accepting.
*/
tmalloc(exists->is_accepting, boolean, ns + 1);
for (i = 1; i <= ns; i++)
exists->is_accepting[i] = FALSE;
ct = 0;
for (cstate = ns; cstate >= 1; cstate--) { /* We work backwards through the states, since we wish to add on additional *elementsattheendofthelistinthehash-table-thisdestroys *laterentries,butthatdoesn'tmatter,sincewehavealreadydealt *withthem.
*/
cs_ptr = hash_rec(&ht, cstate);
cs_ptre = hash_rec(&ht, cstate) + hash_rec_len(&ht, cstate) - 1; /* First see if the set itself contains an accept-state */
ptr = cs_ptr - 1; while (++ptr <= cs_ptre)
if (fsaptr->is_accepting[*ptr]) {
exists->is_accepting[cstate] = TRUE;
ct++; break;
}
if (exists->is_accepting[cstate]) continue; /* Next apply generators ($,x) and see if we get anything new, and *ifitisanacceptstate. *Anythingnewisaddedtotheendofthelist-wedon'tneedto *botherabouthavingtheminincreasingorderthistime.
*/
es = ngens * (ngens + 1) + 1;
ef = (ngens + 1) * (ngens + 1) - 1;
ptr = cs_ptr - 1; while (++ptr <= cs_ptre) {
tableptr = table[*ptr];
tableptre = table[*ptr + 1]; while (tableptr < tableptre) {
if (*tableptr >= es && *tableptr <= ef) {
csi = *(tableptr + 1);
if (csi == 0) continue;
if (fsaptr->is_accepting[csi]) {
exists->is_accepting[cstate] = TRUE;
ct++; break;
} /* see if csi is new */
ht_chptr = cs_ptr - 1;
got = FALSE; while (++ht_chptr <= cs_ptre)
if (csi == *ht_chptr) {
got = TRUE; break;
}
if (!got) /* add csi to the end */
*(++cs_ptre) = csi;
}
tableptr += 2;
}
if (exists->is_accepting[cstate]) break;
}
}
exists->num_accepting = ct;
if (ct == 1 || ct != ns) {
tmalloc(exists->accepting, int, ct + 1);
ct = 0;
for (i = 1; i <= ns; i++)
if (exists->is_accepting[i])
exists->accepting[++ct] = i;
}
tfree(fsaptr->is_accepting);
tfree(exists->is_accepting);
hash_clear(&ht);
tfree(fsarow);
if (destroy)
fsa_clear(fsaptr);
/* Now read the transition table back in */
tempfile = fopen(tempfilename, "r");
compressed_transitions_read(exists, tempfile);
fclose(tempfile);
unlink(tempfilename);
return exists;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.21 Sekunden
(vorverarbeitet am 2026-09-02)
¤
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.