From f0cd2de55c4ddfaf1db47aa838c8b1562a3b6b77 Mon Sep 17 00:00:00 2001
From: "Sven M. Hallberg" <pesco@khjk.org>
Date: Sat, 15 Jun 2013 20:54:53 +0200
Subject: [PATCH] fix match_production for the empty-sequence case

---
 src/backends/lalr.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/backends/lalr.c b/src/backends/lalr.c
index 0dd1d070..773b4577 100644
--- a/src/backends/lalr.c
+++ b/src/backends/lalr.c
@@ -579,15 +579,16 @@ int h_lrtable_put(HLRTable *tbl, size_t state, HCFChoice *x, HLRAction *action)
 bool match_production(HLREnhGrammar *eg, HCFChoice **p,
                       HCFChoice **rhs, size_t endstate)
 {
-  HLRTransition *t;
+  size_t state = endstate;  // initialized to end in case of empty rhs
   for(; *p && *rhs; p++, rhs++) {
-    t = h_hashtable_get(eg->smap, *p);
+    HLRTransition *t = h_hashtable_get(eg->smap, *p);
     assert(t != NULL);
     if(!eq_symbol(t->symbol, *rhs))
       return false;
+    state = t->to;
   }
   return (*p == *rhs    // both NULL
-          && t->to == endstate);
+          && state == endstate);
 }
 
 int h_lalr_compile(HAllocator* mm__, HParser* parser, const void* params)
@@ -1022,7 +1023,7 @@ int test_lalr(void)
   h_pprint_lrtable(stdout, g, (HLRTable *)p->backend_data, 0);
 
   printf("\n==== P A R S E  R E S U L T ====\n");
-  HParseResult *res = h_parse(p, (uint8_t *)"xxn-(n-((n)))-n", 13);
+  HParseResult *res = h_parse(p, (uint8_t *)"xxn-(n-((n)))-n", 2);
   if(res)
     h_pprint(stdout, res->ast, 0, 2);
   else
-- 
GitLab