views:

143

answers:

1

I'm new to ANTLR and i've come up with this lexer rule to parse out comments, will it work?

COMMENT_LINE     : (COMMENT (. - LINE_ENDING)* LINE_ENDING){$channel=hidden};

(I couldn't find anything regarding syntax such as this in the docs)

+2  A: 

Your rule doesn't compile at all. If you use ANTLRWorks to create a new lexer grammar, you can check a box to have it generate a lexer rule that matches single line comments. It generates this:

COMMENT
    : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    ;

Alternatively, you can use something like this to match single line comments:

COMMENT_LINE 
    : COMMENT (options{greedy=false;}: .)* LINE_ENDING {$channel=HIDDEN;}
    ;
Bojan Resnik
Cool. any reason why ANTLRWorks states that "Cannot display rule COMMENT_LINE because start state not found"?
RCIX
Never mind, i figured it out.
RCIX