I’m trying to create a very simple grammar to learn to use ANTLR but I get the following message:
“The following alternatives can never be reached: 2”
This is my grammar attempt:
grammar Robot;
file : command+;
command : ( delay|type|move|click|rclick) ;
delay : 'wait' number ';';
type : 'type' id ';';
move : 'move' number ',' number ';';
click : 'click' ;
rclick : 'rlick' ;
id : ('a'..'z'|'A'..'Z')+ ;
number : ('0'..'9')+ ;
WS : (' ' | '\t' | '\r' | '\n' ) { skip();} ;
I’m using ANTLRWorks plugin for IDEA:

The
..(range) inside parser rules means something different than inside lexer rules. Inside lexer rules, it means: “from char X to char Y”, and inside parser rule it matches “from token M to token N”. And since you madenumbera parser rule, it does not do what you think it does (and are therefor receiving an obscure error message).The solution: make
numbera lexer rule instead (so, capitalize it:Number):And as you can see, I also made
id,clickandrclicklexer rules instead. If you’re not sure what the difference is between parser- and lexer rules, please say so and I’ll add an explanation to this answer.