[net.lang.f77] Minor I/O bug

drf@sdcc3.UUCP (Donald R. Fredkin) (12/20/85)

The f77 compiler complains of a syntax error at the first line of the
following program (4.2bsd on VAX, v2.0 on SUN):

	write(6,10) (-1)
10	format(i2)
	end

Removal of the parentheses around -1 cures the problem.  This seems to violate
section 12.8.2.2 of ANSI X3.9-1978, which states that an <output list item>
can be "(5) Any other expression ..."

tadguy@xanth.UUCP (Tad Guy) (12/24/85)

In article <3117@sdcc3.UUCP> Donald R. Fredkin (drf@sdcc3.UUCP) writes:
>The f77 compiler complains of a syntax error at the first line of the
>following program (4.2bsd on VAX, v2.0 on SUN):
>
>	write(6,10) (-1)
>10	format(i2)
>	end
>
>Removal of the parentheses around -1 cures the problem. This seems to violate
>section 12.8.2.2 of ANSI X3.9-1978, which states that an <output list item>
>can be "(5) Any other expression ..."

I haven't found a fortran compiler yet that will accept that line.  The
compiler sees the starting paran and is assuming that you are starting
an implied do loop:

	integer array(20), i
	write(6,10) (array(i),i=1,20)

And chokes since that isn't what you are doing.  I avoid using () around
lists in writes or reads for this reason and since I haven't found necessary
to use them.

A solution I was told about when I used WATFIV on an IBM 370 was to do this:

	write(6,10) +(-1)

or, more apropriate for your use:

	write(6,10) -(1)

It appears that the presence of a sign forces the element to be evualated
as a expression and not a implied-do-loop.

Hope this helps...
	...tad
	ihnp4!burl!icase!xanth!tadguy

garry@lasspvax.UUCP (Garry Wiegand) (12/31/85)

With respect to: "WRITE (6,10)  (-1)"...

The original works fine using DEC's compiler. Compiler writers can be
tediously sloppy! But most of my misfortunes have come with C :-)

garry wiegand

pmontgom@sdcsmb.UUCP (Peter Montgomery) (12/31/85)

In article <247@xanth.UUCP> tadguy@xanth.UUCP (Tad Guy) writes:
>>The f77 compiler complains of a syntax error at the first line of the
>>following program (4.2bsd on VAX, v2.0 on SUN):
>>
>>      write(6,10) (-1)
>>10    format(i2)
>>      end
>
>I haven't found a fortran compiler yet that will accept that line.

        Both the Control Data FTN 4 (1966 plus extensions, such as
expressions in iolists) and FTN 5 (1977) compilers accept the construct.

        The WRITE statement corresponds to FORTRAN 77.  The FORMAT should
be changed to (i3) or (1x,i2), since FORTRAN 77 does not permit a "-" as a
carriage control.
--
                        Peter Montgomery

        {aero,allegra,bmcg,burdvax,hplabs,
         ihnp4,psivax,randvax,sdcsvax,trwrb}!sdcrdcf!sdcsmb!pmontgom

Don't blame me for the crowded freeways - I don't drive.

atbowler@watmath.UUCP (Alan T. Bowler [SDG]) (01/04/86)

In article <247@xanth.UUCP> tadguy@xanth.UUCP (Tad Guy) writes:
>In article <3117@sdcc3.UUCP> Donald R. Fredkin (drf@sdcc3.UUCP) writes:
>>The f77 compiler complains of a syntax error at the first line of the
>>following program (4.2bsd on VAX, v2.0 on SUN):
>>
>>	write(6,10) (-1)
>>10	format(i2)
>>	end
>>
>>Removal of the parentheses around -1 cures the problem. This seems to violate
>>section 12.8.2.2 of ANSI X3.9-1978, which states that an <output list item>
>>can be "(5) Any other expression ..."
>
>I haven't found a fortran compiler yet that will accept that line.

I suggest you have not looked very hard.  The IBM/370 and Honeywell DPS-8
Fortran 77 compilers work just fine.  Other have pointed out that the 
compiler from other manufacturers work just fine.
>
>A solution I was told about when I used WATFIV on an IBM 370 was to do this:

