On 11/15/16 01:29, Joey Ezechiëls wrote:
> I see, indeed I have seen this around in examples on the web. But what is never made clear is how to inform ANTLR that that is the whitespace rule, surely including the WS rule in the grammar alone is not enough?
No need to "inform ANTLR". The fact that the tokens are being "skip"ed
should be sufficient. ANTLR tries to match the longest token it can
when tokenizing. The WS rule will cause an "end of token" when
appropriate. What you put in your WS rule depends on what is required
for your grammar. For example, you would not put \n or \r in it if
end-of-lines are significant in your grammar.... I have found that
skipping all whitespace in my grammars is appropriate (except in the
lexer when I'm doing pre-processor type stuff; Ter once had an option in
lexer rules to ignore whitespace "options {ignore=WS;}", but he removed
it in later versions of ANTLR).
> I wonder about this because I would prefer to not litter other rules with WS nonterminals, assuming I even have a choice there.
In this case, WS becomes a terminal rule, and it is skipped, it is not
emitted into the token stream, so you do not have to have it in your
parser rules. It is used to only separate tokens. What complicates
things is when your grammar NEEDS to recognize WS, then it becomes a
pain to deal with.