From 84f31d2352094b1aa0acf8f53dc0194cbec73c51 Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Tue, 25 Jul 2017 20:40:03 -0500 Subject: simplify by not creating repeat nodes if min=max=1 --- parse-pattern.y | 8 +++++++- regex.c | 22 +++------------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/parse-pattern.y b/parse-pattern.y index e5acd3c..2c0556a 100644 --- a/parse-pattern.y +++ b/parse-pattern.y @@ -62,7 +62,13 @@ input: ; molecule: - repeat sequence { $$ = mkatom(&(struct atom) { .type = ATOM_REPETITION, .u = { .repeat = { .counts = $1, .child = $2 } } }); } + repeat sequence { + if ($1.min == 1 && $1.max == 1) { + $$ = $2; + } else { + $$ = mkatom(&(struct atom) { .type = ATOM_REPETITION, .u = { .repeat = { .counts = $1, .child = $2 } } }); + } + } ; repeat: diff --git a/regex.c b/regex.c index 154a5df..02a58fa 100644 --- a/regex.c +++ b/regex.c @@ -61,21 +61,12 @@ static void print_literal(const char *s) static int get_precedence(const struct atom *a) { - const struct repeat *r; - - switch (a->type) { - case ATOM_LITERAL: + if (a->type == ATOM_LITERAL) { switch (strlen(a->u.literal)) { case 0: return -1; case 1: return precedence[ATOM_EVERYTHING]; default: return precedence[ATOM_SEQUENCE]; } - - case ATOM_REPETITION: - r = &a->u.repeat.counts; - if (r->min == 1 && r->max == 1) - return get_precedence(a->u.repeat.child); - break; } return precedence[a->type]; @@ -93,8 +84,6 @@ static void print_child_atom(const struct atom *child, const struct atom *parent static void print_atom(const struct atom *a) { - const struct repeat *r; - switch (a->type) { case ATOM_ALTERNATION: print_child_atom(a->u.children[0], a); @@ -108,13 +97,8 @@ static void print_atom(const struct atom *a) break; case ATOM_REPETITION: - r = &a->u.repeat.counts; - if (r->min == 1 && r->max == 1) { - print_atom(a->u.repeat.child); - } else { - print_child_atom(a->u.repeat.child, a); - print_repeat(r); - } + print_child_atom(a->u.repeat.child, a); + print_repeat(&a->u.repeat.counts); break; case ATOM_LITERAL: -- cgit v1.2.3