From d0eb2ce891eabd5dac2d573494a512dc54cf4023 Mon Sep 17 00:00:00 2001 From: "Sven M. Hallberg" Date: Sun, 5 May 2013 22:14:21 +0200 Subject: [PATCH] add pretty printers for symbol and token sets --- src/backends/ll.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/backends/ll.c b/src/backends/ll.c index d669599..9084137 100644 --- a/src/backends/ll.c +++ b/src/backends/ll.c @@ -394,6 +394,22 @@ static HCFChoice **pprint_string(FILE *f, HCFChoice **x) return x; } +static void pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x) +{ + switch(x->type) { + case HCF_CHAR: + fputc('"', f); + pprint_char(f, x->chr); + fputc('"', f); + break; + case HCF_END: + fputc('$', f); + break; + default: + fputs(nonterminal_name(g, x), f); + } +} + static void pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq) { HCFChoice **x = seq->items; @@ -478,6 +494,59 @@ void h_pprint_grammar(FILE *file, const HCFGrammar *g, int indent) } } +void h_pprint_symbolset(FILE *file, const HCFGrammar *g, const HHashSet *set, int indent) +{ + int j; + for(j=0; jcapacity; i++) { + for(hte = &set->contents[i]; hte; hte = hte->next) { + if(hte->key == NULL) + continue; + const HCFChoice *a = hte->key; // production's left-hand symbol + + pprint_symbol(file, g, a); + fputc(',', file); + } + } + + fputs("}\n", file); +} + +void h_pprint_tokenset(FILE *file, const HCFGrammar *g, const HHashSet *set, int indent) +{ + int j; + for(j=0; jcapacity; i++) { + for(hte = &set->contents[i]; hte; hte = hte->next) { + if(hte->key == NULL) + continue; + HCFTerminal a = (intptr_t)hte->key; + // BUG this ignores tokens with value 0! + + if(a == '$') + fputs("\\$", file); + else if(a == end_token) + fputc('$', file); + else + pprint_char(file, a); + } + } + + fputs("]\n", file); +} + /* LL parse table and associated data */