[antlr-interest] Printing the AST

Martin Probst mail at martin-probst.com
Sat Jul 8 07:38:37 PDT 2006


This probably means your AST nodes all have a "null" string value. If  
you construct nodes in code like this:
   AST ast = #[NODE];
it will have no string value. Like this:
   AST ast = #[NODE, "node"];
it will have a value. However, you can access the type and print it  
from the parser, e.g.
   String tokenName = myparser._tokens[ast.getType()];
You'll have to write some trivial recursive code to print the tree  
yourself, e.g.

f(ast) {
   String tokenName = myparser._tokens[ast.getType()];
   print(tokenName);
   print("(");
   for (child: ast.getChildren())
     f(child);
   print(")");
}

HTH,
Martin

Am 08.07.2006 um 03:18 schrieb Amrinder Singh:

> I try to print the tree in the list ofrmat using the function
> "r.tree.toStringTree()" where r is of type  
> MyParser.compilationUnit_return and
> we run the statement "r =  g.compilationUnit();". However, all it  
> prints is a
> list of nulls.
> tree: (null (null null (null null) (null null (null null null) null  
> (null null
> null null null) (null (null (null (null (null null null) null)  
> (null (null
> null))))))))
> How can I print the tree with the symbols?
>
> Amrinder
>
>
>



More information about the antlr-interest mailing list