13

I have the following deffinition of fragment:

fragment CHAR   :'a'..'z'|'A'..'Z'|'\n'|'\t'|'\\'|EOF;  

Now I have to define a lexer rule for string. I did the following :

STRING   : '"'(CHAR)*'"'

However in string I want to match all of my characters except the new line '\n'. Any ideas how I can achieve that?

Andrey
  • 479
  • 2
  • 10
  • 20

1 Answers1

18

You'll also need to exclude " besides line breaks. Try this:

STRING : '"' ~('\r' | '\n' | '"')* '"' ;

The ~ negates char-sets.

ut I want to negate only the new line from my CHAR set

No other way than this AFAIK:

STRING : '"' CHAR_NO_NL* '"' ;

fragment CHAR_NO_NL : 'a'..'z'|'A'..'Z'|'\t'|'\\'|EOF;  
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Well sorry I am new to antlr ... what I am asking is : I previously defined string as a sequence of CHARs. In your deffinition of STRING I can't see where did u put CHAR. So how the program will know that it should take everything from CHARS except the new line character? – Andrey Feb 14 '13 at 23:32
  • Yes but I want to negate only the new line from my CHAR set .. in your case I take everything except the new line – Andrey Feb 15 '13 at 09:53