views:

122

answers:

2

How do I build a token in lexer that can handle recursion inside as this string:

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

?

thanks

A: 

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.

Kaleb Pederson
Not quite. `.*` and `.+` are by default non-greedy in ANTLR. Try the interpreter in ANTLRWorks on the string `${a${b}c}`. You'll see that it matches `${a${b}` and not the entire string `${a${b}c}`. See Parr's ANTLR reference, Ch 4, **Extended BNF Subrules**, page 86.
Bart Kiers
@BartK I understand now. Thanks for the clarifications.
Kaleb Pederson
@Kaleb, you're welcome!
Bart Kiers
A: 

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

Bart Kiers