Added GPLv2+ as license for libgrammatic
[cascardo/grammar.git] / first.c
diff --git a/first.c b/first.c
index 040a04c..f90cdcf 100644 (file)
--- a/first.c
+++ b/first.c
@@ -1,3 +1,23 @@
+/*
+ *  Copyright (C) 2005  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+
 #include <grammar.h>
 #include <stdio.h>
 #include <assert.h>
@@ -376,10 +396,16 @@ void first_iterate (gpointer key, gpointer val, gpointer data)
                            {
                              symbol_t* next_symbol;
                              next_symbol = next_symbols->data;
-                             /* TODO: Check this assertion is correct. */
-                             assert (next_symbol->terminal == FALSE);
+                             /*
+                              * This rule starts with a terminal. Ignore it.
+                              * We could add it as an optimization. TODO
+                              */
+                             if (next_symbol->terminal)
+                               {
+                                 rule_delete (next_rule);
+                               }
                              /* This is an indirect recursive rule. */
-                             if (next_symbol->value == symbol->value)
+                             else if (next_symbol->value == symbol->value)
                                {
                                  first_set->recursive =
                                    g_list_prepend (first_set->recursive,
@@ -542,3 +568,27 @@ GList* first_rule (GHashTable* first, rule_t* rule)
   return l;
   
 }
+
+void first_print_symbol (gpointer key, gpointer val, gpointer data)
+{
+  symbol_t* symbol;
+  symbol = (symbol_t*) key;
+  fprintf (stdout, "%s\n", g_quark_to_string (symbol->value));
+}
+
+void first_print_set (gpointer key, gpointer val, gpointer data)
+{
+  symbol_t* symbol;
+  first_set_t* first_set;
+  symbol = (symbol_t*) key;
+  first_set = (first_set_t*) val;
+  fprintf (stdout, "FIRST of %s:\n", g_quark_to_string (symbol->value));
+  if (first_set->has_empty)
+    fprintf (stdout, "empty symbol\n");
+  g_hash_table_foreach (first_set->terminals, first_print_symbol, NULL);
+}
+
+void first_print (GHashTable* first)
+{
+  g_hash_table_foreach (first, first_print_set, NULL);
+}