[antlr-interest] Selective ignoring of whitespace

Micha micha-1 at fantasymail.de
Thu May 14 12:21:47 PDT 2009


On Thursday 14 May 2009 19:02:56 Ratul Bhadury wrote:
> Hello all,
>
> I've been trying to achieve something using Antlr v3, but have been
> struggling for a while now, and think its now time to ask the help of more
> experienced users, hence this mail.
>
> What I basically want to achieve is a simple parser which will accept lines
> of the nature:
>
> sec = <keyword>
>
> I want any whitespace (spaces and tabs) before the '=' sign to be ignored,
> but any spaces after '=' to be included in what will be regarded as the
> keyword. Therefore:
> sec= hello    -> is valid
> sec      =hello     people  -> is valid
>          sec     =     hello     -> is valid
> se=    hello   -> is rejected

something like this? (this read only one clause),
KEYWORD includes the whitespace at the beginning, but not at the end.

grammar Key;

start : clause EOF;

clause : SECURITY KEYWORD WS* ;

fragment WS : ' ' | '\t';
fragment NUMBER : ('0' .. '9')+;
fragment WORD : ('a' .. 'z')+;

SECURITY : ('S'|'s')('E'|'e')('C'|'c') WS* '=' ;

KEYWORD : WS* (WORD | NUMBER);



 cheers,
 Michael



More information about the antlr-interest mailing list