user2969819
user2969819

Reputation: 58

Unexpected ANTLR grammar behavior

using latest ANTLR Works 1.5, and the following grammar:

grammar TestMethod;

ID : ('a'..'z'|'A'..'Z'|'') ('a'..'z'|'A'..'Z'|'0'..'9'|'')* ;

WS : ( ' ' | '\t' | '\r' | '\n' ) ;

ws : (WS)* ;

id : ID ;

expression : id | method ;

method
: identifier ws '(' ws ')' ;

identifier : ( id ( selector )* '.')? id ;

selector : '.' id | '[' ws expression ws ']' ;

I get a NoViableAltException when submitting abcd starting with expression. I don't get this error when starting with id, so since expression has id as an alternative, why does it not work ?

Upvotes: 1

Views: 47

Answers (1)

Olga
Olga

Reputation: 186

A lot of times, not having an EOF (end of file) at the end of whatever it is you're starting with will cause problems. Try putting

expression : (id | method) EOF;

Also, as a side note, it's a lot easier to put [a-zA-Z0-9] instead of ('a'..'z'|'A'..'Z'|'0'..'9'|''), and [ \r\t\n] instead of ( ' ' | '\t' | '\r' | '\n' ), but your versions should work as well.

Upvotes: 1

Related Questions