convert ! comments to standard comments? 
Author Message
 convert ! comments to standard comments?

Could someon please tell me how to do the following transformation
in Perl (or a shell program)?

I have fortran code with lines such as

        do ivec=1,neval   ! loop over eigenvectors

The ! comment is a very common extension but is not ANSI F77 standard.
How can I convert it to

c       loop over eigenvalues
        do ivec=1,neval

which is standard? I would like to do this for all instances of
! comments .

______________________________________
Vivek Rao                            

http://www.*-*-*.com/ ~vrao/
Univ. of Illinois, Physics Department
1110 W Green St / Urbana IL 61801
______________________________________



Mon, 30 Nov 1998 03:00:00 GMT  
 convert ! comments to standard comments?

Quote:

> I have Fortran code with lines such as
>    do ivec=1,neval   ! loop over eigenvectors
> The ! comment is a very common extension but is not ANSI F77 standard.
> How can I convert it to
> c  loop over eigenvalues
>    do ivec=1,neval

It's 1996.  Your compiler should have an option for handling !-comments.
Otherwise, try this:
        perl -pwe 's/^(.*?)\s*!\s*(.*)$/c\t$2\n$1/' old.f > new.f

Daniel Lewart



Mon, 30 Nov 1998 03:00:00 GMT  
 convert ! comments to standard comments?

: Could someon please tell me how to do the following transformation
: in Perl (or a shell program)?

: I have Fortran code with lines such as

:       do ivec=1,neval   ! loop over eigenvectors

: The ! comment is a very common extension but is not ANSI F77 standard.
: How can I convert it to

: c     loop over eigenvalues
:       do ivec=1,neval

: which is standard? I would like to do this for all instances of
: ! comments .

You don't say whether ! is *only* used as a comment introducer, I assume
it is.

I seem to remember from my Fortran class ( > 20 years ago ;-) that the
indenting must be preserved, so I preserve it.

----------------------
#!/usr/bin/perl -w

$_='     do ivec=1,neval   ! loop over eigenvectors';

$indent = $1 if s/^(\s*)//;            # extract the indent

($stmt, $comment) = split(/\s*!\s*/);  # seperate the statement from comment

print "c", ' ' x (length($indent)-1), "$comment\n$indent$stmt\n";
----------------------

--
  Tad McClellan,      Logistics Specialist (IETMs and SGML guy)

  Just be thankful we don't get all the government we pay for!



Mon, 30 Nov 1998 03:00:00 GMT  
 convert ! comments to standard comments?



Quote:
>Could someon please tell me how to do the following transformation
>in Perl (or a shell program)?

>I have Fortran code with lines such as

>    do ivec=1,neval   ! loop over eigenvectors

>The ! comment is a very common extension but is not ANSI F77 standard.
>How can I convert it to

>c   loop over eigenvalues
>    do ivec=1,neval

>which is standard? I would like to do this for all instances of
>! comments .

Well, it ain't Perl, but just for the flame of it, the following will
work for Fortran lines of fewer than 81 characters and NO ! characters
in character strings (that is, ! only delimits comments).

        program bang2c
        character*80 line
1       continue
        read (5, '(A)', end=99) line ! read next line
        i = index (line(1:80), '!')   ! find bang in line
        if (i .ne. 0) then      ! found a bang
          print '(A)', 'C'//line(i+1:80)  ! Print standard comment
        else
          i = 81
        endif
        if (i .ne. 1) print '(A)', line(1:i-1) ! line sans comment
        go to 1
99      continue
        end

Note that this program won't convert itself because of the '!' in the
call to index.

Best regards,

Joel Williamson
--

Hewlett-Packard Company, Convex Division (HP/CXD)



Mon, 30 Nov 1998 03:00:00 GMT  
 convert ! comments to standard comments?

