summaryrefslogtreecommitdiff
path: root/src/cnp.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/cnp.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/cnp.c')
-rw-r--r--src/cnp.c68
1 files changed, 55 insertions, 13 deletions
diff --git a/src/cnp.c b/src/cnp.c
index fe5a4dc..ce5a527 100644
--- a/src/cnp.c
+++ b/src/cnp.c
@@ -32,6 +32,7 @@ nt_add(Environment *env, NUM X, NUM j)
for (NUM i = 0; i < rg_len(RG); i++) {
BOOL result =
test_select(env, *(env->string+j), X, rg_nth(RG, i));
+ /* fleprintf("i = %ld, after test select\n", i); */
for (NUM k = 0; k < grammar_left_len(GI->g); k++) {
ht_reset(env->result_ts+k, DELETE_KEY);
@@ -83,10 +84,15 @@ test_select(Environment *env, NUM b, NUM X, CCR_MOD(List *) tnts)
goto success;
}
- /* TODO: predicates haven't been implemented yet */
- /* for (NUM i = 0; i < ht_size(result_ps); i++) {
- *
- * } */
+ for (NUM i = 0; i < ht_size(result_ps); i++) {
+ PTD *ptdp = grammar_ptd(gi->g, **((PT **) ht_keys(result_ps+i)));
+
+ if (ptd_run(ptdp, b)) {
+ /* fleprintf("i = %ld\n", i); */
+ result = 1;
+ goto success;
+ }
+ }
for (NUM i = 0; i < list_length(tnts); i++) {
@@ -106,9 +112,20 @@ test_select(Environment *env, NUM b, NUM X, CCR_MOD(List *) tnts)
}
- if (ht_find(gi->sts+X, &b) != NULL) result = 1;
+ if (ht_find(gi->sts+X, &b) != NULL) {
+ result = 1;
+ goto success;
+ }
- /* TODO: predicates haven't been implemented yet */
+ for (NUM i = 0; i < ht_size(gi->sps+X); i++) {
+ PTD *ptdp = grammar_ptd(gi->g, **((PT **) ht_keys(gi->sps+X)));
+ /* fleprintf("i = %ld\n", i); */
+ if (ptd_run(ptdp, b)) {
+ /* fleprintf("i = %ld\n", i); */
+ result = 1;
+ goto success;
+ }
+ }
goto success;
@@ -244,9 +261,13 @@ cnp_call(Environment *env, pair3 label, NUM i, NUM j)
TNT *xtnt = (TNT *) list_nth(tnts, label.z - 1);
- if (xtnt->type != NONTERMINAL) goto cleanup;
+ if (xtnt->type != NONTERMINAL) {
+ fleprintf("Wrong type: %d\n", xtnt->type);
+ goto cleanup;
+ }
NUM X = (NUM) xtnt->data.nt;
+ /* fleprintf("X = %ld, j = %ld\n", X, j); */
pair2 node = (pair2) { .x = X, .y = j };
pair4 u = (pair4)
@@ -258,20 +279,29 @@ cnp_call(Environment *env, pair3 label, NUM i, NUM j)
};
if (!(crf_find_node(env->crfp, node))) {
- if (crf_add_node(env->crfp, node)) goto cleanup;
+ if (crf_add_node(env->crfp, node)) {
+ fleprintf0("fail to add node to crf\n");
+ goto cleanup;
+ }
- if (crf_add_edge(env->crfp, node, u)) goto cleanup;
+ if (crf_add_edge(env->crfp, node, u)) {
+ fleprintf0("fail to add edge to crf\n");
+ goto cleanup;
+ }
/* errors will be stored in ENV, so we simply return */
nt_add(env, X, j);
- /* printf("X = %ld, j = %ld\n", X, j); */
+ /* fleprintf("X = %ld, j = %ld\n", X, j); */
return;
}
if (!(crf_find_edge(env->crfp, node, u))) {
- if (crf_add_edge(env->crfp, node, u)) goto cleanup;
+ if (crf_add_edge(env->crfp, node, u)) {
+ fleprintf0("fail to add edge to crf\n");
+ goto cleanup;
+ }
call_internal_env = env;
call_internal_label5 =
@@ -344,7 +374,17 @@ H_ATTR
BOOL
env_follow_p(CCR_MOD(Environment *) env, NUM X, NUM t)
{
- return ht_find(((env->gi->sts)+X), &t) != NULL;
+ if (ht_find(((env->gi->sts)+X), &t) != NULL) return 1;
+
+ for (NUM i = 0; i < ht_size(env->gi->sps+X); i++) {
+ PTD *ptdp = grammar_ptd(env->gi->g,
+ **((PT **) ht_keys(env->gi->sps+X)));
+ if (ptd_run(ptdp, t)) return 1;
+ }
+
+ /* fleprintf("X = %ld, t = %ld\n", X, t); */
+
+ return 0;
}
void
@@ -688,7 +728,9 @@ cnp_parse(Grammar *g, str *string)
/* while there are terminals */
while (((TNT *) list_nth(right, current_prodecor->z-1))->type ==
- TERMINAL) {
+ TERMINAL ||
+ ((TNT *) list_nth(right, current_prodecor->z-1))->type ==
+ PREDICATE) {
/* fleprintf0("found terminal\n"); */
/* add to BSR set */
errorp =