summaryrefslogtreecommitdiff
path: root/src/grammar.c
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2022-02-05 23:23:03 +0800
committerJSDurand <mmemmew@gmail.com>2022-02-05 23:23:03 +0800
commit1c8cbfd09ff9dd02f6d8d938c45e3f29a35f6f32 (patch)
treee3d6bb3b5246bcfa38662615ad59663d5b0377d1 /src/grammar.c
parent510b10b96b546fcc6c6b6be85050305ddd192a41 (diff)
predicates start working now
Now we have a working implementation of predicates. It now only remains to write the parser of grammars. Of course we shall generate this parser by this parser generator itself, because why not. ;-P
Diffstat (limited to 'src/grammar.c')
-rw-r--r--src/grammar.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/grammar.c b/src/grammar.c
index 0b2be8d..1ea52b0 100644
--- a/src/grammar.c
+++ b/src/grammar.c
@@ -73,6 +73,12 @@ destroy_ptd(PTD *p, int flag)
free(p);
}
+BOOL
+ptd_run(PTD *p, NUM code)
+{
+ return run_dfa(p->dfap, code);
+}
+
static void
destroy_ptd_free_all(void *e)
{
@@ -85,7 +91,7 @@ destroy_ptd_no_free(void *e)
destroy_ptd((PTD *)e, 0);
}
-/* TODO: Add some statistic counts to assist the hash tables. For
+/* REVIEW: Add some statistic counts to assist the hash tables. For
example, store the total number of terminals as an integer; then
the hash table can be hinted at initialization to contain that many
elements, which can reduce the number of time a hash table needs to
@@ -411,9 +417,21 @@ void
print_grammar(CC_MOD(Grammar *) g)
{
printf("Printing a grammar:\n");
+ printf("Non-terminals...\n");
map_list_between(g->names, print_name, print_sep);
+
printf("\n");
+ printf("Predicates...\n");
+
+ for (int i = 0; i < list_length(g->predicates); i++) {
+ PTD *ptdp = (PTD *) list_nth(g->predicates, i);
+
+ printf("%s: %s\n",
+ get_data(ptdp->user_name), get_data(ptdp->raw_name));
+ }
+
+ printf("Rules...\n");
for (int i = 0; i < list_length(g->rule_grps); i++) {
print_rule_group(list_nth(g->rule_grps, i));
printf("\n");
@@ -539,6 +557,17 @@ grammar_rule(CCR_MOD(Grammar *) g, NT nt)
return (Rule_group *) list_nth(g->rule_grps, nt);
}
+PTD *
+grammar_ptd(CCR_MOD(Grammar *) g, PT pt)
+{
+ if ((NUM) pt >= list_length(g->predicates)) {
+ fleprintf("Invalid predicate terminal: %lu\n", pt);
+ return NULL;
+ }
+
+ return (PTD *) list_nth(g->predicates, pt);
+}
+
NUM
grammar_left_len(CCR_MOD(Grammar *)g)
{