summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2021-11-09 20:03:23 +0800
committerJSDurand <mmemmew@gmail.com>2021-11-09 20:03:23 +0800
commit53b8b6ffab5a968db75e9babddf4e2dbb2c688a3 (patch)
tree4ff55ae0027544b06702d24ec4840a2011f724e9 /src/test
parent87b6bad500702b0fcd708ee5b1d99f61a29ec7e6 (diff)
save point: representation of grammar might be too rough.
The current representation of the grammar is the most primitive BNF. This is the simplest to implement, but is difficult to cope with user requirements. Moreover, I find another paper describing the GLR algorithm, so I need to think about the representation of the grammar more. In particular, I would like the generation of the grammar to be incremental, so per chance its data type should be adapted accordingly.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/check_grammar.c51
-rw-r--r--src/test/check_reader.c22
2 files changed, 68 insertions, 5 deletions
diff --git a/src/test/check_grammar.c b/src/test/check_grammar.c
index 1fe27dd..39f66ee 100644
--- a/src/test/check_grammar.c
+++ b/src/test/check_grammar.c
@@ -2,7 +2,8 @@
#include <stdlib.h>
#include "../grammar.h"
-int main(U_ATTR int argc, U_ATTR char **argv)
+int
+main(U_ATTR int argc, U_ATTR char **argv)
{
/* check new_tnt and print it */
TNT *tnt = new_tnt(1, 12);
@@ -24,12 +25,52 @@ int main(U_ATTR int argc, U_ATTR char **argv)
}
/* check new_rule, print_rule, and destroy_rule. */
-
+
Rule *rule = new_rule(1, tnt_string);
-
+
print_rule(rule);
-
- destroy_rule(rule);
+
+ destroy_rule(rule, 1);
+
+ /* check build_grammar, print_grammar, and destroy_grammar */
+
+ List *rules = new_list();
+ List *names = new_list();
+
+
+ for (int i = 0; i < 3; i++) {
+ tnt_string = new_tnt_string("ntn", 3,
+ (NT) 3*(i+1),
+ (T) 3*(i+1)*(i+1),
+ (NT) 3*(i+1)*(i+1)-2);
+
+ if (!tnt_string) {
+ eprintf("i = %d, cannot create tnt string\n", i);
+
+ map_list(rules, destroy_rule_and_free_all);
+
+ destroy_list(rules, 0);
+ destroy_list(names, 1);
+ return 1;
+ }
+
+ rule = new_rule(i, tnt_string);
+ add_to_list(rules, rule);
+
+ char *name = MYALLOC(char, 7);
+ snprintf(name, 7, "Rule %d", i);
+ add_to_list(names, name);
+ }
+
+ Grammar *g = new_grammar();
+
+ build_grammar(g, rules, names);
+
+ print_grammar(g);
+
+ destroy_grammar(g, 1);
+
+ free(g);
return 0;
}
diff --git a/src/test/check_reader.c b/src/test/check_reader.c
new file mode 100644
index 0000000..1d73b3f
--- /dev/null
+++ b/src/test/check_reader.c
@@ -0,0 +1,22 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "../util.h"
+#include "../list.h"
+#include "../grammar.h"
+#include "../reader.h"
+
+/* The test of reading grammars should be postponed till later. */
+
+/* Grammar *
+ * read_grammar(List *args)
+ * {
+ *
+ * } */
+
+
+int
+main(U_ATTR int argc, U_ATTR char **argv)
+{
+ return 77;
+}