Tidying up
authorNick Downing <downing.nick@gmail.com>
Sun, 22 Jul 2018 03:25:57 +0000 (13:25 +1000)
committerNick Downing <downing.nick@gmail.com>
Sun, 22 Jul 2018 03:26:13 +0000 (13:26 +1000)
flex_dfa.py

index ecb0b00..e4b808a 100644 (file)
@@ -65,14 +65,11 @@ class FlexDFA:
     # a dummy rule that accepts the null string and executes EOB action
     eob_state = len(_dfa.start_action)
 
-    # state 0 is the jam state, the EOB state will be added later on
-    self.states = numpy.zeros((0x100, 2), numpy.int16) # base, def
-    n_states = 1
+    # state 0 is the jam state, make it exist but have empty acclist
     self.acclist = numpy.zeros((0x100,), numpy.int16)
     n_acclist = 0
     self.accept = numpy.zeros((0x100,), numpy.int16)
     n_accept = 1
-    self.entries = [(eob_state, 0)] + [(0, 0)] * 0x100 # nxt, chk
 
     # transition_table[i, j] is transition on character j in state i
     # in our way of thinking, 0 is don't care and -1 is failure
@@ -80,10 +77,8 @@ class FlexDFA:
     # the distinction being that failure has no associated action
     # thus all entries of states are filled, with 0 as a catch-all
     transition_table = numpy.zeros((0x100, 0x101), numpy.int16)
-    transition_table[0, 0] = eob_state # all states go to EOB on NUL
-    used = numpy.zeros(0x200, numpy.bool)
-    used[:0x101] = True # account for the jam (don't care) state
 
+    # generate acclist, accept and transition tables
     while n_accept < len(flex_state_to_action):
       action = flex_state_to_action[n_accept]
       state, transition = _dfa.actions[action]
@@ -124,7 +119,7 @@ class FlexDFA:
         self.acclist[n_acclist] = acc
         n_acclist += 1
 
-      # extend transition_table array if required
+      # calculate transition row from _dfa.state character-to-action table
       if n_accept >= transition_table.shape[0]:
         new_transition_table = numpy.zeros(
           (transition_table.shape[0] * 2, 0x101),
@@ -132,8 +127,6 @@ class FlexDFA:
         )
         new_transition_table[:transition_table.shape[0], :] = transition_table
         transition_table = new_transition_table
-
-      # calculate full entry from _dfa.state character-to-action table
       breaks, actions, _ = _dfa.states[state]
       character0 = 0
       for i in range(len(breaks)):
@@ -149,16 +142,14 @@ class FlexDFA:
         character0 = character1
       assert character0 == 0x100
 
-      # remap NUL transition to 0x100 and replace with EOB transition
-      transition_table[n_accept, 0x100] = \
-        transition_table[n_accept, 0]
-      transition_table[n_accept, 0] = eob_state
-      #print(n_accept, transition_table[len(self.states), :])
-
       n_accept += 1
 
-    self.acclist = self.acclist[:n_acclist]
+    # remap NUL transitions to 0x100 and replace with EOB transitions
+    transition_table[:, 0x100] = transition_table[:, 0]
+    transition_table[:, 0] = eob_state
 
+    # finalize the acclist and accept tables
+    self.acclist = self.acclist[:n_acclist]
     if n_accept >= self.accept.shape[0]:
       new_accept = numpy.zeros(
         (self.accept.shape[0] * 2,),
@@ -169,6 +160,13 @@ class FlexDFA:
     self.accept[n_accept] = n_acclist
     self.accept = self.accept[:n_accept + 1]
 
+    # state 0 is the jam state, the EOB state will be added later on
+    self.states = numpy.zeros((0x100, 2), numpy.int16) # base, def
+    n_states = 1
+    self.entries = [(eob_state, 0)] + [(0, 0)] * 0x100 # nxt, chk
+    used = numpy.zeros(0x200, numpy.bool)
+    used[:0x101] = True # account for the jam (don't care) state
+
     # compress the transition table
     while n_states < n_accept:
       # find most similar/earliest existing state to use as default