From 0ad84d93f9bab067724800c0da8949e8ec94e047 Mon Sep 17 00:00:00 2001 From: John Johansen Date: Tue, 9 Nov 2010 11:48:29 -0800 Subject: [PATCH] Factor out expr tree rotation into its own function --- parser/libapparmor_re/regexp.y | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/parser/libapparmor_re/regexp.y b/parser/libapparmor_re/regexp.y index 3eb834fd6..a23493160 100644 --- a/parser/libapparmor_re/regexp.y +++ b/parser/libapparmor_re/regexp.y @@ -562,16 +562,25 @@ static EpsNode epsnode; * a b c T * */ +static void rotate_node(Node *t, int dir) { + // (a | b) | c -> a | (b | c) + // (ab)c -> a(bc) + Node *left = t->child[dir]; + t->child[dir] = left->child[dir]; + left->child[dir] = left->child[!dir]; + left->child[!dir] = t->child[!dir]; + t->child[!dir] = left; +} + void normalize_tree(Node *t, int dir) { - if (dynamic_cast(t)) + if (dynamic_cast(t)) return; for (;;) { if ((&epsnode == t->child[dir]) && (&epsnode != t->child[!dir]) && - (dynamic_cast(t) || - dynamic_cast(t))) { + dynamic_cast(t)) { // (E | a) -> (a | E) // Ea -> aE Node *c = t->child[dir]; @@ -585,11 +594,7 @@ void normalize_tree(Node *t, int dir) dynamic_cast(t->child[dir]))) { // (a | b) | c -> a | (b | c) // (ab)c -> a(bc) - Node *c = t->child[dir]; - t->child[dir] = c->child[dir]; - c->child[dir] = c->child[!dir]; - c->child[!dir] = t->child[!dir]; - t->child[!dir] = c; + rotate_node(t, dir); } else if (dynamic_cast(t) && dynamic_cast(t->child[dir]) && dynamic_cast(t->child[!dir])) {