0

I have a grammar with the following rule - > verb and verb has 3 token values get, put change see below . if i was to read a file that has more than 3 verbs (get,put and change ) I would like the parser to print an error message . Would it be best to have this embedded in the listener or is there a neat way to do inside the grammar?

Is there a way i can count the token values inside the verb for example ?

    verb    : 
       GET    |   
       PUT    |   
       CHANGE    ;
killio
  • 21
  • 1
  • 4

1 Answers1

0

If I understand what you're asking, this will limit input to no more that three of the verbs. It's kind of cheesy and doesn't scale well, but depending on what you're trying to do it might work for you:

grammar Verb;

verb
 : option option? option? EOF
 ;

option
 : GET
 | PUT
 | CHANGE
 ;

GET    : 'get';
PUT    : 'put';
CHANGE : 'change';

SPACE
 : [ \t\r\n] -> skip
 ;

If you want something more general purpose, check out this SO answer: In ANTLR, is there a shortcut notation for expressing alternation of all the permutations of some set of rules? using predicates. It was written for ANTLR 3, though.

Community
  • 1
  • 1
Jeff French
  • 1,151
  • 1
  • 12
  • 19