Move procedure for running NFA into FSA module
This commit is contained in:
21
lib/fsa.c
21
lib/fsa.c
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user