[antlr-interest] antlr works ignoring whitespace

Kay Roepke kroepke at classdump.org
Sun Jan 7 14:58:52 PST 2007


Hi Ron!

On 7. Jan 2007, at 21:53 , Ron AF Greve wrote:

> I am trying to use antlr-works to develop my grammar. It looks like  
> a great tool. I tried to use it to test some input files. However I  
> don't seem to be able to make it ignore whitespace. I tried the v2  
> and v3 ways like skip, channel=99, channel=HIDDEN, filter, but  
> everything seems to fail. As soon as it hits whitespace the grammar  
> fails. Anyone knows how to do this or is it not (yet) possible.
>
> Below my grammar and last attempt with the filter option.

the filter option is not to specify which tokens to skip or hide, it  
is to turn on filter mode in a lexer.
Also, note that the 'protected' keyword is now called 'fragment' in v3.

> Grammar
> ----------------------------------
> grammar HTML;
>
> options {
>
> filter=WHITESPACE;
>
> }

this should be
options {
	filter=true;
}

if you truly want filter mode (see <http://www.antlr.org/wiki/display/ 
ANTLR3/Lexical+filters> for more info on that).
But without knowing what you want to achieve, I'd say you don't want  
filter mode...ANTLRWorks doesn't support filter mode
right now, anyway, because the Interpreter can't handle syntactic  
predicates yet.

> protected
>
> WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+
>
> ;

Use this instead:

WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+  
{ $channel=HIDDEN;} ;

note the missing fragment keyword and the $ in front of the channel.

When you use ANTLRWorks to test sample input it should figure out  
which tokens to ignore automatically.
There's a dropdown list, IIRC, to specify tokens to ignore, but that  
shouldn't be necessary.

I haven't looked too close at your grammar to say whether there's  
another problem somewhere, but this
should get you started.

HTH,

-k


-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list