[comp.lang.fortran] Problem with substrings

i91@nikhefh.UUCP (Fons Rademakers) (06/03/87)

Does somebody know what according to the FORTRAN 77 standard 
should happen in the following substring expression:

        CHARACTER*10 CH, STR
        
	STR = 'ABCDEFGHIJ'
        I   = 6
        NC  = 0
        CH  = 'FOO'//STR(I:I+NC-1)//'BAR'

My results are 1) VAX/VMS FORTRAN: CH = 'FOOBAR'
               2) Gould UTX32 f77: CH = 'FOOBAR'
               3) Apollo FTN     : CH = 'FOOFGHIJ..' CRASH!!! 
					     (the var CH gets scrambled which
                                             results, a few statements later,
                                             in a segmentation fault)

Is Apollo's FTN compiler allowed to crash (and are the other compilers
too permissive) or is it an FTN compiler bug?

-- Fons Rademakers

steved@sun.UUCP (06/03/87)

In article <286@nikhefh.UUCP> i91@nikhefh.UUCP (Fons Rademakers) writes:
>Does somebody know what according to the FORTRAN 77 standard 
>should happen in the following substring expression:
>
>        CHARACTER*10 CH, STR
>        
>	STR = 'ABCDEFGHIJ'
>        I   = 6
>        NC  = 0
>        CH  = 'FOO'//STR(I:I+NC-1)//'BAR'
>


The results of this program fragment are undefined.  The FORTRAN 77
standard states (section 5.7.1) that in a substring reference of the
form:

    V ( e1 : e2)

"The values of e1 and e2 *must* be such that:

      1 <= e1 <= e2 <= len

where len is the length of the character variable or array element"

So a program with a substring like this is does not conform to the
standard.  The standard does not prohibit a FORTRAN implementation
from accepting this as an extension nor does it require that it
signals an error either. 

-- 
---------------------------
Steve Dever          steved@Sun.COM
                          or
Sun Microsystems     sun!steved

BEEBE%SCIENCE@utah-cs.UUCP (06/03/87)

>>         CHARACTER*10 CH, STR
>>         
>> 	STR = 'ABCDEFGHIJ'
>>         I   = 6
>>         NC  = 0
>>         CH  = 'FOO'//STR(I:I+NC-1)//'BAR'
>> 
>> My results are 1) VAX/VMS FORTRAN: CH = 'FOOBAR'
>>                2) Gould UTX32 f77: CH = 'FOOBAR'
>>                3) Apollo FTN     : CH = 'FOOFGHIJ..' CRASH!!! 

Section 5.7.1 of ANSI X3.9-1978 FORTRAN 77 discusses
substring expressions:

	CHARACTER*n S
	S(e1:e2)

It is a REQUIREMENT that 1 <= e1 <= e2 <= n; in your
example, you have e2 = e1-1, which is illegal.  Presumably a
run-time system should detect this error.

I tried such an example on the DEC-20 (TOPS-20), and it
gives a run-time error "?Illegal length for character
expression".
-------

ark@alice.UUCP (06/04/87)

In article <286@nikhefh.UUCP>, i91@nikhefh.UUCP writes:
> Does somebody know what according to the FORTRAN 77 standard 
> should happen in the following substring expression:
> 
>         CHARACTER*10 CH, STR
>         
> 	STR = 'ABCDEFGHIJ'
>         I   = 6
>         NC  = 0
>         CH  = 'FOO'//STR(I:I+NC-1)//'BAR'

This is not a valid Fortran 77 program.  On page 5-9 of the
Fortran 77 specification, the form of a substring expression
is given as

	v ( e1 : e2 )

where v is a string variable and e1 and e2 are optional integer
expressions.  e1 gives the index of the first character of the
substring and e2 gives the index of the last character.

Where len is the length of v, e1 and e2 must be such that

	1 <= e1 <= e2 <= len

If e1 is omitted, 1 is assumed.  If e2 is omitted, len is assumed.

In the program fragment given as an example, I is 6 and
I+NC-1 is 5.  Thus the inequality is not satisfied and the
program is invalid.

The Fortran 77 language definition says elsewhere that it
(the language definition) is restrictive on programs and
permissive on implementations.  That is: the definition says
what is a valid program.  A valid implementation is one that
does the right thing when given a valid program as input.

A fortran 77 implementation is thus permitted to do whatever
it pleases with an invalid program, including crash or deliver
garbage results.

bill@westpt.UUCP (06/04/87)

In article <286@nikhefh.UUCP>, i91@nikhefh.UUCP (Fons Rademakers) writes:
> 
> My results are 1) VAX/VMS FORTRAN: CH = 'FOOBAR'
>                2) Gould UTX32 f77: CH = 'FOOBAR'
                2a) Pr1me  PRIMOS  : CH = 'FOOBAR'
>                3) Apollo FTN     : CH = 'FOOFGHIJ..' CRASH!!! 
> 					     (the var CH gets scrambled which
>                                              results, a few statements later,
>                                              in a segmentation fault)
> 


I can't wait to see which is right and which is wrong.
Aren't standards wonderful.  :-)

bill gunshannon


UUCP:      {philabs,phri}!westpt!bill        PHONE:     (914)446-7747
US SNAIL:  Martin Marietta Data Systems      RADIO:     KB3YV
           USMA, Bldg 600, Room 26           AX.25      KB3YV @ WA2RKN
           West Point, NY  10996

danny@masscomp.UUCP (06/04/87)

In article <286@nikhefh.UUCP> i91@nikhefh.UUCP (Fons Rademakers) writes:
>Does somebody know what according to the FORTRAN 77 standard 
>should happen in the following substring expression:
>
>        CHARACTER*10 CH, STR
>        
>	STR = 'ABCDEFGHIJ'
>        I   = 6
>        NC  = 0
>        CH  = 'FOO'//STR(I:I+NC-1)//'BAR'

Some compilers have extended to allow "null substrings"; they are
sometimes useful.  Often, a null substring is denoted by STR(start:finish)
where the value of the finish expression is start-1.  Another implementation
might say that if finish<start then the length of the substring is 0.

But according to the standard, the end value for the substring must be
less than the start value. Therefore, the program is not standard 
conforming.  What processors do with non-standard-conforming programs
is up to the processor, so the Apollo processor, although it 
produced a segmentation fault at runtime, could be called standard-
conforming (assuming it did everything else according to the standard ;-)

ark@alice.UUCP (06/06/87)

In article <844@westpt.usma.edu>, bill@westpt.UUCP writes:
 [description of a statement that does different things on different machines]
> I can't wait to see which is right and which is wrong.
> Aren't standards wonderful.  :-)

When you define a language standard, you can do one of several
things.  The particular thing Fortran 77 does is sometimes described
by the phrase "permissive on implementations, restrictive on users."

In English, this means that the Fortran 77 standard defines what is
a valid Fortran 77 program, and then goes on to say that a valid
implementation is one that does the right thing when given a
valid program as input.

That means that the standard says nothing at all about what the
implementation should do when given an invalid program.  If you
want your program to work on a variety of implementations, it's
up to you to ensure it is valid.