DFA *dfaLess(int i, int j) { if (i == j) return dfaFalse(); else { int var_index[2]; var_index[0] = i, var_index[1] = j; dfaSetup(5, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(1, "00"); dfaStoreException(2, "10"); dfaStoreState(3); /* state 2 */ dfaAllocExceptions(1); dfaStoreException(2, "X0"); dfaStoreState(4); /* state 3 */ dfaAllocExceptions(0); dfaStoreState(3); /* state 4 */ dfaAllocExceptions(0); dfaStoreState(4); return dfaBuild("0---+"); } }
DFA *dfaPlus1(int i, int j, int n) { if (n == 0) return dfaEq1(i, j); else if (i == j) return dfaFalse(); else { DFA *aut; int var_index[2]; int state_no; char *finals; var_index[0] = i; var_index[1] = j; dfaSetup(n+4, 2, var_index); /* boolvar */ dfaAllocExceptions(0); dfaStoreState(1); /* state 1 */ dfaAllocExceptions(2); dfaStoreException(1, "00"); dfaStoreException(3, "01"); dfaStoreState(2); /* state 2 - Reject */ dfaAllocExceptions(0); dfaStoreState(2); /* state 3 .. (n+1) */ for (state_no = 3; state_no <= n+1; state_no++) { dfaAllocExceptions(1); dfaStoreException(state_no+1,"0X"); dfaStoreState(2); } /* state n+2 */ dfaAllocExceptions(1); dfaStoreException(n+3, "1X"); dfaStoreState(2); /* state n+3 - Accept */ dfaAllocExceptions(0); dfaStoreState(n+3); { int k; finals = (char *) mem_alloc((n+4) * (sizeof(char *))); for (k = 0; k < n+4; k++) finals[k] = '-'; finals[0] = '0'; finals[n+3] = '+'; } aut = dfaBuild(finals); mem_free(finals); return aut; } }
// Constructs a DFA for the equation coeffs*variables+constant=0 // in two's complement arithmetic DFA *build_DFA_eq_2sc(int vars, int *coeffs, int constant, int *indices) { int min, max, states, next_index, next_label, result, target, count; long i; unsigned long j, transitions; struct map_ent *map; char *statuces; DFA *equality, *temp; if (preprocess(vars, coeffs, &constant, 0)) return dfaFalse(); //initialization min = 0; max = 0; for (i = 0; i < vars; i++) if (coeffs[i] > 0) max += coeffs[i]; else min += coeffs[i]; if (constant > max) max = constant; else if (constant < min) min = constant; states = 2 * max - 2 * min + 3; //This array maps state labels (carries) to state indices map = (struct map_ent *) malloc(states * sizeof(struct map_ent)) - min; for (i = min; i < max + 1; i++) { map[i].s = 0; map[i].sr = 0; map[i].i = -1; map[i].ir = -1; } map[constant].sr = 1; //the first state to be expanded next_index = 0; //the next available state index next_label = constant; //the next available state label map[constant].i = -1; map[constant].ir = 0; count = 0; transitions = 1 << vars; //number of transitions from each state //Begin building dfaSetup(states, vars, indices); while (next_label < max + 1) { //there is a state to expand (excuding sink) if (map[next_label].i == count) map[next_label].s = 2; else map[next_label].sr = 2; dfaAllocExceptions(transitions / 2); for (j = 0; j < transitions; j++) { result = next_label + count_ones(j, vars, coeffs); if (!(result & 1)) { target = result / 2; if (target == next_label) { if (map[target].s == 0) { map[target].s = 1; next_index++; map[target].i = next_index; } dfaStoreException(map[target].i, bintostr(j, vars)); } else { if (map[target].sr == 0) { map[target].sr = 1; next_index++; map[target].ir = next_index; } dfaStoreException(map[target].ir, bintostr(j, vars)); } } } dfaStoreState(states - 1); count++; for (next_label = min; (next_label <= max) && (map[next_label].i != count) && (map[next_label].ir != count); next_label++) ; //find next state to expand } for (; count < states; count++) { dfaAllocExceptions(0); dfaStoreState(states - 1); } //define accepting and rejecting states statuces = (char *) malloc(states + 1); for (i = 0; i < states; i++) statuces[i] = '-'; for (next_label = min; next_label <= max; next_label++) if (map[next_label].s == 2) statuces[map[next_label].i] = '+'; statuces[states] = '\0'; temp = dfaBuild(statuces); equality = dfaMinimize(temp); dfaFree(temp); return equality; }