title: ANTLR grammar to recognize DIGIT keys and INTEGERS too
body: <p>I'm trying to create an ANTLR grammar to parse sequences of keys that optionally have a repeat count. For example, <code>(a b c r5)</code> means &quot;repeat keys a, b, and c five times.&quot;</p>
  <p>I have the grammar working for <code>KEYS : ('a'..'z'|'A'..'Z')</code>.</p>
  <p>But when I try to add digit keys <code>KEYS : ('a'..'z'|'A'..'Z'|'0'..'9')</code> with an input expression like <code>(a 5 r5)</code>, the parse fails on the middle 5 because it can't tell if the 5 is an INTEGER or a KEY. (Or so I think; the error messages are difficult to interpret &quot;NoViableAltException&quot;).</p>
  <p>I have tried these grammatical forms, which work ('r' means &quot;repeatcount&quot;):</p>
  <pre><code>repeat : '(' LETTERKEYS INTEGER ')' - works for a-zA-Z
  repeat : '(' LETTERKEYS 'r' INTEGER ')'; - works for a-zA-Z
  </code></pre>
  <p>But I fail with</p>
  <pre><code>repeat : '(' LETTERSandDIGITKEYS INTEGER ')' - fails on '(a 5 r5)'
  repeat : '(' LETTERSandDIGITKEYS 'r' INTEGER ')'; - fails on '(a 5 r5)'
  </code></pre>
  <p>Maybe the grammar can't do the recognition; maybe I need to recognize all the 5's keys in the same way (as KEYS or DIGITS or INTEGERS) and in the parse tree visitor interpret the middle DIGIT instances as keys, and the last set of DIGITS as an INTEGER count?</p>
  <p>Is it possible to define a grammar that allows me to repeat digit keys as well as letter keys so that expressions like <code>(a 5 123 r5)</code> will be recognized correctly? (That is, &quot;repeat keys a,5,1,2,3 five times.&quot;) I'm not tied to that specific syntax, although it would be nice to use something similar.</p>
  <p>Thank you.</p>