I need help with my ANTLR 4 grammar after deciding to switch to v4 from v3. I am not very experienced with ANTLR so I am really sorry if my question is dumb ;)
In v3 I used the following code to detect Java-style comments:
COMMENT : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;} | '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;} ;
In v4 there are no rule-specific options. The actions (move to hidden channel) are also invalid.
Could somebody please give me a hint how to do it in ANTLR v4?
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
ANTLR (ANother Tool for Language Recognition) is a tool for processing structured text. It does this by giving us access to language processing primitives like lexers, grammars, and parsers as well as the runtime to process text against them. It's often used to build tools and frameworks.
A lexer (often called a scanner) breaks up an input stream of characters into vocabulary symbols for a parser, which applies a grammatical structure to that symbol stream.
The v4 equivalent would look like:
COMMENT : ( '//' ~[\r\n]* '\r'? '\n' | '/*' .*? '*/' ) -> channel(HIDDEN) ;
which will put all single- and multi line comment on the HIDDEN
channel. However, if you're not doing anything with these HIDDEN
-tokens, you could also skip
these tokens, which would look like this:
COMMENT : ( '//' ~[\r\n]* '\r'? '\n' | '/*' .*? '*/' ) -> skip ;
Note that to tell the lexer or parser to match ungreedy, you don't use options {greedy=false;}
anymore, but append a ?
, similar to many regex implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With