views:

163

answers:

1

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.

A: 

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;).

Bart Kiers
Looking at the code for IntStream.java, negative integers are allowed as a parameter to LA and will get prior matched tokens. So it just becomes a matter of checking prior matched tokens, hopefully nioo can limit how far back to check. Maybe other languages are different, but Java will allow you to check prior tokens.
WayneH
@WayneH, I'll check it out, thanks. Hopefully nioo will read you comment as well.
Bart Kiers