[antlr-interest] Lexer problem

Richard Clark rdclark at gmail.com
Mon Mar 10 22:29:52 PDT 2008


On Mon, Mar 10, 2008 at 10:15 PM, Brent Yates <brent.yates at gmail.com> wrote:

> That being the case, how do I get alts1 and 2 to match when the ID='aaa'
> or ID='bbb' and to not fall into alt3 if they don't match completely?


How about post-processing in the Lexer instead of predicates?

TEST
@init { $type = ID; }
    :   POUND WS?
        ID (WS DECIMAL_DIGIT { $channel=HIDDEN; if ($ID.text.equals("aaa"))
$type = DECIMAL_DIGIT; })?
    ;

I also noticed that 'aaa' and 'bbb' are valid IDs, so collapsed all the
branches to start with ID. I don't know if you can use this trick in your
production code.

Writing complex lexer rules gets tricky (e.g. when trying to write a filter)
because 1) the lexer doesn't backtrack, and 2) it matches non-fragment rules
in top-down order so you have to be careful with your ordering. I've had to
use a whole lot of trial and error.

Good luck :)

...Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080310/11189b09/attachment.html 


More information about the antlr-interest mailing list