summaryrefslogtreecommitdiff
path: root/src/slr_table.c
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2022-01-21 21:53:50 +0800
committerJSDurand <mmemmew@gmail.com>2022-01-21 21:53:50 +0800
commit53865aad225ffbe5cf3c42736e5a2095092f9fff (patch)
tree697ecbee7fa69ae8c58a0ec2f69cdf84d7cd313f /src/slr_table.c
parent5730d6c04258e192195bfbbbed76d68fd78ed458 (diff)
temporary save point
Just to save some work.
Diffstat (limited to 'src/slr_table.c')
-rw-r--r--src/slr_table.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/slr_table.c b/src/slr_table.c
new file mode 100644
index 0000000..95b9168
--- /dev/null
+++ b/src/slr_table.c
@@ -0,0 +1,69 @@
+#include "list.h"
+#include "ht.h"
+#include "slr_table.h"
+
+struct rule_s{
+ NUM nt_num;
+ NUM rule_num;
+};
+
+struct item_s {
+ rule r;
+ NUM pos;
+};
+
+struct item_set_s {
+ /* a list of items */
+ List *nonkernels;
+ /* a list of nonterminal symbols: a kernel item for a nonterminal
+ consists of all rules for that nonterminal, so we only need to
+ store the nonterminal, which are represented as NUM's. */
+ List *kernels;
+};
+
+typedef enum SLR_ACTION_e {
+ SHIFT,
+ REDUCE,
+ ACCEPT,
+ REJECT
+} SLR_ACTION;
+
+struct action_s {
+ SLR_ACTION type;
+ NUM arg1;
+ NUM arg2;
+};
+
+struct slr_table_s {
+ /* a list of states/rows
+
+ Each row contains a hash table mapping non-terminals, terminals,
+ and predicate terminals to an action set. */
+ List *rows;
+ /* a list of item sets; these are the states */
+ List *states;
+};
+
+BOOL
+closure(item_set *is, Grammar *g)
+{
+ NUM nonlen = list_length(is->nonkernels);
+
+ for (NUM i = 0; i < nonlen; i++) {
+ item it = *((item *)list_nth(is->nonkernels, i));
+ Rule_group *rg = grammar_rule(g, it.r.nt_num);
+ List *tnts = rg_nth(rg, it.r.rule_num);
+ if (list_length(tnts) == 0) continue;
+
+ NUM *temp = MYALLOC(NUM, 1);
+ *temp = *((NUM *) list_nth(tnts, it.pos));
+
+ if (add_to_list(is->kernels, temp)) {
+ free(temp);
+ fleprintf("Fail to add to list at i = %ld\n", i);
+ return 1;
+ }
+ }
+
+ return 0;
+}