]> granicus.if.org Git - re2c/commitdiff
Tried to improve code readability.
authorUlya Trofimovich <skvadrik@gmail.com>
Mon, 8 Jun 2015 13:41:55 +0000 (14:41 +0100)
committerUlya Trofimovich <skvadrik@gmail.com>
Mon, 8 Jun 2015 13:41:55 +0000 (14:41 +0100)
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.

re2c/src/codegen/emit_dfa.cc
re2c/src/codegen/prepare_dfa.cc
re2c/src/dfa/dfa.cc
re2c/src/dfa/dfa.h

index 9ffdea61772171deafb57a97b44f59b211be51e8..001380cd42a65bf8690b753dc6976e54ccf84cd6 100644 (file)
@@ -113,7 +113,7 @@ void DFA::emit(Output & output, uint32_t& ind, const RegExpMap* specMap, const s
        std::set<label_t> 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)
index 4007bbc10346bcccfc93e47a4e338f6bdc796d44..41d4317ce399f64e07f093e2fdeece624976c0a0 100644 (file)
@@ -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;
 
index ca1b68465378f47e0dbfe99fb1750a69ab2becd2..a0a0909109187cf36f88a7dd22e8c41c588716bd 100644 (file)
@@ -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 ()
 
 {
index d85b8b2b4420491d5f04423e63363347cd812e8b..8609f91bc34a2c034f8ee9ab01e8940c8db79615 100644 (file)
@@ -24,7 +24,6 @@ public:
        const Char * free_rep;
 
 protected:
-       bool bSaveOnHead;
        accept_t accept_map;
 
 public: