]> granicus.if.org Git - re2c/commitdiff
Removed redundant computation.
authorUlya Trofimovich <skvadrik@gmail.com>
Sat, 26 Nov 2016 18:09:06 +0000 (18:09 +0000)
committerUlya Trofimovich <skvadrik@gmail.com>
Sat, 26 Nov 2016 18:15:47 +0000 (18:15 +0000)
Tags used in the given sub-regexp are already tracked by tag index;
boolean array is redundant.

re2c/src/ir/nfa/regexps2nfa.cc

index 237192abf704dbcbad3ad6f858d26088e667eebd..48d5d4eb1b194f4550dc74eae21ffaa2af557838 100644 (file)
@@ -6,8 +6,10 @@ namespace re2c {
 // we have AST and which is immutable (it may be shared by different
 // regexps); after it, we have NFA where join points of alternatives
 // are lost.
+// WARNING: regexp structure should be traversed in the same way as
+// when adding tags: we assume that tag index increases sequentially.
 static nfa_state_t *regexp2nfa(nfa_t &nfa, size_t nrule,
-       bool *tbit, size_t &tidx, const RegExp *re, nfa_state_t *t)
+       size_t &tidx, const RegExp *re, nfa_state_t *t)
 {
        nfa_state_t *s = NULL;
        switch (re->type) {
@@ -23,22 +25,18 @@ static nfa_state_t *regexp2nfa(nfa_t &nfa, size_t nrule,
                        size_t i = tidx;
 
                        t0 = &nfa.states[nfa.size++];
-                       s1 = regexp2nfa(nfa, nrule, tbit, tidx, re->alt.re1, t0);
+                       s1 = regexp2nfa(nfa, nrule, tidx, re->alt.re1, t0);
                        for (t2 = t; i < tidx; ++i) {
-                               if (tbit[i]) {
-                                       q = &nfa.states[nfa.size++];
-                                       q->make_tag(nrule, t2, i, true);
-                                       t2 = q;
-                               }
+                               q = &nfa.states[nfa.size++];
+                               q->make_tag(nrule, t2, i, true);
+                               t2 = q;
                        }
 
-                       s2 = regexp2nfa(nfa, nrule, tbit, tidx, re->alt.re2, t2);
+                       s2 = regexp2nfa(nfa, nrule, tidx, re->alt.re2, t2);
                        for (t1 = t; i < tidx; ++i) {
-                               if (tbit[i]) {
-                                       q = &nfa.states[nfa.size++];
-                                       q->make_tag(nrule, t1, i, true);
-                                       t1 = q;
-                               }
+                               q = &nfa.states[nfa.size++];
+                               q->make_tag(nrule, t1, i, true);
+                               t1 = q;
                        }
                        t0->make_nil(nrule, t1);
 
@@ -47,13 +45,13 @@ static nfa_state_t *regexp2nfa(nfa_t &nfa, size_t nrule,
                        break;
                }
                case RegExp::CAT:
-                       s = regexp2nfa(nfa, nrule, tbit, tidx, re->cat.re2, t);
-                       s = regexp2nfa(nfa, nrule, tbit, tidx, re->cat.re1, s);
+                       s = regexp2nfa(nfa, nrule, tidx, re->cat.re2, t);
+                       s = regexp2nfa(nfa, nrule, tidx, re->cat.re1, s);
                        break;
                case RegExp::ITER: {
                        // see note [Kleene star is expressed in terms of plus]
                        nfa_state_t *q = &nfa.states[nfa.size++];
-                       s = regexp2nfa(nfa, nrule, tbit, tidx, re->iter, q);
+                       s = regexp2nfa(nfa, nrule, tidx, re->iter, q);
                        q->make_alt(nrule, t, s);
                        break;
                }
@@ -64,18 +62,18 @@ static nfa_state_t *regexp2nfa(nfa_t &nfa, size_t nrule,
                        } else {
                                s = t;
                        }
-                       tbit[tidx++] = true;
+                       ++tidx;
                        break;
        }
        return s;
 }
 
 static nfa_state_t *regexp2nfa_rule(nfa_t &nfa, size_t nrule,
-       bool *tbit, size_t &tidx, const RegExpRule *rule)
+       size_t &tidx, const RegExpRule *rule)
 {
        nfa_state_t *s = &nfa.states[nfa.size++];
        s->make_fin(nrule);
-       return regexp2nfa(nfa, nrule, tbit, tidx, rule->re, s);
+       return regexp2nfa(nfa, nrule, tidx, rule->re, s);
 }
 
 void regexps2nfa(const std::vector<const RegExpRule*> &regexps, nfa_t &nfa)
@@ -86,18 +84,14 @@ void regexps2nfa(const std::vector<const RegExpRule*> &regexps, nfa_t &nfa)
                return;
        }
 
-       bool *tbit = new bool[nfa.tags->size()]();
        size_t tidx = 0;
-
-       nfa_state_t *s = regexp2nfa_rule(nfa, 0, tbit, tidx, regexps[0]);
+       nfa_state_t *s = regexp2nfa_rule(nfa, 0, tidx, regexps[0]);
        for (size_t i = 1; i < nregexps; ++i) {
                nfa_state_t *t = &nfa.states[nfa.size++];
-               t->make_alt(i, s, regexp2nfa_rule(nfa, i, tbit, tidx, regexps[i]));
+               t->make_alt(i, s, regexp2nfa_rule(nfa, i, tidx, regexps[i]));
                s = t;
        }
        nfa.root = s;
-
-       delete[] tbit;
 }
 
 } // namespace re2c