From d67e12a825697290d3d41f59b9aba2c2fc0d3112 Mon Sep 17 00:00:00 2001
From: "Sven M. Hallberg" <pesco@khjk.org>
Date: Fri, 21 Jun 2013 20:52:27 +0200
Subject: [PATCH] better factor out lr table writes

---
 src/backends/lalr.c |  8 ++++----
 src/backends/lr.h   |  2 --
 src/backends/lr0.c  | 13 ++++++++++---
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/backends/lalr.c b/src/backends/lalr.c
index 698b106d..39c4afd8 100644
--- a/src/backends/lalr.c
+++ b/src/backends/lalr.c
@@ -130,10 +130,10 @@ static inline bool has_conflicts(HLRTable *table)
   return !h_slist_empty(table->inadeq);
 }
 
-// place a new entry in tbl; records conflicts in tbl->inadeq
+// place a new terminal entry in tbl; records conflicts in tbl->inadeq
 // returns 0 on success, -1 on conflict
 // ignores forall entries
-int h_lrtable_put(HLRTable *tbl, size_t state, HCFChoice *x, HLRAction *action)
+static int terminal_put(HLRTable *tbl, size_t state, HCFChoice *x, HLRAction *action)
 {
   HLRAction *prev = h_hashtable_get(tbl->rows[state], x);
   if(prev && prev != action) {
@@ -257,7 +257,7 @@ int h_lalr_compile(HAllocator* mm__, HParser* parser, const void* params)
             if(fs->end_branch) {
               HCFChoice *terminal = h_arena_malloc(arena, sizeof(HCFChoice));
               terminal->type = HCF_END;
-              if(h_lrtable_put(table, state, terminal, action) < 0)
+              if(terminal_put(table, state, terminal, action) < 0)
                 inadeq = true;
             }
             H_FOREACH(fs->char_branches, void *key, HStringMap *m)
@@ -268,7 +268,7 @@ int h_lalr_compile(HAllocator* mm__, HParser* parser, const void* params)
               terminal->type = HCF_CHAR; 
               terminal->chr = key_char((HCharKey)key);
 
-              if(h_lrtable_put(table, state, terminal, action) < 0)
+              if(terminal_put(table, state, terminal, action) < 0)
                 inadeq = true;
             H_END_FOREACH  // lookahead character
         } H_END_FOREACH // enhanced production
diff --git a/src/backends/lr.h b/src/backends/lr.h
index f76bd33f..ee0c1f3e 100644
--- a/src/backends/lr.h
+++ b/src/backends/lr.h
@@ -120,12 +120,10 @@ HHashValue h_hash_transition(const void *p);
 
 HLRDFA *h_lr0_dfa(HCFGrammar *g);
 HLRTable *h_lr0_table(HCFGrammar *g, const HLRDFA *dfa);
-int h_lrtable_put(HLRTable *tbl, size_t state, HCFChoice *x, HLRAction *action);
 
 int h_lalr_compile(HAllocator* mm__, HParser* parser, const void* params);
 void h_lalr_free(HParser *parser);
 
-const HLRAction *h_lr_lookup(const HLRTable *table, size_t state, const HCFChoice *symbol);
 const HLRAction *h_lrengine_action(const HLREngine *engine);
 void h_lrengine_step(HLREngine *engine, const HLRAction *action);
 HParseResult *h_lrengine_result(HLREngine *engine);
diff --git a/src/backends/lr0.c b/src/backends/lr0.c
index 9f350b63..1bd63e54 100644
--- a/src/backends/lr0.c
+++ b/src/backends/lr0.c
@@ -161,6 +161,14 @@ HLRDFA *h_lr0_dfa(HCFGrammar *g)
 
 /* LR(0) table generation */
 
+static inline
+void put_shift(HLRTable *table, size_t state, const HCFChoice *symbol,
+               size_t nextstate)
+{
+  HLRAction *action = h_shift_action(table->arena, nextstate);
+  h_hashtable_put(table->rows[state], symbol, action);
+}
+
 HLRTable *h_lr0_table(HCFGrammar *g, const HLRDFA *dfa)
 {
   HAllocator *mm__ = g->mm__;
@@ -174,15 +182,14 @@ HLRTable *h_lr0_table(HCFGrammar *g, const HLRDFA *dfa)
   // add dummy shift entry for the start symbol so h_lrengine_step can always
   // find a shift.
   // NB: nextstate=0 is used for the "victory condition" by h_lrengine_result.
-  h_hashtable_put(table->rows[0], g->start, h_shift_action(arena, 0));
+  put_shift(table, 0, g->start, 0);
 
   // add shift entries
   for(HSlistNode *x = dfa->transitions->head; x; x = x->next) {
     // for each transition x-A->y, add "shift, goto y" to table entry (x,A)
     HLRTransition *t = x->elem;
 
-    HLRAction *action = h_shift_action(arena, t->to);
-    h_hashtable_put(table->rows[t->from], t->symbol, action);
+    put_shift(table, t->from, t->symbol, t->to);
   }
 
   // add reduce entries, record inadequate states
-- 
GitLab