proper LALR example with conflict in LR(0)

This commit is contained in:
Sven M. Hallberg 2013-06-14 12:24:18 +02:00
parent fd297b636c
commit 92f16a4d14

View file

@ -667,6 +667,8 @@ int h_lalr_compile(HAllocator* mm__, HParser* parser, const void* params)
H_FOREACH(eg->smap, HCFChoice *lhs, HLRTransition *t) H_FOREACH(eg->smap, HCFChoice *lhs, HLRTransition *t)
if(t->symbol != item->lhs) if(t->symbol != item->lhs)
continue; continue;
assert(lhs->type == HCF_CHOICE); // XXX could be CHARSET
for(HCFSequence **p=lhs->seq; *p; p++) { for(HCFSequence **p=lhs->seq; *p; p++) {
HCFChoice **rhs = (*p)->items; HCFChoice **rhs = (*p)->items;
if(!match_production(eg, rhs, item->rhs, state)) if(!match_production(eg, rhs, item->rhs, state))
@ -986,21 +988,21 @@ HParserBackendVTable h__lalr_backend_vtable = {
// dummy! // dummy!
int test_lalr(void) int test_lalr(void)
{ {
/* for k=2: /*
S -> E
S -> A | B E -> E '-' T
A -> X Y a | T
B -> Y b T -> '(' E ')'
X -> x | '' | N
Y -> y -- for k=3 use "yy" N -> '0' -- also try [0-9] for the charset paths
*/ */
// XXX make LALR example HParser *N = h_sequence(h_ch('n'), NULL);
HParser *X = h_optional(h_in((uint8_t *)"rst", 3)); HParser *E = h_indirect();
HParser *Y = h_sequence(h_ch('y'), h_ch('y'), NULL); HParser *T = h_choice(h_sequence(h_ch('('), E, h_ch(')'), NULL), N, NULL);
HParser *A = h_sequence(X, Y, h_ch('a'), NULL); HParser *E_ = h_choice(h_sequence(E, h_ch('-'), T, NULL), T, NULL);
HParser *B = h_sequence(Y, h_ch('b'), NULL); h_bind_indirect(E, E_);
HParser *p = h_choice(A, B, NULL); HParser *p = h_sequence(E, NULL);
printf("\n==== G R A M M A R ====\n"); printf("\n==== G R A M M A R ====\n");
HCFGrammar *g = h_cfgrammar(&system_allocator, p); HCFGrammar *g = h_cfgrammar(&system_allocator, p);