Related articles |
---|
[5 earlier articles] |
Re: Testing strategy for compiler gneuner2@comcast.net (George Neuner) (2010-06-18) |
Re: Testing strategy for compiler gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-06-18) |
Re: Testing strategy for compiler ott@mirix.org (Matthias-Christian Ott) (2010-06-19) |
Re: Testing strategy for compiler gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-06-19) |
Re: Testing strategy for compiler jm@bourguet.org (Jean-Marc Bourguet) (2010-06-21) |
Re: Testing strategy for compiler dot@dotat.at (Tony Finch) (2010-06-21) |
Re: Testing strategy for compiler gneuner2@comcast.net (George Neuner) (2010-06-21) |
Re: Testing strategy for compiler news@cuboid.co.uk (Andy Walker) (2010-06-22) |
Re: Pascal design, was Testing strategy for compiler marcov@turtle.stack.nl (Marco van de Voort) (2010-06-22) |
Re: Pascal loops, was Testing strategy for compiler gneuner2@comcast.net (George Neuner) (2010-06-22) |
Re: Testing strategy for compiler barry.j.kelly@gmail.com (Barry Kelly) (2010-06-22) |
Re: Unnatural iteration [was: Testing strategy for compiler] paul.biggar@gmail.com (Paul Biggar) (2010-06-23) |
Re: Testing strategy for compiler gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-06-23) |
[2 later articles] |
From: | George Neuner <gneuner2@comcast.net> |
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
<gah@ugcs.caltech.edu> wrote:
>George Neuner <gneuner2@comcast.net> 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
zero.
In Pascal it's also illegal to write
FOR <var> := <start> TO <finish> where start > finish
or
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:
type
LupeCtrl: record
index : integer;
start : integer;
finish: integer;
step : integer;
end;
var
myloop: LupeCtrl;
procedure screwup( var x: LupeCtrl )
begin
with x do
index := index + 3;
finish := finish - 10;
step := -step;
end;
end;
:
myloop.start := -12;
mylopp.finish := +12;
myloop.step := 2
for myloop.index := myloop.start
to myloop.finish
by myloop.step
do begin
:
screwup( myloop );
:
end;
:
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
>construct.
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.
George
[*] the FOR loop was excluded from the first version of Oberon.
Return to the
comp.compilers page.
Search the
comp.compilers archives again.