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

John B. Brodie jbb at acm.org
Thu Aug 4 19:09:09 PDT 2011


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