Question

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)

Was it helpful?

Solution

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;}
    ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top