Assume we want to define a context free grammar of say a programming language, where on each line everything after the character # until the end of line is considered a comment and should be ignored. How to express that in a context free grammar?
Without multi-line statements, it's rather simple; assuming
line is the non-terminal for well-formed line, change its right-hand-side appearances to
line (# .*)?
(borrowing regular expression syntax for brevity).
Otherwise, if you explicitly handle line breaks, replace occurrences of the line-break token
CRLF (or so) similarly by
(# .*)? CRLF
If line breaks remain implicit, one chance you have left is make it part of the lexer; don't only skip whitepace, but also
(# .*)? [\r\n]. You can also create a token
COMMENT for that and place it in all places of the grammar where it may appear. This strategy doesn't work for nested multiline comments, though, a common problem in older languages.
Can just treat comments as whitespaces.