mumble

A Lisp written in C, following the *Build Your Own Lisp* book
Log | Files | Refs | README

tree_traversal.c (2922B)


      1 #include "../mpc.h"
      2 
      3 int main(int argc, char *argv[]) {
      4 
      5   mpc_parser_t *Input  = mpc_new("input");
      6   mpc_parser_t *Node  = mpc_new("node");
      7   mpc_parser_t *Leaf  = mpc_new("leaf");
      8   mpc_ast_t *ast, *tree, *child, *child_sub, *ast_next;
      9   mpc_ast_trav_t *trav;
     10   mpc_result_t r;
     11   int index, lb, i;
     12 
     13   mpca_lang(MPCA_LANG_PREDICTIVE,
     14         " node : '(' <node> ',' /foo/ ',' <node> ')' | <leaf>;"
     15         " leaf : /bar/;"
     16         " input : /^/ <node> /$/;",
     17         Node, Leaf, Input, NULL);
     18 
     19   if (argc > 1) {
     20 
     21     if (mpc_parse_contents(argv[1], Input, &r)) {
     22       ast = r.output;
     23     } else {
     24       mpc_err_print(r.error);
     25       mpc_err_delete(r.error);
     26       mpc_cleanup(3, Node, Leaf, Input);
     27       return EXIT_FAILURE;
     28     }
     29 
     30   } else {
     31 
     32     if (mpc_parse_pipe("<stdin>", stdin, Input, &r)) {
     33       ast = r.output;
     34     } else {
     35       mpc_err_print(r.error);
     36       mpc_err_delete(r.error);
     37       mpc_cleanup(3, Node, Leaf, Input);
     38       return EXIT_FAILURE;
     39     }
     40 
     41   }
     42 
     43   /* Get index or child of tree */
     44   tree = ast->children[1];
     45 
     46   index = mpc_ast_get_index(tree, "node|>");
     47   child = mpc_ast_get_child(tree, "node|>");
     48 
     49   if(child == NULL) {
     50     mpc_cleanup(3, Node, Leaf, Input);
     51     mpc_ast_delete(ast);
     52     return EXIT_FAILURE;
     53   }
     54 
     55   printf("Index: %d; Child: \"%s\"\n", index, child->tag);
     56 
     57   /* Get multiple indexes or children of trees */
     58   index     = mpc_ast_get_index_lb(child, "node|leaf|regex", 0);
     59   child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", 0);
     60 
     61   while(index != -1) {
     62     printf("-- Index: %d; Child: \"%s\"\n", index, child_sub->tag);
     63 
     64     lb = index + 1;
     65     index     = mpc_ast_get_index_lb(child, "node|leaf|regex", lb);
     66     child_sub = mpc_ast_get_child_lb(child, "node|leaf|regex", lb);
     67   }
     68 
     69   /* Traversal */
     70   printf("Pre order tree traversal.\n");
     71   trav = mpc_ast_traverse_start(ast, mpc_ast_trav_order_pre);
     72 
     73   ast_next = mpc_ast_traverse_next(&trav);
     74 
     75   while(ast_next != NULL) {
     76     printf("Tag: %s; Contents: %s\n",
     77       ast_next->tag,
     78       ast_next->contents);
     79     ast_next = mpc_ast_traverse_next(&trav);
     80   }
     81 
     82   mpc_ast_traverse_free(&trav);
     83 
     84   printf("Post order tree traversal.\n");
     85 
     86   trav = mpc_ast_traverse_start(ast, mpc_ast_trav_order_post);
     87 
     88   ast_next = mpc_ast_traverse_next(&trav);
     89 
     90   while(ast_next != NULL) {
     91     printf("Tag: %s; Contents: %s\n",
     92       ast_next->tag,
     93       ast_next->contents);
     94     ast_next = mpc_ast_traverse_next(&trav);
     95   }
     96 
     97   mpc_ast_traverse_free(&trav);
     98 
     99   printf("Partial traversal.\n");
    100 
    101   trav = mpc_ast_traverse_start(ast, mpc_ast_trav_order_post);
    102 
    103   ast_next = mpc_ast_traverse_next(&trav);
    104 
    105   for(i=0; i<2 && ast_next != NULL; i++) {
    106     printf("Tag: %s; Contents: %s\n",
    107       ast_next->tag,
    108       ast_next->contents);
    109     ast_next = mpc_ast_traverse_next(&trav);
    110   }
    111 
    112   mpc_ast_traverse_free(&trav);
    113 
    114   /* Clean up and return */
    115   mpc_cleanup(3, Node, Leaf, Input);
    116   mpc_ast_delete(ast);
    117 
    118   return EXIT_SUCCESS;
    119 }