#include <grammar.h>
+#include <first.h>
+#include <item.h>
#ifdef DEBUG
#include <stdio.h>
#endif
-typedef struct
-{
- symbol_t* left;
- rule_t* right;
- GList* dot;
-} item_t;
-
-item_t* item_new (symbol_t* left, rule_t* right)
+item_t* item_new (symbol_t* left, rule_t* right, symbol_t* lookahead)
{
item_t* item;
item = g_malloc (sizeof (item_t));
item->left = left;
item->right = right;
item->dot = grammar_get_rule (right);
+ item->lookahead = lookahead;
return item;
}
{
item_t* newitem;
int n;
- newitem = item_new (symbol_copy (item->left), rule_copy (item->right));
+ newitem = item_new (symbol_copy (item->left), rule_copy (item->right),
+ symbol_copy (item->lookahead));
n = g_list_position (grammar_get_rule (item->right), item->dot);
newitem->dot = g_list_nth (grammar_get_rule (newitem->right), n);
return newitem;
return c;
if ((c = rule_cmp (a->right, b->right)) != 0)
return c;
+ if ((c = symbol_cmp (a->lookahead, b->lookahead)) != 0)
+ return c;
na = g_list_position (grammar_get_rule (a->right), a->dot);
nb = g_list_position (grammar_get_rule (b->right), b->dot);
if (na < nb)
guint hash;
item = (item_t*) data;
hash = rule_hash (item->right) * 37 + symbol_hash (item->left);
+ hash = hash * 37 + symbol_hash (item->lookahead);
return hash;
}
{
g_free (item->left);
rule_delete (item->right);
+ g_free (item->lookahead);
g_free (item);
}
{
fprintf (stdout, ".");
}
- fprintf (stdout, "\n");
+ fprintf (stdout, ", %s\n", g_quark_to_string (item->lookahead->value));
}
#endif
}
#endif
+rule_t* rule_new_item (item_t* item)
+{
+
+ rule_t* rule;
+ GList* l;
+ rule = rule_new ();
+ l = g_list_next (item->dot);
+ while (l != NULL)
+ {
+ rule_append (rule, symbol_copy (l->data));
+ l = g_list_next (l);
+ }
+ rule_append (rule, symbol_copy (item->lookahead));
+ return rule;
+
+}
+
void item_set_closure_step (GHashTable* item_set, grammar_t* grammar,
- item_t* item)
+ GHashTable* first, item_t* item)
{
if (item->dot != NULL)
{
if (symbol->terminal == FALSE)
{
GList* rules;
+ GList* terminals;
+ rule_t* rule;
+ rule = rule_new_item (item);
+ terminals = first_rule (first, rule);
+ rule_delete (rule);
rules = grammar_get_rules (grammar, symbol);
while (rules != NULL)
{
- rule_t* rule;
- item_t* newitem;
- rule = rule_copy (rules->data);
- newitem = item_new (symbol_copy (symbol), rule);
- if (!item_set_add (item_set, newitem))
- item_delete (newitem);
+ GList* lookahead;
+ lookahead = terminals;
+ while (lookahead != NULL)
+ {
+ rule_t* rule;
+ item_t* newitem;
+ rule = rule_copy (rules->data);
+ newitem = item_new (symbol_copy (symbol), rule,
+ symbol_copy (lookahead->data));
+ if (!item_set_add (item_set, newitem))
+ item_delete (newitem);
+ lookahead = g_list_next (lookahead);
+ }
rules = g_list_next (rules);
}
+ g_list_free (terminals);
}
}
}
-GHashTable* item_set_closure (GHashTable* item_set, grammar_t* grammar)
+GHashTable* item_set_closure (GHashTable* item_set, grammar_t* grammar,
+ GHashTable* first)
{
int size;
int last_size;
g_hash_table_foreach (item_set, put_key_on_list, &l);
while (l != NULL)
{
- item_set_closure_step (item_set, grammar, l->data);
+ item_set_closure_step (item_set, grammar, first, l->data);
l = g_list_next (l);
}
g_list_free (l);
}
GHashTable* item_set_goto (GHashTable* item_set, grammar_t* grammar,
- symbol_t* symbol)
+ GHashTable* first, symbol_t* symbol)
{
GList* l;
GHashTable* newitem_set;
}
l = g_list_next (l);
}
- return item_set_closure (newitem_set, grammar);
+ return item_set_closure (newitem_set, grammar, first);
}
#endif
GHashTable* item_collection_goto (GHashTable* collection, grammar_t* grammar,
- GHashTable* item_set, symbol_t* symbol)
+ GHashTable* first, GHashTable* item_set,
+ symbol_t* symbol)
{
GHashTable* symbols;
GHashTable* newitem_set;
{
return NULL;
}
- goto_item_set = item_set_goto (item_set, grammar, symbol);
+ goto_item_set = item_set_goto (item_set, grammar, first, symbol);
if (!item_collection_add (collection, goto_item_set, &old_item_set))
{
g_hash_table_insert (symbols, symbol, old_item_set);
}
}
-void item_set_collection (grammar_t* grammar, symbol_t* start)
+GHashTable* item_set_collection (grammar_t* grammar, GHashTable* first,
+ symbol_t* start)
{
GHashTable* collection;
GHashTable* item_set;
GList* new_item_sets;
rule = rule_new ();
rule_append (rule, symbol_copy (start));
- item = item_new (symbol_new (FALSE, -1), rule);
+ item = item_new (symbol_new (FALSE, -1), rule, symbol_new (TRUE, 0));
item_set = item_set_new ();
item_set_add (item_set, item);
- item_set_closure (item_set, grammar);
+ item_set_closure (item_set, grammar, first);
collection = g_hash_table_new_full (item_set_hash, item_set_equal,
g_hash_table_destroy, NULL);
item_collection_add (collection, item_set, NULL);
{
symbol = (symbol_t*) item->dot->data;
if ((new_item_set =
- item_collection_goto (collection, grammar,
+ item_collection_goto (collection, grammar, first,
next_item_set, symbol)) != NULL)
{
g_list_append (new_item_sets, new_item_set);
#ifdef DEBUG
item_collection_print (collection);
#endif
- g_hash_table_destroy (collection);
+ return collection;
}