Added debug code to item generation
authorThadeu Lima de Souza Cascardo <cascardo@dcc.ufmg.br>
Thu, 29 Sep 2005 17:31:50 +0000 (17:31 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@dcc.ufmg.br>
Thu, 29 Sep 2005 17:31:50 +0000 (17:31 +0000)
Code to print item sets was added to item.c.

git-archimport-id: cascardo@tlscascardo--private/libgrammatic--dev--0.1--patch-14

item.c

diff --git a/item.c b/item.c
index 5a41dca..fd017e2 100644 (file)
--- a/item.c
+++ b/item.c
@@ -1,4 +1,7 @@
 #include <grammar.h>
+#ifdef DEBUG
+#include <stdio.h>
+#endif
 
 typedef struct
 {
@@ -66,6 +69,30 @@ void item_delete (item_t* item)
   g_free (item);
 }
 
+#ifdef DEBUG
+void item_print (item_t* item)
+{
+  GList* l;
+  fprintf (stdout, "%s -> ", g_quark_to_string (item->left->value));
+  l = grammar_rule_get (item->right);
+  while (l != NULL)
+    {
+      symbol_t* symbol;
+      symbol = (symbol_t*) l->data;
+      if (l == item->dot)
+       {
+         fprintf (stdout, ".");
+       }
+      fprintf (stdout, " %s ", g_quark_to_string (symbol->value));
+    }
+  if (item->dot == NULL)
+    {
+      fprintf (stdout, ".");
+    }
+  fprintf (stdout, "\n");
+}
+#endif
+
 GHashTable* item_set_new ()
 {
   return g_hash_table_new_full (item_hash, item_equal, item_delete, NULL);
@@ -120,6 +147,18 @@ GHashTable* item_set_copy (GHashTable* item_set)
   return newitem_set;
 }
 
+#ifdef DEBUG
+void item_set_print_each (gpointer key, gpointer val, gpointer data)
+{
+  item_print (key);
+}
+
+void item_set_print (GHashTable* item_set)
+{
+  g_hash_table_foreach (item_set, item_set_print_each, NULL);
+}
+#endif
+
 void item_set_closure_step (GHashTable* item_set, Grammar* grammar,
                            item_t* item)
 {
@@ -288,6 +327,45 @@ state_t* item_collection_lookup (GHashTable* collection,
   return state;
 }
 
+#ifdef DEBUG
+void item_collection_print_each (gpointer key, gpointer val, gpointer data)
+{
+  GHashTable* item_set;
+  state_t* state;
+  item_set = (GHashTable*) key;
+  state = (state_t*) val;
+  fprintf (stdout, "Item %d:\n", state->code);
+  item_set_print (item_set);
+  fprintf (stdout, "\n");
+}
+
+void item_set_print_goto (gpointer key, gpointer val, gpointer data)
+{
+  symbol_t* symbol;
+  gint code;
+  state_t* state;
+  symbol = (symbol_t*) symbol;
+  code = GINT_TO_POINTER (val);
+  state = (state_t*) data;
+  fprintf (stdout, "GOTO (%d, %s) =\t %d\n", state->code,
+          g_quark_to_string (symbol->value), code);
+}
+
+void item_collection_print_goto (gpointer key, gpointer val, gpointer data)
+{
+  state_t* state;
+  state = (state_t*) val;
+  g_hash_table_foreach (state->symbols, item_set_print_goto, state);
+  fprintf (stdout, "\n");
+}
+
+void item_collection_print (GHashTable* collection)
+{
+  g_hash_table_foreach (collection, item_collection_print_each, NULL);
+  g_hash_table_foreach (collection, item_collection_print_goto, NULL);
+}
+#endif
+
 GHashTable* item_collection_goto (GHashTable* collection, Grammar* grammar,
                                  GHashTable* item_set, symbol_t* symbol)
 {
@@ -363,4 +441,8 @@ void item_set_collection (Grammar* grammar, symbol_t* start)
       new_item_sets = g_list_next (new_item_sets);
     }
   g_list_free (new_item_sets);
+#ifdef DEBUG
+  item_collection_print (collection);
+#endif
+  g_hash_table_destroy (collection);
 }