ANTLR4 grammar rule skip all comments except...

2,380 views
Skip to first unread message

Jörg Pichler

unread,
Jul 12, 2013, 3:53:36 AM7/12/13
to
Hi everybody,

I have the following lexer rule:

COMMENT : ('*'|'!'|'.') (.)*? '\n' -> skip;

My Input might contain *ABC or *CDE, now I want to have those (and only those) tokens to be available in my parse listener (in one way or another), meaning that the parser needs to get these tokens. Is there a special trick to do this?

Thanks.

Sam Harwell

unread,
Jul 12, 2013, 9:23:27 PM7/12/13
to antlr-di...@googlegroups.com

No. The “skip” command completely suppresses even the generation of the token. If you want to generate the Token but hide it from the parser, use the following command instead:

 

COMMENT : ('*'|'!'|'.') (.)*? '\n' -> channel(HIDDEN);

 

Also, you should be aware that your current rule will not allow a COMMENT to appear at the end of a file which does not contain a final newline character. You might consider the following rule instead:

 

COMMENT : ('*'|'!'|'.') ~'\n'-> channel(HIDDEN);

 

Thank you,

--

Sam Harwell

Owner, Lead Developer

http://tunnelvisionlabs.com

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
Message has been deleted
0 new messages