[antlr-interest] Difference between negative literal and negative fragment ?

Peter Boughton boughtonp at gmail.com
Fri Jan 20 12:32:54 PST 2012


See this example lexer grammar:

	fragment START_TAG    : '<';
	fragment WORDCHAR     : 'a'..'z' | 'A'..'Z' | '0'..'9' | '_';
	
	TAG_START : START_TAG WORDCHAR+ { pushMode(IN_TAG); };
	
	ANY_GENERAL : ~START_TAG+;
	// ANY_GENERAL : ~'<'+;

Using the first ANY_GENERAL rule, it consumes everything.

Swapping for the second ANY_GENERAL rule, it works as intended.

I don't understand why they are not doing the same thing?

Are there any other situations where using a literal over a fragment
will have different behaviour?


More information about the antlr-interest mailing list