Broadcasting Tokens on Different Channels

Most programming languages ignore whitespace and comments in between tokens, which means they can appear anywhere. That presents a problem for a parser since it has to constantly check for optional whitespace and comment tokens. The common solution is to simply have the lexer match those tokens but throw them out, which is what we’ve done so far in this book. For example, our Cymbol grammar from ​Parsing Cymbol​ threw out whitespace and comments using the skip lexer command.

​ WS ​:​ ​[​ ​\​t​\​n​\​r​]+​ ​->​ skip ​;​
​ 
​ SL_COMMENT
​  ​:​ ​'//'​ ​.*?​ ​'\n'​ ​->​ skip
​  ​;​

That works great for many applications, ...

Get The Definitive ANTLR 4 Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.