2013-04-17 15:10:33 +02:00
|
|
|
#include <assert.h>
|
2013-05-05 19:34:12 +02:00
|
|
|
#include <ctype.h>
|
2013-04-17 15:10:33 +02:00
|
|
|
#include "../internal.h"
|
|
|
|
|
#include "../parsers/parser_internal.h"
|
|
|
|
|
|
|
|
|
|
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
/* Grammar representation and analysis */
|
|
|
|
|
|
|
|
|
|
typedef struct HCFGrammar_ {
|
2013-05-05 11:17:57 +02:00
|
|
|
HCFChoice *start; // start symbol (nonterminal)
|
2013-04-27 04:24:09 +02:00
|
|
|
HHashSet *nts; // HCFChoices, each representing the alternative
|
|
|
|
|
// productions for one nonterminal
|
|
|
|
|
HHashSet *geneps; // set of NTs that can generate the empty string
|
2013-04-30 20:17:51 +02:00
|
|
|
HHashTable *first; // memoized first sets of the grammar's symbols
|
|
|
|
|
HHashTable *follow; // memoized follow sets of the grammar's NTs
|
2013-04-27 04:24:09 +02:00
|
|
|
HArena *arena;
|
|
|
|
|
} HCFGrammar;
|
|
|
|
|
|
2013-05-07 18:47:32 +02:00
|
|
|
// mapping input bytes or end to tokens
|
|
|
|
|
// we want to use these, cast to void *, as elements in hashsets
|
|
|
|
|
// therefore we must avoid 0 as a token value because NULL means "not in set".
|
|
|
|
|
typedef uintptr_t HCFToken;
|
|
|
|
|
static inline HCFToken char_token(char c) { return (0x100 | c); }
|
|
|
|
|
static inline char token_char(HCFToken t) { return (0xFF & t); }
|
|
|
|
|
static HCFToken end_token = 0x200;
|
2013-04-30 17:49:35 +02:00
|
|
|
|
2013-04-27 23:20:13 +02:00
|
|
|
bool h_eq_ptr(const void *p, const void *q) { return (p==q); }
|
2013-04-27 04:24:09 +02:00
|
|
|
HHashValue h_hash_ptr(const void *p) { return (uintptr_t)p; }
|
|
|
|
|
|
2013-05-07 23:37:02 +02:00
|
|
|
HCFGrammar *h_cfgrammar_new(HAllocator *mm__)
|
2013-04-27 04:24:09 +02:00
|
|
|
{
|
|
|
|
|
HCFGrammar *g = h_new(HCFGrammar, 1);
|
|
|
|
|
assert(g != NULL);
|
|
|
|
|
|
|
|
|
|
g->arena = h_new_arena(mm__, 0); // default blocksize
|
2013-04-27 23:20:13 +02:00
|
|
|
g->nts = h_hashset_new(g->arena, h_eq_ptr, h_hash_ptr);
|
2013-04-27 04:24:09 +02:00
|
|
|
g->geneps = NULL;
|
2013-04-30 17:49:35 +02:00
|
|
|
g->first = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr);
|
2013-04-30 21:55:44 +02:00
|
|
|
g->follow = h_hashtable_new(g->arena, h_eq_ptr, h_hash_ptr);
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
return g;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-30 17:49:35 +02:00
|
|
|
|
|
|
|
|
// helper
|
2013-04-27 23:20:13 +02:00
|
|
|
static void collect_nts(HCFGrammar *grammar, HCFChoice *symbol);
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
/* Convert 'parser' into CFG representation by desugaring and compiling the set
|
|
|
|
|
* of nonterminals.
|
|
|
|
|
* A NULL return means we are unable to represent the parser as a CFG.
|
|
|
|
|
*/
|
2013-05-07 23:37:02 +02:00
|
|
|
HCFGrammar *h_cfgrammar(HAllocator* mm__, const HParser *parser)
|
2013-04-27 04:24:09 +02:00
|
|
|
{
|
|
|
|
|
// convert parser to CFG form ("desugar").
|
|
|
|
|
HCFChoice *desugared = h_desugar(mm__, parser);
|
|
|
|
|
if(desugared == NULL)
|
|
|
|
|
return NULL; // -> backend not suitable for this parser
|
|
|
|
|
|
2013-05-07 23:37:02 +02:00
|
|
|
HCFGrammar *g = h_cfgrammar_new(mm__);
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
// recursively traverse the desugared form and collect all HCFChoices that
|
2013-04-28 15:21:48 +02:00
|
|
|
// represent a nonterminal (type HCF_CHOICE or HCF_CHARSET).
|
2013-04-27 23:20:13 +02:00
|
|
|
collect_nts(g, desugared);
|
2013-04-27 04:24:09 +02:00
|
|
|
if(h_hashset_empty(g->nts)) {
|
2013-05-05 11:17:57 +02:00
|
|
|
// desugared is a terminal. wrap it in a singleton HCF_CHOICE.
|
2013-04-28 15:21:48 +02:00
|
|
|
HCFChoice *nt = h_new(HCFChoice, 1);
|
|
|
|
|
nt->type = HCF_CHOICE;
|
|
|
|
|
nt->seq = h_new(HCFSequence *, 2);
|
|
|
|
|
nt->seq[0] = h_new(HCFSequence, 1);
|
|
|
|
|
nt->seq[0]->items = h_new(HCFChoice *, 2);
|
|
|
|
|
nt->seq[0]->items[0] = desugared;
|
|
|
|
|
nt->seq[0]->items[1] = NULL;
|
|
|
|
|
nt->seq[1] = NULL;
|
|
|
|
|
h_hashset_put(g->nts, nt);
|
2013-05-05 11:17:57 +02:00
|
|
|
g->start = nt;
|
|
|
|
|
} else {
|
|
|
|
|
g->start = desugared;
|
2013-04-27 04:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-27 23:20:13 +02:00
|
|
|
// XXX call collect_geneps here?
|
|
|
|
|
|
2013-04-27 04:24:09 +02:00
|
|
|
return g;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add all nonterminals reachable from symbol to grammar. */
|
2013-04-27 23:20:13 +02:00
|
|
|
static void collect_nts(HCFGrammar *grammar, HCFChoice *symbol)
|
2013-04-27 04:24:09 +02:00
|
|
|
{
|
|
|
|
|
HCFSequence **s; // for the rhs (sentential form) of a production
|
|
|
|
|
HCFChoice **x; // for a symbol in s
|
|
|
|
|
|
|
|
|
|
if(h_hashset_present(grammar->nts, symbol))
|
|
|
|
|
return; // already visited, get out
|
|
|
|
|
|
|
|
|
|
switch(symbol->type) {
|
|
|
|
|
case HCF_CHAR:
|
|
|
|
|
case HCF_END:
|
|
|
|
|
break; // it's a terminal symbol, nothing to do
|
|
|
|
|
|
|
|
|
|
case HCF_CHARSET:
|
|
|
|
|
case HCF_CHOICE:
|
2013-05-05 19:34:12 +02:00
|
|
|
// exploiting the fact that HHashSet is also a HHashTable to number the
|
|
|
|
|
// nonterminals.
|
|
|
|
|
// NB top-level (start) symbol gets 0.
|
|
|
|
|
h_hashtable_put(grammar->nts, symbol,
|
|
|
|
|
(void *)(uintptr_t)grammar->nts->used);
|
|
|
|
|
|
|
|
|
|
if(symbol->type == HCF_CHOICE) {
|
|
|
|
|
// each element s of symbol->seq (HCFSequence) represents the RHS of
|
|
|
|
|
// a production. call self on all symbols (HCFChoice) in s.
|
|
|
|
|
for(s = symbol->seq; *s != NULL; s++) {
|
|
|
|
|
for(x = (*s)->items; *x != NULL; x++) {
|
|
|
|
|
collect_nts(grammar, *x);
|
|
|
|
|
}
|
2013-04-27 04:24:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2013-04-28 15:21:48 +02:00
|
|
|
default: // should not be reachable
|
2013-04-27 04:24:09 +02:00
|
|
|
assert_message(0, "unknown HCFChoice type");
|
|
|
|
|
}
|
2013-04-17 15:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-30 17:49:35 +02:00
|
|
|
|
|
|
|
|
// helper
|
|
|
|
|
static void collect_geneps(HCFGrammar *grammar);
|
|
|
|
|
|
2013-04-27 23:20:13 +02:00
|
|
|
/* Does the given symbol derive the empty string (under g)? */
|
2013-04-30 17:48:24 +02:00
|
|
|
bool h_symbol_derives_epsilon(HCFGrammar *g, const HCFChoice *symbol)
|
2013-04-27 23:20:13 +02:00
|
|
|
{
|
|
|
|
|
if(g->geneps == NULL)
|
|
|
|
|
collect_geneps(g);
|
|
|
|
|
assert(g->geneps != NULL);
|
|
|
|
|
|
|
|
|
|
switch(symbol->type) {
|
|
|
|
|
case HCF_END: // the end token doesn't count as empty
|
|
|
|
|
case HCF_CHAR:
|
|
|
|
|
case HCF_CHARSET:
|
|
|
|
|
return false;
|
|
|
|
|
default: // HCF_CHOICE
|
|
|
|
|
return h_hashset_present(g->geneps, symbol);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-30 20:16:16 +02:00
|
|
|
/* Does the sentential form s derive the empty string? s NULL-terminated. */
|
|
|
|
|
bool h_sequence_derives_epsilon(HCFGrammar *g, HCFChoice **s)
|
2013-04-27 23:20:13 +02:00
|
|
|
{
|
|
|
|
|
// return true iff all symbols in s derive epsilon
|
2013-04-30 20:16:16 +02:00
|
|
|
for(; *s; s++) {
|
|
|
|
|
if(!h_symbol_derives_epsilon(g, *s))
|
2013-04-27 23:20:13 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Populate the geneps member of g; no-op if called multiple times. */
|
|
|
|
|
static void collect_geneps(HCFGrammar *g)
|
|
|
|
|
{
|
2013-05-05 22:13:50 +02:00
|
|
|
if(g->geneps != NULL)
|
2013-04-27 23:20:13 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g->geneps = h_hashset_new(g->arena, h_eq_ptr, h_hash_ptr);
|
|
|
|
|
assert(g->geneps != NULL);
|
|
|
|
|
|
|
|
|
|
// iterate over the grammar's symbols, the elements of g->nts.
|
|
|
|
|
// add any we can identify as deriving epsilon to g->geneps.
|
|
|
|
|
// repeat until g->geneps no longer changes.
|
2013-05-05 22:13:50 +02:00
|
|
|
size_t prevused;
|
2013-04-27 23:20:13 +02:00
|
|
|
do {
|
2013-05-05 22:13:50 +02:00
|
|
|
prevused = g->geneps->used;
|
2013-04-27 23:20:13 +02:00
|
|
|
size_t i;
|
|
|
|
|
HHashTableEntry *hte;
|
|
|
|
|
for(i=0; i < g->nts->capacity; i++) {
|
|
|
|
|
for(hte = &g->nts->contents[i]; hte; hte = hte->next) {
|
2013-05-05 19:34:12 +02:00
|
|
|
if(hte->key == NULL)
|
|
|
|
|
continue;
|
2013-04-30 17:48:24 +02:00
|
|
|
const HCFChoice *symbol = hte->key;
|
2013-04-27 23:20:13 +02:00
|
|
|
|
2013-04-28 15:21:48 +02:00
|
|
|
// only "choice" nonterminals can derive epsilon.
|
2013-04-27 23:20:13 +02:00
|
|
|
if(symbol->type != HCF_CHOICE)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-04-28 17:04:49 +02:00
|
|
|
// this NT derives epsilon if any of its productions does.
|
2013-04-27 23:20:13 +02:00
|
|
|
HCFSequence **p;
|
|
|
|
|
for(p = symbol->seq; *p != NULL; p++) {
|
2013-04-30 20:16:16 +02:00
|
|
|
if(h_sequence_derives_epsilon(g, (*p)->items)) {
|
2013-05-05 22:13:50 +02:00
|
|
|
h_hashset_put(g->geneps, symbol);
|
2013-04-27 23:20:13 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-05 22:13:50 +02:00
|
|
|
} while(g->geneps->used != prevused);
|
2013-04-27 23:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-30 17:49:35 +02:00
|
|
|
|
2013-04-30 20:16:16 +02:00
|
|
|
/* Compute first set of sentential form s. s NULL-terminated. */
|
|
|
|
|
HHashSet *h_first_sequence(HCFGrammar *g, HCFChoice **s);
|
2013-04-30 17:49:35 +02:00
|
|
|
|
2013-04-30 20:16:16 +02:00
|
|
|
/* Compute first set of symbol x. Memoized. */
|
|
|
|
|
HHashSet *h_first_symbol(HCFGrammar *g, const HCFChoice *x)
|
2013-04-30 17:49:35 +02:00
|
|
|
{
|
|
|
|
|
HHashSet *ret;
|
|
|
|
|
HCFSequence **p;
|
|
|
|
|
uint8_t c;
|
|
|
|
|
|
|
|
|
|
// memoize via g->first
|
|
|
|
|
assert(g->first != NULL);
|
|
|
|
|
ret = h_hashtable_get(g->first, x);
|
|
|
|
|
if(ret != NULL)
|
|
|
|
|
return ret;
|
|
|
|
|
ret = h_hashset_new(g->arena, h_eq_ptr, h_hash_ptr);
|
|
|
|
|
assert(ret != NULL);
|
|
|
|
|
h_hashtable_put(g->first, x, ret);
|
|
|
|
|
|
|
|
|
|
switch(x->type) {
|
|
|
|
|
case HCF_END:
|
2013-05-07 18:47:32 +02:00
|
|
|
h_hashset_put(ret, (void *)end_token);
|
2013-04-30 17:49:35 +02:00
|
|
|
break;
|
|
|
|
|
case HCF_CHAR:
|
2013-05-07 18:47:32 +02:00
|
|
|
h_hashset_put(ret, (void *)char_token(x->chr));
|
2013-04-30 17:49:35 +02:00
|
|
|
break;
|
|
|
|
|
case HCF_CHARSET:
|
|
|
|
|
c=0;
|
|
|
|
|
do {
|
|
|
|
|
if(charset_isset(x->charset, c))
|
2013-05-07 18:47:32 +02:00
|
|
|
h_hashset_put(ret, (void *)char_token(c));
|
2013-04-30 17:49:35 +02:00
|
|
|
} while(c++ < 255);
|
|
|
|
|
break;
|
|
|
|
|
case HCF_CHOICE:
|
|
|
|
|
// this is a nonterminal
|
|
|
|
|
// return the union of the first sets of all productions
|
|
|
|
|
for(p=x->seq; *p; ++p)
|
2013-04-30 20:16:16 +02:00
|
|
|
h_hashset_put_all(ret, h_first_sequence(g, (*p)->items));
|
2013-04-30 17:49:35 +02:00
|
|
|
break;
|
|
|
|
|
default: // should not be reached
|
|
|
|
|
assert_message(0, "unknown HCFChoice type");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-30 20:16:16 +02:00
|
|
|
HHashSet *h_first_sequence(HCFGrammar *g, HCFChoice **s)
|
2013-04-30 17:49:35 +02:00
|
|
|
{
|
|
|
|
|
// the first set of the empty sequence is empty
|
|
|
|
|
if(*s == NULL)
|
|
|
|
|
return h_hashset_new(g->arena, h_eq_ptr, h_hash_ptr);
|
|
|
|
|
|
|
|
|
|
// first(X tail) = first(X) if X does not derive epsilon
|
|
|
|
|
// = first(X) u first(tail) otherwise
|
|
|
|
|
|
2013-04-30 20:16:16 +02:00
|
|
|
HCFChoice *x = s[0];
|
|
|
|
|
HCFChoice **tail = s+1;
|
2013-04-30 17:49:35 +02:00
|
|
|
|
|
|
|
|
HHashSet *first_x = h_first_symbol(g, x);
|
|
|
|
|
if(h_symbol_derives_epsilon(g, x)) {
|
|
|
|
|
// return the union of first(x) and first(tail)
|
2013-04-30 20:16:16 +02:00
|
|
|
HHashSet *first_tail = h_first_sequence(g, tail);
|
2013-04-30 17:49:35 +02:00
|
|
|
HHashSet *ret = h_hashset_new(g->arena, h_eq_ptr, h_hash_ptr);
|
|
|
|
|
h_hashset_put_all(ret, first_x);
|
|
|
|
|
h_hashset_put_all(ret, first_tail);
|
|
|
|
|
return ret;
|
|
|
|
|
} else {
|
|
|
|
|
return first_x;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
|
2013-04-30 20:17:51 +02:00
|
|
|
/* Compute follow set of symbol x. Memoized. */
|
|
|
|
|
HHashSet *h_follow(HCFGrammar *g, const HCFChoice *x)
|
|
|
|
|
{
|
|
|
|
|
// consider all occurances of X in g
|
|
|
|
|
// the follow set of X is the union of:
|
|
|
|
|
// given a production "A -> alpha X tail":
|
|
|
|
|
// if tail derives epsilon:
|
|
|
|
|
// first(tail) u follow(A)
|
|
|
|
|
// else:
|
|
|
|
|
// first(tail)
|
|
|
|
|
|
|
|
|
|
HHashSet *ret;
|
|
|
|
|
|
|
|
|
|
// memoize via g->follow
|
|
|
|
|
assert(g->follow != NULL);
|
|
|
|
|
ret = h_hashtable_get(g->follow, x);
|
|
|
|
|
if(ret != NULL)
|
|
|
|
|
return ret;
|
|
|
|
|
ret = h_hashset_new(g->arena, h_eq_ptr, h_hash_ptr);
|
|
|
|
|
assert(ret != NULL);
|
|
|
|
|
h_hashtable_put(g->follow, x, ret);
|
|
|
|
|
|
|
|
|
|
// 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) {
|
2013-05-05 19:34:12 +02:00
|
|
|
if(hte->key == NULL)
|
|
|
|
|
continue;
|
2013-04-30 20:17:51 +02:00
|
|
|
const HCFChoice *a = hte->key; // production's left-hand symbol
|
|
|
|
|
|
|
|
|
|
// X can only occur in a proper HCF_CHOICE
|
|
|
|
|
if(a->type != HCF_CHOICE) continue;
|
|
|
|
|
|
|
|
|
|
// iterate over the productions for A
|
|
|
|
|
HCFSequence **p;
|
|
|
|
|
for(p=a->seq; *p; p++) {
|
|
|
|
|
HCFChoice **s = (*p)->items; // production's right-hand side
|
|
|
|
|
|
|
|
|
|
for(; *s; s++) {
|
|
|
|
|
if(*s == x) { // occurance found
|
|
|
|
|
HCFChoice **tail = s+1;
|
|
|
|
|
|
|
|
|
|
h_hashset_put_all(ret, h_first_sequence(g, tail));
|
|
|
|
|
if(h_sequence_derives_epsilon(g, tail))
|
|
|
|
|
h_hashset_put_all(ret, h_follow(g, a));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-05-05 19:34:12 +02:00
|
|
|
static void pprint_char(FILE *f, char c)
|
|
|
|
|
{
|
|
|
|
|
switch(c) {
|
|
|
|
|
case '"': fputs("\\\"", f); break;
|
|
|
|
|
case '\\': fputs("\\\\", f); break;
|
|
|
|
|
case '\b': fputs("\\b", f); break;
|
|
|
|
|
case '\t': fputs("\\t", f); break;
|
|
|
|
|
case '\n': fputs("\\n", f); break;
|
|
|
|
|
case '\r': fputs("\\r", f); break;
|
|
|
|
|
default:
|
|
|
|
|
if(isprint(c)) {
|
|
|
|
|
fputc(c, f);
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(f, "\\x%.2X", c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pprint_charset_char(FILE *f, char c)
|
|
|
|
|
{
|
|
|
|
|
switch(c) {
|
|
|
|
|
case '"': fputc(c, f); break;
|
|
|
|
|
case '-': fputs("\\-", f); break;
|
|
|
|
|
case ']': fputs("\\-", f); break;
|
|
|
|
|
default: pprint_char(f, c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pprint_charset(FILE *f, const HCharset cs)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
fputc('[', f);
|
|
|
|
|
for(i=0; i<256; i++) {
|
|
|
|
|
if(charset_isset(cs, i))
|
|
|
|
|
pprint_charset_char(f, i);
|
|
|
|
|
|
|
|
|
|
// detect ranges
|
|
|
|
|
if(i+2<256 && charset_isset(cs, i+1) && charset_isset(cs, i+2)) {
|
|
|
|
|
fputc('-', f);
|
|
|
|
|
for(; i<256 && charset_isset(cs, i); i++);
|
|
|
|
|
i--; // back to the last in range
|
|
|
|
|
pprint_charset_char(f, i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fputc(']', f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
// find nt's number in g
|
|
|
|
|
size_t n = (uintptr_t)h_hashtable_get(g->nts, nt);
|
|
|
|
|
|
|
|
|
|
// NB the start symbol (number 0) is always "A".
|
|
|
|
|
int i;
|
|
|
|
|
for(i=14; i>=0 && (n>0 || i==14); i--) {
|
|
|
|
|
buf[i] = 'A' + n%26;
|
|
|
|
|
n = n/26; // shift one digit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf+i+1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static HCFChoice **pprint_string(FILE *f, HCFChoice **x)
|
|
|
|
|
{
|
|
|
|
|
fputc('"', f);
|
|
|
|
|
for(; *x; x++) {
|
|
|
|
|
if((*x)->type != HCF_CHAR)
|
|
|
|
|
break;
|
|
|
|
|
pprint_char(f, (*x)->chr);
|
|
|
|
|
}
|
|
|
|
|
fputc('"', f);
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-05 22:14:21 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-05 19:34:12 +02:00
|
|
|
static void pprint_sequence(FILE *f, const HCFGrammar *g, const HCFSequence *seq)
|
|
|
|
|
{
|
|
|
|
|
HCFChoice **x = seq->items;
|
|
|
|
|
|
|
|
|
|
if(*x == NULL) { // the empty sequence
|
|
|
|
|
fputs(" \"\"", f);
|
|
|
|
|
} else {
|
|
|
|
|
while(*x) {
|
|
|
|
|
fputc(' ', f); // separator
|
|
|
|
|
|
2013-05-05 22:13:50 +02:00
|
|
|
if((*x)->type == HCF_CHAR) {
|
|
|
|
|
// condense character strings
|
2013-05-05 19:34:12 +02:00
|
|
|
x = pprint_string(f, x);
|
2013-05-05 22:13:50 +02:00
|
|
|
} else {
|
|
|
|
|
pprint_symbol(f, g, *x);
|
2013-05-05 19:34:12 +02:00
|
|
|
x++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fputc('\n', f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
void pprint_ntrules(FILE *f, const HCFGrammar *g, const HCFChoice *nt,
|
|
|
|
|
int indent, int len)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int column = indent + len;
|
|
|
|
|
|
|
|
|
|
const char *name = nonterminal_name(g, nt);
|
|
|
|
|
|
|
|
|
|
// print rule head (symbol name)
|
|
|
|
|
for(i=0; i<indent; i++) fputc(' ', f);
|
|
|
|
|
fputs(name, f);
|
|
|
|
|
i += strlen(name);
|
|
|
|
|
for(; i<column; i++) fputc(' ', f);
|
|
|
|
|
fputs(" ->", f);
|
|
|
|
|
|
|
|
|
|
HCFSequence **p;
|
|
|
|
|
switch(nt->type) {
|
|
|
|
|
case HCF_CHARSET:
|
|
|
|
|
pprint_charset(f, nt->charset);
|
|
|
|
|
break;
|
|
|
|
|
case HCF_CHOICE:
|
|
|
|
|
p = nt->seq;
|
|
|
|
|
if(*p == NULL) break; // shouldn't happen
|
|
|
|
|
pprint_sequence(f, g, *p++); // print first production on the same line
|
|
|
|
|
for(; *p; p++) { // print the rest below with "or" bars
|
|
|
|
|
for(i=0; i<column; i++) fputc(' ', f); // indent
|
|
|
|
|
fputs(" |", f);
|
|
|
|
|
pprint_sequence(f, g, *p);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default: // should not be reached
|
|
|
|
|
fputs(" ???\n", f);
|
|
|
|
|
assert_message(0, "unexpected nonterminal type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void h_pprint_grammar(FILE *file, const HCFGrammar *g, int indent)
|
|
|
|
|
{
|
|
|
|
|
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 *a = hte->key; // production's left-hand symbol
|
|
|
|
|
|
|
|
|
|
pprint_ntrules(file, g, a, indent, len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-05 22:14:21 +02:00
|
|
|
void h_pprint_symbolset(FILE *file, const HCFGrammar *g, const HHashSet *set, int indent)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for(j=0; j<indent; j++) fputc(' ', file);
|
|
|
|
|
|
|
|
|
|
fputc('{', file);
|
|
|
|
|
|
|
|
|
|
// iterate over set
|
|
|
|
|
size_t i;
|
|
|
|
|
HHashTableEntry *hte;
|
2013-05-07 18:51:42 +02:00
|
|
|
const HCFChoice *a = NULL;
|
2013-05-05 22:14:21 +02:00
|
|
|
for(i=0; i < set->capacity; i++) {
|
|
|
|
|
for(hte = &set->contents[i]; hte; hte = hte->next) {
|
|
|
|
|
if(hte->key == NULL)
|
|
|
|
|
continue;
|
2013-05-07 18:51:42 +02:00
|
|
|
if(a != NULL) // we're not on the first element
|
|
|
|
|
fputc(',', file);
|
|
|
|
|
|
|
|
|
|
a = hte->key; // production's left-hand symbol
|
2013-05-05 22:14:21 +02:00
|
|
|
|
|
|
|
|
pprint_symbol(file, g, a);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fputs("}\n", file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void h_pprint_tokenset(FILE *file, const HCFGrammar *g, const HHashSet *set, int indent)
|
|
|
|
|
{
|
|
|
|
|
int j;
|
|
|
|
|
for(j=0; j<indent; j++) fputc(' ', file);
|
|
|
|
|
|
|
|
|
|
fputc('[', file);
|
|
|
|
|
|
|
|
|
|
// iterate over set
|
|
|
|
|
size_t i;
|
|
|
|
|
HHashTableEntry *hte;
|
|
|
|
|
for(i=0; i < set->capacity; i++) {
|
|
|
|
|
for(hte = &set->contents[i]; hte; hte = hte->next) {
|
|
|
|
|
if(hte->key == NULL)
|
|
|
|
|
continue;
|
2013-05-07 18:47:32 +02:00
|
|
|
HCFToken a = (HCFToken)hte->key;
|
2013-05-05 22:14:21 +02:00
|
|
|
|
2013-05-07 18:47:32 +02:00
|
|
|
if(a == end_token)
|
2013-05-05 22:14:21 +02:00
|
|
|
fputc('$', file);
|
2013-05-07 18:47:32 +02:00
|
|
|
else if(token_char(a) == '$')
|
|
|
|
|
fputs("\\$", file);
|
2013-05-05 22:14:21 +02:00
|
|
|
else
|
2013-05-07 18:47:32 +02:00
|
|
|
pprint_char(file, token_char(a));
|
2013-05-05 22:14:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fputs("]\n", file);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
/* LL parse table and associated data */
|
|
|
|
|
|
|
|
|
|
typedef struct HLLTable_ {
|
|
|
|
|
unsigned int **arr; // Nonterminals numbered from 1, 0 = error.
|
|
|
|
|
} HLLTable;
|
|
|
|
|
|
|
|
|
|
typedef struct HLLData_ {
|
|
|
|
|
HCFGrammar *grammar;
|
|
|
|
|
HLLTable *table;
|
|
|
|
|
} HLLData;
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
/* Interface to look up an entry in the parse table. */
|
|
|
|
|
unsigned int h_ll_lookup(const HLLTable *table, unsigned int nonterminal, uint8_t token)
|
|
|
|
|
{
|
|
|
|
|
assert(nonterminal > 0);
|
|
|
|
|
return table->arr[n*257+token];
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// XXX predict_set
|
|
|
|
|
|
|
|
|
|
int h_ll_compile(HAllocator* mm__, const HParser* parser, const void* params)
|
|
|
|
|
{
|
|
|
|
|
// Convert parser to a CFG. This can fail as indicated by a NULL return.
|
2013-05-07 23:37:02 +02:00
|
|
|
HCFGrammar *grammar = h_cfgrammar(mm__, parser);
|
2013-04-27 04:24:09 +02:00
|
|
|
if(grammar == NULL)
|
|
|
|
|
return -1; // -> Backend unsuitable for this parser.
|
|
|
|
|
|
|
|
|
|
// TODO: eliminate common prefixes
|
|
|
|
|
// TODO: eliminate left recursion
|
|
|
|
|
// TODO: avoid conflicts by splitting occurances?
|
|
|
|
|
|
|
|
|
|
// XXX generate table and store in parser->data.
|
|
|
|
|
// XXX any other data structures needed?
|
|
|
|
|
|
|
|
|
|
return -1; // XXX 0 on success
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* LL driver */
|
|
|
|
|
|
|
|
|
|
HParseResult *h_ll_parse(HAllocator* mm__, const HParser* parser, HParseState* parse_state)
|
|
|
|
|
{
|
|
|
|
|
// get table from parser->data.
|
|
|
|
|
// run driver.
|
2013-04-17 15:10:33 +02:00
|
|
|
return NULL; // TODO
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
|
2013-04-17 15:10:33 +02:00
|
|
|
HParserBackendVTable h__ll_backend_vtable = {
|
|
|
|
|
.compile = h_ll_compile,
|
|
|
|
|
.parse = h_ll_parse
|
|
|
|
|
};
|
2013-05-05 22:15:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// dummy!
|
|
|
|
|
int test_ll(void)
|
|
|
|
|
{
|
|
|
|
|
const HParser *c = h_many(h_ch('x'));
|
|
|
|
|
const HParser *q = h_sequence(c, h_ch('y'), NULL);
|
|
|
|
|
const HParser *p = h_choice(q, h_end_p(), NULL);
|
|
|
|
|
|
2013-05-07 23:37:02 +02:00
|
|
|
HCFGrammar *g = h_cfgrammar(&system_allocator, p);
|
2013-05-05 22:15:40 +02:00
|
|
|
|
|
|
|
|
if(g == NULL) {
|
2013-05-07 23:37:02 +02:00
|
|
|
fprintf(stderr, "h_cfgrammar failed\n");
|
2013-05-05 22:15:40 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h_pprint_grammar(stdout, g, 0);
|
|
|
|
|
collect_geneps(g);
|
|
|
|
|
printf("generate epsilon: ");
|
|
|
|
|
h_pprint_symbolset(stdout, g, g->geneps, 0);
|
|
|
|
|
printf("first(A) = ");
|
|
|
|
|
h_pprint_tokenset(stdout, g, h_first_symbol(g, g->start), 0);
|
|
|
|
|
printf("follow(C) = ");
|
|
|
|
|
h_pprint_tokenset(stdout, g, h_follow(g, h_desugar(&system_allocator, c)), 0);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|