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. */
|
/* 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)
|
||||||
|
|
@ -206,8 +203,8 @@ static int fill_table_row(size_t kmax, HCFGrammar *g, HCFStringMap *row,
|
||||||
// XXX debug
|
// XXX debug
|
||||||
if(A == g->start) {
|
if(A == g->start) {
|
||||||
printf("predict(");
|
printf("predict(");
|
||||||
pprint_sequence(stdout, g, rhs);
|
h_pprint_sequence(stdout, g, rhs);
|
||||||
printf(" ) = ");
|
printf(") = ");
|
||||||
h_pprint_stringset(stdout, g, pred, 0);
|
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
|
// XXX debug
|
||||||
if(A == g->start) {
|
if(A == g->start) {
|
||||||
printf("row(");
|
printf("row(");
|
||||||
pprint_symbol(stdout, g, A);
|
h_pprint_symbol(stdout, g, A);
|
||||||
printf(") = ");
|
printf(") = ");
|
||||||
h_pprint_stringset(stdout, g, row, 0);
|
h_pprint_stringset(stdout, g, row, 0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -624,7 +624,7 @@ static HCFChoice **pprint_string(FILE *f, HCFChoice **x)
|
||||||
return 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) {
|
switch(x->type) {
|
||||||
case HCF_CHAR:
|
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;
|
HCFChoice **x = seq->items;
|
||||||
|
|
||||||
if(*x == NULL) { // the empty sequence
|
if(*x == NULL) { // the empty sequence
|
||||||
fputs(" \"\"", f);
|
fputs("\"\"", f);
|
||||||
} else {
|
} else {
|
||||||
while(*x) {
|
while(*x) {
|
||||||
fputc(' ', f); // separator
|
if(x != seq->items) fputc(' ', f); // internal separator
|
||||||
|
|
||||||
if((*x)->type == HCF_CHAR) {
|
if((*x)->type == HCF_CHAR) {
|
||||||
// condense character strings
|
// condense character strings
|
||||||
x = pprint_string(f, x);
|
x = pprint_string(f, x);
|
||||||
} else {
|
} else {
|
||||||
pprint_symbol(f, g, *x);
|
h_pprint_symbol(f, g, *x);
|
||||||
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);
|
fputc('\n', f);
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static void pprint_ntrules(FILE *f, const HCFGrammar *g, const HCFChoice *nt,
|
||||||
void pprint_ntrules(FILE *f, const HCFGrammar *g, const HCFChoice *nt,
|
int indent, int len)
|
||||||
int indent, int len)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int column = indent + len;
|
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
|
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. */
|
/* Pretty-printers for grammars and associated data. */
|
||||||
void h_pprint_grammar(FILE *file, const HCFGrammar *g, int indent);
|
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_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);
|
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