title: Characters Matching Multiple Lexer Rules in ANTLR
body: <p>I've defined multiple lexer rules that potentially matches the same character sequence.  For example:</p>
  
  <pre><code>LBRACE:  '{' ;
  RBRACE: '}' ;
  LPARENT: '(' ;
  RPARENT: ')' ;
  LBRACKET: '[' ;
  RBRACKET: ']' ;
  SEMICOLON: ';' ;
  ASTERISK: '*'  ;
  AMPERSAND: '&amp;'  ;
  
  IGNORED_SYMBOLS:   ('!' | '#' | '%' | '^' | '-' | '+' | '=' | 
                      '\\'| '|' | ':' | '"' | '\''| '&lt;' | '&gt;' | ',' | '.' |'?' | '/'  ) ;
  
  
  // WS comments*****************************
  WS: (' '|'\n'| '\r'|'\t'|'\f' )+ {$channel=HIDDEN;};
  ML_COMMENT: '/*' .* '*/' {$channel=HIDDEN;};
  SL_COMMENT: '//' .* '\r'? '\n' {$channel=HIDDEN;};
  
  STRING_LITERAL:  '"' (STR_ESC | ~( '"' ))* '"'; 
  fragment STR_ESC:  '\\'  '"'  ; 
  
  CHAR_LITERAL :  '\'' (CH_ESC | ~( '\'' )) '\''  ;  
  fragment CH_ESC :  '\\' '\''; 
  </code></pre>
  
  <p>My IGNORED_SYMBOLS and ASTERISK match /, " and * respectively.  Since they're placed (unintentionally) before my comment and string literal rules which also match /* and ", I expect the comment and string literal rules would be disabled (unintentionally) .  But surprisely, the ML_COMMENT, SL_COMMENT and STRING_LITERAL rules still work correctly.</p>
  
  <p>This is somewhat confusing. Isn't that a /, whether it is part of /* or just a standalone /, will always be matched and consumed by the IGNORED_SYMBOLS first before it has any chance to be matched by the ML_COMMENT?</p>
  
  <p>What is the way the lexer decides which rules to apply if the characters match more than one rule?</p>