LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Fortran do loop variables: restrictions on control variable memberships? (http://www.linuxquestions.org/questions/programming-9/fortran-do-loop-variables-restrictions-on-control-variable-memberships-914907/)

kaiserkarl13 11-22-2011 08:55 AM

Fortran do loop variables: restrictions on control variable memberships?
 
Is there a restriction that the control variable of a DO loop cannot be an element of an array? For example, the following code does not compile on both compilers I've checked:

Code:

program loop
  implicit none
  integer :: n(2)
  do n(1) = 1, 10
    do n(2) = 1, 20
    end do
  end do
end program

However, the following code DOES compile (which makes very little sense):

Code:

program loop
  implicit none
  integer :: n(2)
  associate ( m=>n(1), p=>n(2) )
  do m = 1, 10
    do p = 1, 20
    end do
  end do
  end associate
end program

Clearly these two loops are precisely the same thing in memory. All ASSOCIATE does is create another name for the same variable. Is there a rule somewhere in the standard that says control variables for DO loops must be declared scalars and not part of a larger object (except, evidently, through association)?

firstfire 11-22-2011 12:55 PM

Hi.

I think it is a grammatical restriction. If you take a look at the source code of `f2c' you will find that control variable should be a `name' (excerpt from src/gram.exec):
Code:

exec:    iffable
        | SDO end_spec label opt_comma dospecw
                {
                if($3->labdefined)
                        execerr("no backward DO loops", CNULL);
                $3->blklevel = blklevel+1;
                exdo($3->labelno, NPNULL, $5);
                }
        | SDO end_spec opt_comma dospecw
                {
                exdo((int)(ctls - ctlstack - 2), NPNULL, $4);
                NOEXT("DO without label");
                }
................
dospecname SEQUALS exprlist
                { $$ = mkchain((char *)$1, $3); }
        ;             
               
dospecwdospec
        | SWHILE {westart(0);} SLPAR expr SRPAR
                { $$ = mkchain(CNULL, (chainp)$4); }
        ;     
           
................

`name' is further defined in gram.head:
Code:

name:    SNAME
                { $$ = mkname(token); }
        ;

Finally SNAME is a lexer symbol, i.e. a word in your program. So loop counter can only be a variable, at least in f2c.

In theory this restriction could be easily thrown, but for some reasons it is not. Semantically, using an array element as loop counter makes sense and in some languages (in C for example) is allowed.

makyo 11-22-2011 09:41 PM

Hi.

In Modern Fortran Explained, Metcalf et al, page 60, 4.4 The do construct:
Quote:

... [the index] variable is a named scalar integer variable ...
I find the book to be pricey, but worth it if you are going investigate or seriously write to the standards ... cheers, makyo

http://www.amazon.com/Explained-Nume...2019575&sr=8-1


All times are GMT -5. The time now is 11:33 PM.