debug table generation

This commit is contained in:
Sven M. Hallberg 2013-05-23 23:25:32 +02:00
parent 4c5ca5ceab
commit f6983a5041
2 changed files with 25 additions and 12 deletions

View file

@ -3,6 +3,8 @@
#include "../cfgrammar.h" #include "../cfgrammar.h"
#include "../parsers/parser_internal.h" #include "../parsers/parser_internal.h"
static const size_t DEFAULT_KMAX = 1;
/* Generating the LL(k) parse table */ /* Generating the LL(k) parse table */
@ -17,11 +19,6 @@ typedef struct HLLkTable_ {
} HLLkTable; } HLLkTable;
// XXX adaptation to LL(1), to be removed
typedef HCharKey HCFToken;
static const HCFToken end_token = 0x200;
#define char_token char_key
/* Interface to look up an entry in the parse table. */ /* Interface to look up an entry in the parse table. */
const HCFSequence *h_llk_lookup(const HLLkTable *table, const HCFChoice *x, const HCFSequence *h_llk_lookup(const HLLkTable *table, const HCFChoice *x,
HInputStream lookahead) HInputStream lookahead)
@ -116,7 +113,7 @@ static void *combine_entries(HHashSet *workset, void *dst, const void *src)
if(dst == CONFLICT) { // previous conflict if(dst == CONFLICT) { // previous conflict
h_hashset_put(workset, src); h_hashset_put(workset, src);
} else if(dst != src) { // new conflict } else if(dst == src) { // new conflict
h_hashset_put(workset, dst); h_hashset_put(workset, dst);
h_hashset_put(workset, src); h_hashset_put(workset, src);
dst = CONFLICT; dst = CONFLICT;
@ -161,12 +158,15 @@ static void stringmap_merge(HHashSet *workset, HCFStringMap *dst, HCFStringMap *
if(dst_) if(dst_)
stringmap_merge(workset, dst_, src_); stringmap_merge(workset, dst_, src_);
else else
dst_ = src_; h_hashtable_put(dst->char_branches, (void *)c, src_);
} }
} }
} }
} }
void pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq);
void pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x);
/* Generate entries for the production "A" in the given table row. */ /* Generate entries for the production "A" in the given table row. */
static int fill_table_row(size_t kmax, HCFGrammar *g, HCFStringMap *row, static int fill_table_row(size_t kmax, HCFGrammar *g, HCFStringMap *row,
const HCFChoice *A) const HCFChoice *A)
@ -202,8 +202,23 @@ static int fill_table_row(size_t kmax, HCFGrammar *g, HCFStringMap *row,
// merge predict set into the row // merge predict set into the row
// accumulates conflicts in new workset // accumulates conflicts in new workset
stringmap_merge(nextset, row, pred); stringmap_merge(nextset, row, pred);
// XXX debug
if(A == g->start) {
printf("predict(");
pprint_sequence(stdout, g, rhs);
printf(" ) = ");
h_pprint_stringset(stdout, g, pred, 0);
} }
} }
}
// XXX debug
if(A == g->start) {
printf("row(");
pprint_symbol(stdout, g, A);
printf(") = ");
h_pprint_stringset(stdout, g, row, 0);
}
// switch to the updated workset // switch to the updated workset
h_hashtable_free(workset); h_hashtable_free(workset);
@ -254,8 +269,6 @@ static int fill_table(size_t kmax, HCFGrammar *g, HLLkTable *table)
return 0; return 0;
} }
static const size_t DEFAULT_KMAX = 1;
int h_llk_compile(HAllocator* mm__, HParser* parser, const void* params) int h_llk_compile(HAllocator* mm__, HParser* parser, const void* params)
{ {
size_t kmax = params? (uintptr_t)params : DEFAULT_KMAX; size_t kmax = params? (uintptr_t)params : DEFAULT_KMAX;
@ -460,7 +473,7 @@ int test_llk(void)
*/ */
HParser *X = h_optional(h_ch('x')); HParser *X = h_optional(h_ch('x'));
HParser *Y = h_epsilon_p(); //h_sequence(h_ch('y'), NULL); HParser *Y = h_sequence(h_ch('y'), NULL);
HParser *A = h_sequence(X, Y, h_ch('a'), NULL); HParser *A = h_sequence(X, Y, h_ch('a'), NULL);
HParser *B = h_sequence(Y, h_ch('b'), NULL); HParser *B = h_sequence(Y, h_ch('b'), NULL);
HParser *p = h_choice(A, B, NULL); HParser *p = h_choice(A, B, NULL);

View file

@ -624,7 +624,7 @@ static HCFChoice **pprint_string(FILE *f, HCFChoice **x)
return x; return x;
} }
static void pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x) void pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x)
{ {
switch(x->type) { switch(x->type) {
case HCF_CHAR: case HCF_CHAR:
@ -643,7 +643,7 @@ static void pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x)
} }
} }
static void pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq) void pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq)
{ {
HCFChoice **x = seq->items; HCFChoice **x = seq->items;