开发者

Lexer antlr3 token problem

开发者 https://www.devze.com 2022-12-25 13:44 出处:网络
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?

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消