I would not use the getText() method if I were you as that is just a convenience I created if you are just hacking something up. Look at what that method does and copy it to produce your c_str directly.
Next, you are assuming that there are always 2 children, but ANTLR produces a tree which has 0..n nodes. So, look at the function makeDot() in antlr3basetreeadaptor.c and copy it. This code does in fact use getText(), but it does so because I don't know how long you will want to keep the strings around so they are tracked by the string factory and released when you free the tree. Jim > -----Original Message----- > From: [email protected] [mailto:antlr-interest- > [email protected]] On Behalf Of Thomas Davis > Sent: Friday, September 10, 2010 5:39 AM > To: [email protected] > Subject: [antlr-interest] Recursive Tree Walking C Target > > Just wondering if anyone had any tips for recursively walking an > ANTLR_BASE_TREE produced from a parser. I seem to be getting some memory > issues. > > I.e. A snippet of my evaluate method is: > > std::string ConditionTree::evaluate(pANTLR3_BASE_TREE root) { > > //Variable declarations > std::string value1 = ""; > std::string value2 = ""; > > //Firstly get the textual description from the node std::string > nodeText = (const char*) root->getText(root)->chars; > qDebug() << "Node Text: " << nodeText.c_str() << "\n"; > > //Get the nodes children and check to make sure there are some > pANTLR3_VECTOR children = root->children; > qDebug() << "Got the chidlren"; > > if(children != NULL) { > int count = children->count; > qDebug() << "Number of Children: " << count; > > pANTLR3_BASE_TREE c1 = (pANTLR3_BASE_TREE) children->get(children,0); > pANTLR3_BASE_TREE c2 = (pANTLR3_BASE_TREE) children->get(children,1); > value1 = evaluate(c1); > value2 = evaluate(c2); > > } > > There's extra bits of code between and surrounding but this is just > showing the basics of what i'm trying to achieve. When i first enter > this method i > do: > > pANTLR3_VECTOR children = m_tree->children; pANTLR3_BASE_TREE root = > (pANTLR3_BASE_TREE) children->get(children,0); m_currValue = > evaluate(root); > > Because i need to ignore the first part of the node. The problem i get > now is that passing root in i wont get the correct node text, and either > with the children, its just printing out garbage. Is there anything i'm > doing thats obviously wrong. I just had the thought that maybe i should > be declaring root as a class variable to keep it in scope and cleaning > it up in the destructor?? In my full implementation you can get several > levels of recursion deep, i.e. evaluate(c1) may go a number of extra > levels down. > > Any help would be great on this problem, its probably something obvious, > lol > :) > > Tom > > List: http://www.antlr.org/mailman/listinfo/antlr-interest > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your- > email-address List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
