Fixed assertion in first
[cascardo/grammar.git] / first.c
diff --git a/first.c b/first.c
index e79d98d..0b25ecb 100644 (file)
--- a/first.c
+++ b/first.c
@@ -372,12 +372,10 @@ void first_iterate (gpointer key, gpointer val, gpointer data)
                              first_set->has_empty = TRUE;
                              rule_delete (next_rule);
                            }
-                         else
+                         else if (next_symbol->terminal == FALSE)
                            {
                              symbol_t* next_symbol;
                              next_symbol = next_symbols->data;
-                             /* TODO: Check this assertion is correct. */
-                             assert (next_symbol->terminal == FALSE);
                              /* This is an indirect recursive rule. */
                              if (next_symbol->value == symbol->value)
                                {
@@ -392,6 +390,10 @@ void first_iterate (gpointer key, gpointer val, gpointer data)
                                  new_rules = g_list_prepend (new_rules, next_rule);
                                }
                            }
+                         else
+                           {
+                             rule_delete (next_rule);
+                           }
                          next_rules = g_list_next (next_rules);
                        }
                    }
@@ -457,7 +459,7 @@ void first_check (gpointer key, gpointer val, gpointer data)
  * We should iterate through the rules for each nonterminal until only
  * terminals are known to be in the first set of it.
  */
-GHashTable* grammar_first (Grammar* grammar)
+GHashTable* grammar_first (grammar_t* grammar)
 {
   GHashTable* first;
   gboolean stop;
@@ -474,7 +476,7 @@ GHashTable* grammar_first (Grammar* grammar)
   return first;
 }
 
-void put_key_on_list (gpointer key, gpointer val, gpointer data)
+static void put_key_on_list (gpointer key, gpointer val, gpointer data)
 {
   GList** l;
   l = (GList**) data;
@@ -497,3 +499,48 @@ GList* first_get (GHashTable* first, symbol_t* symbol)
   return l;
 
 }
+
+GList* first_rule (GHashTable* first, rule_t* rule)
+{
+
+  GHashTable* terminals;
+  GList* symbols;
+  GList* l;
+
+  l = NULL;
+
+  symbols = grammar_get_rule (rule);
+
+  terminals = g_hash_table_new_full (symbol_hash, symbol_equal, g_free, NULL);
+
+  while (symbols != NULL)
+    {
+      first_set_t* first_set;
+      symbol_t* symbol;
+      symbol = (symbol_t*) symbols->data;
+      if (symbol->terminal)
+       {
+         g_hash_table_insert (terminals, symbol_copy (symbol), NULL);
+         break;
+       }
+      if (!g_hash_table_lookup_extended (first, symbol, NULL,
+                                        (gpointer*) &first_set))
+       {
+         g_hash_table_destroy (terminals);
+         return NULL;
+       }
+      first_set_union (terminals, first_set->terminals);
+      if (first_set->has_empty == FALSE)
+       break;
+      symbols = g_list_next (symbols);
+    }
+
+  if (symbols == NULL)
+    l = g_list_prepend (l, symbol_new (TRUE, 0));
+  g_hash_table_foreach (terminals, put_key_on_list, &l);
+
+  g_hash_table_destroy (terminals);
+
+  return l;
+  
+}