0

I have a question concerning Abstract Syntax Trees generated with Boost Spirit Library.

I've found many informations about deleting nodes and subtrees in a Binary Search Tree, but I cannot find the same information for ASTs. I have a node in an AS-Tree, and this node is root to a subtree of a complete tree. Now I want to delete the node and all of its children.

I don't know how to do it, and the Boost Spirit Documentation didn't help either.

Has anyone got any tips for me?

The Tree is generated with (Boost 1.46.1):

tree_parse_info<> info = ast_parse(expression.c_str(), parser, skipparser);

And the Expression is something like this:

(variable_17 OR variable_18) AND function( variable_17) <= 30 OR function( subkey_18) <= 30

I use

tree_match<iterator_t>::tree_iterator tree_it = info.trees.begin();

to get the beginning of the tree, and then I do check if one of the subtrees is redundant (doensn't have anything to do with the deleting itself). `Then I traverse through the tree using

tree_match<iterator_t>::tree_iterator children_it = tree_it->children.begin()

and calling the same function with its children (recursive). I can't post the complete code,but that's the most important part of it. I thought, that i can traverse to the leafnodes of a redundant subtree, and set them to null, or something like this. And then I go up the tree again, and delete all other children one after another. However, nothing has worked so far.

An example for traversing the tree: The Traversing It's the answer.

If I can't delete any nodes, does anyone has an idea, how to create a new tree, based on the existing one, skiping the redundant parts of it.

Community
  • 1
  • 1
user2791904
  • 139
  • 1
  • 9
  • 3
    Can you give a [SSCCE](http://sscce.org/)? If you can show an AST, I could tell you. AST's are user-defined in Spirit, so it's really just how you would handle that type regardless of Spirit; just by (re)setting members. – sehe Oct 01 '13 at 23:07
  • @sehe Looking at the mailing list, he's using Spirit Classic. The AST he is using is `tree_parse_info<>` and that is provided by the library. I have absolutely no experience with that. – llonesmiz Oct 02 '13 at 05:10
  • I have added information. Thought I could erase children_it or something like this, do delete the node, but didn't work. – user2791904 Oct 02 '13 at 07:04
  • @cv_and_he oh, hehe. I saw that post too, but I didn't even recognize that API as Spirit V1 :/ – sehe Oct 02 '13 at 09:05
  • So there is no way do delete Nodes using Spirit Classic? How can I create a new AST-Tree, without the redundant Subtree? – user2791904 Oct 02 '13 at 09:08
  • 1
    @user2791904 `info.trees` and `tree_it->children` seem to be by default `std::vector`. I guess you could use the erase-remove idiom if you are careful about iteration invalidation. If you define `BOOST_SPIRIT_USE_LIST_FOR_TREES` the containers will be `std::list` and you could simply use `info.trees.erase(redundant_iterator)` or `tree_it->children.erase(redundant_iterator)`. I think this is correct but I have no way of knowing it because you haven't provided an example that we can experiment with as sehe suggested. – llonesmiz Oct 02 '13 at 09:27
  • That's the same solution I'm working on. It seems to work, allthough im still working on reconnecting all my nodes, because the structure of the tree has changed. But that has nothing to do with the original Problem. I don't know how to mark your comment as answer, though. – user2791904 Oct 02 '13 at 10:41

0 Answers0