From 2b643c729baec91d4c51b89135c575dc33d5079a Mon Sep 17 00:00:00 2001 From: petermlm Date: Sun, 17 Apr 2016 00:17:45 +0100 Subject: [PATCH 1/4] Made functions to get child nodes of AST by tag --- mpc.c | 32 ++++++++++++++++++++++++++++++++ mpc.h | 5 +++++ 2 files changed, 37 insertions(+) diff --git a/mpc.c b/mpc.c index 9e84a47..e5c350e 100644 --- a/mpc.c +++ b/mpc.c @@ -2776,6 +2776,38 @@ void mpc_ast_print_to(mpc_ast_t *a, FILE *fp) { mpc_ast_print_depth(a, 0, fp); } +int mpc_ast_get_index(mpc_ast_t *ast, char *tag) { + return mpc_ast_get_index_lb(ast, tag, 0); +} + +int mpc_ast_get_index_lb(mpc_ast_t *ast, char *tag, int lb) { + int i; + + for(i=lb; ichildren_num; i++) { + if(strcmp(ast->children[i]->tag, tag) == 0) { + return i; + } + } + + return -1; +} + +mpc_ast_t *mpc_ast_get_child(mpc_ast_t *ast, char *tag) { + return mpc_ast_get_child_lb(ast, tag, 0); +} + +mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, char *tag, int lb) { + int i; + + for(i=lb; ichildren_num; i++) { + if(strcmp(ast->children[i]->tag, tag) == 0) { + return ast->children[i]; + } + } + + return NULL; +} + mpc_val_t *mpcf_fold_ast(int n, mpc_val_t **xs) { int i, j; diff --git a/mpc.h b/mpc.h index a9d8a72..02a6b4e 100644 --- a/mpc.h +++ b/mpc.h @@ -276,6 +276,11 @@ void mpc_ast_delete(mpc_ast_t *a); void mpc_ast_print(mpc_ast_t *a); void mpc_ast_print_to(mpc_ast_t *a, FILE *fp); +int mpc_ast_get_index(mpc_ast_t *ast, char *tag); +int mpc_ast_get_index_lb(mpc_ast_t *ast, char *tag, int lb); +mpc_ast_t *mpc_ast_get_child(mpc_ast_t *ast, char *tag); +mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, char *tag, int lb); + /* ** Warning: This function currently doesn't test for equality of the `state` member! */ From 97d634a708122387a6322e0f908f4b10e4210c87 Mon Sep 17 00:00:00 2001 From: petermlm Date: Sun, 17 Apr 2016 18:43:10 +0100 Subject: [PATCH 2/4] Added const to declarations --- mpc.c | 8 ++++---- mpc.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mpc.c b/mpc.c index e5c350e..6c65255 100644 --- a/mpc.c +++ b/mpc.c @@ -2776,11 +2776,11 @@ void mpc_ast_print_to(mpc_ast_t *a, FILE *fp) { mpc_ast_print_depth(a, 0, fp); } -int mpc_ast_get_index(mpc_ast_t *ast, char *tag) { +int mpc_ast_get_index(mpc_ast_t *ast, const char *tag) { return mpc_ast_get_index_lb(ast, tag, 0); } -int mpc_ast_get_index_lb(mpc_ast_t *ast, char *tag, int lb) { +int mpc_ast_get_index_lb(mpc_ast_t *ast, const char *tag, int lb) { int i; for(i=lb; ichildren_num; i++) { @@ -2792,11 +2792,11 @@ int mpc_ast_get_index_lb(mpc_ast_t *ast, char *tag, int lb) { return -1; } -mpc_ast_t *mpc_ast_get_child(mpc_ast_t *ast, char *tag) { +mpc_ast_t *mpc_ast_get_child(mpc_ast_t *ast, const char *tag) { return mpc_ast_get_child_lb(ast, tag, 0); } -mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, char *tag, int lb) { +mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, const char *tag, int lb) { int i; for(i=lb; ichildren_num; i++) { diff --git a/mpc.h b/mpc.h index 02a6b4e..82ee6ba 100644 --- a/mpc.h +++ b/mpc.h @@ -276,10 +276,10 @@ void mpc_ast_delete(mpc_ast_t *a); void mpc_ast_print(mpc_ast_t *a); void mpc_ast_print_to(mpc_ast_t *a, FILE *fp); -int mpc_ast_get_index(mpc_ast_t *ast, char *tag); -int mpc_ast_get_index_lb(mpc_ast_t *ast, char *tag, int lb); -mpc_ast_t *mpc_ast_get_child(mpc_ast_t *ast, char *tag); -mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, char *tag, int lb); +int mpc_ast_get_index(mpc_ast_t *ast, const char *tag); +int mpc_ast_get_index_lb(mpc_ast_t *ast, const char *tag, int lb); +mpc_ast_t *mpc_ast_get_child(mpc_ast_t *ast, const char *tag); +mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, const char *tag, int lb); /* ** Warning: This function currently doesn't test for equality of the `state` member! From d8243d01f1c5c40f771545839f1d1605cd70d80d Mon Sep 17 00:00:00 2001 From: petermlm Date: Sun, 17 Apr 2016 18:44:24 +0100 Subject: [PATCH 3/4] Added example for tree traversal --- examples/tree_traversal.c | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 examples/tree_traversal.c diff --git a/examples/tree_traversal.c b/examples/tree_traversal.c new file mode 100644 index 0000000..3ec0299 --- /dev/null +++ b/examples/tree_traversal.c @@ -0,0 +1,73 @@ +#include "../mpc.h" + +int main(int argc, char *argv[]) { + + mpc_parser_t *Input = mpc_new("input"); + mpc_parser_t *Node = mpc_new("node"); + mpc_parser_t *Leaf = mpc_new("leaf"); + mpc_ast_t *ast, *tree, *child, *child_sub; + mpc_result_t r; + int index, lb; + + mpca_lang(MPCA_LANG_PREDICTIVE, + " node : '(' ',' /foo/ ',' ')' | ;" + " leaf : /bar/;" + " input : /^/ /$/;", + Node, Leaf, Input, NULL); + + if (argc > 1) { + + if (mpc_parse_contents(argv[1], Input, &r)) { + ast = r.output; + } else { + mpc_err_print(r.error); + mpc_err_delete(r.error); + mpc_cleanup(3, Node, Leaf, Input); + return EXIT_FAILURE; + } + + } else { + + if (mpc_parse_pipe("", stdin, Input, &r)) { + ast = r.output; + } else { + mpc_err_print(r.error); + mpc_err_delete(r.error); + mpc_cleanup(3, Node, Leaf, Input); + return EXIT_FAILURE; + } + + } + + /* Get index or child of tree */ + tree = ast->children[1]; + + index = mpc_ast_get_index(tree, "node|>"); + child = mpc_ast_get_child(tree, "node|>"); + + if(child == NULL) { + mpc_cleanup(3, Node, Leaf, Input); + mpc_ast_delete(ast); + return EXIT_FAILURE; + } + + printf("Index: %d; Child: \"%s\"\n", index, child->tag); + + /* Get multiple indexes or children of trees */ + index = mpc_ast_get_index_lb(child, "node|leaf|regex", 0); + child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", 0); + + while(index != -1) { + printf("-- Index: %d; Child: \"%s\"\n", index, child_sub->tag); + + lb = index + 1; + index = mpc_ast_get_index_lb(child, "node|leaf|regex", lb); + child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", lb); + } + + /* Clean up and return */ + mpc_cleanup(3, Node, Leaf, Input); + mpc_ast_delete(ast); + + return EXIT_SUCCESS; +} From aa17e0723e57e03ca9b9a42e50f9bfeecececb4f Mon Sep 17 00:00:00 2001 From: petermlm Date: Sun, 17 Apr 2016 18:51:04 +0100 Subject: [PATCH 4/4] 2 space indentation --- examples/tree_traversal.c | 106 +++++++++++++++++++------------------- mpc.c | 28 +++++----- 2 files changed, 67 insertions(+), 67 deletions(-) diff --git a/examples/tree_traversal.c b/examples/tree_traversal.c index 3ec0299..b31f660 100644 --- a/examples/tree_traversal.c +++ b/examples/tree_traversal.c @@ -2,72 +2,72 @@ int main(int argc, char *argv[]) { - mpc_parser_t *Input = mpc_new("input"); - mpc_parser_t *Node = mpc_new("node"); - mpc_parser_t *Leaf = mpc_new("leaf"); - mpc_ast_t *ast, *tree, *child, *child_sub; - mpc_result_t r; - int index, lb; + mpc_parser_t *Input = mpc_new("input"); + mpc_parser_t *Node = mpc_new("node"); + mpc_parser_t *Leaf = mpc_new("leaf"); + mpc_ast_t *ast, *tree, *child, *child_sub; + mpc_result_t r; + int index, lb; - mpca_lang(MPCA_LANG_PREDICTIVE, - " node : '(' ',' /foo/ ',' ')' | ;" - " leaf : /bar/;" - " input : /^/ /$/;", - Node, Leaf, Input, NULL); + mpca_lang(MPCA_LANG_PREDICTIVE, + " node : '(' ',' /foo/ ',' ')' | ;" + " leaf : /bar/;" + " input : /^/ /$/;", + Node, Leaf, Input, NULL); - if (argc > 1) { - - if (mpc_parse_contents(argv[1], Input, &r)) { - ast = r.output; - } else { - mpc_err_print(r.error); - mpc_err_delete(r.error); - mpc_cleanup(3, Node, Leaf, Input); - return EXIT_FAILURE; - } + if (argc > 1) { + if (mpc_parse_contents(argv[1], Input, &r)) { + ast = r.output; } else { - - if (mpc_parse_pipe("", stdin, Input, &r)) { - ast = r.output; - } else { - mpc_err_print(r.error); - mpc_err_delete(r.error); - mpc_cleanup(3, Node, Leaf, Input); - return EXIT_FAILURE; - } - + mpc_err_print(r.error); + mpc_err_delete(r.error); + mpc_cleanup(3, Node, Leaf, Input); + return EXIT_FAILURE; } - /* Get index or child of tree */ - tree = ast->children[1]; + } else { - index = mpc_ast_get_index(tree, "node|>"); - child = mpc_ast_get_child(tree, "node|>"); - - if(child == NULL) { - mpc_cleanup(3, Node, Leaf, Input); - mpc_ast_delete(ast); - return EXIT_FAILURE; + if (mpc_parse_pipe("", stdin, Input, &r)) { + ast = r.output; + } else { + mpc_err_print(r.error); + mpc_err_delete(r.error); + mpc_cleanup(3, Node, Leaf, Input); + return EXIT_FAILURE; } - printf("Index: %d; Child: \"%s\"\n", index, child->tag); + } - /* Get multiple indexes or children of trees */ - index = mpc_ast_get_index_lb(child, "node|leaf|regex", 0); - child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", 0); + /* Get index or child of tree */ + tree = ast->children[1]; - while(index != -1) { - printf("-- Index: %d; Child: \"%s\"\n", index, child_sub->tag); + index = mpc_ast_get_index(tree, "node|>"); + child = mpc_ast_get_child(tree, "node|>"); - lb = index + 1; - index = mpc_ast_get_index_lb(child, "node|leaf|regex", lb); - child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", lb); - } - - /* Clean up and return */ + if(child == NULL) { mpc_cleanup(3, Node, Leaf, Input); mpc_ast_delete(ast); + return EXIT_FAILURE; + } - return EXIT_SUCCESS; + printf("Index: %d; Child: \"%s\"\n", index, child->tag); + + /* Get multiple indexes or children of trees */ + index = mpc_ast_get_index_lb(child, "node|leaf|regex", 0); + child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", 0); + + while(index != -1) { + printf("-- Index: %d; Child: \"%s\"\n", index, child_sub->tag); + + lb = index + 1; + index = mpc_ast_get_index_lb(child, "node|leaf|regex", lb); + child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", lb); + } + + /* Clean up and return */ + mpc_cleanup(3, Node, Leaf, Input); + mpc_ast_delete(ast); + + return EXIT_SUCCESS; } diff --git a/mpc.c b/mpc.c index 6c65255..a305b12 100644 --- a/mpc.c +++ b/mpc.c @@ -2777,35 +2777,35 @@ void mpc_ast_print_to(mpc_ast_t *a, FILE *fp) { } int mpc_ast_get_index(mpc_ast_t *ast, const char *tag) { - return mpc_ast_get_index_lb(ast, tag, 0); + return mpc_ast_get_index_lb(ast, tag, 0); } int mpc_ast_get_index_lb(mpc_ast_t *ast, const char *tag, int lb) { - int i; + int i; - for(i=lb; ichildren_num; i++) { - if(strcmp(ast->children[i]->tag, tag) == 0) { - return i; - } + for(i=lb; ichildren_num; i++) { + if(strcmp(ast->children[i]->tag, tag) == 0) { + return i; } + } - return -1; + return -1; } mpc_ast_t *mpc_ast_get_child(mpc_ast_t *ast, const char *tag) { - return mpc_ast_get_child_lb(ast, tag, 0); + return mpc_ast_get_child_lb(ast, tag, 0); } mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, const char *tag, int lb) { - int i; + int i; - for(i=lb; ichildren_num; i++) { - if(strcmp(ast->children[i]->tag, tag) == 0) { - return ast->children[i]; - } + for(i=lb; ichildren_num; i++) { + if(strcmp(ast->children[i]->tag, tag) == 0) { + return ast->children[i]; } + } - return NULL; + return NULL; } mpc_val_t *mpcf_fold_ast(int n, mpc_val_t **xs) {