add API for pretty-printing for grammar symbols and symbol sequences
This commit is contained in:
parent
f6983a5041
commit
748845ca0c
3 changed files with 19 additions and 15 deletions
|
|
@ -164,9 +164,6 @@ static void stringmap_merge(HHashSet *workset, HCFStringMap *dst, HCFStringMap *
|
|||
}
|
||||
}
|
||||
|
||||
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. */
|
||||
static int fill_table_row(size_t kmax, HCFGrammar *g, HCFStringMap *row,
|
||||
const HCFChoice *A)
|
||||
|
|
@ -206,8 +203,8 @@ static int fill_table_row(size_t kmax, HCFGrammar *g, HCFStringMap *row,
|
|||
// XXX debug
|
||||
if(A == g->start) {
|
||||
printf("predict(");
|
||||
pprint_sequence(stdout, g, rhs);
|
||||
printf(" ) = ");
|
||||
h_pprint_sequence(stdout, g, rhs);
|
||||
printf(") = ");
|
||||
h_pprint_stringset(stdout, g, pred, 0);
|
||||
}
|
||||
}
|
||||
|
|
@ -215,7 +212,7 @@ static int fill_table_row(size_t kmax, HCFGrammar *g, HCFStringMap *row,
|
|||
// XXX debug
|
||||
if(A == g->start) {
|
||||
printf("row(");
|
||||
pprint_symbol(stdout, g, A);
|
||||
h_pprint_symbol(stdout, g, A);
|
||||
printf(") = ");
|
||||
h_pprint_stringset(stdout, g, row, 0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -624,7 +624,7 @@ static HCFChoice **pprint_string(FILE *f, HCFChoice **x)
|
|||
return x;
|
||||
}
|
||||
|
||||
void pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x)
|
||||
void h_pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x)
|
||||
{
|
||||
switch(x->type) {
|
||||
case HCF_CHAR:
|
||||
|
|
@ -643,32 +643,37 @@ void pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x)
|
|||
}
|
||||
}
|
||||
|
||||
void pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq)
|
||||
void h_pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq)
|
||||
{
|
||||
HCFChoice **x = seq->items;
|
||||
|
||||
if(*x == NULL) { // the empty sequence
|
||||
fputs(" \"\"", f);
|
||||
fputs("\"\"", f);
|
||||
} else {
|
||||
while(*x) {
|
||||
fputc(' ', f); // separator
|
||||
if(x != seq->items) fputc(' ', f); // internal separator
|
||||
|
||||
if((*x)->type == HCF_CHAR) {
|
||||
// condense character strings
|
||||
x = pprint_string(f, x);
|
||||
} else {
|
||||
pprint_symbol(f, g, *x);
|
||||
h_pprint_symbol(f, g, *x);
|
||||
x++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// adds some separators expected below
|
||||
static void pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq)
|
||||
{
|
||||
fputc(' ', f);
|
||||
h_pprint_sequence(f, g, seq);
|
||||
fputc('\n', f);
|
||||
}
|
||||
|
||||
static
|
||||
void pprint_ntrules(FILE *f, const HCFGrammar *g, const HCFChoice *nt,
|
||||
int indent, int len)
|
||||
static void pprint_ntrules(FILE *f, const HCFGrammar *g, const HCFChoice *nt,
|
||||
int indent, int len)
|
||||
{
|
||||
int i;
|
||||
int column = indent + len;
|
||||
|
|
@ -738,7 +743,7 @@ void h_pprint_symbolset(FILE *file, const HCFGrammar *g, const HHashSet *set, in
|
|||
|
||||
a = hte->key; // production's left-hand symbol
|
||||
|
||||
pprint_symbol(file, g, a);
|
||||
h_pprint_symbol(file, g, a);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -83,5 +83,7 @@ const HCFStringMap *h_follow(size_t k, HCFGrammar *g, const HCFChoice *x);
|
|||
|
||||
/* Pretty-printers for grammars and associated data. */
|
||||
void h_pprint_grammar(FILE *file, const HCFGrammar *g, int indent);
|
||||
void h_pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq);
|
||||
void h_pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x);
|
||||
void h_pprint_symbolset(FILE *file, const HCFGrammar *g, const HHashSet *set, int indent);
|
||||
void h_pprint_stringset(FILE *file, const HCFGrammar *g, const HCFStringMap *set, int indent);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue