[antlr-interest] C# target doesn't do numbers

Gavin Lambert antlr at mirality.co.nz
Wed Jun 27 03:51:14 PDT 2007


At 22:34 27/06/2007, Vaclav Barta wrote:
 >Err, what is a fragment rule and how do I make it? My ANTLR
 >knowledge is rather dismal... What I tried was removing
 >Digit altogether (it was used in another place in the full
 >grammar, but that could be worked around), and
 >
 >Integer : ( '0'..'9' )+ ;
 >
 >does work - but still, it would be nice to understand why... :-)

Perhaps you should read the ANTLR book -- or the website :)

A fragment rule is easily created -- just put the word 'fragment' 
in front of it.  This is what was known as a 'protected' rule in 
v2; a lexer rule that is intended as a fragment of another rule's 
definition rather than a possible output of the lexer in its own 
right.

Because the Digit rule was listed first and wasn't a fragment, 
when the lexer saw a 1 it was generating a Digit token.  Since 
your parser didn't recognise Digits, only Integers, you had a 
failed match.  Making it a fragment removes it as a top-level 
candidate and the lexer will output an Integer instead.



More information about the antlr-interest mailing list