[antlr-interest] Breaking a loop
John Green
greenj at ix.netcom.com
Mon Nov 7 11:54:29 PST 2005
Many thanks to Eric and to Sergey for suggesting recursion. That's exactly what I needed.
I wish I'd thought of it myself. :)
Cheers
Eric Mahurin wrote:
> John Green wrote:
>> I've got a pre-existing language with a requirement like this:
>>
>> ( ( (io_opt)* state_end )=> (io_opt)* state_end {break;}
>> | .
>> )*
>
>
> Sorry, I meant to post this to the group. I accidentally did a reply
> instead of reply-all. Used to another group where the reply-to field is
> the group.
>
> The way to recursively specify this is (not sure if I have the right
> ANTLR syntax):
>
> x : ( (io_opt)* state_end )=> (io_opt)* state_end
> | . x
>
> I find that many complex loops with strange termination conditions are
> easier specified with recursion. Terence, have you considered putting
> tail-call optimization into ANTLR so that there isn't a downside with
> specifying loops with recursion (actually infers a loop rather than
> recursing)? For reference, here is how I would specify the above in my
> Grammar parser:
>
> Tail { |x|
> (io_opt.repeat0 + state_end).lookahead | # .lookahead equivalent to
> self as syntactic predicate
> ANY + x
> }
>
> or a shorthand I have is (repeat0 method generates a Tail recursion
> grammar):
>
> ANY.repeat0((o_opt.repeat0 + state_end).lookahead)
More information about the antlr-interest
mailing list