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

Scott Smith ssmith at mainstreamdata.com
Thu Aug 4 18:15:23 PDT 2011


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.

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 "=".

What am I missing?

Thanks

Scott



More information about the antlr-interest mailing list