polished generating function code more
This commit is contained in:
parent
107d8c092b
commit
6b8a3f262f
1 changed files with 0 additions and 92 deletions
|
|
@ -10,98 +10,6 @@
|
|||
#include "../src/backends/lr.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void h_pprint_gfexpr(FILE *file, const HCFGrammar *g, HCFSequence *seq) {
|
||||
HCFChoice **x = seq->items;
|
||||
|
||||
if (*x == NULL) { // empty sequence
|
||||
fprintf(file, "1\n");
|
||||
} else {
|
||||
while (*x) {
|
||||
if (x != seq->items) {
|
||||
fprintf(file, " + ");
|
||||
}
|
||||
// consume items
|
||||
// if a string,
|
||||
// count its length
|
||||
// output t^length
|
||||
|
||||
if ((*x)->type == HCF_CHAR) {
|
||||
uint32_t count = 0;
|
||||
for(; *x; x++, count++) {
|
||||
if ((*x)->type != HCF_CHAR) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
fprintf(file, "t^%d", count);
|
||||
} else {
|
||||
uint32_t count=0, n, i=0;
|
||||
switch((*x)->type) {
|
||||
case HCF_CHAR:
|
||||
// should not be possible
|
||||
break;
|
||||
case HCF_END:
|
||||
// does not generate any output symbols: value 0
|
||||
break;
|
||||
case HCF_CHARSET:
|
||||
for(i=0; i<256; i++) {
|
||||
if (charset_isset((*x)->charset, i)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
fprintf(file, "%d*t", count);
|
||||
break;
|
||||
default:
|
||||
n = (uint8_t)(uintptr_t)h_hashtable_get(g->nts, x);
|
||||
|
||||
fprintf(file, "%c(t)", 'A'+n);
|
||||
}
|
||||
x++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void h_pprint_gfeqns_NOTUSED(FILE *file, const HCFGrammar *g) {
|
||||
if (g->nts->used < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// determine maximum string length of symbol names
|
||||
int len;
|
||||
size_t s;
|
||||
for(len=1, s=26; s < g->nts->used; len++, s*=26);
|
||||
|
||||
// iterate over g->nts
|
||||
size_t i;
|
||||
HHashTableEntry *hte;
|
||||
for(i=0; i < g->nts->capacity; i++) {
|
||||
for(hte = &g->nts->contents[i]; hte; hte = hte->next) {
|
||||
if (hte->key == NULL) {
|
||||
continue;
|
||||
}
|
||||
const HCFChoice *lhs = hte->key; // production's left-hand symbol
|
||||
assert(lhs->type == HCF_CHOICE);
|
||||
|
||||
uint8_t n = (uint8_t)(uintptr_t)h_hashtable_get(g->nts, lhs);
|
||||
fprintf(file, "%c(t) = ", 'A'+n);
|
||||
|
||||
HCFSequence **p = lhs->seq;
|
||||
if (*p == NULL) {
|
||||
return; // shouldn't happen
|
||||
}
|
||||
|
||||
h_pprint_gfexpr(file, g, *p);
|
||||
for(; *p; p++) {
|
||||
fprintf(file, "\t");
|
||||
h_pprint_gfexpr(file, g, *p);
|
||||
fprintf(file, "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *nonterminal_name(const HCFGrammar *g, const HCFChoice *nt)
|
||||
{
|
||||
static char buf[16] = {0}; // 14 characters in base 26 are enough for 64 bits
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue