diff options
Diffstat (limited to 'regex.c')
-rw-r--r-- | regex.c | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,35 @@ +#include <stdlib.h> + +#include "ast.h" + +static void repeat(struct atom *a) +{ + struct atom *child = a->u.repeat.child; + simplify(child); + + const struct repeat *r = &a->u.repeat.counts; + if (r->min == 1 && r->max == 1) { + *a = *child; + free(child); + } +} + +static void children(struct atom *a) +{ + simplify(a->u.children[0]); + simplify(a->u.children[1]); +} + +void simplify(struct atom *a) +{ + switch (a->type) { + case ATOM_REPETITION: + repeat(a); + break; + + case ATOM_ALTERNATION: + case ATOM_SEQUENCE: + children(a); + } +} + |