Fortran syntax 
Author Message
 Fortran syntax

fortrans control constructs (if, forall, where, do) have two forms:

- "one-liner"
                   forall(i=1:9) a(i)=i
- block form  
                   forall(i=1:9)
                      a(i)=i
                   end forall

But: a) The "one-liner" form of "do" is missing.
     b) The block form of "if" needs the redundant "then" keyword.

Is there a reason (besides history) for this slightly unsystematic syntax?  

Meik

--
Meik Hellmund
Institut fuer Mathematik, Uni Leipzig

http://www.*-*-*.com/ ~hellmund



Fri, 24 Nov 2006 16:35:56 GMT  
 Fortran syntax
Hello,

On Mon, 07 Jun 2004 10:35:56 +0200, Meik Hellmund

Quote:

>Fortrans control constructs (if, forall, where, do) have two forms:

>- "one-liner"
>                   forall(i=1:9) a(i)=i
>- block form  
>                   forall(i=1:9)
>                      a(i)=i
>                   end forall

forall is an assignment, not a control statement.
A block forall is a sequence of forall statements.

Quote:

>But: a) The "one-liner" form of "do" is missing.

Perhaps because there are not very many one-statement loops?

One line do loop:

do i = 1, n; <do something 1>; < do something 2>; <etc>; enddo;

Quote:
>     b) The block form of "if" needs the redundant "then" keyword.

if( <logical> ) <is what's here a statement or not?>

Quote:

>Is there a reason (besides history) for this slightly unsystematic syntax?  

Yes.  (See above.)

Quote:

>Meik

HTH

--
Cheers!

Dan Nagle
Purple Sage Computing Solutions, Inc.



Fri, 24 Nov 2006 18:25:31 GMT  
 Fortran syntax

Quote:

> One line do loop:

> do i = 1, n; <do something 1>; < do something 2>; <etc>; enddo;

Of course. But that was not the intent of my question. It was
about the  systematics of the syntax.

Quote:

> if( <logical> ) <is what's here a statement or not?>

What to you mean by this?
Since "then" must be followed by a statement separator, it is redundant.

Cheers, Meik

--
Meik Hellmund
Institut fuer Mathematik, Uni Leipzig

http://www.math.uni-leipzig.de/~hellmund



Fri, 24 Nov 2006 21:20:56 GMT  
 Fortran syntax

Quote:

>Hello,

>On Mon, 07 Jun 2004 10:35:56 +0200, Meik Hellmund

>>Fortrans control constructs (if, forall, where, do) have two forms:=20

>>- "one-liner"=20
>>                   forall(i=3D1:9) a(i)=3Di
>>- block form =20
>>                   forall(i=3D1:9)
>>                      a(i)=3Di
>>                   end forall

>forall is an assignment, not a control statement.
>A block forall is a sequence of forall statements.

>>But: a) The "one-liner" form of "do" is missing.

>Perhaps because there are not very many one-statement loops?

>One line do loop:

>do i =3D 1, n; <do something 1>; < do something 2>; <etc>; enddo;

In C++ one can write a for loop in a single statement, for example

for (i=0;i<5;i++) cout << i;

I think the original poster is asking why something like

do i=0,4 print*,i

is not allowed, with the "end do" implied, instead of

do i=0,4
   print*,i
end do

By contrast, for conditional statements, either

if (i > 0) then
   print*,i
end if

or

if (i > 0) print*,i

with an implied "end if" is allowed.

One possible answer is that in Fortran 90/95, array operations and the implied
do loops in write statements reduce the need for loops with few statements
in the body. For example, instead of

do i=1,n
   a(i) = b(i)
end do

one writes

a(1:n) = b(1:n)

and instead of

do i=1,n
   print*,i**2
end do

one writes (ignoring formatting niceties)

print*,(i**2,i=1,n)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---



Fri, 24 Nov 2006 22:05:57 GMT  
 Fortran syntax
One can write in Fortran 77/90/95:  print *, (j, j=0,5)

Skip


-|
-|>Hello,
-|>
-|>On Mon, 07 Jun 2004 10:35:56 +0200, Meik Hellmund
-|>
-|>>
-|>>Fortrans control constructs (if, forall, where, do) have two forms:=20
-|>>
-|>>- "one-liner"=20
-|>>                   forall(i=3D1:9) a(i)=3Di
-|>>- block form =20
-|>>                   forall(i=3D1:9)
-|>>                      a(i)=3Di
-|>>                   end forall
-|>
-|>forall is an assignment, not a control statement.
-|>A block forall is a sequence of forall statements.
-|>
-|>>
-|>>But: a) The "one-liner" form of "do" is missing.
-|>
-|>Perhaps because there are not very many one-statement loops?
-|>
-|>One line do loop:
-|>
-|>do i =3D 1, n; <do something 1>; < do something 2>; <etc>; enddo;
-|
-|In C++ one can write a for loop in a single statement, for example
-|
-|for (i=0;i<5;i++) cout << i;
-|
-|I think the original poster is asking why something like
-|
-|do i=0,4 print*,i
-|
-|is not allowed, with the "end do" implied, instead of
-|
-|do i=0,4
-|   print*,i
-|end do
-|
-|By contrast, for conditional statements, either
-|
-|if (i > 0) then
-|   print*,i
-|end if
-|
-|or
-|
-|if (i > 0) print*,i
-|
-|with an implied "end if" is allowed.
-|
-|One possible answer is that in Fortran 90/95, array operations and the implied
-|do loops in write statements reduce the need for loops with few statements
-|in the body. For example, instead of
-|
-|do i=1,n
-|   a(i) = b(i)
-|end do
-|
-|one writes
-|
-|a(1:n) = b(1:n)
-|
-|and instead of
-|
-|do i=1,n
-|   print*,i**2
-|end do
-|
-|one writes (ignoring formatting niceties)
-|
-|print*,(i**2,i=1,n)
-|
-|
-|
-|----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
-|http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
-|---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

   Herman D. (Skip) Knoble, Research Associate
   (a computing professional for 38 years)
   Email: SkipKnobleLESS at SPAMpsu dot edu
   Web: http://www.personal.psu.edu/hdk
   Penn State Information Technology Services
    Academic Services and Emerging Technologies
     Graduate Education and Research Services
   Penn State University
     214C Computer Building
     University Park, PA 16802-21013
   Phone:+1 814 865-0818   Fax:+1 814 863-7049



Fri, 24 Nov 2006 22:40:06 GMT  
 Fortran syntax


Quote:
> By contrast, for conditional statements, either
> if (i > 0) then
>    print*,i
> end if
> or
> if (i > 0) print*,i
> with an implied "end if" is allowed.

        But the block form is relatively recent, introduced in Fortran 77
I believe.

--
Ivan Reid, Electronic & Computer Engineering,     ___     CMS  Collaboration,

        KotPT -- "for stupidity above and beyond the call of duty".



Sat, 25 Nov 2006 01:53:18 GMT  
 Fortran syntax

Quote:

> But: a) The "one-liner" form of "do" is missing.
>      b) The block form of "if" needs the redundant "then" keyword.

Without the "then", the block form of "if" treads dangerously close
to syntactic ambiguity.  It isn't ambiguous today because empty statements
are not allowed.  But if we ever allow them (as has occasionally been
suggested), the "then" will be critically important.

I don't know whether or not that was in the minds of the people who
did the initial block "if" syntax; that would have been about 30 years
ago, well before my time on the committee.  But I'd say that it might have
been a good thing for them to anticipate as a possibility; maybe they did.

On a one-liner form of DO: You'd need some extra syntax to make it come
even close to working.  I suppose that the extra syntax might be slightly
shorter than the 2 semicolons and the "enddo" that you need now.  You
certainly can't just throw a statement after the DO without something there
to disambiguate the parsing.

Btw, I have a lot fewer one-line DO statements than I used to.  Seems like
a lot of the ones I used to have work fine as whole array expressions, often
very simple ones like X=0.0 (for array X).  I still do have one-liners,
but far fewer than I used to.

--
Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain



Sat, 25 Nov 2006 02:43:34 GMT  
 Fortran syntax

Quote:


>>By contrast, for conditional statements, either
>>if (i > 0) then
>>   print*,i
>>end if
>>if (i > 0) print*,i
>>with an implied "end if" is allowed.
>  But the block form is relatively recent, introduced in Fortran 77

Before Fortran 66 there was the arithmetic IF, where three
statement numbers were given, and the branch went to the first,
second, or third depending on an expression being less than,
equal to, or greater than zero, respectively.

Fortran 66 added the logical IF statement that would conditionally
execute a single statement, or a GOTO around multiple statements.

When the structured IF/THEN form was added, while retaining
the existing form, it made some sense to require the THEN.

The original DO statement included the statement number
of the last statement of the loop.   There never was
a single statement form of DO loop, similar to the C
for statement.

For comparison purposes, PL/I uses the DO statement for two
purposes.   One is a loop construct, similar to Fortran,
and the other is for statement grouping similar to the
C { and } characters.

IF A=B THEN I=1;

IF A=B THEN DO;
    I=2;
    J=1;
END;

-- glen



Sat, 25 Nov 2006 03:00:13 GMT  
 Fortran syntax

Quote:




>>> By contrast, for conditional statements, either

>>> if (i > 0) then
>>>   print*,i
>>> end if

>>> if (i > 0) print*,i

>>> with an implied "end if" is allowed.

>>  But the block form is relatively recent, introduced in Fortran 77

> Before Fortran 66 there was the arithmetic IF, where three
> statement numbers were given, and the branch went to the first,
> second, or third depending on an expression being less than,
> equal to, or greater than zero, respectively.

> Fortran 66 added the logical IF statement that would conditionally
> execute a single statement, or a GOTO around multiple statements.

> When the structured IF/THEN form was added, while retaining
> the existing form, it made some sense to require the THEN.

> The original DO statement included the statement number
> of the last statement of the loop.   There never was
> a single statement form of DO loop, similar to the C
> for statement.

