[antlr-interest] Creating an AST from a Parsers output

David Holroyd dave at badgers-in-foil.co.uk
Sun Aug 12 04:13:31 PDT 2007


On Fri, Aug 10, 2007 at 11:09:25AM -0700, Eric Crahen wrote:
> I'd like to create an AST representation of a Java source file, not the
> grammar for the language, a source file. I can't seem to find any way to
> actually convert the output of a Lexer or Parser into a Tree. One of the
> classes in the tree package, DOTTreeGenerator seems like it might have been
> a step in this direction, but the Lexer and Parser classes generated with
> ANTRL 3.0 don't have the e() method.

The reference to an 'e()' method is based on an example grammar defining
a rule called 'e'.  You should replace e() with a method call relevant
to the grammar you are using, e.g. compilationUnit().

To have ANTLRv3 parser create AST's, you need the grammar to specify the
option 'output=AST'.  With this option enabled, it becomes possible to
retrieve the AST produced by a rule from the rule return value (i.e. the
'Tree t = (Tree)r.tree' line in the example you found).

IIRC, the example Java grammars supplied with ANTLR don't build trees,
but I think someone on this list was working on a version that does..?


ta,
dave

-- 
http://david.holroyd.me.uk/


More information about the antlr-interest mailing list