diff options
author | JSDurand <mmemmew@gmail.com> | 2022-01-31 15:59:11 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2022-01-31 15:59:11 +0800 |
commit | eb007d554251456a2a508849edf91b15aab1333e (patch) | |
tree | bd88e78debdd646da87aa60f1bf2904eaa4370ca /src/test | |
parent | a8bd5e9d85ac9928bd29add82e887f82642af893 (diff) |
cnp: save point
Now we need to implement predicates, in order to have practical
applications.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/check_cnp.c | 185 |
1 files changed, 7 insertions, 178 deletions
diff --git a/src/test/check_cnp.c b/src/test/check_cnp.c index 00c8a64..7a0a325 100644 --- a/src/test/check_cnp.c +++ b/src/test/check_cnp.c @@ -97,174 +97,19 @@ main(int UNUSED argc, char ** UNUSED argv) printf("\nPrinting the input...\n%s\n", get_data((str *) string)); - Environment *env = new_env(g, (str *) string); + Environment *env = cnp_parse(g, (str *) string); - if (crf_add_node(env_crfp(env), (pair2) { 0 })) goto cleanup; - - nt_add(env, 0, 0); - - CHECK_ENV_ERROR(env, goto cleanup;); - - NUM current_grade = 0; - - prodecor *current_prodecor = NULL; - - Rule_group *rg = NULL; - List *right = NULL, *tnts = NULL; - - NUM *num_string = NULL, num_string_len = 0; - BOOL errorp = 0; - - env_str(env, &num_string, &num_string_len); - - while ((current_prodecor = - pop_procr(env_r(env), env_u(env), ¤t_grade))) { - /* fleprintf("{ %ld, %ld, %ld, %ld, %ld }\n", - * current_prodecor->x, current_prodecor->y, - * current_prodecor->z, current_prodecor->w, - * current_grade); - * print_procr(env_r(env)); */ - - /* different cases */ - - rg = grammar_rule(env_grammar(env), current_prodecor->x); - right = rg_nth(rg, current_prodecor->y); - - /* separate the empty rules out */ - if (list_length(right) == 0) { - errorp = - bsr_add(env_grammar(env), env_bsrp(env), - current_prodecor->x, current_prodecor->y, 0, - current_grade, current_grade, current_grade); - - if (errorp) goto cleanup; - - if (env_follow_p(env, current_prodecor->x, - *(num_string+current_grade))) { - rtn(env, current_prodecor->x, - current_prodecor->w, current_grade); - } - - goto continue_label; - } - - /* fleprintf0("non empty rule\n"); */ - - /* if at the end of a rule, return if possible */ - if (current_prodecor->z == list_length(right)) { - if (env_follow_p(env, current_prodecor->x, - *(num_string+current_grade))) { - rtn(env, current_prodecor->x, - current_prodecor->w, current_grade); - } - - goto continue_label; - } - - /* fleprintf0("not at the end of the rule\n"); */ - - /* if at the start of a rule, no need for test_select */ - if (current_prodecor->z == 0) { - /* fleprintf0("at the start of the rule\n"); */ - current_prodecor->z = 1; - - /* otherwise test select */ + if (env) { + if (!(env_error_p(env))) { + printf("Successfully parsed the input:\n"); + bsr_print(env_bsrp(env), env_grammar(env), 1); } else { - /* fleprintf0("start by test select\n"); */ - tnts = - array_to_list(list_array(right)+current_prodecor->z, - list_length(right)-current_prodecor->z); - if (test_select(env, *(num_string+current_grade), - current_prodecor->x, tnts)) { - /* fleprintf0("successful test\n"); */ - free(tnts); - tnts = NULL; - ++(current_prodecor->z); - } else { - /* fleprintf0("failed test\n"); */ - free(tnts); - tnts = NULL; - goto continue_label; - } + printf("There are errors!\n"); } - CHECK_ENV_ERROR(env, goto cleanup;); - - /* while there are terminals */ - while (((TNT *) list_nth(right, current_prodecor->z-1))->type == - TERMINAL) { - /* fleprintf0("found terminal\n"); */ - /* add to BSR set */ - errorp = - bsr_add(env_grammar(env), env_bsrp(env), - current_prodecor->x, current_prodecor->y, - current_prodecor->z, current_prodecor->w, - current_grade, current_grade+1); - - if (errorp) goto cleanup; - - current_grade++; - - /* if at the end of the rule, return if possible */ - if (current_prodecor->z == list_length(right)) { - if (env_follow_p(env, current_prodecor->x, - *(num_string+current_grade))) { - /* fleprintf0("we choose to return\n"); */ - rtn(env, current_prodecor->x, - current_prodecor->w, current_grade); - /* fleprintf0("hi\n"); */ - } - - goto continue_label; - } - /* fleprintf0("terminal not at the end\n"); */ - /* else test select */ - tnts = - array_to_list(list_array(right)+current_prodecor->z, - list_length(right)-current_prodecor->z); - if (test_select(env, *(num_string+current_grade), - current_prodecor->x, tnts)) { - /* fleprintf0("Successful test\n"); */ - free(tnts); - tnts = NULL; - (current_prodecor->z)++; - } else { - /* fleprintf0("Failed test\n"); */ - /* printf("next thing is "); */ - /* print_tnt(list_nth(right, current_prodecor->z)); */ - /* printf("\n"); */ - free(tnts); - tnts = NULL; - goto continue_label; - } - } - - /* fleprintf0("encountered non-terminal\n"); */ - - /* when a nonterminal is encountered, we call it */ - cnp_call(env, (pair3) { - .x = current_prodecor->x, - .y = current_prodecor->y, - .z = current_prodecor->z - }, current_prodecor->w, current_grade); - - /* fleprintf("after call { %ld, %ld, %ld, %ld, %ld }\n", - * current_prodecor->x, current_prodecor->y, - * current_prodecor->z, current_prodecor->w, - * current_grade); */ - - continue_label: - - CHECK_ENV_ERROR(env, goto cleanup;); - - free(current_prodecor); + destroy_env(env); } - cleanup: - bsr_print(env_bsrp(env), env_grammar(env), 1); - - destroy_env(env); - destroy_grammar(g, 1); destroy_list(rules, 1); @@ -273,19 +118,3 @@ main(int UNUSED argc, char ** UNUSED argv) return 0; } - -/* archives - - printf("\nprint first for 0:\n"); - env_print_first(env, 0); - - printf("\nprint first for 1:\n"); - env_print_first(env, 1); - - printf("\nprint follow for 0:\n"); - env_print_follow(env, 0); - - printf("\nprint follow for 1:\n"); - env_print_follow(env, 1); - - */ |