WATFIV never claimed to be Fortran 77, just an extension of Fortran IV (alias
66) and as such was not required to accept this.  The correct way arround
it, if you were working with Fortran compilers that conform to the 1966
standard, is to assign to a variable and use the variable in the WRITE
statement.

levy@ttrdc.UUCP (Daniel R. Levy) (01/04/86)

In article <115@sdcsmb.UUCP>, pmontgom@sdcsmb.UUCP (Peter Montgomery) writes:
>In article <247@xanth.UUCP> tadguy@xanth.UUCP (Tad Guy) writes:
>>>The f77 compiler complains of a syntax error at the first line of the
>>>following program (4.2bsd on VAX, v2.0 on SUN):
>>>      write(6,10) (-1)
>>>10    format(i2)
>>>      end
>>I haven't found a fortran compiler yet that will accept that line.
>
>        Both the Control Data FTN 4 (1966 plus extensions, such as
>expressions in iolists) and FTN 5 (1977) compilers accept the construct.
>        The WRITE statement corresponds to FORTRAN 77.  The FORMAT should
>be changed to (i3) or (1x,i2), since FORTRAN 77 does not permit a "-" as a
>carriage control.
>                        Peter Montgomery

I think the f77 compiler is expecting an implied DO and then barfing when it
doesn't get one.  VMS Fortran swallows this OK.  If the write is changed to
something like

      write(6,10) (-1,i=1,1)

f77 is happy.

But yes it is a bug.

My favorite I/O bug is

      character*10 buf
      integer num
      buf = '12'
      read(buf,'(I2)',err=20) num
      write(6,*)num
      stop
20    write(6,*)'Error!',num
      stop
      end

The output will be

Error! 12

[can anybody figure out WHY, and/or suggest a patch?  Oh yes, if you do

      read(buf,'(I2)',iostat=i) num

you get a core dump.  Both Berkeley and AT&T Unix f77's are guilty of this.
If the formatted I/O is done from a file it works fine; only with internal
I/O it seems to barf.]
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|       dan levy | yvel nad      |  my own and are not at all those of my em-
|         an engihacker @        |  ployer or the administrator of any computer
| at&t computer systems division |  upon which I may hack.
|        skokie, illinois        |
 --------------------------------   Path: ..!ihnp4!ttrdc!levy

levy@ttrdc.UUCP (Daniel R. Levy) (01/07/86)

In article <670@ttrdc.UUCP>, levy@ttrdc.UUCP (That's me) writes:
>
>But yes it is a bug.
>
>My favorite I/O bug is
>
>      character*10 buf
>      integer num
>      buf = '12'
>      read(buf,'(I2)',err=20) num
>      write(6,*)num
>      stop
>20    write(6,*)'Error!',num
>      stop
>      end
>
>The output will be
>
>Error! 12

First, I may owe a retraction on the first statement.  I got mail from someone
who seemed to know what they were talking about that allowed that (-1) as an
output list expression might violate some obscure rule of the Fortran 77
standard (I don't have that venerable document handy so I can't check whether
this is kosher or not, though it escapes me why one would WANT to put a lonely
constant or even variable within parens and call that an expression).  If it
IS a violation, than f77, of course, is well within its rights to barf at this
"expression."

Second, the same mail pointed out to me that the second example worked fine
under BSD 4.3.  I will not dispute that since I do not have a 4.3 system handy
to test with (it does barf on 4.2 and earlier, and on the Sys5r2 and earlier
f77's that I have had a chance to try).  I still would like to know if the
fault lay in the library or in the compiled code that called it, and how it
could be patched if it were feasible in the 4.2 (or better, the Sys5) version.
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|       dan levy | yvel nad      |  my own and are not at all those of my em-
|         an engihacker @        |  ployer or the administrator of any computer
| at&t computer systems division |  upon which I may hack.
|        skokie, illinois        |
 --------------------------------   Path: ..!ihnp4!ttrdc!levy