nioo
nioo

Reputation:

Lexer antlr3 token problem

Can I construct a token

ENDPLUS: '+' (options (greedy = false;):.) * '+'
       ;

being considered by the lexer only if it is preceded by a token PREwithout including in ENDPLUS?

PRE: '<<'
       ;

Thanks.

Upvotes: 1

Views: 753

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170158

No, AFAIK, this is not possible "out of the box". One only has look-ahead-control over the tokens stream in the lexer or parser by using the attribute input and calling LA(int) (look-ahead) on it. For example, the following lexer rule:

Token
  :  {input.LA(2) == 'b'}? . 
  ;

matches any single character as long as that single character is followed by a b. Unfortunately, there's no input.LA(-1) feature to look behind in the token stream. The {...}? part is called a "syntactic predicate" in case you're wondering, or wanting to Google it.

A discussion, and some pointers on how to go about solving it, are given here: http://www.antlr.org/pipermail/antlr-interest/2004-July/008673.html

Note that it's {greedy=false;}, not (greedy=false;).

Upvotes: 1

Related Questions