[antlr-interest] [newbie] Precedence of rules
rdclark at gmail.com
Thu Jul 10 10:11:04 PDT 2008
On Tue, Jul 8, 2008 at 8:34 AM, Robert Munteanu
<robert.munteanu at gmail.com> wrote:
> COMMA : ',';
> STRING : ('a'..'z'|'A'..'Z')+;
> STRING_WITH_COMMA : STRING (COMMA STRING)+ ;
In the Lexer, the first rule that matches wins. So "hello, world"
matches STRING, COMMA, STRING (3 tokens). If you wanted
STRING_COMMA_STRING to come out as a single token, you would have to
list it first.
Alternately, you could change the name to lowercase to make it a
parser rule (as suggested).
My rule of thumb is to keep the lexer rules simple and move sequences
such as "STRING (COMMA STRING)*" into the parser.
More information about the antlr-interest