[antlr-interest] Matching a token from only one rule?

Ryan Twitchell metatheorem at gmail.com
Mon Sep 27 05:40:07 PDT 2010


 Hi all,

At the start of one parser rule I would like, as one alternative, to
match nearly any input ending before a certain character value.  I would
like this to match as a single token if possible.  I am not sure how to
achieve this, and have tried a number of things so far.  Here is my best
shot so far:

elem
    :    DECL ';'
    |    ID '=' expr ';'
    ;

DECL: (DECL_CHAR+ ';') => DECL_CHAR+
    ;

fragment
DECL_CHAR
    :    ~(';'|'=')
    ;

Working with the above, ANTLR reports that tokens such as ID can never
be matched, since DECL matches them already.  I had not thought this
would be the case with a syntactic predicate in front of the alternative.


So far, I have only had success by incorporating the end character into
the token, as follows.  But I believe this will lead to the token
matching in other, unexpected places.

DECL:  DECL_CHAR+ ';'
    ;

The important problem is that I don't want DECL to match at other parts
of the grammar. 

TIA for any advice,

Ryan Twitchell



More information about the antlr-interest mailing list