2 * Copyright (C) 2005 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 static void put_key_val_on_list (gpointer key, gpointer val, gpointer data)
34 *l = g_list_prepend (*l, val);
35 *l = g_list_prepend (*l, key);
38 static void put_key_on_list (gpointer key, gpointer val, gpointer data)
42 *l = g_list_prepend (*l, key);
45 void lr1_gen_add (gpointer key, gpointer val, gpointer data)
49 transition_t* transition;
53 g_hash_table_foreach (key, put_key_on_list, (gpointer) &l);
58 item = (item_t*) l->data;
60 if (item->left->value == -1 && item->left->terminal == FALSE)
62 if (item->dot == NULL)
65 printf ("ACCEPT: %p\n", key);
67 transition = transition_accept_new ();
68 lr1_add (data, key, symbol_new (TRUE, 0), transition);
73 printf ("START: %p\n", key);
75 lr1_push (data, key, NULL);
78 else if (item->dot == NULL)
81 printf ("REDUCE: %p, %s\n", key,
82 g_quark_to_string (item->lookahead->value));
84 transition = transition_reduce_new (item->left, item->right);
85 lr1_add (data, key, item->lookahead, transition);
96 g_hash_table_foreach (val, put_key_val_on_list, (gpointer) &l);
102 symbol = (symbol_t*) l->data;
106 printf ("SHIFT: %p, %s, %p\n", key,
107 g_quark_to_string (symbol->value), l->data);
109 transition = transition_shift_new (l->data);
110 lr1_add (data, key, symbol, transition);
120 lr1_t* lr1_gen (grammar_t* grammar, symbol_t* start, nextcb cb, gpointer data)
124 GHashTable* collection;
127 lr1 = lr1_new (cb, data);
129 first = grammar_first (grammar);
134 collection = item_set_collection (grammar, first, start);
136 g_hash_table_foreach (collection, lr1_gen_add, lr1);