[antlr-interest] Curious behaviour: unused rule has unknown effects

Gavin Lambert antlr at mirality.co.nz
Tue Mar 24 11:48:27 PDT 2009


At 07:27 25/03/2009, Rodrigo C. L. wrote:
 >Defining the literals as tokens is the same as putting them in 
the
 >lexer?

Not quite; if you put them in the tokens block then ANTLR issues 
warnings (for no readily apparent reason).

The best thing to do is to just make them proper lexer rules; ie. 
convert this:

toBeRemoved returns [String out]
       : 'switch' '(' expression ')' '{' ( case_statement |
default_statement | statement )* '}'
       ;

into this:

SWITCH: 'switch';
LPAREN: '(';
RPAREN: ')';
LBRACE: '{';
RBRACE: '}';

toBeRemoved returns [String out]
       : SWITCH LPAREN expression RPAREN LBRACE ( case_statement |
default_statement | statement )* RBRACE
       ;

The above is effectively what ANTLR will be doing behind the 
scenes when you use literals in parser rules anyway, but doing it 
explicitly means (a) you get to specify the token name (which 
improves readability, since the default names are T53 etc) and (b) 
you can group all the lexer rules together, which aids in 
readability, discovery, and resolution of ambiguity.



More information about the antlr-interest mailing list