[antlr-interest] line 0:0: expecting ALPHA, found ''

Kay Roepke kroepke at dolphin-services.de
Mon Feb 6 19:59:43 PST 2006


On 7. Feb 2006, at 3:42 Uhr, karl wettin wrote:

> I'm new to antlr. Just ported my javacc-grammar and get this funky  
> error. I get it even with the most simple tests, so I guess I do  
> something really silly. All the examples bundled works great, even  
> with my StringReader. Greatfull for any help.
>
> header { package se.snigel.tpb.analysis.antlr; }
> class ANTLRParser extends Parser;
> expr: ALPHA;
> class ANTLRLexer extends Lexer;
> protected ALPHA :  'a'..'z';

Hi Karl!

Your problem is the protected lexer rule ALPHA. "protected" means  
that the lexer rule is internal to the lexer. This rule doesn't give
you a token (i.e. it will never come out of a nextToken call on the  
lexer). Thus the parser doesn't get a token and complains that it needs
an ALPHA token but hasn't received one from the lexer.
Just drop the "protected" keyword and all is well.

protected lexer rules can be used to implement "helper" rules for the  
lexer. I think there are a couple of examples in the docs over at
http://www.antlr.org/doc/index.html
specifically look for http://www.antlr.org/doc/lexer.html#lexicalrules
and http://www.jguru.com/faq/view.jsp?EID=125 has a nice explanation  
of this, too.

HTH,

Kay

P.S.: Welcome aboard ;)


More information about the antlr-interest mailing list