6

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

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
OscarRyz
  • 196,001
  • 113
  • 385
  • 569

1 Answers1

4

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.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • BTW what's the convention `Number` or `NUMBER` ? – OscarRyz Jun 10 '11 at 19:44
  • @Oscar, there's no real convention. However, at a later stage (more complicated grammars), you're likely to use so called "imaginary tokens", which I like to distinguish from "normal" tokens. So I use all-caps for imaginary tokens and a single capital for "normal" tokens (token == lexer rule). Whatever fancies you though... – Bart Kiers Jun 10 '11 at 19:48
  • *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.* Actually I don't :P I *think* I know but I wouldn't mind to read your explanation – OscarRyz Jun 10 '11 at 19:56
  • I was about to create a new question for this and I found: http://stackoverflow.com/questions/4297770/practical-difference-between-parser-rules-and-lexer-rules-in-antlr/4298122#4298122 – OscarRyz Jun 10 '11 at 19:57
  • @Oscar, I can never find any of my previous answers, so I was about to edit my current answer and add an explanation! :) Good to see you _can_ find stuff on SO! – Bart Kiers Jun 10 '11 at 19:59
  • Same happens to me.. I think the search when you create a question is slightly different from the regular search. At least that's the impression I have. – OscarRyz Jun 10 '11 at 20:07
  • I asked on meta http://meta.stackexchange.com/questions/94674/do-the-search-in-new-question-work-different-to-the-regular-search – OscarRyz Jun 10 '11 at 21:17
  • Thanks, will keep an eye on it. – Bart Kiers Jun 10 '11 at 21:26