Subject: [PATCH] dfa: print detail of DFA states in a debug mode
From: Norihiro Tanaka <[email-masked]>
To: [email-masked]
Date: Mon, 13 Oct 2014 19:39:21 +0900
List-Id: <debbugs-submit.debbugs.gnu.org>

If compile grep with -DDEBUG option, it outputs tokens etc.  In addition
to them, DFA states and transitions are output by this patch.  Debugging
them, the patch will be very useful.

BTW, the patch doesn't make any changes without -DDEBUG option.
From 26b2316b96c0c5e5a977485ea040d58222faf465 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Sat, 22 Mar 2014 17:00:36 +0900
Subject: [PATCH] dfa: print detail of DFA states in a debug mode

* src/dfa.c (prtok): replace `%c' to `%02x' in format of printf().
(state_index): print detail of new state.
(dfastate) print detail of DFA states.
---
 src/dfa.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 71 insertions(+), 1 deletion(-)

diff --git a/src/dfa.c b/src/dfa.c
index 58a4b83..d9e1c33 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -510,7 +510,7 @@ prtok (token t)
   else if (t < NOTCHAR)
     {
       int ch = t;
-      fprintf (stderr, "%c", ch);
+      fprintf (stderr, "0x%02x", ch);
     }
   else
     {
@@ -2120,6 +2120,28 @@ state_index (struct dfa *d, position_set const *s, int context)
         return i;
     }
 
+#ifdef DEBUG
+  fprintf (stderr, "new state %d\n nextpos:", i);
+  for (j = 0; j < s->nelem; ++j)
+    {
+      fprintf (stderr, " %d:", s->elems[j].index);
+      prtok (d->tokens[s->elems[j].index]);
+    }
+  fprintf (stderr, "\n context:");
+  if (context ^ CTX_ANY)
+    {
+      if (context & CTX_NONE)
+        fprintf (stderr, " CTX_NONE");
+      if (context & CTX_LETTER)
+        fprintf (stderr, " CTX_LETTER");
+      if (context & CTX_NEWLINE)
+        fprintf (stderr, " CTX_NEWLINE");
+    }
+  else
+    fprintf (stderr, " CTX_ANY");
+  fprintf (stderr, "\n");
+#endif
+
   /* We'll have to create a new state.  */
   d->states = maybe_realloc (d->states, d->sindex, &d->salloc,
                              sizeof *d->states);
@@ -2581,6 +2603,10 @@ dfastate (state_num s, struct dfa *d, state_num trans[])
   bool next_isnt_1st_byte = false; /* We can't add state0.  */
   size_t i, j, k;
 
+#ifdef DEBUG
+  fprintf (stderr, "build state %d\n", s);
+#endif
+
   zeroset (matches);
 
   for (i = 0; i < d->states[s].elems.nelem; ++i)
@@ -2632,6 +2658,16 @@ dfastate (state_num s, struct dfa *d, state_num trans[])
             continue;
         }
 
+#ifdef DEBUG
+      fprintf (stderr, " nextpos %d:", pos.index);
+      prtok (d->tokens[pos.index]);
+      fprintf (stderr, " of");
+      for (j = 0; j < NOTCHAR; j++)
+      if (tstbit (j,  matches))
+        fprintf (stderr, " 0x%02x", j);
+      fprintf (stderr, "\n");
+#endif
+
       for (j = 0; j < ngrps; ++j)
         {
           /* If matches contains a single character only, and the current
@@ -2792,6 +2828,29 @@ dfastate (state_num s, struct dfa *d, state_num trans[])
       else
         state_letter = state;
 
+#ifdef DEBUG
+      fprintf (stderr, "group %d\n nextpos:", i);
+      for (j = 0; j < grps[i].nelem; ++j)
+        {
+          fprintf (stderr, " %d:", grps[i].elems[j]);
+          prtok (d->tokens[grps[i].elems[j]]);
+        }
+      fprintf (stderr, "\n follows:");
+      for (j = 0; j < follows.nelem; ++j)
+        {
+          fprintf (stderr, " %d:", follows.elems[j].index);
+          prtok (d->tokens[follows.elems[j].index]);
+        }
+      fprintf (stderr, "\n states:");
+      if (possible_contexts & CTX_NEWLINE)
+        fprintf (stderr, " NEWLINE:%d", state_newline);
+      if (possible_contexts & CTX_LETTER)
+        fprintf (stderr, " LETTER:%d", state_letter);
+      if (possible_contexts & CTX_NONE)
+        fprintf (stderr, " NONE:%d", state);
+      fprintf (stderr, "\n");
+#endif
+
       /* Set the transitions for each character in the current label.  */
       for (j = 0; j < CHARCLASS_WORDS; ++j)
         for (k = 0; k < CHARCLASS_WORD_BITS; ++k)
@@ -2808,6 +2867,17 @@ dfastate (state_num s, struct dfa *d, state_num trans[])
             }
     }
 
+#ifdef DEBUG
+  fprintf (stderr, "trans table %d\n", s);
+  for (i = 0; i < NOTCHAR; ++i)
+    {
+      if (!(i & 0xf))
+        fprintf (stderr, "\n");
+      fprintf (stderr, " %2d", trans[i]);
+    }
+  fprintf (stderr, "\n");
+#endif
+
   for (i = 0; i < ngrps; ++i)
     free (grps[i].elems);
   free (follows.elems);
-- 
2.1.1