Version 2 (modified by eamiri, 9 years ago) (diff)


Should the compiler support nested loops?

A question that we need to answer for further development of the compiler is whether or not we can avoid nested loops in the input language of the compiler. We need a while loop whenever we process something sequentially, so on the surface it seems that we can do all sequential processing inside one loop without nesting. But a closer look suggests that we should allow nested loops for higher performance.

Consider the following example. When parsing XML files, we need a while loop to find and mask XML comments. Now assume that we have special input using XML syntax, in which the content of XML comments are written a specific language, say CXML. Now assume CXML has its own comments. Now we need to parse the content of XML comments, but we want to mask out CXML comments. One can think of two approaches.

1- First find an XML mask. Then separately parse XML and CXML. One step of CXML parsing is masking out CXML comments. This approach does not need nested loops.

2- Inside the loop that finds XML comments, nest another loop that finds CXML comments. So when the outer loop is finished by scanning through the input file just once, we have constructed CXML comment mask as well as XML mask. It seems that this approach will lead to higher performance, but there are two complexities to handle. First the compiler should suppport nested loop. Second, the condition of the inner and outer loop might have different forms.

Still one might argue that situations in which a nested loop is necessary are rare and so it is not a high priority for the compiler to support it.