Re: Testing strategy for compiler

George Neuner <>
Mon, 21 Jun 2010 17:16:53 -0400

          From comp.compilers

Related articles
[5 earlier articles]
Re: Testing strategy for compiler (George Neuner) (2010-06-18)
Re: Testing strategy for compiler (glen herrmannsfeldt) (2010-06-18)
Re: Testing strategy for compiler (Matthias-Christian Ott) (2010-06-19)
Re: Testing strategy for compiler (glen herrmannsfeldt) (2010-06-19)
Re: Testing strategy for compiler (Jean-Marc Bourguet) (2010-06-21)
Re: Testing strategy for compiler (Tony Finch) (2010-06-21)
Re: Testing strategy for compiler (George Neuner) (2010-06-21)
Re: Testing strategy for compiler (Andy Walker) (2010-06-22)
Re: Pascal design, was Testing strategy for compiler (Marco van de Voort) (2010-06-22)
Re: Pascal loops, was Testing strategy for compiler (George Neuner) (2010-06-22)
Re: Testing strategy for compiler (Barry Kelly) (2010-06-22)
Re: Unnatural iteration [was: Testing strategy for compiler] (Paul Biggar) (2010-06-23)
Re: Testing strategy for compiler (glen herrmannsfeldt) (2010-06-23)
[2 later articles]
| List of all articles for this month |

From: George Neuner <>
Newsgroups: comp.compilers
Date: Mon, 21 Jun 2010 17:16:53 -0400
Organization: A noiseless patient Spider
References: 10-06-037 10-06-044 10-06-050 10-06-054
Keywords: testing
Posted-Date: 21 Jun 2010 18:08:30 EDT

On Sat, 19 Jun 2010 18:41:42 +0000 (UTC), glen herrmannsfeldt
<> wrote:

>George Neuner <> wrote:
>> I once used a Pascal compiler that happily accepted garbage like
>> for n := 1 to 10 by -1
>> do ...
>> and generated an unreachable loop body.
>Is that illegal in PASCAL?

Technically yes. The language standard defines the FOR loop step to
be a cardinal (unsigned) type which further is not permitted to be

In Pascal it's also illegal to write

      FOR <var> := <start> TO <finish> where start > finish
      FOR <var> := <start> DOWNTO <finish> where finish > start

However, any of the start, finish and step may be variables, so these
rules really can only be enforced where the values can be determined
at compile time.

Pascal's FOR loop is required to terminate in all cases - deliberately
causing an infinite loop is illegal. The compiler is required to
check that step <> zero on entry to the loop and it is illegal to
modify any of the loop control values (index, finish, step) from
within the body of the loop. You aren't supposed to use a FOR loop to
iterate over data structures which might change size during execution.

Some compilers enforced this by using hidden control variables whose
values were set at loop entry, so something clever like:

          LupeCtrl: record
                              index : integer;
                              start : integer;
                              finish: integer;
                              step : integer;
          myloop: LupeCtrl;

    procedure screwup( var x: LupeCtrl )
          with x do
              index := index + 3;
              finish := finish - 10;
              step := -step;

      myloop.start := -12;
      mylopp.finish := +12;
      myloop.step := 2

      for myloop.index := myloop.start
                                        to myloop.finish
                                        by myloop.step
      do begin
            screwup( myloop );

either would not compile (if the compiler realized what was happening)
or would iterate 13 times (-12..+12 inclusive) regardless of
modification of the named control variables. Some compilers that used
hidden control variables would ensure that the named loop index had
the correct value at the start of each iteration.
(I don't recall ever having seen a compiler reset the finish or step
value, but I also don't recall ever mucking with them 8-)

However, the compiler was not required to use hidden loop control and
checking for indirect modification and/or correcting the loop index
was implementation dependent. There were compilers that would accept
code like the above (or even simpler) and give the programmer complete
control over the FOR loop.

>I haven't thought about PASCAL recently, but I don't believe that
>such a loop is illegal in the other languages with a similar loop

Ada went it's own way with a new FOR loop syntax, but some other
Pascal derivatives I have used - Modula 2, Modula 3, Oberon 2[*] -
have eliminated the DOWNTO keyword and permit an integer step so that
all FOR loops are of the form:

      FOR <var> := <start> TO <finish> (BY <step>)? DO

with the caveat that some compilers required the step be specified to
count backwards rather than inferring it from start and finish. Most
of the compilers I've tried will warn if they can determine statically
that the sign of the step doesn't match the direction implied by start
and finish.


[*] the FOR loop was excluded from the first version of Oberon.

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.