[antlr-interest] Rule for Matching All Symbols Except Some Options

ALEC LEE alecleekya at hotmail.com
Sun Sep 25 06:04:32 PDT 2011


I have a lexer rule listing all the acceptable symbols: 
  
ALL_SYMBOLS:   ('!' | '#' | '%' | '^' | '-' | '+' | '='   |  '<' | '>'  | ',' | '.' |'?' | '/' | '*'  ); // and more...... 
 
But sometimes I also want a parser rule to match all symbols except say "! #", "+ =" or  "+ - * /".  Because I have different needs at different time, I want to specify a rule using ALL_SYMBOLS minus some symbols style.  How should one write such a rule?  Can semantic predicate achieve this?  
 
I've never used semantic predicate before.  Are these the proper way to use it:
 
all_symbols_except_hash_equal: ALL_SYMBOLS {"#=".indexOf($text)< 0 }? ;  // can it be done this way?  Can $text be used inside a semantic predicate?
all_symbols_except_comma_plus_minus: {",+-".indexOf($text) < 0}?=>ALL_SYMBOLS ; 
// or should it be done this way? But $text seems not available inside a predicate and there is an failed predicate exception message printed in the screen
 		 	   		  


More information about the antlr-interest mailing list