generalize grammar data structure allocation to k>1

This commit is contained in:
Sven M. Hallberg 2013-05-22 21:10:47 +02:00
parent 46525ede2d
commit 194ddee90b

View file

@ -124,13 +124,30 @@ static void ensure_k(HCFGrammar *g, size_t k)
// NB: we don't actually use first/follow[0] but allocate it anyway // NB: we don't actually use first/follow[0] but allocate it anyway
// so indices of the array correspond neatly to values of k // so indices of the array correspond neatly to values of k
assert(k==1); // XXX // allocate the new arrays
g->first = h_arena_malloc(g->arena, (k+1)*sizeof(HHashTable *)); HHashTable **first = h_arena_malloc(g->arena, (k+1)*sizeof(HHashTable *));
g->follow = h_arena_malloc(g->arena, (k+1)*sizeof(HHashTable *)); HHashTable **follow = h_arena_malloc(g->arena, (k+1)*sizeof(HHashTable *));
g->first[0] = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr);
g->follow[0] = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr); if(g->kmax > 0) {
g->first[1] = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr); // we are resizing, copy the old tables over
g->follow[1] = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr); for(size_t i=0; i<=g->kmax; i++) {
first[i] = g->first[0];
follow[i] = g->follow[0];
}
} else {
// we are initializing, allocate the first (in fact, dummy) tables
first[0] = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr);
follow[0] = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr);
}
// allocate the new tables
for(size_t i=g->kmax+1; i<=k; i++) {
first[i] = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr);
follow[i] = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr);
}
g->first = first;
g->follow = follow;
g->kmax = k; g->kmax = k;
} }