aboutsummaryrefslogtreecommitdiff
path: root/parts
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2025-07-01 00:11:29 +0300
committerkartofen <mladenovnasko0@gmail.com>2025-07-01 00:11:29 +0300
commita67266ff72280b85fed7ec498967a855a5735639 (patch)
treef53d551e49e2263b5208b9adf1d4c50943f4d59d /parts
parent7743cb4f8a06ab79a521c4346aac74b47c8ce224 (diff)
major refactor, more modular (wow because obviously modularity is always a good thing yes), and etc
Diffstat (limited to 'parts')
-rw-r--r--parts/grammar.h22
-rw-r--r--parts/symbol.h12
-rw-r--r--parts/table.h50
-rw-r--r--parts/toklist.h9
-rw-r--r--parts/util-tables.h8
5 files changed, 101 insertions, 0 deletions
diff --git a/parts/grammar.h b/parts/grammar.h
new file mode 100644
index 0000000..328f88e
--- /dev/null
+++ b/parts/grammar.h
@@ -0,0 +1,22 @@
+#ifndef GRAMMAR_H
+#define GRAMMAR_H
+
+extern struct production {
+ symbol LHS;
+ symbol *RHS;
+ size_t nRHS;
+} grammar[];
+
+extern const size_t total_productions;
+
+void grammar_print()
+{
+ for(size_t i = 0; i < total_productions; i++) {
+ printf("%d --> ", grammar[i].LHS);
+ for(size_t j = 0; j < grammar[i].nRHS; j++)
+ printf("%d ", grammar[i].RHS[j]);
+ printf("\n");
+ }
+}
+
+#endif
diff --git a/parts/symbol.h b/parts/symbol.h
new file mode 100644
index 0000000..d3cb5cd
--- /dev/null
+++ b/parts/symbol.h
@@ -0,0 +1,12 @@
+#ifndef SYMBOL_H
+#define SYMBOL_H
+
+typedef unsigned int symbol;
+extern const size_t total_symbols;
+// extern char *symbol_to_str[] ...
+
+extern int symbol_is_terminal(symbol s);
+extern int symbol_is_input_end(symbol s);
+extern int symbol_is_valid(symbol s);
+
+#endif
diff --git a/parts/table.h b/parts/table.h
new file mode 100644
index 0000000..3b54312
--- /dev/null
+++ b/parts/table.h
@@ -0,0 +1,50 @@
+#ifndef TABLE_H
+#define TABLE_H
+
+extern struct action {
+ enum action_type {
+ ACTION_NOT_SET = 0, ACTION_SHIFT,
+ ACTION_GOTO, ACTION_REDUCE,
+ ACTION_ACCEPT
+ } type;
+ size_t arg;
+} *table[];
+
+extern size_t table_states;
+
+extern int table_fill();
+extern void table_free();
+void table_print();
+void table_print_cstyle();
+
+#include "symbol.h"
+void table_print()
+{
+ printf(" ");
+ for(size_t sym = 0; sym < total_symbols; sym++) printf("%2zu ", sym);
+ printf("\n");
+
+ char action_to_char[] = {[ACTION_SHIFT] = 's', [ACTION_REDUCE] = 'r', [ACTION_GOTO] = 'g'};
+ for(size_t i = 0; i < table_states; i++) {
+ printf("%2zu ", i);
+ for(size_t sym = 0; sym < total_symbols; sym++)
+ if(table[i][sym].type == ACTION_ACCEPT) printf(" a ");
+ else if(table[i][sym].type) printf("%c%-2zu ", action_to_char[table[i][sym].type], table[i][sym].arg);
+ else printf(" ");
+ printf("\n");
+ }
+
+
+}
+
+void table_print_cstyle()
+{
+ for(size_t i = 0; i < table_states; i++) {
+ printf("{");
+ for(size_t sym = 0; sym < total_symbols; sym++)
+ printf("{%d, %zu},", table[i][sym].type, table[i][sym].arg);
+ printf("},\n");
+ }
+}
+
+#endif
diff --git a/parts/toklist.h b/parts/toklist.h
new file mode 100644
index 0000000..b6fd10d
--- /dev/null
+++ b/parts/toklist.h
@@ -0,0 +1,9 @@
+#ifndef TOKLIST_H
+#define TOKLIST_H
+
+#include "symbol.h"
+
+extern symbol toklist_eat();
+extern symbol toklist_peek();
+
+#endif
diff --git a/parts/util-tables.h b/parts/util-tables.h
new file mode 100644
index 0000000..a6d788a
--- /dev/null
+++ b/parts/util-tables.h
@@ -0,0 +1,8 @@
+#ifndef UTIL_TABLES_H
+#define UTIL_TABLES_H
+
+// extern int *nullable
+extern int **follow;
+extern int **first;
+
+#endif