From: Thadeu Lima de Souza Cascardo Date: Thu, 29 Sep 2005 17:31:50 +0000 (+0000) Subject: Added debug code to item generation X-Git-Tag: cascardo@tlscascardo--private,libgrammatic--lr1--0.1--base-0~4 X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fgrammar.git;a=commitdiff_plain;h=615eebcbb67a7f22c8248d862ac3e27fea8198cc Added debug code to item generation Code to print item sets was added to item.c. git-archimport-id: cascardo@tlscascardo--private/libgrammatic--dev--0.1--patch-14 --- diff --git a/item.c b/item.c index 5a41dca..fd017e2 100644 --- a/item.c +++ b/item.c @@ -1,4 +1,7 @@ #include +#ifdef DEBUG +#include +#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); }