[antlr-interest] Selective ignoring of whitespace

Ratul Bhadury hola.ratul at gmail.com
Tue May 19 02:12:02 PDT 2009


Thanks so much Michael :)

That worked perfectly. I wouldn't yet know if its the best approach or not,
but it works as I want it to, so I'm more than satisfied! I'll read up more
on syntactic predicates.

If it helps others, this is what Michael proposed...

grammar Space;

@members {

  public static void main(String[] args) throws Exception {
       SpaceLexer lex = new SpaceLexer(new ANTLRStringStream("sec  =  fooz,
foo z foofooz"));
       CommonTokenStream tokens = new CommonTokenStream(lex);

       SpaceParser parser = new SpaceParser(tokens);

       try {
           parser.clause();
       } catch (RecognitionException e)  {
           e.printStackTrace();
       }
   }

}



clause  : keyword_clause EOF;

keyword_clause
   :
     keyword_field { System.out.println($keyword_field.text);} EQ
search_term_list
   ;

keyword_field:  KEYWORD_SEC | KEYWORD_CP ;

search_term_list    :    search_term (',' search_term)* ;

search_term: KEYWORD {System.out.println($KEYWORD.text);} ;

fragment KEYWORD_SEC: 'sec';
fragment KEYWORD_CP: 'cp';
fragment KEYWORD: CHAR (CHAR | ' ')* ;


ALL_KEYWORD:
           ( (KEYWORD_SEC WS* EQ) => KEYWORD_SEC { $type = KEYWORD_SEC; }
           | (KEYWORD_CP WS* EQ) => KEYWORD_CP { $type = KEYWORD_CP; }
           | (KEYWORD) => KEYWORD { $type = KEYWORD; }
           )
           ;

EQ            : '=';
 fragment CHAR   :
ALPHA_UC|ALPHA_LC|
DIGIT|'$'|'£'|'&'|'^'|'%'|'('|')'|'['|']'|':'|';'|'*'|'<'|'>'|'/'|'\\'|'@'|'#'|'?'|'!'|'.';

fragment ALPHA_UC   : 'A'..'Z';
fragment ALPHA_LC   : 'a'..'z';
fragment DIGIT        : '0'..'9';
WS        :    (' ' | '\t')+ {$channel=HIDDEN;}
       ;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090519/89bf08b7/attachment.html 


More information about the antlr-interest mailing list