[antlr-interest] Are there any functioning Java 1.5 grammars for ANTLR?

Oliver Wong owong at castortech.com
Thu Mar 23 14:22:58 PST 2006


I went to http://www.antlr.org/grammar/list to look for a Java 1.5
grammar for ANTLR, but I couldn't get any of the ones I tried to work.

The "Updated Java 1.5 Grammar" by Scott Wisniewski, for example, Java
generates code which seems to be using a keyword "internal" which
doesn't exist in Java. Specifically, it generates code like:

<code>
internal class JavaLexer extends antlr.CharScanner implements
JavaTokenTypes, TokenStream
 {
</code>

With "Yet another java 1.5 grammar" by Michael Stahl, when I try to a
very simple Java program with the following code:

<code>
import antlr.RecognitionException;
import antlr.TokenStreamException;
import antlr.collections.AST;

public class Test {
  public static void main(String args[]) throws RecognitionException,
TokenStreamException {
    JavaLexer lexer = new JavaLexer(System.in);
    JavaRecognizer recognizer = new JavaRecognizer(lexer);
    recognizer.compilationUnit();
    AST tree = recognizer.getAST();
    System.out.println(tree.toStringTree());
  }
}
</code>

The resulting AST only contains the first import statement of the parsed
file, rather than the whole file. Specifically, when I try to parse the
code for Test which I've posted above (i.e. running the parser on
itself), the output is:

<output>
( import ( . antlr RecognitionException ) )
</output>

Instead of the expect output of something like

<expectedOutput>
( compilationUnit
  (import
    (. antlr RecongitionException )
  )
  (import
    (. antlr TokenStreamException )
etc.
</expectedOutput>

And so on, for every other grammar I've tried. I simply haven't had any
luck in getting any of the grammar files to generating working parsers.

I'm using ANTLR 2.7.6. What am I doing wrong? It seems implausible that
EVERY grammar file posted on there is broken, so I must be
misunderstanding something.

	- Oliver


More information about the antlr-interest mailing list