[antlr-interest] Newbie question about lex token matching priority

Rob Greene robgreene at gmail.com
Fri Jan 30 18:49:38 PST 2009


Hi Curt --

First off, I'm a total hack at ANTLR.  And that's in the golfing sense
with grass flying everywhere and not in the geek sense of being
totally awesome.  Awesome is reserved for others on this list.  So, if
I mislead you, sorry in advance!!

Anyway, from your description, think PARENNUM isn't a lexer rule but a
grammar rule.

So you'd have something like:

language: data+ ;
data: '(' NUMBER ')' NAME ;

and the same lex rules for NUMBER and NAME as before.  Also, don't
forget a whitespace rule (I assume you need one?)

WS: (' ' | '\t' | '\n' | '\r')+ {skip();}

I hope I'm leading you in the right direction at least!
-Rob

On Thu, Jan 29, 2009 at 10:14 AM, Curt Carpenter
<Curt.Carpenter at microsoft.com> wrote:
> Hi all, I am 1 day in on ANTLR, so be gentle. J
>
>
>
> I have gone through the tutorials and such, and have created a grammar from
> scratch, debugged it and have it mostly working, except for one problem. I
> want to parse something like this:
>
> (#)name
>
> Where # is a number, but name can be virtually anything except space. I
> think. I don't own the language, so please don't suggest that name should be
> further restricted. So I defined the lex rules as so:
>
> PARENNUM       : '(' NUMBER ')';
>
> NUMBER             : '-'? ('0'..'9')+;
>
> NAME                   : ('!'..'\u00FE')+; // ansi only
>
>
>
> You can see the problem at NAME. (0)curt is a valid name. But what I really
> want is to parse as PARENNUM=(0) NAME=curt. I have a parse rule to match
> that. But, the lex rules match longest string first, so (0)curt is always
> tokenized as NAME. Is there any way to change the priority of matching lex
> tokens to be the order they're defined, rather than order only breaking ties
> in string length?
>
>
>
> Or is there some other way to accomplish the simple parse rule I'm trying to
> solve?
>
>
>
> Thanks,
>
>
>
> Curt
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>


More information about the antlr-interest mailing list