2013-04-17 15:10:33 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include "../internal.h"
|
2013-05-07 23:56:47 +02:00
|
|
|
#include "../cfgrammar.h"
|
2013-04-17 15:10:33 +02:00
|
|
|
#include "../parsers/parser_internal.h"
|
|
|
|
|
|
2013-05-23 23:25:32 +02:00
|
|
|
static const size_t DEFAULT_KMAX = 1;
|
|
|
|
|
|
2013-04-27 04:24:09 +02:00
|
|
|
|
2013-05-11 19:04:59 +02:00
|
|
|
/* Generating the LL(k) parse table */
|
2013-04-27 04:24:09 +02:00
|
|
|
|
2013-06-04 21:47:09 +02:00
|
|
|
/* Maps each nonterminal (HCFChoice) of the grammar to a HStringMap that
|
2013-05-23 21:01:37 +02:00
|
|
|
* maps lookahead strings to productions (HCFSequence).
|
2013-05-08 17:04:19 +02:00
|
|
|
*/
|
2013-05-11 19:04:59 +02:00
|
|
|
typedef struct HLLkTable_ {
|
2013-05-08 18:04:08 +02:00
|
|
|
HHashTable *rows;
|
2013-05-11 15:14:10 +02:00
|
|
|
HCFChoice *start; // start symbol
|
2013-05-08 18:04:08 +02:00
|
|
|
HArena *arena;
|
|
|
|
|
HAllocator *mm__;
|
2013-05-11 19:04:59 +02:00
|
|
|
} HLLkTable;
|
2013-04-27 04:24:09 +02:00
|
|
|
|
2013-05-22 20:38:36 +02:00
|
|
|
|
2013-04-27 04:24:09 +02:00
|
|
|
/* Interface to look up an entry in the parse table. */
|
2013-05-20 21:28:16 +02:00
|
|
|
const HCFSequence *h_llk_lookup(const HLLkTable *table, const HCFChoice *x,
|
2013-06-21 23:22:07 +02:00
|
|
|
const HInputStream *stream)
|
2013-04-27 04:24:09 +02:00
|
|
|
{
|
2013-06-04 21:47:09 +02:00
|
|
|
const HStringMap *row = h_hashtable_get(table->rows, x);
|
2013-05-08 17:04:19 +02:00
|
|
|
assert(row != NULL); // the table should have one row for each nonterminal
|
|
|
|
|
|
2013-05-23 21:01:37 +02:00
|
|
|
assert(!row->epsilon_branch); // would match without looking at the input
|
|
|
|
|
// XXX cases where this could be useful?
|
|
|
|
|
|
2013-06-21 23:22:07 +02:00
|
|
|
return h_stringmap_get_lookahead(row, *stream);
|
2013-04-27 04:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-08 18:04:08 +02:00
|
|
|
/* Allocate a new parse table. */
|
2013-05-11 19:04:59 +02:00
|
|
|
HLLkTable *h_llktable_new(HAllocator *mm__)
|
2013-05-08 18:04:08 +02:00
|
|
|
{
|
|
|
|
|
// NB the parse table gets an arena separate from the grammar so we can free
|
|
|
|
|
// the latter after table generation.
|
|
|
|
|
HArena *arena = h_new_arena(mm__, 0); // default blocksize
|
|
|
|
|
assert(arena != NULL);
|
|
|
|
|
HHashTable *rows = h_hashtable_new(arena, h_eq_ptr, h_hash_ptr);
|
|
|
|
|
assert(rows != NULL);
|
|
|
|
|
|
2013-05-11 19:04:59 +02:00
|
|
|
HLLkTable *table = h_new(HLLkTable, 1);
|
2013-05-08 18:04:08 +02:00
|
|
|
assert(table != NULL);
|
|
|
|
|
table->mm__ = mm__;
|
|
|
|
|
table->arena = arena;
|
|
|
|
|
table->rows = rows;
|
|
|
|
|
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-11 19:04:59 +02:00
|
|
|
void h_llktable_free(HLLkTable *table)
|
2013-05-08 18:04:08 +02:00
|
|
|
{
|
2013-05-11 22:22:02 +02:00
|
|
|
if(table == NULL)
|
|
|
|
|
return;
|
2013-05-08 18:04:08 +02:00
|
|
|
HAllocator *mm__ = table->mm__;
|
|
|
|
|
h_delete_arena(table->arena);
|
|
|
|
|
h_free(table);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-23 21:01:37 +02:00
|
|
|
void *const CONFLICT = (void *)(uintptr_t)(-1);
|
|
|
|
|
|
2013-05-23 22:19:13 +02:00
|
|
|
// helper for stringmap_merge
|
|
|
|
|
static void *combine_entries(HHashSet *workset, void *dst, const void *src)
|
2013-05-08 18:49:05 +02:00
|
|
|
{
|
2013-05-23 22:19:13 +02:00
|
|
|
assert(dst != NULL);
|
|
|
|
|
assert(src != NULL);
|
|
|
|
|
|
2013-05-23 21:01:37 +02:00
|
|
|
if(dst == CONFLICT) { // previous conflict
|
2013-05-23 22:19:13 +02:00
|
|
|
h_hashset_put(workset, src);
|
2013-05-24 01:31:56 +02:00
|
|
|
} else if(dst != src) { // new conflict
|
2013-05-23 22:19:13 +02:00
|
|
|
h_hashset_put(workset, dst);
|
|
|
|
|
h_hashset_put(workset, src);
|
2013-05-23 21:01:37 +02:00
|
|
|
dst = CONFLICT;
|
|
|
|
|
}
|
2013-05-23 22:19:13 +02:00
|
|
|
|
2013-05-23 21:01:37 +02:00
|
|
|
return dst;
|
|
|
|
|
}
|
2013-05-08 18:49:05 +02:00
|
|
|
|
2013-05-23 22:19:13 +02:00
|
|
|
// add the mappings of src to dst, marking conflicts and adding the conflicting
|
|
|
|
|
// values to workset.
|
2013-06-04 21:47:09 +02:00
|
|
|
static void stringmap_merge(HHashSet *workset, HStringMap *dst, HStringMap *src)
|
2013-05-23 21:01:37 +02:00
|
|
|
{
|
|
|
|
|
if(src->epsilon_branch) {
|
|
|
|
|
if(dst->epsilon_branch)
|
2013-05-23 22:19:13 +02:00
|
|
|
dst->epsilon_branch =
|
2014-03-24 17:30:23 +01:00
|
|
|
combine_entries(workset, dst->epsilon_branch, src->epsilon_branch);
|
2013-05-23 21:01:37 +02:00
|
|
|
else
|
|
|
|
|
dst->epsilon_branch = src->epsilon_branch;
|
2013-05-24 01:49:39 +02:00
|
|
|
} else {
|
|
|
|
|
// if there is a non-conflicting value on the left (dst) side, it means
|
|
|
|
|
// that prediction is already unambiguous. we can drop the right (src)
|
|
|
|
|
// side we were going to extend with.
|
|
|
|
|
if(dst->epsilon_branch && dst->epsilon_branch != CONFLICT)
|
|
|
|
|
return;
|
2013-05-23 21:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(src->end_branch) {
|
|
|
|
|
if(dst->end_branch)
|
2013-05-23 22:19:13 +02:00
|
|
|
dst->end_branch =
|
2014-03-24 17:30:23 +01:00
|
|
|
combine_entries(workset, dst->end_branch, src->end_branch);
|
2013-05-23 21:01:37 +02:00
|
|
|
else
|
|
|
|
|
dst->end_branch = src->end_branch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// iterate over src->char_branches
|
|
|
|
|
const HHashTable *ht = src->char_branches;
|
|
|
|
|
for(size_t i=0; i < ht->capacity; i++) {
|
|
|
|
|
for(HHashTableEntry *hte = &ht->contents[i]; hte; hte = hte->next) {
|
2013-05-08 18:49:05 +02:00
|
|
|
if(hte->key == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-05-23 21:01:37 +02:00
|
|
|
HCharKey c = (HCharKey)hte->key;
|
2013-06-04 21:47:09 +02:00
|
|
|
HStringMap *src_ = hte->value;
|
2013-05-08 18:49:05 +02:00
|
|
|
|
2013-05-23 21:01:37 +02:00
|
|
|
if(src_) {
|
2013-06-04 21:47:09 +02:00
|
|
|
HStringMap *dst_ = h_hashtable_get(dst->char_branches, (void *)c);
|
2014-03-24 17:22:52 +01:00
|
|
|
if(dst_) {
|
2013-05-23 22:19:13 +02:00
|
|
|
stringmap_merge(workset, dst_, src_);
|
2014-03-24 17:22:52 +01:00
|
|
|
} else {
|
2014-03-24 17:30:23 +01:00
|
|
|
if(src_->arena != dst->arena)
|
|
|
|
|
src_ = h_stringmap_copy(dst->arena, src_);
|
2013-05-23 23:25:32 +02:00
|
|
|
h_hashtable_put(dst->char_branches, (void *)c, src_);
|
2014-03-24 17:30:23 +01:00
|
|
|
}
|
2013-05-23 21:01:37 +02:00
|
|
|
}
|
2013-05-08 18:49:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-05-23 21:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 22:40:00 +02:00
|
|
|
/* Generate entries for the productions of A in the given table row. */
|
2013-06-04 21:47:09 +02:00
|
|
|
static int fill_table_row(size_t kmax, HCFGrammar *g, HStringMap *row,
|
2013-05-23 22:19:13 +02:00
|
|
|
const HCFChoice *A)
|
2013-05-23 21:01:37 +02:00
|
|
|
{
|
2013-05-23 22:54:49 +02:00
|
|
|
HHashSet *workset;
|
2013-05-23 22:19:13 +02:00
|
|
|
|
|
|
|
|
// initialize working set to the productions of A
|
|
|
|
|
workset = h_hashset_new(g->arena, h_eq_ptr, h_hash_ptr);
|
|
|
|
|
for(HCFSequence **s = A->seq; *s; s++)
|
|
|
|
|
h_hashset_put(workset, *s);
|
|
|
|
|
|
|
|
|
|
// run until workset exhausted or kmax hit
|
|
|
|
|
size_t k;
|
|
|
|
|
for(k=1; k<=kmax; k++) {
|
2013-05-23 22:54:49 +02:00
|
|
|
// allocate a fresh workset for the next round
|
|
|
|
|
HHashSet *nextset = h_hashset_new(g->arena, h_eq_ptr, h_hash_ptr);
|
|
|
|
|
|
|
|
|
|
// iterate over the productions in workset...
|
2013-05-23 22:19:13 +02:00
|
|
|
const HHashTable *ht = workset;
|
|
|
|
|
for(size_t i=0; i < ht->capacity; i++) {
|
|
|
|
|
for(HHashTableEntry *hte = &ht->contents[i]; hte; hte = hte->next) {
|
|
|
|
|
if(hte->key == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
HCFSequence *rhs = (void *)hte->key;
|
|
|
|
|
assert(rhs != NULL);
|
|
|
|
|
assert(rhs != CONFLICT); // just to be sure there's no mixup
|
|
|
|
|
|
|
|
|
|
// calculate predict set; let values map to rhs
|
2013-06-04 21:47:09 +02:00
|
|
|
HStringMap *pred = h_predict(k, g, A, rhs);
|
2013-05-23 22:19:13 +02:00
|
|
|
h_stringmap_replace(pred, NULL, rhs);
|
2013-05-23 21:01:37 +02:00
|
|
|
|
2013-05-23 22:54:49 +02:00
|
|
|
// merge predict set into the row
|
|
|
|
|
// accumulates conflicts in new workset
|
|
|
|
|
stringmap_merge(nextset, row, pred);
|
2013-05-23 22:19:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-05-23 21:01:37 +02:00
|
|
|
|
2013-05-23 22:54:49 +02:00
|
|
|
// switch to the updated workset
|
2013-05-24 04:33:27 +02:00
|
|
|
h_hashset_free(workset);
|
2013-05-23 22:54:49 +02:00
|
|
|
workset = nextset;
|
|
|
|
|
|
2013-05-23 22:19:13 +02:00
|
|
|
// if the workset is empty, row is without conflict; we're done
|
|
|
|
|
if(h_hashset_empty(workset))
|
|
|
|
|
break;
|
2013-05-23 21:01:37 +02:00
|
|
|
|
2013-05-23 22:19:13 +02:00
|
|
|
// clear conflict markers for next iteration
|
|
|
|
|
h_stringmap_replace(row, CONFLICT, NULL);
|
2013-05-23 21:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-23 22:54:49 +02:00
|
|
|
h_hashset_free(workset);
|
|
|
|
|
return (k>kmax)? -1 : 0;
|
2013-05-08 18:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-11 19:04:59 +02:00
|
|
|
/* Generate the LL(k) parse table from the given grammar.
|
2013-05-08 18:04:08 +02:00
|
|
|
* Returns -1 on error, 0 on success.
|
|
|
|
|
*/
|
2013-05-23 22:19:13 +02:00
|
|
|
static int fill_table(size_t kmax, HCFGrammar *g, HLLkTable *table)
|
2013-05-08 18:04:08 +02:00
|
|
|
{
|
2013-05-11 15:14:10 +02:00
|
|
|
table->start = g->start;
|
|
|
|
|
|
2013-05-08 18:49:05 +02:00
|
|
|
// 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
|
2013-05-14 16:15:58 +02:00
|
|
|
assert(a->type == HCF_CHOICE);
|
2013-05-08 18:49:05 +02:00
|
|
|
|
|
|
|
|
// create table row for this nonterminal
|
2013-06-04 21:47:09 +02:00
|
|
|
HStringMap *row = h_stringmap_new(table->arena);
|
2013-05-08 18:49:05 +02:00
|
|
|
h_hashtable_put(table->rows, a, row);
|
|
|
|
|
|
2013-05-23 22:19:13 +02:00
|
|
|
if(fill_table_row(kmax, g, row, a) < 0) {
|
2013-05-23 21:01:37 +02:00
|
|
|
// unresolvable conflicts in row
|
2013-05-11 19:04:59 +02:00
|
|
|
// NB we don't worry about deallocating anything, h_llk_compile will
|
2013-05-08 18:49:05 +02:00
|
|
|
// delete the whole arena for us.
|
2013-05-23 21:01:37 +02:00
|
|
|
return -1;
|
2013-05-08 18:49:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2013-05-08 18:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-11 19:04:59 +02:00
|
|
|
int h_llk_compile(HAllocator* mm__, HParser* parser, const void* params)
|
2013-04-27 04:24:09 +02:00
|
|
|
{
|
2013-05-23 22:19:13 +02:00
|
|
|
size_t kmax = params? (uintptr_t)params : DEFAULT_KMAX;
|
|
|
|
|
assert(kmax>0);
|
2013-05-23 21:01:37 +02:00
|
|
|
|
2013-04-27 04:24:09 +02:00
|
|
|
// 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?
|
|
|
|
|
|
2013-05-11 22:02:59 +02:00
|
|
|
// generate table and store in parser->backend_data.
|
2013-05-11 19:04:59 +02:00
|
|
|
HLLkTable *table = h_llktable_new(mm__);
|
2013-05-23 22:19:13 +02:00
|
|
|
if(fill_table(kmax, grammar, table) < 0) {
|
2013-05-08 18:04:08 +02:00
|
|
|
// the table was ambiguous
|
|
|
|
|
h_cfgrammar_free(grammar);
|
2013-05-11 19:04:59 +02:00
|
|
|
h_llktable_free(table);
|
2013-05-08 18:04:08 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-05-11 22:02:59 +02:00
|
|
|
parser->backend_data = table;
|
2013-05-08 18:04:08 +02:00
|
|
|
|
|
|
|
|
// free grammar and its arena.
|
|
|
|
|
// desugared parsers (HCFChoice and HCFSequence) are unaffected by this.
|
|
|
|
|
h_cfgrammar_free(grammar);
|
2013-04-27 04:24:09 +02:00
|
|
|
|
2013-05-08 18:04:08 +02:00
|
|
|
return 0;
|
2013-04-27 04:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-11 22:02:59 +02:00
|
|
|
void h_llk_free(HParser *parser)
|
|
|
|
|
{
|
|
|
|
|
HLLkTable *table = parser->backend_data;
|
|
|
|
|
h_llktable_free(table);
|
|
|
|
|
parser->backend_data = NULL;
|
|
|
|
|
parser->backend = PB_PACKRAT;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
|
2013-05-11 19:04:59 +02:00
|
|
|
/* LL(k) driver */
|
2013-04-27 04:24:09 +02:00
|
|
|
|
2015-09-03 16:24:47 +02:00
|
|
|
typedef struct {
|
|
|
|
|
HArena *arena; // will hold the results
|
|
|
|
|
HArena *tarena; // tmp, deleted after parse
|
|
|
|
|
HSlist *stack;
|
|
|
|
|
HCountedArray *seq; // accumulates current parse result
|
|
|
|
|
} HLLkState;
|
|
|
|
|
|
|
|
|
|
// in order to construct the parse tree, we delimit the symbol stack into
|
|
|
|
|
// frames corresponding to production right-hand sides. since only left-most
|
|
|
|
|
// derivations are produced this linearization is unique.
|
|
|
|
|
// the 'mark' allocated below simply reserves a memory address to use as the
|
|
|
|
|
// frame delimiter.
|
|
|
|
|
// nonterminals, instead of being popped and forgotten, are put back onto the
|
|
|
|
|
// stack below the mark to tell us which validations and semantic actions to
|
|
|
|
|
// execute on their corresponding result.
|
|
|
|
|
// also on the stack below the mark, we store the previously accumulated
|
|
|
|
|
// value for the surrounding production.
|
|
|
|
|
static int dummy;
|
|
|
|
|
static void *MARK = &dummy; // stack frame delimiter
|
|
|
|
|
|
|
|
|
|
static HLLkState *llk_parse_start_(HAllocator* mm__, const HParser* parser)
|
2013-04-27 04:24:09 +02:00
|
|
|
{
|
2013-05-11 22:02:59 +02:00
|
|
|
const HLLkTable *table = parser->backend_data;
|
2013-05-11 21:41:53 +02:00
|
|
|
assert(table != NULL);
|
|
|
|
|
|
2015-09-03 16:24:47 +02:00
|
|
|
HLLkState *s = h_new(HLLkState, 1);
|
|
|
|
|
s->arena = h_new_arena(mm__, 0);
|
|
|
|
|
s->tarena = h_new_arena(mm__, 0);
|
|
|
|
|
s->stack = h_slist_new(s->tarena);
|
|
|
|
|
s->seq = h_carray_new(s->arena);
|
2013-05-11 15:14:10 +02:00
|
|
|
|
|
|
|
|
// initialize with the start symbol on the stack.
|
2015-09-03 16:24:47 +02:00
|
|
|
h_slist_push(s->stack, table->start);
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 21:05:56 +02:00
|
|
|
// returns partial result or NULL (no parse)
|
2015-09-03 16:24:47 +02:00
|
|
|
static HCountedArray *llk_parse_chunk_(HLLkState *s, const HParser* parser,
|
2015-09-04 12:55:37 +02:00
|
|
|
HInputStream* stream)
|
2015-09-03 16:24:47 +02:00
|
|
|
{
|
|
|
|
|
HParsedToken *tok = NULL; // will hold result token
|
|
|
|
|
HCFChoice *x = NULL; // current symbol (from top of stack)
|
|
|
|
|
|
|
|
|
|
const HLLkTable *table = parser->backend_data;
|
|
|
|
|
assert(table != NULL);
|
|
|
|
|
|
|
|
|
|
HArena *arena = s->arena;
|
|
|
|
|
HArena *tarena = s->tarena;
|
|
|
|
|
HSlist *stack = s->stack;
|
|
|
|
|
HCountedArray *seq = s->seq;
|
|
|
|
|
|
|
|
|
|
if(!seq)
|
|
|
|
|
return NULL; // parse already failed
|
2013-05-11 15:14:10 +02:00
|
|
|
|
|
|
|
|
// when we empty the stack, the parse is complete.
|
|
|
|
|
while(!h_slist_empty(stack)) {
|
2015-09-04 21:05:56 +02:00
|
|
|
tok = NULL;
|
|
|
|
|
|
2013-05-11 20:40:33 +02:00
|
|
|
// pop top of stack for inspection
|
2015-09-03 16:24:47 +02:00
|
|
|
x = h_slist_pop(stack);
|
2013-05-11 15:14:10 +02:00
|
|
|
assert(x != NULL);
|
2013-05-11 19:26:22 +02:00
|
|
|
|
2015-09-03 16:24:47 +02:00
|
|
|
if(x != MARK && x->type == HCF_CHOICE) {
|
2013-05-11 20:40:33 +02:00
|
|
|
// x is a nonterminal; apply the appropriate production and continue
|
2013-05-11 15:14:10 +02:00
|
|
|
|
|
|
|
|
// look up applicable production in parse table
|
2013-06-21 23:22:07 +02:00
|
|
|
const HCFSequence *p = h_llk_lookup(table, x, stream);
|
2013-05-11 21:41:53 +02:00
|
|
|
if(p == NULL)
|
|
|
|
|
goto no_parse;
|
2015-09-04 21:05:56 +02:00
|
|
|
if(p == H_NEED_INPUT)
|
|
|
|
|
goto need_input;
|
2013-05-11 15:14:10 +02:00
|
|
|
|
2013-05-24 14:08:13 +02:00
|
|
|
// an infinite loop case that shouldn't happen
|
|
|
|
|
assert(!p->items[0] || p->items[0] != x);
|
|
|
|
|
|
2015-09-04 21:05:56 +02:00
|
|
|
// push stack frame
|
|
|
|
|
h_slist_push(stack, seq); // save current partial value
|
|
|
|
|
h_slist_push(stack, x); // save the nonterminal
|
|
|
|
|
h_slist_push(stack, MARK); // frame delimiter
|
|
|
|
|
|
|
|
|
|
// open a fresh result sequence
|
|
|
|
|
seq = h_carray_new(arena);
|
|
|
|
|
|
2013-05-11 15:14:10 +02:00
|
|
|
// push production's rhs onto the stack (in reverse order)
|
|
|
|
|
HCFChoice **s;
|
|
|
|
|
for(s = p->items; *s; s++);
|
|
|
|
|
for(s--; s >= p->items; s--)
|
|
|
|
|
h_slist_push(stack, *s);
|
2013-05-11 20:40:33 +02:00
|
|
|
|
|
|
|
|
continue; // no result to record
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the top of stack is such that there will be a result...
|
2013-06-04 21:59:39 +02:00
|
|
|
tok = h_arena_malloc(arena, sizeof(HParsedToken));
|
|
|
|
|
tok->index = stream->index;
|
|
|
|
|
tok->bit_offset = stream->bit_offset;
|
2015-09-03 16:24:47 +02:00
|
|
|
if(x == MARK) {
|
2013-05-11 20:40:33 +02:00
|
|
|
// hit stack frame boundary...
|
|
|
|
|
// wrap the accumulated parse result, this sequence is finished
|
|
|
|
|
tok->token_type = TT_SEQUENCE;
|
|
|
|
|
tok->seq = seq;
|
|
|
|
|
|
|
|
|
|
// recover original nonterminal and result sequence
|
|
|
|
|
x = h_slist_pop(stack);
|
|
|
|
|
seq = h_slist_pop(stack);
|
|
|
|
|
// tok becomes next left-most element of higher-level sequence
|
2013-05-11 15:14:10 +02:00
|
|
|
}
|
2013-05-11 19:26:22 +02:00
|
|
|
else {
|
2013-05-11 20:40:33 +02:00
|
|
|
// x is a terminal or simple charset; match against input
|
2013-05-11 15:14:10 +02:00
|
|
|
|
|
|
|
|
// consume the input token
|
2013-05-20 21:28:16 +02:00
|
|
|
uint8_t input = h_read_bits(stream, 8, false);
|
2013-05-11 15:14:10 +02:00
|
|
|
|
|
|
|
|
switch(x->type) {
|
|
|
|
|
case HCF_END:
|
2013-05-20 21:28:16 +02:00
|
|
|
if(!stream->overrun)
|
2013-05-11 19:04:59 +02:00
|
|
|
goto no_parse;
|
2015-09-04 12:55:37 +02:00
|
|
|
if(!stream->last_chunk)
|
2015-09-03 16:24:47 +02:00
|
|
|
goto need_input;
|
2013-06-04 21:59:39 +02:00
|
|
|
h_arena_free(arena, tok);
|
2013-05-11 15:14:10 +02:00
|
|
|
tok = NULL;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case HCF_CHAR:
|
2015-08-25 14:14:48 +02:00
|
|
|
if(stream->overrun)
|
2015-09-03 16:24:47 +02:00
|
|
|
goto need_input;
|
2013-05-20 21:28:16 +02:00
|
|
|
if(input != x->chr)
|
2013-05-11 19:04:59 +02:00
|
|
|
goto no_parse;
|
2013-05-11 15:14:10 +02:00
|
|
|
tok->token_type = TT_UINT;
|
|
|
|
|
tok->uint = x->chr;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case HCF_CHARSET:
|
2013-05-20 21:28:16 +02:00
|
|
|
if(stream->overrun)
|
2015-09-03 16:24:47 +02:00
|
|
|
goto need_input;
|
2013-05-20 21:28:16 +02:00
|
|
|
if(!charset_isset(x->charset, input))
|
2013-05-11 19:04:59 +02:00
|
|
|
goto no_parse;
|
2013-05-11 15:14:10 +02:00
|
|
|
tok->token_type = TT_UINT;
|
2013-05-20 21:28:16 +02:00
|
|
|
tok->uint = input;
|
2013-05-11 15:14:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: // should not be reached
|
|
|
|
|
assert_message(0, "unknown HCFChoice type");
|
2013-05-11 19:04:59 +02:00
|
|
|
goto no_parse;
|
2013-05-11 15:14:10 +02:00
|
|
|
}
|
2013-05-11 20:40:33 +02:00
|
|
|
}
|
2013-05-11 15:14:10 +02:00
|
|
|
|
2013-05-11 20:40:33 +02:00
|
|
|
// 'tok' has been parsed; process it
|
2013-05-11 15:14:10 +02:00
|
|
|
|
2013-05-14 11:54:43 +02:00
|
|
|
// perform token reshape if indicated
|
|
|
|
|
if(x->reshape)
|
2013-11-23 12:53:11 -06:00
|
|
|
tok = (HParsedToken *)x->reshape(make_result(arena, tok), x->user_data);
|
2013-05-14 11:54:43 +02:00
|
|
|
|
2013-05-11 20:40:33 +02:00
|
|
|
// call validation and semantic action, if present
|
2013-11-23 13:01:55 -06:00
|
|
|
if(x->pred && !x->pred(make_result(tarena, tok), x->user_data))
|
2013-05-11 20:40:33 +02:00
|
|
|
goto no_parse; // validation failed -> no parse
|
|
|
|
|
if(x->action)
|
2013-11-23 12:53:11 -06:00
|
|
|
tok = (HParsedToken *)x->action(make_result(arena, tok), x->user_data);
|
2013-05-11 20:40:33 +02:00
|
|
|
|
|
|
|
|
// append to result sequence
|
|
|
|
|
h_carray_append(seq, tok);
|
2013-05-11 15:14:10 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-03 16:24:47 +02:00
|
|
|
// success
|
2013-05-11 15:14:10 +02:00
|
|
|
// since we started with a single nonterminal on the stack, seq should
|
|
|
|
|
// contain exactly the parse result.
|
|
|
|
|
assert(seq->used == 1);
|
2015-09-03 16:24:47 +02:00
|
|
|
return seq;
|
2013-05-11 19:04:59 +02:00
|
|
|
|
2013-05-24 20:10:21 -07:00
|
|
|
no_parse:
|
|
|
|
|
h_delete_arena(arena);
|
2015-09-03 16:24:47 +02:00
|
|
|
s->arena = NULL;
|
2013-05-24 20:10:21 -07:00
|
|
|
return NULL;
|
2015-09-03 16:24:47 +02:00
|
|
|
|
|
|
|
|
need_input:
|
2015-09-04 12:55:37 +02:00
|
|
|
if(stream->last_chunk)
|
2015-09-03 16:24:47 +02:00
|
|
|
goto no_parse;
|
2015-09-04 21:05:56 +02:00
|
|
|
if(tok)
|
|
|
|
|
h_arena_free(arena, tok); // no result, yet
|
|
|
|
|
h_slist_push(stack, x); // try this symbol again next time
|
2015-09-03 16:24:47 +02:00
|
|
|
return seq;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static HParseResult *llk_parse_finish_(HAllocator *mm__, HLLkState *s)
|
|
|
|
|
{
|
|
|
|
|
HParseResult *res = NULL;
|
|
|
|
|
|
|
|
|
|
if(s->seq) {
|
|
|
|
|
assert(s->seq->used == 1);
|
|
|
|
|
res = make_result(s->arena, s->seq->elements[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h_delete_arena(s->tarena);
|
|
|
|
|
h_free(s);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HParseResult *h_llk_parse(HAllocator* mm__, const HParser* parser, HInputStream* stream)
|
|
|
|
|
{
|
|
|
|
|
HLLkState *s = llk_parse_start_(mm__, parser);
|
|
|
|
|
|
2015-09-04 12:55:37 +02:00
|
|
|
assert(stream->last_chunk);
|
|
|
|
|
s->seq = llk_parse_chunk_(s, parser, stream);
|
2015-09-03 16:24:47 +02:00
|
|
|
|
|
|
|
|
return llk_parse_finish_(mm__, s);
|
2013-04-17 15:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-03 19:18:07 +02:00
|
|
|
void h_llk_parse_start(HSuspendedParser *s)
|
|
|
|
|
{
|
|
|
|
|
s->backend_state = llk_parse_start_(s->mm__, s->parser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void h_llk_parse_chunk(HSuspendedParser *s, HInputStream *input)
|
|
|
|
|
{
|
|
|
|
|
HLLkState *state = s->backend_state;
|
|
|
|
|
|
2015-09-04 12:55:37 +02:00
|
|
|
state->seq = llk_parse_chunk_(state, s->parser, input);
|
2015-09-03 19:18:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HParseResult *h_llk_parse_finish(HSuspendedParser *s)
|
|
|
|
|
{
|
|
|
|
|
HLLkState *state = s->backend_state;
|
|
|
|
|
HInputStream empty = {
|
|
|
|
|
.index = 0,
|
|
|
|
|
.bit_offset = 0,
|
|
|
|
|
.overrun = 0,
|
|
|
|
|
.endianness = s->endianness,
|
|
|
|
|
.length = 0,
|
2015-09-04 12:55:37 +02:00
|
|
|
.input = NULL,
|
|
|
|
|
.last_chunk = true
|
2015-09-03 19:18:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// signal end of input (no-op parse already done)
|
2015-09-04 12:55:37 +02:00
|
|
|
state->seq = llk_parse_chunk_(state, s->parser, &empty);
|
2015-09-03 19:18:07 +02:00
|
|
|
|
|
|
|
|
return llk_parse_finish_(s->mm__, s->backend_state);
|
|
|
|
|
}
|
2013-04-27 04:24:09 +02:00
|
|
|
|
|
|
|
|
|
2013-05-11 19:04:59 +02:00
|
|
|
HParserBackendVTable h__llk_backend_vtable = {
|
|
|
|
|
.compile = h_llk_compile,
|
2013-05-11 22:02:59 +02:00
|
|
|
.parse = h_llk_parse,
|
2015-09-03 19:18:07 +02:00
|
|
|
.free = h_llk_free,
|
|
|
|
|
|
|
|
|
|
.parse_start = h_llk_parse_start,
|
|
|
|
|
.parse_chunk = h_llk_parse_chunk,
|
|
|
|
|
.parse_finish = h_llk_parse_finish
|
2013-04-17 15:10:33 +02:00
|
|
|
};
|
2013-05-05 22:15:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// dummy!
|
2013-05-11 19:04:59 +02:00
|
|
|
int test_llk(void)
|
2013-05-05 22:15:40 +02:00
|
|
|
{
|
2013-05-20 21:28:16 +02:00
|
|
|
/* for k=2:
|
|
|
|
|
|
|
|
|
|
S -> A | B
|
|
|
|
|
A -> X Y a
|
|
|
|
|
B -> Y b
|
|
|
|
|
X -> x | ''
|
|
|
|
|
Y -> y -- for k=3 use "yy"
|
|
|
|
|
*/
|
|
|
|
|
|
2013-05-23 14:42:43 +02:00
|
|
|
HParser *X = h_optional(h_ch('x'));
|
2013-05-24 23:00:00 +02:00
|
|
|
HParser *Y = h_sequence(h_ch('y'), h_ch('y'), NULL);
|
|
|
|
|
HParser *A = h_sequence(X, Y, h_ch('a'), NULL);
|
|
|
|
|
HParser *B = h_sequence(Y, h_ch('b'), NULL);
|
2013-05-23 14:42:43 +02:00
|
|
|
HParser *p = h_choice(A, B, NULL);
|
2013-05-05 22:15:40 +02:00
|
|
|
|
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);
|
2013-05-22 22:45:25 +02:00
|
|
|
printf("derive epsilon: ");
|
2013-05-05 22:15:40 +02:00
|
|
|
h_pprint_symbolset(stdout, g, g->geneps, 0);
|
|
|
|
|
printf("first(A) = ");
|
2013-05-24 20:10:21 -07:00
|
|
|
h_pprint_stringset(stdout, h_first(3, g, g->start), 0);
|
|
|
|
|
// printf("follow(C) = ");
|
|
|
|
|
// h_pprint_stringset(stdout, h_follow(3, g, h_desugar(&system_allocator, NULL, c)), 0);
|
2013-05-05 22:15:40 +02:00
|
|
|
|
2013-05-24 23:00:00 +02:00
|
|
|
if(h_compile(p, PB_LLk, (void *)3)) {
|
2013-05-23 14:42:43 +02:00
|
|
|
fprintf(stderr, "does not compile\n");
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
2013-05-24 23:00:00 +02:00
|
|
|
|
|
|
|
|
HParseResult *res = h_parse(p, (uint8_t *)"xyya", 4);
|
2013-05-14 11:51:54 +02:00
|
|
|
if(res)
|
|
|
|
|
h_pprint(stdout, res->ast, 0, 2);
|
|
|
|
|
else
|
|
|
|
|
printf("no parse\n");
|
2013-05-11 19:06:23 +02:00
|
|
|
|
2013-05-05 22:15:40 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|