From e5e182ff95dafe3e5ae63533e94b9e3cd2d44d7d Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Tue, 1 Aug 2017 16:36:45 +0100 Subject: [PATCH] Don't loose mtag versions in TDFA intitalizer. --- re2c/src/dfa/closure.cc | 35 ++++++++++++++++-------------- re2c/src/dfa/closure.h | 5 ++--- re2c/src/dfa/determinization.cc | 8 ++----- re2c/src/skeleton/generate_code.cc | 20 ++++++++--------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/re2c/src/dfa/closure.cc b/re2c/src/dfa/closure.cc index 353233c0..04bda906 100644 --- a/re2c/src/dfa/closure.cc +++ b/re2c/src/dfa/closure.cc @@ -15,24 +15,22 @@ static void closure_leftmost(const closure_t &clos1, closure_t &clos, static int32_t compare_posix(const clos_t &c1, const clos_t &c2, Tagpool &tagpool, const std::vector &tags); static void prune(closure_t &clos, std::valarray &rules); static void lower_lookahead_to_transition(closure_t &clos); -static tcmd_t *generate_versions(closure_t &clos, const std::vector &tags, - Tagpool &tagpool, tcpool_t &tcpool, tagver_t &maxver, newvers_t &newvers); +static tcmd_t *generate_versions(dfa_t &dfa, closure_t &clos, Tagpool &tagpool, newvers_t &newvers); static void orders(closure_t &clos, Tagpool &tagpool, const std::vector &tags); static bool cmpby_rule_state(const clos_t &x, const clos_t &y); -tcmd_t *closure(closure_t &clos1, closure_t &clos2, Tagpool &tagpool, - tcpool_t &tcpool, std::valarray &rules, tagver_t &maxver, - newvers_t &newvers, closure_t *shadow, const std::vector &tags) +tcmd_t *closure(dfa_t &dfa, closure_t &clos1, closure_t &clos2, + Tagpool &tagpool, newvers_t &newvers, closure_t *shadow) { // build tagged epsilon-closure of the given set of NFA states if (tagpool.opts->posix_captures) { - closure_posix(clos1, clos2, shadow, tagpool, tags); - prune(clos2, rules); - orders(clos2, tagpool, tags); + closure_posix(clos1, clos2, shadow, tagpool, dfa.tags); + prune(clos2, dfa.rules); + orders(clos2, tagpool, dfa.tags); std::sort(clos2.begin(), clos2.end(), cmpby_rule_state); } else { closure_leftmost(clos1, clos2, shadow, tagpool); - prune(clos2, rules); + prune(clos2, dfa.rules); } // see note [the difference between TDFA(0) and TDFA(1)] @@ -42,8 +40,8 @@ tcmd_t *closure(closure_t &clos1, closure_t &clos2, Tagpool &tagpool, } // merge tags from different rules, find nondeterministic tags - tcmd_t *cmd = generate_versions(clos2, tags, tagpool, tcpool, maxver, newvers); - if (shadow) generate_versions(*shadow, tags, tagpool, tcpool, maxver, newvers); + tcmd_t *cmd = generate_versions(dfa, clos2, tagpool, newvers); + if (shadow) generate_versions(dfa, *shadow, tagpool, newvers); return cmd; } @@ -339,13 +337,13 @@ void lower_lookahead_to_transition(closure_t &clos) } } -tcmd_t *generate_versions(closure_t &clos, const std::vector &tags, - Tagpool &tagpool, tcpool_t &tcpool, tagver_t &maxver, newvers_t &newvers) +tcmd_t *generate_versions(dfa_t &dfa, closure_t &clos, Tagpool &tagpool, newvers_t &newvers) { tcmd_t *cmd = NULL; const size_t ntag = tagpool.ntags; - tagver_t *vers = tagpool.buffer; + tagver_t *vers = tagpool.buffer, &maxver = dfa.maxtagver; tagtree_t &tagtree = tagpool.history; + const std::vector &tags = dfa.tags; clositer_t b = clos.begin(), e = clos.end(), c; newver_cmp_t cmp = {tagtree}; newvers_t newacts(cmp); @@ -386,12 +384,17 @@ tcmd_t *generate_versions(closure_t &clos, const std::vector &tags, const hidx_t h = i->first.history; const size_t t = i->first.tag; if (history(tags[t])) { - cmd = tcpool.make_add(cmd, abs(m), abs(v), tagtree, h, t); + cmd = dfa.tcpool.make_add(cmd, abs(m), abs(v), tagtree, h, t); } else { - cmd = tcpool.make_set(cmd, abs(m), tagtree.last(h, t)); + cmd = dfa.tcpool.make_set(cmd, abs(m), tagtree.last(h, t)); } } + // mark tags with history + for (newvers_t::iterator j = newvers.begin(); j != newvers.end(); ++j) { + if (history(tags[j->first.tag])) dfa.mtagvers.insert(abs(j->second)); + } + // update tag versions in closure for (c = b; c != e; ++c) { const hidx_t h = c->ttran; diff --git a/re2c/src/dfa/closure.h b/re2c/src/dfa/closure.h index fc433973..77fca4e5 100644 --- a/re2c/src/dfa/closure.h +++ b/re2c/src/dfa/closure.h @@ -53,9 +53,8 @@ struct newver_cmp_t typedef std::map newvers_t; -tcmd_t *closure(closure_t &clos1, closure_t &clos2, Tagpool &tagpool, - tcpool_t &tcpool, std::valarray &rules, tagver_t &maxver, - newvers_t &newvers, closure_t *shadow, const std::vector &tags); +tcmd_t *closure(dfa_t &dfa, closure_t &clos1, closure_t &clos2, + Tagpool &tagpool, newvers_t &newvers, closure_t *shadow); } // namespace re2c diff --git a/re2c/src/dfa/determinization.cc b/re2c/src/dfa/determinization.cc index 07fa5ca2..492a3ff1 100644 --- a/re2c/src/dfa/determinization.cc +++ b/re2c/src/dfa/determinization.cc @@ -100,20 +100,16 @@ dfa_t::dfa_t(const nfa_t &nfa, const opt_t *opts, clos_t c0 = {NULL, nfa.root, ZERO_TAGS, INITIAL_TAGS, HROOT, HROOT}; clos1.push_back(c0); - acts = closure(clos1, clos2, tagpool, tcpool, rules, maxtagver, newvers, dump.shadow, tags); + acts = closure(*this, clos1, clos2, tagpool, newvers, dump.shadow); find_state(*this, dfa_t::NIL, 0/* any */, kernels, clos2, acts, dump); for (size_t i = 0; i < kernels.size(); ++i) { newvers.clear(); for (size_t c = 0; c < nchars; ++c) { reach(kernels[i], clos1, charset[c]); - acts = closure(clos1, clos2, tagpool, tcpool, rules, maxtagver, newvers, dump.shadow, tags); + acts = closure(*this, clos1, clos2, tagpool, newvers, dump.shadow); find_state(*this, i, c, kernels, clos2, acts, dump); } - // mark tags with history - for (newvers_t::iterator j = newvers.begin(); j != newvers.end(); ++j) { - if (history(tags[j->first.tag])) mtagvers.insert(abs(j->second)); - } } if (!opts->posix_captures) { diff --git a/re2c/src/skeleton/generate_code.cc b/re2c/src/skeleton/generate_code.cc index 1a927099..30ed8c87 100644 --- a/re2c/src/skeleton/generate_code.cc +++ b/re2c/src/skeleton/generate_code.cc @@ -202,7 +202,7 @@ void emit_start(OutputFile &o, size_t maxfill, const std::string &name, o.ws("\n"); o.ws("\ntypedef struct yymtag_t {"); o.ws("\n").wind(1).ws("struct yymtag_t *pred;"); - o.ws("\n").wind(1).ws("const YYCTYPE *tag;"); + o.ws("\n").wind(1).ws("const YYCTYPE *elem;"); o.ws("\n} yymtag_t;"); o.ws("\n"); @@ -252,15 +252,15 @@ void emit_start(OutputFile &o, size_t maxfill, const std::string &name, o.ws("\n"); o.ws("\nstatic void yymtag(yymtag_t **pt, const YYCTYPE *t, yymtagpool_t *tp)"); o.ws("\n{"); - o.ws("\n").wind(1).ws("yymtag_t *t = yymtagpool_next(tp);"); - o.ws("\n").wind(1).ws("t->pred = *pt;"); - o.ws("\n").wind(1).ws("t->tag = t;"); - o.ws("\n").wind(1).ws("*pt = t;"); + o.ws("\n").wind(1).ws("yymtag_t *n = yymtagpool_next(tp);"); + o.ws("\n").wind(1).ws("n->pred = *pt;"); + o.ws("\n").wind(1).ws("n->elem = t;"); + o.ws("\n").wind(1).ws("*pt = n;"); o.ws("\n}"); o.ws("\n"); o.ws("\nstatic int check_mtag_").wstring(name) - .ws("(unsigned *pkix, YYKEYTYPE *keys, const yymtag_t *tag,\n") + .ws("(unsigned *pkix, YYKEYTYPE *keys, const yymtag_t *mtag,\n") .wind(1).ws("const YYCTYPE *input, const YYCTYPE *token, const char *name)"); o.ws("\n{"); // o.ws("\n").wind(1).ws("check_key_count_").wstring(name).ws("(1) && return 1;"); @@ -269,14 +269,14 @@ void emit_start(OutputFile &o, size_t maxfill, const std::string &name, o.ws("\n").wind(1).ws("*pkix = kix + n + 1;"); // o.ws("\n").wind(1).ws("check_key_count_").wstring(name).ws("(n) && return 1;"); o.ws("\n").wind(1).ws("for (; n > 0; --n) {"); - o.ws("\n").wind(2).ws("if (tag == NULL) {"); + o.ws("\n").wind(2).ws("if (mtag == NULL) {"); o.ws("\n").wind(3).ws("fprintf(stderr, \"error: lex_").wstring(name).ws(": at position %ld, key %u: \"") .ws("\n").wind(4).ws("\"history for tag '%s' is too short\\n\",") .ws("\n").wind(4).ws("token - input, kix + n, name);"); o.ws("\n").wind(3).ws("return 1;"); o.ws("\n").wind(2).ws("}"); - o.ws("\n").wind(2).ws("const YYCTYPE *tag = tag->offs;"); - o.ws("\n").wind(2).ws("tag = tag->pred;"); + o.ws("\n").wind(2).ws("const YYCTYPE *tag = mtag->elem;"); + o.ws("\n").wind(2).ws("mtag = mtag->pred;"); o.ws("\n").wind(2).ws("const YYKEYTYPE\n") .wind(3).ws("exp = keys[kix + n],\n") .wind(3).ws("act = (YYKEYTYPE)(tag - token),\n") @@ -288,7 +288,7 @@ void emit_start(OutputFile &o, size_t maxfill, const std::string &name, o.ws("\n").wind(3).ws("return 1;"); o.ws("\n").wind(2).ws("}"); o.ws("\n").wind(1).ws("}"); - o.ws("\n").wind(1).ws("if (tag != NULL) {"); + o.ws("\n").wind(1).ws("if (mtag != NULL) {"); o.ws("\n").wind(2).ws("fprintf(stderr, \"error: lex_").wstring(name).ws(": at position %ld, key %u: \"") .ws("\n").wind(3).ws("\"history for tag '%s' is too long\\n\",") .ws("\n").wind(3).ws("token - input, kix, name);"); -- 2.40.0