Move procedure for running NFA into FSA module

This commit is contained in:
2024-11-02 15:40:58 +00:00
parent 557ab451a8
commit 018aec5339
3 changed files with 85 additions and 80 deletions

View File

@@ -65,3 +65,24 @@ void fsa_add_rule(fsa_t *fsa, int from, int to, int input)
rule->next = to;
++state->count;
}
bool fsa_accepts(const fsa_t *dfa, const char *input, int len)
{
const char *end = input + len;
int current = dfa->initial;
while (input < end) {
bool found = false;
const fsa_rule_t *rules = dfa->states[current].rules;
for (int i = 0; i < dfa->states[current].count; ++i) {
if (rules[i].input == *input) {
current = rules[i].next;
found = true;
break;
}
}
if (!found)
return false;
++input;
}
return dfa->states[current].final;
}

View File

@@ -35,4 +35,6 @@ void fsa_free(const fsa_t *fsa);
int fsa_add_state(fsa_t *fsa);
void fsa_add_rule(fsa_t *fsa, int from, int to, int input);
bool fsa_accepts(const fsa_t *dfa, const char *input, int len);
#endif