From c1270ecf871b35b6503dba5778d09814895548b0 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Sun, 1 May 2016 13:29:47 +0100 Subject: [PATCH] Keep rule number in each NFA state (not only in final states). This is currently not necessary, but we'll need it to distinguish between different rules when comparing tags: if rules are different, we don't care about tag difference; otherwise tags must be equal. --- re2c/src/ir/dfa/determinization.cc | 2 +- re2c/src/ir/nfa/nfa.cc | 23 ++++++++++++----------- re2c/src/ir/nfa/nfa.h | 18 +++++++++--------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/re2c/src/ir/dfa/determinization.cc b/re2c/src/ir/dfa/determinization.cc index a8c242fe..6d1f8f1f 100644 --- a/re2c/src/ir/dfa/determinization.cc +++ b/re2c/src/ir/dfa/determinization.cc @@ -141,7 +141,7 @@ dfa_t::dfa_t( tags[n->value.ctx.info] = true; break; case nfa_state_t::FIN: - fin[n->value.fin.rule] = true; + fin[n->rule] = true; break; default: break; diff --git a/re2c/src/ir/nfa/nfa.cc b/re2c/src/ir/nfa/nfa.cc index acf450d2..ad12dd08 100644 --- a/re2c/src/ir/nfa/nfa.cc +++ b/re2c/src/ir/nfa/nfa.cc @@ -40,7 +40,7 @@ static size_t calc_size_all(const std::vector &rs) return size; } -static nfa_state_t *compile(const RegExp *re, nfa_t &nfa, nfa_state_t *t) +static nfa_state_t *compile(const RegExp *re, size_t rule, nfa_t &nfa, nfa_state_t *t) { nfa_state_t *s = NULL; switch (re->tag) { @@ -49,20 +49,21 @@ static nfa_state_t *compile(const RegExp *re, nfa_t &nfa, nfa_state_t *t) break; case RegExp::SYM: s = &nfa.states[nfa.size++]; - s->ran(t, re->pld.sym.range); + s->ran(rule, t, re->pld.sym.range); break; case RegExp::ALT: s = &nfa.states[nfa.size++]; - s->alt(compile(re->pld.alt.re1, nfa, t), - compile(re->pld.alt.re2, nfa, t)); + s->alt(rule, + compile(re->pld.alt.re1, rule, nfa, t), + compile(re->pld.alt.re2, rule, nfa, t)); break; case RegExp::CAT: - s = compile(re->pld.cat.re2, nfa, t); - s = compile(re->pld.cat.re1, nfa, s); + s = compile(re->pld.cat.re2, rule, nfa, t); + s = compile(re->pld.cat.re1, rule, nfa, s); break; case RegExp::ITER: s = &nfa.states[nfa.size++]; - s->alt(t, compile(re->pld.iter.re, nfa, s)); + s->alt(rule, t, compile(re->pld.iter.re, rule, nfa, s)); break; } return s; @@ -162,7 +163,7 @@ static nfa_state_t *compile_rule( // base2var is filled in right-to-left, this is crucial std::vector base2var(nctxs + 1, CtxFix::RIGHTMOST); for (size_t i = nctxs; i > 0; --i) { - t = compile(rs[i], nfa, t); + t = compile(rs[i], nrule, nfa, t); const std::string *name = ctxnames[i - 1]; if (base[i - 1] == i - 1) { const size_t idx = nfa.contexts.size(); @@ -173,7 +174,7 @@ static nfa_state_t *compile_rule( trail.make_var(idx); } nfa_state_t *q = &nfa.states[nfa.size++]; - q->ctx(t, idx); + q->ctx(nrule, t, idx); t = q; } else { if (name != NULL) { @@ -185,7 +186,7 @@ static nfa_state_t *compile_rule( } } r.htag = nfa.contexts.size(); - t = compile(rs[0], nfa, t); + t = compile(rs[0], nrule, nfa, t); bool null = nullable(rs[0]); for (size_t i = 0; i < nctxs && null && ctxnames[i] != NULL; ++i) { @@ -206,7 +207,7 @@ static nfa_state_t *compile_rules( s = compile_rule(rs[0], nfa, 0); for (size_t i = 1; i < nrs; ++i) { nfa_state_t *q = &nfa.states[nfa.size++]; - q->alt(s, compile_rule(rs[i], nfa, i)); + q->alt(i, s, compile_rule(rs[i], nfa, i)); s = q; } } diff --git a/re2c/src/ir/nfa/nfa.h b/re2c/src/ir/nfa/nfa.h index daaedee1..9d7d4fd2 100644 --- a/re2c/src/ir/nfa/nfa.h +++ b/re2c/src/ir/nfa/nfa.h @@ -42,38 +42,38 @@ struct nfa_state_t nfa_state_t *out; size_t info; } ctx; - struct - { - size_t rule; - } fin; } value; + size_t rule; bool mark; - void alt(nfa_state_t *s1, nfa_state_t *s2) + void alt(size_t r, nfa_state_t *s1, nfa_state_t *s2) { type = ALT; value.alt.out1 = s1; value.alt.out2 = s2; + rule = r; mark = false; } - void ran(nfa_state_t *s, const Range *r) + void ran(size_t r, nfa_state_t *s, const Range *p) { type = RAN; value.ran.out = s; - value.ran.ran = r; + value.ran.ran = p; + rule = r; mark = false; } - void ctx(nfa_state_t *s, size_t i) + void ctx(size_t r, nfa_state_t *s, size_t i) { type = CTX; value.ctx.out = s; value.ctx.info = i; + rule = r; mark = false; } void fin(size_t r) { type = FIN; - value.fin.rule = r; + rule = r; mark = false; } }; -- 2.40.0