Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The following alternatives can never be reached: 2

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:

This is what it looks like

like image 738
OscarRyz Avatar asked Jun 10 '11 19:06

OscarRyz


1 Answers

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 made number a parser rule, it does not do what you think it does (and are therefor receiving an obscure error message).

The solution: make number a lexer rule instead (so, capitalize it: Number):

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();} ;

And as you can see, I also made id, click and rclick lexer 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.

like image 90
Bart Kiers Avatar answered Oct 10 '22 00:10

Bart Kiers