[antlr-interest] Recursive Tree Walking C Target
Thomas Davis
thomas.davis at student.adelaide.edu.au
Fri Sep 10 05:39:20 PDT 2010
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
More information about the antlr-interest
mailing list