[antlr-interest] handling single-line comments
John Ridgway
john at jacelridge.com
Wed Dec 12 10:19:19 PST 2007
The problem is the '\r'? in NEWLINE. You need to add it to the
COMMENT rule as well. Here's the appropriate portion of the Java
grammar's lexer rules:
WS : ( ' '|'\r'|'\t'|'\u000C'|'\n' ) {$channel=HIDDEN;}
;
COMMENT
: '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
;
LINE_COMMENT
: '//' ~( '\n'|'\r' )* '\r'? '\n' {$channel=HIDDEN;}
;
Note that the LINE_COMMENT rule contains ~('\n'|'\r'), not just (~'\n').
You can get the whole grammar from the ANTLR web-site.
Peace
- John
On Dec 12, 2007, at 11:10 AM, Mark Volkmann wrote:
> Here's a grammar containing just three lexer rules.
>
> grammar BadLexer;
>
> COMMENT: '#' (~'\n')* NEWLINE { $channel = HIDDEN; };
> NEWLINE: ('\r'? '\n')+ { $channel = HIDDEN; };
> WHITESPACE: (' '|'\t')+ { $channel = HIDDEN; };
>
> When I run Grammar ... Check Grammar on it in ANTLRWorks I get the
> following warning.
>
> warning(200): BadLexer.g:3:20: Decision can match input such as
> "{'\u0000'..'\t', '\u000B'..'\uFFFE'}" using multiple alternatives: 1,
> 2
> As a result, alternative(s) 2 were disabled for that input
>
> There must be a better way to write the COMMENT rule. Suggestions
> would be appreciated.
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
More information about the antlr-interest
mailing list