> For comparison purposes, PL/I uses the DO statement for two
> purposes.   One is a loop construct, similar to Fortran,
> and the other is for statement grouping similar to the
> C { and } characters.

> IF A=B THEN I=1;

Every once in a while, you see the perfectly legal
Fortran statement
        IF( A==B ) THEN I = 1
and people are usually surprised about what it does. ;)

Dick Hendrickson

- Show quoted text -

Quote:

> IF A=B THEN DO;
>    I=2;
>    J=1;
> END;

> -- glen



Sat, 25 Nov 2006 03:54:05 GMT  
 Fortran syntax

[An exhaustive answer to my question. Many thanks!]

Quote:
>On a one-liner form of DO: You'd need some extra syntax to make it come
>even close to working.

Since I was curious about the (purely syntactic) similarities/differences of
if/forall/where/do,  I would suggest parentheses:
  do (i=1,N,M) a(i)=i
But of course nobody really needs it.

Quote:
> You
>certainly can't just throw a statement after the DO without something there
>to disambiguate the parsing.

Somehow I can't invent an ambiguous example. Is this related to what
{*filter*} Hendrickson mentioned in this threat:

Quote:
>Every once in a while, you see the perfectly legal
>Fortran statement
>        IF( A==B ) THEN I = 1
>and people are usually surprised about what it does. ;)

Correct me if I am wrong but I believe this can only happen in
Fortran 77 where whitespaces are not token separators.

Thanks to all who answered!

Meik



Sat, 25 Nov 2006 16:51:18 GMT  
 Fortran syntax
[...]

Quote:
>>Every once in a while, you see the perfectly legal
>>Fortran statement
>>        IF( A==B ) THEN I = 1
>>and people are usually surprised about what it does. ;)

> Correct me if I am wrong but I believe this can only happen in
> Fortran 77 where whitespaces are not token separators.

It can only happen in fixed source form, which is still valid in all
later Fortran standards up to now.

--

Experimentelle Physik V   http://www.physik.uni-dortmund.de/~wacker
Universitaet Dortmund     Tel.: +49 231 755 3587
D-44221 Dortmund          Fax:  +49 231 755 4547



Sat, 25 Nov 2006 18:50:16 GMT  
 Fortran syntax

Quote:


> >On a one-liner form of DO: You'd need some extra syntax to make it come
> >even close to working. You
> >certainly can't just throw a statement after the DO without something there
> >to disambiguate the parsing.

> Somehow I can't invent an ambiguous example....

I hadn't even bothered to try since it seemed so intuitively obvious
to me that there would be problems.  You generally can never tell for sure
when an expression is complete - you have to look ahead and see if there
is anything else there.  Off the top of my head...

   do 10 i = 1 , n  something = 1.0

Does the DO loop go to N or perhaps to NS?  Is it SOMETHING or OMETHING
that is set to 1.0?

Quote:
> Is this related to what
>{*filter*} Hendrickson mentioned in this threat:

Yes, in that the blank insignificance of fixed source form makes
parsing much more complicated.  (Implicit typing doesn't help either).
Note that, as Klaus Wacker stated, fixed source form *IS* still part
of f90, f95, and f2003.  In f90 it isn't even labelled as obsolescent
- it is in the standard with as full a stature as any other feature.
Do not confuse personal preference with the standard.  I don't like
fixed source form and I don't use it with new code, but it is still
part of the standard and things have to work with it also.

I was one of the several people who advocated that fixed source form
be added to the obsolescent list in f95 (as it was).  But obsolscent
features are also still in the standard; obsolescence is just warning
that they might come out in some future version.  One of the main
reasons I advocated for this in particular is that I see fixed source
form forcing syntax to be more complicated in many ways.  Perhaps the
classic example is the parens in the parameter statement; without the
parens, it is ambiguous.  (Usually you can tell from other parts of
the context, because of limitations on where various statements can
be, but it is possible to construct truly ambiguous edge cases).

I have at least once advocated allowing some new features to be
specified to work only in free source form so that the syntax
could be simpler, but I didn't have any luck with that advocacy.
(I don't recall the particular case(s), but I recall that it at
least once happened).

(I also advocated, in one of my formal comments on the f90 public
draft, that implicit none be made the default in free source form,
since that wouldn't directly break compatibility with old fixed
source code.  Though several of my comments were accepted, that
one wasn't.)

--
Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain



Sat, 25 Nov 2006 22:56:29 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. fortran syntax question

2. SOFTWARE: ftnchek 2.9 for OS/2 (FORTRAN syntax checker)

3. Fortran syntax question (involving **)

4. fortran syntax

5. Fortran syntax

6. Fortran Syntax

7. Passing address of a C++ function to Fortran - syntax??

8. Implementing letrec-syntax using only let-syntax and syntax-rules

9. paper comparing Fortran 90, C++, Matlab syntax

10. how did fortran settle on the syntax: string(index1)(index2)

11. where can I find abstract syntax definition of Fortran

12. WITH syntax in Fortran?

 

 
Powered by phpBB® Forum Software