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

Curt Carpenter Curt.Carpenter at microsoft.com
Thu Jan 29 08:14:46 PST 2009


Hi all, I am 1 day in on ANTLR, so be gentle. :)

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090129/4b7fd05d/attachment.html 


More information about the antlr-interest mailing list