[antlr-interest] Another (hopefully easy) newby question

Scott Smith ssmith at mainstreamdata.com
Fri Aug 5 11:31:57 PDT 2011


I woke up this morning and realized what the problem was.  You can't access the "tree" member variable in filter_return directly because it's private.  So, I have to use getTree().  The return type for getTree() is a standard java Object.  However, I know it's a CommonTree (since that's what I specified in the parser definition).  So, I simply needed to cast the return to CommonTree.  CommonTree supports the toStringTree() and things now work as I wanted.  toStringTree() was coming up undefined because of the type of return for getTree() and that's what had me confused.

Thanks for your help

Scott

-----Original Message-----
From: John B. Brodie [mailto:jbb at acm.org] 
Sent: Thursday, August 04, 2011 8:09 PM
To: Scott Smith
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Another (hopefully easy) newby question

On Fri, 2011-08-05 at 01:15 +0000, Scott Smith wrote:
> I have created a parser/lexer.  When I run it as a standard parser (no ASTs),
> it runs fine.  I've verified with the debugger, that it generates a reasonable tree.
> 
> But, I want to run it to generate ASTs.  So, I modified the code to do that
> (using ^ to promote operators and ! to eliminate some things).  I believe that
> is working just fine as well.
> 
> So, here's the problem.  My test harness looks like:
> 
>       String filename = ".\\somefile.txt";
>       CharStream stream = new ANTLRFileStream(filename);
> MyFilterLexer lexer = new MyFilterLexer(stream);
> TokenStream tokenStream = new CommonTokenStream(lexer);
>       MyFilterParser parser = new MyFilterParser(tokenStream);
>       filter_return f = parser.filter();
>       System.out.println(f.getTree().toString());
> 
> I want the print out to display the AST version of the parsed data.  I know it
> exists (at least mostly) because I can modify the top level parser item in my
> combined grammer as follows:
> 
> filter:
>   FQ '='^ filter_expr EOF!
>     {
>       if ($filter_expr.tree != null)
>         System.out.println($filter_expr.tree.toStringTree());
>       else
>         System.out.println("must be null");
>     }
>   ;
> 
> This compiles and does display (almost all) the ASTs as I would expect.  Note that
> it is referencing the "filter_expr" and not the "filter".  It does not display the
> "(= FQ" but the rest of my ASTs appear to be properly parsed and ordered.  If I
> change to use $filter instead, then I get the "must be null" message.

when using the ^ and/or ! meta-operators, the result tree is not created
until the end of the rule.

technically your printing action is not at the end of the rule because
stuff could appear after that action.

put your print code in the rule's @after{...} section and a reference to
the rulename.tree will not be null.

> 
> Isn't there a way to display the string of ASTs from the filter_return object? 
> Scott Stanchfield's video would say you could do "System.out.println(f.tree.getStringTree());",
> but that doesn't compile with Antlr 3.4.
> 
> If I run the harness as I have it above, it simply prints "=".
> 

the toString() method of CommonTree does not recurse into the tree's
children (if any). so that is why you see just the root node printed by
your driver.

need to use the toStringTree() method in your driver in order to get the
entire tree printed --- just as you did in your rule's printing action.





More information about the antlr-interest mailing list