[antlr-interest] MissingTokenException

Kevin J. Cummings cummings at kjchome.homeip.net
Wed Feb 16 07:54:17 PST 2011


On 02/16/2011 10:38 AM, Wilbur Lang wrote:
> Hi,
> 
> I'm working on ANTLR to create a parser but failed with 
> MissingTokenException. Following is the grammar:
> 
> 
> grammar test1;
> 
> moduleId
>      :    TYPEREFERENCE
>      ;
> 
> WS  :   ( ' '| '\t'| '\r'| '\n') {$channel=HIDDEN;}
>      ;
> 
> fragment UPPER :    ('A'..'Z') ;
> fragment LOWER :    ('a'..'z') ;
> fragment DIGIT :    ('0'..'9') ;
> HYPHEN :    '-' ;
> 
> CAPITALREFERENCE
>      :    UPPER (UPPER|DIGIT)* (HYPHEN (UPPER|DIGIT)+)*
>      ;
> 
> TYPEREFERENCE
>      :    UPPER (UPPER|LOWER|DIGIT)* (HYPHEN (UPPER|LOWER|DIGIT)+)*
>      ;
> 
> When I input sentence 'T3', ANTLR failed with 'MissingTokenException'. 
> When I comment 'CAPITALREFERENCE', ANTLR worked. How can I correct the 
> grammar?

I'd do the following:

fragment TYPEREFERENCE : ;

CAPITALREFERENCE
    : UPPER (UPPER | DIGIT | LOWER { $type=TYPEREFERENCE; })*
     (HYPHEN (UPPER | DIGIT | LOWER { $type=TYPEREFERENCE; })+ )*
    ;

That way you'll only change the token type to TYPEREFERENCE if 1 or more
LOWER characters are found....

> Please note, I need 'CAPITALREFERENCE' to distinguish such as 'Tabc-01' 
> and 'TABC-01'.
> 
> Thanks.
> 
> Wilbur Lang

-- 
Kevin J. Cummings
kjchome at verizon.net
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list