[antlr-interest] pass state from parser to lexer

Benjamin S Wolf jokeserver at gmail.com
Tue Jul 3 18:45:13 PDT 2012


On Tue, Jul 3, 2012 at 10:31 AM, Bart Kiers <bkiers at gmail.com> wrote:
> On Tue, Jul 3, 2012 at 6:13 PM, Benjamin S Wolf <jokeserver at gmail.com>
> wrote:
>>
>> I believe you can also use ~ as a negation, eg.
>>
>> BODY : '#' ~'#'* '#' ;
>>
>> (if # is your delimiter, as an example)
>
>
> The delimiter is variable and is provided (at runtime?) by the user, as
> indicated by Scobie.

Oh, I see. Sorry I missed that. If the user gets to specify then you
can't hard-code it into the Lexer, which is what those rules will
assume you can do.

Bart's suggestion works for specifying the delimiter at lexer
initialization time. Here's a similar way to use a delimiter specified
at lex time.

BODY : delimiter=. ( {input.LA(1) != $delimiter}?=> . )* . ;

or

@init { char delimiter; }
BODY : a=. {delimiter=$a;} ( {input.LA(1) != delimiter}?=> . )* . ;

Use delimiter=~WS or similar if you don't want your grammar using
whitespace characters as delimiters.


More information about the antlr-interest mailing list