And a tougher regular expression problem

So, here’s a tougher regular expressionproblem – one I have not yet worked out myself.

An NFA Graph created to describe a regular exp...
An NFA Graph created to describe a regular expression (Photo credit: Wikipedia)

BASIC‘s essential looping structure is a FOR (STEP) ... NEXT loop. In ZX80/ZX81 BASIC (the dialect I am aiming to emulate with the BINSIC DSL) this is of the form FOR v = num TO num STEP num... NEXT v, where v is a single letter counter variable and num a valid numerical expression.

Groovy does not implement a FOR loop as such but it is not difficult to cover the simple and common case of a STEP 1 loop (though other values might be more difficult), but the tricky bit is that BASIC FOR ... NEXT loops can be nested. So what regular expression will grab the insides of a loop?

Update: The best thing about blogging is that sometimes it helps to clarify the problem. And it has done so here even as I wrote this. I don’t need to worry about the nested loops at all – all I need to do is substitute some code for the FOR statement and replace the NEXT with a }. This has the added advantage of saving poor BASIC coders from disordering their NEXT statements.