Can I construct a token
ENDPLUS: '+' (options (greedy = false;):.) * '+'
;
being considered by the lexer only if it is 开发者_开发问答preceded by a token PRE
without including in ENDPLUS?
PRE: '<<'
;
Thanks.
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;)
.
精彩评论