From 36abdd25de44092b2d1678a84abc9ff3e85d4689 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Mon, 6 Apr 2015 05:11:56 +0100 Subject: [PATCH] Continued adding "--skeleton" switch. Moved input-string-generation-from-DFA algorithm earlier, to the point when DFA is barely constructed and contains only 'Match' states and starting state (no saving, accepting or split states). This simplifies string generation: if destination state is NULL, it is either next to final state (if all spans lead to it), or default state (if some spans lead to other states). --- re2c/bootstrap/parser.cc | 12 ++++++++ re2c/code.cc | 65 +++++++++++++--------------------------- re2c/go.h | 7 ----- re2c/parser.y | 12 ++++++++ 4 files changed, 45 insertions(+), 51 deletions(-) diff --git a/re2c/bootstrap/parser.cc b/re2c/bootstrap/parser.cc index edae1746..d24aa199 100644 --- a/re2c/bootstrap/parser.cc +++ b/re2c/bootstrap/parser.cc @@ -2377,6 +2377,12 @@ void parse(Scanner& i, Output & o) } } dfa_map[it->first] = genCode(it->second.second); + + if (flag_skeleton) + { + dfa_map[it->first]->output_skeleton_prolog (o, topIndent); + } + dfa_map[it->first]->prepare(o.max_fill); } if (parseMode != Scanner::Rules && dfa_map.find(it->first) != dfa_map.end()) @@ -2399,6 +2405,12 @@ void parse(Scanner& i, Output & o) if (parseMode != Scanner::Reuse) { dfa_map[""] = genCode(spec); + + if (flag_skeleton) + { + dfa_map[""]->output_skeleton_prolog (o, topIndent); + } + dfa_map[""]->prepare(o.max_fill); } if (parseMode != Scanner::Rules && dfa_map.find("") != dfa_map.end()) diff --git a/re2c/code.cc b/re2c/code.cc index a52730ee..68274091 100644 --- a/re2c/code.cc +++ b/re2c/code.cc @@ -999,7 +999,6 @@ void DFA::prepare(uint & max_fill) } s->go.span[i].to = ow; - s->go.span[i].is_default = true; } } } @@ -1093,48 +1092,30 @@ void DFA::output_skeleton_epilog (OutputFile & o, uint ind) o << "return 0; }\n"; } -static void generate_data (State * s, bool def, const std::vector > & xs, std::vector, bool> > & ys) +static void generate_data (State * s, const std::vector > & xs, std::vector, bool> > & ys) { - switch (s->action->type) + if (s == NULL || (s->go.nSpans == 1 && s->go.span[0].to == NULL)) { - case Action::NONE: - assert (false); - break; - case Action::RULE: - case Action::ACCEPT: - for (uint i = 0; i < xs.size (); ++i) - { - ys.push_back (std::make_pair (std::vector (xs[i]), def)); - if (!def) - { - ys.back ().first.pop_back (); - } - } - break; - case Action::MATCH: - case Action::INITIAL: - case Action::SAVE: - case Action::MOVE: - if (!s->generated) + for (uint i = 0; i < xs.size (); ++i) + { + ys.push_back (std::make_pair (std::vector (xs[i]), false)); + } + } + else if (!s->generated) + { + s->generated = true; + for (uint i = 0; i < s->go.nSpans; ++i) + { + std::vector > zs; + for (uint j = 0; j < xs.size (); ++j) { - s->generated = true; - for (uint i = 0; i < s->go.nSpans; ++i) - { - std::vector > zs; - for (uint j = 0; j < xs.size (); ++j) - { - std::vector z (xs[j]); - if (s->go.span[i].to->action->type != Action::MOVE) - { - z.push_back (s->go.span[i].ub - 1); - } - zs.push_back (z); - } - generate_data (s->go.span[i].to, s->go.span[i].is_default, zs, ys); - } - s->generated = false; + std::vector z (xs[j]); + z.push_back (s->go.span[i].ub - 1); + zs.push_back (z); } - break; + generate_data (s->go.span[i].to, zs, ys); + } + s->generated = false; } } @@ -1146,7 +1127,7 @@ void DFA::generate (Output & output, uint ind) std::vector, bool> > ys; std::vector x; xs.push_back (x); - generate_data (head, false, xs, ys); + generate_data (head, xs, ys); ys.push_back (std::make_pair (std::vector (output.max_fill), false)); // pad with YYMAXFILL zeroes o << indent (ind) << "// These strings correspond to paths in DFA.\n"; @@ -1236,10 +1217,6 @@ void DFA::emit(Output & output, uint& ind, const RegExpMap* specMap, const std:: } next_fill_index = save_fill_index; - if (flag_skeleton) - { - output_skeleton_prolog (output, ind); - } // Generate prolog if (bProlog) { diff --git a/re2c/go.h b/re2c/go.h index 2a99e1df..15ae8431 100644 --- a/re2c/go.h +++ b/re2c/go.h @@ -16,14 +16,7 @@ struct If; // forward struct Span { uint ub; - bool is_default; State * to; - - inline Span () - : ub (0) - , is_default (false) - , to (NULL) - {} uint show (std::ostream&, uint) const; }; diff --git a/re2c/parser.y b/re2c/parser.y index 3a9d050d..8bc660f0 100644 --- a/re2c/parser.y +++ b/re2c/parser.y @@ -657,6 +657,12 @@ void parse(Scanner& i, Output & o) } } dfa_map[it->first] = genCode(it->second.second); + + if (flag_skeleton) + { + dfa_map[it->first]->output_skeleton_prolog (o, topIndent); + } + dfa_map[it->first]->prepare(o.max_fill); } if (parseMode != Scanner::Rules && dfa_map.find(it->first) != dfa_map.end()) @@ -679,6 +685,12 @@ void parse(Scanner& i, Output & o) if (parseMode != Scanner::Reuse) { dfa_map[""] = genCode(spec); + + if (flag_skeleton) + { + dfa_map[""]->output_skeleton_prolog (o, topIndent); + } + dfa_map[""]->prepare(o.max_fill); } if (parseMode != Scanner::Rules && dfa_map.find("") != dfa_map.end()) -- 2.40.0