title: Proper way to resolve ANTLR lexer rule ambiguities?
body: <p>Please see the source code available at: <a href="https://gist.github.com/1684022" rel="noreferrer">https://gist.github.com/1684022</a>.</p>
  
  <p>I've got two tokens defined:</p>
  
  <pre><code>ID  :   ('a'..'z' | 'A'..'Z') ('0'..'9' | 'a'..'z' | 'A'..'Z' | ' ')*;
  
  PITCH   
      :   (('A'|'a') '#'?)
      |   (('B'|'b') '#'?) 
      |   (('C'|'c') '#'?);
  </code></pre>
  
  <p>Obviously, the letter "A" would be an ambiguity. </p>
  
  <p>I further define:</p>
  
  <pre><code>note    :   PITCH;
  name    :   ID;
  main    :   name ':' note '\n'?
  </code></pre>
  
  <p>Now, if I enter "A:A" as input to the parser, I always get an error. Either the parser expects PITCH or ID depending on whether ID or PITCH is defined first:</p>
  
  <pre><code>mismatched input 'A' expecting ID
  </code></pre>
  
  <p>What is the proper way to resolve this so that it works as intended?</p>
  
  <hr>
  
  <p>As is described, although it makes intuitive sense how the parsing should work, ANTLR doesn't do the "right thing". That is, even though the <code>main</code> rule says a <code>name</code>/<code>ID</code> should come first, the lexer seems to be ignorant of this and identifies "A" as a <code>PITCH</code> because it follows the "longest match"/"which comes first" rule rather than the more reasonable "what the rule says" rule.</p>
  
  <p>Is the only solution to fake/hack it by matching both ID and PITCH, and then recombining them later as dasblinkenlight says?</p>