#include "limits.h" #include "time.h" #include "../cnp.h" #define TITO struct timespec tic, toc #define TIC do { clock_gettime(CLOCK_MONOTONIC_RAW, &tic); } while (0) #define TOC do { \ clock_gettime(CLOCK_MONOTONIC_RAW, &toc); \ printf("\nTotal time = %f seconds\n", \ (toc.tv_nsec - tic.tv_nsec) / 1000000000.0 + \ toc.tv_sec - tic.tv_sec); \ } while (0) #define READ_INTO_CPA(N, U, L, I, VA, VI, CP) do { \ U = new_utf8(N, L); \ I = get_info((str *)U, 0); \ VA = MYALLOC(NUM, 2); \ VI = 0; \ for (NUM index = 0; \ I.value >= 0 && index < str_length((str *) U); \ index += I.step, VI++) { \ I = get_info((str *)U, index); \ *(VA+VI) = I.value; \ } \ CP = MYALLOC(cpa, 1); \ CP->array = VA; \ CP->size = VI; \ add_to_list(names, CP); \ destroy_str((str *)U, 1); \ } while (0) #define READ_TNT_STRING(LEFT, FORMAT, LEN, ...) do { \ tnt_string = new_tnt_string(FORMAT, LEN, __VA_ARGS__); \ if (!tnt_string) { \ fleprintf("left = %d, f = %s, l = %d, " \ "cannot create tnt string\n", \ LEFT, FORMAT, LEN); \ map_list(rules, destroy_rule_and_free_all); \ destroy_list(rules, 0); \ map_list(names, destroy_cpa_and_free_all); \ destroy_list(names, 0); \ return 1; \ } \ rule = new_rule(LEFT, tnt_string); \ add_to_list(rules, rule); \ } while (0) int main(int UNUSED argc, char ** UNUSED argv) { /* have a grammar for testing */ List *tnt_string = NULL; Rule *rule = NULL; List *rules = new_list(); List *names = new_list(); List *preds = new_list(); char *user_name = NULL; char *raw_name = NULL; str *user_name_s = NULL, *raw_name_s = NULL; NUM *pred_bytes = NULL, pred_bytes_len = 0; char *name = MYALLOC(char, 2); utf8* uname = NULL; str_info info = EMPTY_STR_INFO; NUM *varray = NULL; NUM vindex = 0; cpa *cpap = NULL; *(name+1) = 0; *name = 'S'; READ_INTO_CPA(name, uname, 1, info, varray, vindex, cpap); READ_TNT_STRING(0, "pn", 2, (PT) 0, (NT) 0); rule = new_rule(0, new_list()); add_to_list(rules, rule); SAFE_MALLOC(char, user_name, 6, return 1;); *(user_name) = 'a'; *(user_name+1) = 's'; *(user_name+2) = 'c'; *(user_name+3) = 'i'; *(user_name+4) = 'i'; *(user_name+5) = 0; user_name_s = (str *) new_utf8(user_name, 5); SAFE_MALLOC(char, raw_name, 8, return 1;); *(raw_name+0) = '^'; *(raw_name+1) = 'a'; *(raw_name+2) = '-'; *(raw_name+3) = 'z'; *(raw_name+4) = 'A'; *(raw_name+5) = '-'; *(raw_name+6) = 'Z'; *(raw_name+7) = 0; raw_name_s = (str *) new_utf8(raw_name, 6); SAFE_MALLOC(NUM, pred_bytes, 52, return 1;); for (int i = 0; i < 26; i++) *(pred_bytes+pred_bytes_len++) = 'a' + i; for (int i = 0; i < 26; i++) *(pred_bytes+pred_bytes_len++) = 'A' + i; if (add_to_list(preds, new_ptd(user_name_s, raw_name_s, dfa_from_ranges_both (2, (NUM[]) { 'a', 'z', 'A', 'Z' }, 2, (NUM[]) { 'b', 'w', 'B', 'W' })))) { fleprintf0("Fail to add a predicate\n"); return 1; } free(pred_bytes); Grammar *g = new_grammar(); build_grammar(g, rules, names, preds); print_grammar(g); utf8 *string = new_utf8("aaaxxxxaaaaaaaa", 15); printf("\nPrinting the input...\n%s\n", get_data((str *) string)); TITO; TIC; Environment *env = cnp_parse(g, (str *) string); TOC; if (env) { if (!(env_error_p(env))) { BOOL result = bsr_lookup (env_bsrp(env), 0, 0, str_length((str *) string)); if (result) { printf("\nSuccessfully parsed the input!\n"); } else { printf("\nThe input does not parse!\n"); } printf("\nAll BSRs follow:\n\n"); if (argc == 1) bsr_print(env_bsrp(env), env_grammar(env), 1); } else { printf("There are errors!\n"); } destroy_env(env); } destroy_grammar(g, 1); destroy_list(rules, 1); free(string); return 0; }