[antlr-interest] Fragments in parser rules - generate error?

Ken Williams ken.williams at thomsonreuters.com
Wed Jun 2 09:45:34 PDT 2010


Hi,

I have the following toy grammar:

-------------------------
date    :    DIGIT+ SLASH DIGIT+ SLASH DIGIT+ ;

SLASH    :    '/' ;
DIGITS    :    DIGIT+ ;
fragment DIGIT    : '0'..'9' ;
-------------------------

(No options{} declarations etc.)

I know I shouldn't be using fragments in this way, but it was an accident -
'DIGIT' used to be a lexer rule, but then I changed it into a fragment,
added DIGITS as a new lexer rule, and forgot to remove DIGIT+ from the
parser rule.

The problem I encountered was that even though the grammar check succeeded
and the lexer/parser classes were successfully generated, the 'date' rule
doesn't match the intended input.

It seems like the right solution would be for Antlr to generate an error
when a fragment is used in a parser rule.

Or is there a reason I'm not thinking of to allow them?

Thanks.

-- 
Ken Williams
Sr. Research Scientist
Thomson Reuters
Phone: 651-848-7712
ken.williams at thomsonreuters.com




More information about the antlr-interest mailing list