Added function to get first from sequence of grammar symbols
authorThadeu Lima de Souza Cascardo <cascardo@dcc.ufmg.br>
Thu, 29 Sep 2005 03:09:51 +0000 (03:09 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@dcc.ufmg.br>
Thu, 29 Sep 2005 03:09:51 +0000 (03:09 +0000)
Function first_rule returns a list with all the symbols in first of a
rule or sequence of grammar symbols.

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

first.c
first.h

diff --git a/first.c b/first.c
index e79d98d..cd3f524 100644 (file)
--- a/first.c
+++ b/first.c
@@ -497,3 +497,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;
+  
+}
diff --git a/first.h b/first.h
index 1f0794c..29bf501 100644 (file)
--- a/first.h
+++ b/first.h
@@ -5,5 +5,6 @@
 
 GHashTable* grammar_first (Grammar*);
 GList* first_get (GHashTable*, symbol_t*);
+GList* first_rule (GHashTable*, rule_t*);
 
 #endif