views:

180

answers:

2

I am trying to write a comment matching rule in ANTLR, which is currently the following:

LINE_COMMENT
    : '--' (options{greedy=false;}: .)* NEWLINE {Skip();}
    ;

NEWLINE : '\r'|'\n'|'\r\n' {Skip();};

This code works fine except in the case that a comment is the last characters of a file, in which case it throws a NoViableAlt exception. How can i fix this?

+1  A: 

Why not:

LINE_COMMENT     : '--' (~ NEWLINE)* ;
fragment NEWLINE : '\r' '\n'? | '\n' ;

If you haven't come across this yet, lexical rules (all uppercase) can only consist of constants and tokens, not other lexemes. You need a parser rule for that.

cletus
Don't you mean `fragment NEWLINE` instead of `token NEWLINE`? And since `NEWLINE` potentially exists of more than one character, does the negation still work in the `LINE_COMMENT` rule?
Bart Kiers
except the change to `fragment` and a modification to the NEWLINE fragment, this works. Thanks! can't elieve i didn't think of it...
RCIX
@RCIX: corrected the `fragment` part. Sorry, bit rusty. Glad it worked.
cletus
+1  A: 

I'd go for:

LINE_COMMENT
  :  '--' ~( '\r' | '\n' )* {Skip();}
  ;

NEWLINE 
  :  ( '\r'? '\n' | '\r' ) {Skip();}
  ;
Bart Kiers