From: Ulya Trofimovich Date: Mon, 8 Jun 2015 13:41:55 +0000 (+0100) Subject: Tried to improve code readability. X-Git-Tag: 0.15~217 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bb14c16df6e27508feff1bc52816c36415d283cf;p=re2c Tried to improve code readability. Now rule states and default state are added to DFA in separate independent passes. Removed 'bSaveOnHead' DFA property as it can be calculated right before setting initial state action. --- diff --git a/re2c/src/codegen/emit_dfa.cc b/re2c/src/codegen/emit_dfa.cc index 9ffdea61..001380cd 100644 --- a/re2c/src/codegen/emit_dfa.cc +++ b/re2c/src/codegen/emit_dfa.cc @@ -113,7 +113,7 @@ void DFA::emit(Output & output, uint32_t& ind, const RegExpMap* specMap, const s std::set used_labels; count_used_labels (used_labels, start_label, initial_label, o.get_force_start_label ()); - head->action.set_initial (initial_label, bSaveOnHead); + head->action.set_initial (initial_label, head->action.type == Action::SAVE); // Generate prolog if (bProlog) diff --git a/re2c/src/codegen/prepare_dfa.cc b/re2c/src/codegen/prepare_dfa.cc index 4007bbc1..41d4317c 100644 --- a/re2c/src/codegen/prepare_dfa.cc +++ b/re2c/src/codegen/prepare_dfa.cc @@ -175,20 +175,12 @@ void DFA::prepare(OutputFile & o, uint32_t & max_fill) } } - // insert actions + // create rule states State ** rules = new State * [nRules]; memset(rules, 0, (nRules)*sizeof(*rules)); - - State *accfixup = NULL; for (State * s = head; s; s = s->next) { - State * ow; - - if (!s->rule) - { - ow = accfixup; - } - else + if (s->rule) { if (!rules[s->rule->accept]) { @@ -197,31 +189,40 @@ void DFA::prepare(OutputFile & o, uint32_t & max_fill) rules[s->rule->accept] = n; addState(&s->next, n); } - - ow = rules[s->rule->accept]; + for (uint32_t i = 0; i < s->go.nSpans; ++i) + { + if (!s->go.span[i].to) + { + s->go.span[i].to = rules[s->rule->accept]; + } + } } + } + // create default state (if needed) + State * default_state = NULL; + for (State * s = head; s; s = s->next) + { for (uint32_t i = 0; i < s->go.nSpans; ++i) { if (!s->go.span[i].to) { - if (!ow) + if (!default_state) { - ow = accfixup = new State; - addState(&s->next, accfixup); + default_state = new State; + addState(&s->next, default_state); } - - s->go.span[i].to = ow; + s->go.span[i].to = default_state; } } } - - if (accfixup) + + // find backup states and create accept state (if needed) + if (default_state) { uint32_t nSaves = 0; uint32_t * saves = new uint32_t[nRules]; memset(saves, ~0, (nRules)*sizeof(*saves)); - // mark backtracking points for (State * s = head; s; s = s->next) { if (s->rule) @@ -233,26 +234,19 @@ void DFA::prepare(OutputFile & o, uint32_t & max_fill) if (saves[s->rule->accept] == ~0u) { saves[s->rule->accept] = nSaves++; + accept_map[saves[s->rule->accept]] = rules[s->rule->accept]; } s->action.set_save (saves[s->rule->accept]); - bSaveOnHead |= s == head; } } } } - for (uint32_t i = 0; i < nRules; ++i) - { - if (saves[i] != ~0u) - { - accept_map[saves[i]] = rules[i]; - } - } delete [] saves; if (accept_map.size () > 1) { o.set_used_yyaccept (); } - accfixup->action.set_accept (&accept_map); + default_state->action.set_accept (&accept_map); } delete [] rules; diff --git a/re2c/src/dfa/dfa.cc b/re2c/src/dfa/dfa.cc index ca1b6846..a0a09091 100644 --- a/re2c/src/dfa/dfa.cc +++ b/re2c/src/dfa/dfa.cc @@ -44,7 +44,6 @@ DFA::DFA(Ins *ins, uint32_t ni, uint32_t lb, uint32_t ub, const Char *rep) , toDo(NULL) , free_ins(ins) , free_rep(rep) - , bSaveOnHead (false) , accept_map () { diff --git a/re2c/src/dfa/dfa.h b/re2c/src/dfa/dfa.h index d85b8b2b..8609f91b 100644 --- a/re2c/src/dfa/dfa.h +++ b/re2c/src/dfa/dfa.h @@ -24,7 +24,6 @@ public: const Char * free_rep; protected: - bool bSaveOnHead; accept_t accept_map; public: