[antlr-interest] Ignore newlines, except for a specific rule

Anderson Lizardo anderson.lizardo at gmail.com
Mon May 19 17:56:32 PDT 2008


Hi all,

I'm writing a grammar for a domain-specific language called SLED [1].
It has a strange requirement where it ignores every newline (i.e.
treats it as whitespace) except for a specific expression where the
newline is relevant for splitting it into sub-expressions. In
particular, in this "special" expression, only the first token is
required, the other are optional. Here is the relevant grammar
snippet:

////
constructor: opcode operand* (':' const_type)? branches? ;
////

I tried adding the newline to the WS rule (so it could be ignored) and
the NEWLINE explicit for the "special" rule, like this:

///
constructor: opcode operand* (':' const_type)? branches? NEWLINE ;
...
NEWLINE: '\n' { $channel = HIDDEN; } ;

WS:	(' '|'\t'|'\n')+ { $channel = HIDDEN; } ;
///

But then the newline seems to be consumed by the WS rule before the
"constructor" rule is tried. Excluding the newline from the WS rule
and adding it to the rules where it may appear is unfeasible as it may
occur on pretty much everywhere where a whitespace is accepted.

Any suggestions? Thanks,

[1] http://www.cs.virginia.edu/zephyr/csdl/toolkit.html
-- 
Anderson Lizardo


More information about the antlr-interest mailing list