[antlr-interest] Problems with Java 1.5 grammar

Nigel Sheridan-Smith nbsherid at secsme.org.au
Tue Jun 28 16:01:36 PDT 2005

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Ivan Dubrov
> Sent: Saturday, 25 June 2005 4:06 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Problems with Java 1.5 grammar
> Hello,
> I've tried to use one of the Java 1.5 grammars (this one:
> http://www.antlr.org/grammar/1093454600181/java15-grammar.zip), but
> encountered problems with the following code:
> import java.io.StringReader;
> public class Test {
>     public static void main(String[] args) throws Exception {
>         JavaLexer l = new JavaLexer(new StringReader("hello"));
>         JavaRecognizer r = new JavaRecognizer(l);
>         r.expression();
>         System.out.println("OK");
>     }
> }
> In my opinion, "hello" string is the Java expression, so the code should
> print "OK". The grammar tells the same (although I'm not very good at
> ANTLR grammars). But this code throws exception with message 'unexpected
> token "hello"'.
> Anybody can tell me what's the problem here? Anobody tried to use these
> grammars to parse pieces of code?

AFAIK you can't exactly call any rule in the parser to kick-start it - only
ones that aren't called by any other rule (a "top-level production"). 

The reason for this is that the FOLLOW sets are calculated at that rule,
based on the rules that might have called it... However, I think that should
only affect the rule on exit, not entry ("hello" is the first token to
match), so I'm not sure if that is causing the behaviour that you are
seeing, or whether it will be the next problem you face ;).

For some info on FIRST and FOLLOW sets...


Can you rebuild the grammar with -traceParser option on? And send us the


Nigel Sheridan-Smith
PhD research student

Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435 

More information about the antlr-interest mailing list