[antlr-interest] ANTLR 3.3 Bug?

Kyle Ferrio kferrio at gmail.com
Fri Dec 31 08:39:23 PST 2010


David,

Sam's advice is wise.  BOth the ANTLR IDE and ANTLRWorks rely on antlr
itself for many diagnostics, which may take some time to interpret. (I say
this as someone who tends to understand error messages after the error is
fixed.)  I just happen to have the ANTLR IDE 2.1.1 open right now, so I did
this, without needing to know more about your grammar:

*grammar DB;

program : declaration* ;

declaration
    : tickSpec
    | location
    ;

tickSpec
    : 'TICK' '=' INTEGER
    ;

location
    : 'LOC' '=' '(' numericalRange ',' numericalRange ')'
    | '$TITLE'
    | '$LEGEND'
    ;

// This is a total guess.
numericalRange
    : INTEGER '..' INTEGER
    ;

// This is a pretty good guess.
INTEGER : '0'..'9'+ ;

WS : (' '|'\t'|'\r'|'\n'|'\f')+ {$channel=HIDDEN;};*

ANTLR IDE saves this grammar with no errors, and the following input parses
exactly as expected in the interpreter:

*TICK = 11
LOC = (1..4,7..11)
*
A couple suggestions from experience:  Like Sam's advice about apostrophes,
be very careful about ANTLR's own rule-delimiting tokens: semicolons, colons
and vbars (|'s). (Syntax highlighting in ANTLR IDE will not help you here.)
The usual divide-and-conquer approach to debugging applies. Since your
grammar worked fine for me with just a little glue, it may be that problems
are being detected some "distance" away from inception.  And as always make
sure you have a suitable whitespace rule and that its not left on the
default channel unless whitespace is actually semantic in your language (not
recommended).

Good Luck!

Kyle


On Thu, Dec 30, 2010 at 8:37 PM, David Beckedorff <
david.beckedorff at gmail.com> wrote:

> Antlr-interest List,
>
> I'm trying to build a parser with the ANTLR IDE 2.1.1 Eclipse plugin and
> the
> ANTLR 3.3 library. I'm getting a bunch of cryptic error messages for some
> of
> my parser rules.
>
> For example, for this rule:
>
> tickSpec
>    :   'TICK' '=' INTEGER
>    ;
>
> I get:
>
> error(100): /EZM Project 01/src/com/dlb/ezm/ezm01/Ezm01.g:174:14: syntax
> error: antlr: unexpected token: ' '
>  |---> :   'TICK' '=' INTEGER
>
>
> and for the rule:
>
> location
>    :   ( 'LOC' '=' '(' numericalRange ',' numericalRange ')' )
>    |   '$TITLE'
>    |   '$LEGEND'
>    ;
>
> I get:
>
> error(100): /EZM Project 01/src/com/dlb/ezm/ezm01/Ezm01.g:182:15: syntax
> error: antlr: unexpected token: ' '
>  |---> :   ( 'LOC' '=' '(' numericalRange ',' numericalRange ')' )
>
> error(100): /EZM Project 01/src/com/dlb/ezm/ezm01/Ezm01.g:182:23: syntax
> error: antlr: unexpected token: ' numericalRange '
>  |---> :   ( 'LOC' '=' '(' numericalRange ',' numericalRange ')' )
>
>
>
> Why do I get an unexpected token ' ' error message for the whitespace
> between tokens like 'TICK' or 'LOC' and '=' ? Why doesn't ANTLR just absorb
> the whitespace and move on to the '=' token??
>
> I have a whitespace rule in effect, namely:
>
> WS : (' '|'\t'|'\n'|'\r'|'\f')+ { $channel = HIDDEN; } ;
>
>
> -David Beckedorff
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list