Range element cannot be used in parser rule?

I have the following grammar:

grammar tryout; tryout : my_cmd ; my_cmd : 'start' '0'..'9'+ Name_string ; Digit : '0'..'9' ; Name_string : ('A'..'Z' | 'a'..'z') ('A'..'Z' | 'a'..'z' | '0'..'9' | '_')* ; 

If I see a diagram in ANTLRworks, '0' .. '9' + shows as an empty element, therefore, Java code compilation does not work because the generated code has an "if ()" operator; if I run on the command line, compilation also fails.

The fix is ​​to move '0' .. '9' + to the lexer rule.

 grammar tryout; tryout : my_cmd ; my_cmd : 'start' Digit+ Name_string ; Digit : '0'..'9' ; Name_string : ('A'..'Z' | 'a'..'z') ('A'..'Z' | 'a'..'z' | '0'..'9' | '_')* ; 

But I wonder if this is a mistake. Why can't a range element be used in a parser rule? This is on ANTLR v3.4.

+4
source share
1 answer

Inside the parser rules .. does not work as a range operator for characters, as in lexer rules. Also note that even if you defined literals inside parser rules, ANTLR created lexer rules for them on the fly by doing the following:

 my_cmd : 'start' '0'..'9'+ Name_string ; 

is equivalent to:

 my_cmd : Start D0..D9+ Name_string ; Start : 'start'; D0 : '0'; D9 : '9'; 

If I need memory, earlier versions of ANTLR v3 support for the range operator inside the parser rules mean: matching any token between D0 and D9 , but that / was extremely fragile. Adding a rule between D0 and D9 will change its value:

 D0 : '0'; FOO : 'foo'; D9 : '9'; 

Parser rule:

 my_cmd : '0'..'9'+ ; 

will now correspond to one of the following tokens: D0 , FOO or D9 .

This support .. inside the parser rules has been removed (at least) v3.3 and higher. Therefore, do not use the rules .. inside the parser rules.

+1
source

All Articles