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

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Sun Sep 25 07:27:23 PDT 2011


It seems very strange to have that as a lexer rule, defined as it is, 
without knowing more about your specific use-case. It would make more 
sense to me to have lexer rules for every different 'meaning' of symbols 
and then have a parse rule for each combination you might want to use. 
The reason I say this is simply in terms of the tokens that are 
generated - it seems very odd to have a single token for all of those 
symbols. Also, it looks like it leads to pretty hard-to-read grammars.

Sam

On 25/09/2011 14:04, ALEC LEE wrote:
>
> 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
>   		 	   		
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list