[antlr-interest] Getting started with Java target?

Gavin Lambert antlr at mirality.co.nz
Mon Jun 9 14:12:06 PDT 2008


At 07:44 10/06/2008, Kevin Kelley wrote:
>I have used ANTLRworks (which is nice, by the 
>way) to generate a Java lexer and parser, and 
>have gotten that code and the ANTLR runtime JAR 
>into a project of mine.  What I want to do now 
>is to get the tree produced by the 
>parser.  Unfortunately, the “how to use the Java 
>runtime” section of the website is incomplete, 
>and all the examples I can Google my way to seem 
>to be for ANTLR 2 or to have other problems.
>
>I have a parser, and am giving it the token 
>stream from the lexer and then invoking the 
>method corresponding to the root rule on the 
>parser, which presumably parses the token 
>stream.  I think what I want to get out of the 
>parser is an 
>“org.antler.runtime.tree.CommonTree” or 
>something similar­but I can’t for the life of me 
>figure out how to get the tree out of the parser.

Have you set 'output=AST' in the options?  If 
not, then ANTLR doesn't produce a tree as 
output.  The three output options are as follows:
   - no output: simply executes actions as things are recognised
   - output=template: uses StringTemplate to generate text output
   - output=AST: generates a tree as output

Note that even with 'output=AST' the default tree 
output is flat.  You'll need to add ^ operators 
or rewrite rules to create a structured tree.

The best reference on ANTLR is the ANTLR book; if 
you don't have that, then your next best bet is 
to look at the example scripts (separate zipfile 
on the download page) and at 
http://www.linguamantra.org/.  At this stage the 
wiki isn't much help as a tutorial, though it's 
not bad as a quick reference.



More information about the antlr-interest mailing list