[cited poster cc'd]



: Could someon please tell me how to do the following transformation
: in Perl (or a shell program)?
:
: I have Fortran code with lines such as
:
:       do ivec=1,neval   ! loop over eigenvectors
:
: The ! comment is a very common extension but is not ANSI F77 standard.
: How can I convert it to
:
: c     loop over eigenvalues
:       do ivec=1,neval
:
: which is standard? I would like to do this for all instances of
: ! comments .

Here's what I'd use:

#! perl -i.bak
while(<>) {
    if (($code, $comment) = /(.*?)\s+!\s*(.*)$/) {
        print "c $comment\n$code\n";
    } else {
        print;
    }

Quote:
}

Of course, you might want to change the number of spaces printed after
the initial c (for aesthetic purposes).

Greg
--

University of Alabama in Huntsville
CS Department Systems Support Team



Mon, 30 Nov 1998 03:00:00 GMT  
 convert ! comments to standard comments?

[posted and cc'ed to original author]



Quote:
>Could someon please tell me how to do the following transformation
>in Perl (or a shell program)?

>I have Fortran code with lines such as

>    do ivec=1,neval   ! loop over eigenvectors

>The ! comment is a very common extension but is not ANSI F77 standard.
>How can I convert it to

>c   loop over eigenvalues
>    do ivec=1,neval

>which is standard? I would like to do this for all instances of
>! comments .

While I'm not familiar with fortran, if the only ! characters in your code
are comments, the following should work:

$string =~ s/^(\s*)([^!]+)\!\s*(.*)$/c$1$3\n$1$2/;

This will also preserve your leading whitespace.  However, there will probably
be whitespace at the end of your "do" line.  To get around this, you may
want to split this out into a few statements:

($whitespace,$cmd,$comment) = $string =~ /^(\s*)([^!]+)\!\s*(.*)$/;
$cmd =~ s/\s+$//;
$string = join('','c',$whitespace,$comment,"\n",$whitespace,$cmd);

Again, as I'm not familiar with the syntax of fortran, and where spaces are
allowed and so forth, you might want to fine-tune the regexp to meet your
needs.

Good luck,


  "I'm sorry, but you just made me lose my sense of humor, which is deeply
  regrettable."
    -- Larry Wall



Mon, 30 Nov 1998 03:00:00 GMT  
 convert ! comments to standard comments?


Quote:

>----------------------
>#!/usr/bin/perl -w

>$_='     do ivec=1,neval   ! loop over eigenvectors';

>$indent = $1 if s/^(\s*)//;            # extract the indent

>($stmt, $comment) = split(/\s*!\s*/);  # seperate the statement from comment

>print "c", ' ' x (length($indent)-1), "$comment\n$indent$stmt\n";
>----------------------

For the example line of code that was presented in the original
example, that method is appropriate.

Any general-purpose program will have to be smarter than
that. Consider the following example code,

      PROGRAM MYTEST
      CHARACTER*20 S
      S = "Hello, world!"      ! initialize s
      PRINT *,S                ! output s
      STOP
      END

Which gets mangled into something like,

      PROGRAM MYTEST
      CHARACTER*20 S
c     "
      S = "Hello, world
c     output s
      PRINT *,S
      STOP
      END

It'll also produce strange looking output if a tab character is in the
whitespace following the ! comment (because the length-1 calculation
assumes that the whitespace is composed of spaces).

User beware,
-Randy
--

UNIX Systems Programmer            Voice:  (515) 965-7057
Perl Hacker - Powered by FreeBSD!  FAX:    (515) 965-7305



Mon, 30 Nov 1998 03:00:00 GMT  
 convert ! comments to standard comments?


        >Could someon please tell me how to do the following transformation
        >in Perl (or a shell program)?

        >I have Fortran code with lines such as

        >    do ivec=1,neval   ! loop over eigenvectors

        >The ! comment is a very common extension but is not ANSI F77 standard.

---The ! comment is now standard.  AFAIK,
the ANSI Fortran 77 standard was withdrawn.

        >How can I convert it to

        >c   loop over eigenvalues
        >    do ivec=1,neval

        >which is standard?

---No need to.  Get a standard compiler.  (Or write a program
to do the conversion.)

        > I would like to do this for all instances of
        >! comments .



Tue, 01 Dec 1998 03:00:00 GMT  
 convert ! comments to standard comments?


:> I have Fortran code with lines such as
:>   do ivec=1,neval   ! loop over eigenvectors

:> The ! comment is a very common extension but is not ANSI F77 standard.
:> How can I convert it to
:> c loop over eigenvalues
:>   do ivec=1,neval

:It's 1996.  Your compiler should have an option for handling !-comments.
:Otherwise, try this:
:       perl -pwe 's/^(.*?)\s*!\s*(.*)$/c\t$2\n$1/' old.f > new.f

yes, but it is still F(19)77. F90 adds them as standard.

Your perl script will do the simple case listed above, but at least be
aware that it will break on character literals.

stuff like:
      data cvar /37H WOW! IS THIS ARCHAIC SYNTAX OR WHAT?/
      cvar2 =                      ' I''ll SAY! I DON''T THINK I''VE SEE
     1N SUCH RUBBISH BEFORE!'

turns into:
c       IS THIS ARCHAIC SYNTAX OR WHAT?/
      data cvar /37H WOW
c       I DON''T THINK I''VE SEE
      cvar2 =                      ' I''ll SAY
c       '
     1N SUCH RUBBISH BEFORE

[though handling general cases doesn't seem worth the effort. Just look
for them afterwards]

--
tim butler



Wed, 02 Dec 1998 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Counting commented lines (full-line comments only)

2. comments on comments

3. meta-comments for comments on proposals

4. comments on comments

5. comments on comments

6. Single Pound # comments versus Double Pound ## comments

7. Standard for Process Control (was: More X3J20 Comments)

8. Standard comments for verilog ie veridoc ?

9. Units Checking is Ada - Solicitation of Comments on Standard Proposal

10. Final comment on draft (probably soon to be official) COBOL Standard

11. ABSOLUTELY last chance to comment on next COBOL ANSI/ISO Standard

12. Comments on COBOL 2002 draft Standard

 

 
Powered by phpBB® Forum Software