[antlr-interest] C++ Function to display AST?

Peggy Fieland madcapmaggie at yahoo.com
Mon Jun 26 08:19:22 PDT 2006


I'm sure you'll get many answers to this,
but here is the function I use
(note you need the include files, "using namespace
std", etc...

Peggy
/* *****************************************/
void walkTree(RefAST  t, int depth)
{
  if (!t)
    return;

  int counter = t->getNumberOfChildren();
  if (depth > 0)
    {
      for (int i = 0; i < depth; i++)
	cout << " " ;
    }
  string text = t->getText();
  int type = t->getType();

  cout << " TOKEN " << t->getText()  << " at depth "
<< depth 
       << " of type " << t->getType() ;
  cout  << " and number of children " << counter <<
endl;

  if (t->getFirstChild())
    walkTree(t->getFirstChild(), depth+1);

  if (t->getNextSibling())
    walkTree(t->getNextSibling(), depth);

}



--- "Wigg, J D" <wiggjd at lsbu.ac.uk> wrote:

> Please could someone point me to an implementation
> of a C++ function
> to display (screen and/or text) an AST (created by a
> C++ version of
> TreeParser)?
>  
> Thanks,
>  
> David.
>  
>  
> 



More information about the antlr-interest mailing list