From 1c8cbfd09ff9dd02f6d8d938c45e3f29a35f6f32 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sat, 5 Feb 2022 23:23:03 +0800 Subject: 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 --- src/grammar.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/grammar.c') 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) { -- cgit v1.2.3-18-g5258