[antlr-interest] Breaking a loop

Eric Mahurin eric.mahurin at gmail.com
Sun Nov 6 12:44:48 PST 2005


On 11/4/05, John Green <greenj at ix.netcom.com> wrote:
>
> I've got a pre-existing language with a requirement like this:
>
> ( ( (io_opt)* state_end )=> (io_opt)* state_end {break;}
> | .
> )*
>
> Hopefully my question is obvious from the example. I need to break the
> loop when I hit a certain production. Until that production is reached,
> arbitrary tokens are consumed by the ".". Assuming {break;} is a bad hack,
> what's a good way to do this in Antlr?
>
> Cheers,
> john at joanju dot com
>
>
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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20051106/61915476/attachment.html


More information about the antlr-interest mailing list