开发者

string recursion antlr lexer token

开发者 https://www.devze.com 2022-12-25 01:50 出处:网络
How do I build a token in lexer that can handle recurs开发者_如何学Goion inside as this string:

How do I build a token in lexer that can handle recurs开发者_如何学Goion inside as this string:

${*anything*${*anything*}*anything*}

?


Yes, you can use recursion inside lexer rules.

Take the following example:

${a ${b} ${c ${ddd} c} a}

which will be parsed correctly by the following grammar:

parse
  : DollarVar
  ;

DollarVar
  : '${' (DollarVar | EscapeSequence | ~Special)+ '}'
  ;

fragment 
Special
  :  '\\' | '$' | '{' | '}'
  ;

fragment
EscapeSequence
  :  '\\' Special
  ;

as the interpreter inside ANTLRWorks shows:

alt text http://img185.imageshack.us/img185/5471/recq.png


ANTLR's lexers do support recursion, as @BartK adeptly points out in his post, but you will only see a single token within the parser. If you need to interpret the various pieces within that token, you'll probably want to handle it within the parser.

IMO, you'd be better off doing something in the parser:

variable: DOLLAR LBRACE id variable id RBRACE;

By doing something like the above, you'll see all the necessary pieces and can build an AST or otherwise handle accordingly.

0

精彩评论

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