Article 5891 of alt.sources:
Path: jac.zko.dec.com!crl.dec.com!pa.dec.com!decwrl!hookup!news.mathworks.com!gatech!purdue!haven.umd.edu!cs.umd.edu!ra.nrl.navy.mil!mitch.nrl.navy.mil!grunes
From: grunes@news.nrl.navy.mil (Mitchell R Grunes)
Newsgroups: alt.sources,comp.lang.fortran
Subject: diagramf: Simple Diagrammer for Fortran Language Programs
Date: Fri, 2 Jun 1995 19:36:27 GMT
Organization: NRL Code 8140
Lines: 504
Message-ID: <grunes.233.802121787@news.nrl.navy.mil>
NNTP-Posting-Host: mitch.nrl.navy.mil
Xref: jac.zko.dec.com alt.sources:5891 comp.lang.fortran:18556

Archive-name: diagramf
Submitted-by: grunes@nrlvax.nrl.navy.mil (Mitchell R Grunes)

****diagramf: Simple Diagrammer for Fortran Language Programs****

This is an update, with minor improvements.

Sorry, little or no help can be provided for this program--
and I would need an example of where it failed.
DO NOT POST responses to newsgroups--especially not alt.sources.

Some early versions were called "diagram".

I include 2 files:
  diagramf.for     Fortran language source code.
  diagramf.bat     Sample procedure to run without answering
                   questions.  Would only work under MS-DOS,
                   and if executable were in c:\gsar89\grunes
---diagramf.for--------------CUT HERE-----------------------------
c-----------------------------------------------------------------------
        program diagramf        !Diagrammer for Fortran

c EXAMPLE OF OUTPUT:

c     +---------------subroutine a(x)                  |   1
c     |+--------------do i=1,5                         |   2
c     ||+---------------if(i/2*2.eq.i)then             |   3
c     |||                 x(i)=1                       |   4
c     ||+---------------else                           |   5
c     |||                 x(i)=2                       |   6
c     ||+---------------endif                          |   7
c     |+--------------enddo                            |   8
c     +---------------end                              |   9

c Diagrams FORTRAN if-else-elseif-endif and do-enddo constructs,
c  start and end of routines, and puts a * next to goto, return, end=
c  and err=.

c Program by Mitchell R Grunes, ATSC/NRL (grunes@nrlvax.nrl.navy.mil).
c Revision date: 5/9/95.
c If you find it useful, please send me an e-mail comment--
c  but do NOT send money.

c This program was written in FORTRAN, the One True Language.

c Note--this is a quick and dirty attempt--may not always work quite right.
c  It does not yet handle CASE statements, since I don't use them myself.
c  It can also be confused if an INCLUDE block contains a structure that
c    begins inside and ends outside (or vice-versa).

c It is assumed that no fortran carriage control exists on the output
c  file, unless screen output is specified.

c I hope this works for you, but bear in mind that nothing short of
c  a full-fledged language parser could really do the job.  Perhaps
c  worth about what you paid for it.    (-:

c Versions: To diagram Fortran: diagramf.for
c                      IDL:     diagrami.for
c                      C:       diagramc.for
c MS-DOS procedures to call above programs without asking so many questions,
c  append output to file diagram.out:
c                       Fortran: diagramf.bat
c                       IDL:     diagrami.bat
c                       C:       diagramc.bat

        character*160 a,b
        character*132 filnam,filnam2
        character*16 aa
        character*5 form
        character*8 fm
        character*1 c,c2
        logical find
        external find
        common icol,icol1
        character*10 label(100)
        logical fout
        print*,'FORTRAN source filename?'
        read(*,1)filnam
1       format(a132)
        open(1,file=filnam,status='old')
        print*,'Output file (blank=screen)?'
        read(*,1)filnam2
        fout=filnam2.gt.' '
        if(fout)open(2,file=filnam2,status='unknown')
                                !ASCII 12 is a form feed
        if(fout)write(2,*)char(12),
     &   '=============--',filnam(1:LenA(filnam)),'--============='
        print*,'Column for line #(76 for 80 col screen, 0 for none):'
        iline=0
        read*,iline
        print*,'Embed include files (0=no):'
        read*,iembed
        if(iline.gt.8)iline=iline-8
        aa='| | | | | | | | '
        i1=0            !# of nest levels before current line
        i2=0            !# of nest levels on  current line
        i3=0            !# of nest levels after current line
        i4=0            !1 to flag start or end of block
        InSub=0         !Inside a subroutine or function?
        InMain=0        !Inside an explicit (PROGRAM statement) mainline
        nMainEnd=0      !# of mainline programs ended so far
        nlabel=0        !# of labels for do loop ends
        iAlphaNum=0     !Last char of line is alpha-numeric
        nline=0
        iunit=1
10      a=' '
        read(iunit,11,end=99)a
11      format(a160)
        nline=nline+1
        fm=' '
        write(fm,'(i5)')nline
        form=fm
        if(nline/100*100.eq.nline.and.fout)
     &   print*,'                 (line',form,')'

        if(a(1:1).eq.char(12))then
          if(fout)write(2,'(a1,:)')char(12)
          if(.not.fout)print*,'------------FORM FEED------------'
          b=a(2:160)
          a=b
        endif

        b=' '           !turn tabs to spaces
        j=1
        do i=1,160
          if(a(i:i).eq.char(9))then
            j=(j-1)/8*8+8+1
          elseif(j.le.160)then
            b(j:j)=a(i:i)
            j=j+1
          endif
        enddo

        a=' '                   !Pre-processed output
        i=1                     !Some basic pre-processing
        j=1
        iquote=0                !no ' yet
        idquote=0               !no " yet
        i72flag=0               !nothing over column 72 yet
        iOldAlphaNum=iAlphaNum  !last line ended in alpha-numeric?
        iAlphaNum=0
        iContinue=0             !continued line?
        j=1
        if(b(1:1).eq.'c')goto 15        !comment line
        do i=1,160
          c=b(i:i)
          if(c.ge.'A'.and.c.le.'Z')c=char(ichar(c)+32)  !handle upper case
                                !ASCII 33 is '!'
          if(c.eq.char(33).and.iquote.eq.0.and.idquote.eq.0)goto 15
          if(i.gt.72.and.i72flag.eq.0.and.c.ne.' ')then
            i72flag=1
            PRINT*,'***WARNING--PAST COLUMN 72 at line',form
            print*,char(7)
          endif
          if(i.eq.6.and.c.ne.' ')iContinue=1
          if(i.gt.6.and.c.ne.' ')then   !Used to look for identifiers
            iAlphaNum=0                 !that wrap between lines.
            if((c.ge.'a'.and.c.le.'z').or.(c.ge.'0'.and.c.le.'9'))then
              iAlphaNum=1
              if(iContinue.eq.1)then
                if(iOldAlphaNum.ne.0)then
                  PRINT*,'***POSSIBLE SPLIT IDENTIFIER at line',form
                  print*,char(7)
                endif
              endif
            endif
            iContinue=2
          endif

          if(c.eq.''''.and.i.ne.6.and.idquote.eq.0)iquote=1-iquote
          if(c.eq.'"' .and.i.ne.6.and.iquote .eq.0)idquote=1-idquote
          if(iquote.eq.1)then
            if(find(a,'include ',2).and.iembed.ne.0)then
              iquote=0
              idquote=0
            endif
          endif
          if(iquote.ne.0.or.idquote.ne.0)c=' '
          if(j.gt.1)then                !(kill multiple spaces,&around =)
            c2=a(j-1:j-1)
            if(c.eq.' '.and.c2.eq.' ')j=j-1
            if(c.eq.'='.and.c2.eq.' ')j=j-1
            if(c.eq.' '.and.c2.eq.'=')j=j-1
            if(c.eq.' '.and.c2.eq.'=')c='='
          endif
          a(j:j)=c
          j=j+1
        enddo

15      i2=i1
        i3=i1
        i4=0

        igoto=0         !no goto on line
        if(find(a,'go to',1+8+64).or.find(a,'goto',1+8+64).or.
     &     find(a,'end=',1+16).or.find(a,'err=',1+16).or.
     &     find(a,'return',1+8+64))igoto=1

        if(find(a,'include ''',2).and.iembed.ne.0)then
          filnam=a(icol:160)
          if(.not.find(filnam,'''',0))goto 20
          filnam(icol-1:132)=' '
          if(fout)print*,'including file ',filnam(1:50)
          close(3)
          open(3,file=filnam,status='old',err=20)
          iunit=3
          nlinesave=nline
          nline=0
          i2=i2+1
          i3=i3+1
          i4=i4+1
        elseif(find(a,'then  ',1+8))then
          i4=i4+1
          if(.not.(find(a,'else if',128+256).or.
     &             find(a,'elseif', 128+256)))then
            i2=i2+1
            i3=i3+1
          endif
        elseif(find(a,'elseif',128+256).or.
     &         find(a,'else if',128+256))then
          i4=i4+1
          i3=i3-1
        elseif(find(a,'else  ',256).or.find(a,'entry ',4))then
          i4=i4+1
        elseif(find(a,'endif  ',256).or.find(a,'end if  ',256))then
          i3=i3-1
          i4=i4+1
        elseif(find(a,'enddo  ',256).or.find(a,'end do  ',256))then
          i3=i3-1
          i4=i4+1
          nlabel=nlabel-1
        elseif(find(a,'dowhile',128+256).or.
     &         find(a,'do while',128+256))then
          i2=i2+1
          i3=i3+1
          i4=i4+1
          icol2=icol
          nlabel=nlabel+1
          label(nlabel)='\\\\'
        elseif(find(a,' do ',256))then
          if(icol1.lt.7.or.a(7:max(7,icol1)).eq.' ')then
            i2=i2+1
            i3=i3+1
            i4=i4+1
            icol2=icol
            do while(icol2.lt.159.and.a(icol2:icol2).eq.' ')
              icol2=icol2+1
            enddo
            c=a(icol2:icol2)
            if(find(a(icol2:160),' ',0).and.c.ge.'0'.and.c.le.'9')then
              nlabel=nlabel+1
              label(nlabel)=a(icol2:icol2+icol-2)
            else
              nlabel=nlabel+1
              label(nlabel)='\\\\'
            endif
          endif
        elseif(find(a,'function ',4).or.find(a,'subroutine ',2).or.
     &         find(a,'program ',2) .or.find(a,'block data ',2).or.
     &         find(a,'blockdata ',2))then
          if(fout)print*,'Line',form,' ',b(1:60)
          if(InSub.eq.0)then
            InSub=1
            if(find(a,'program ',2))InMain=1
            i2=i2+1
            i3=i3+1
            i4=i4+1
            if(i3.ne.1)then
              PRINT*,'***ERROR--INVALID DIAGRAMMING INDEX line',form
              if(fout)
     &         WRITE(2,*)'***ERROR--INVALID DIAGRAMMING INDEX!***'
              print*,char(7)
            endif
          endif
          i3=1
        elseif(find(a,'end  ',256))then
          if(InSub.ne.0)then
            i3=i3-1
            i4=i4+1
            InSub=0
            if(InMain.gt.0)then
              InMain=0
              nMainEnd=nMainEnd+1
              if(fout)print*,'***MAINLINE END line ',form
              if(nMainEnd.gt.1)then
                PRINT*,'***ERROR--TOO MANY MAINLINE ENDS!***'
                if(fout)
     &           WRITE(2,*)'***ERROR--TOO MANY MAINLINE ENDS!***'
                print*,char(7)
              endif
            endif
          else
            nMainEnd=nMainEnd+1
            if(fout)print*,'***MAINLINE END line ',form
            if(nMainEnd.gt.1)then
              PRINT*,'***ERROR--TOO MANY MAINLINE ENDS!***'
              if(fout)WRITE(2,*)'***ERROR--TOO MANY MAINLINE ENDS!***'
              print*,char(7)
            endif
          endif
          if(i3.ne.0)then
            i3=0
            PRINT*,'***ERROR--INVALID DIAGRAMMING INDEX line',form
            if(fout)WRITE(2,*)'***ERROR--INVALID DIAGRAMMING INDEX!***'
            print*,char(7)
          endif
        endif

20      if(b(1:5).ne.' ')then                   ! Search for DO labels
          do i=1,nlabel
            j=nlabel+1-i                        !  (in reverse order)
            jjj=9
            dowhile (label(j)(jjj:jjj).eq.' '.and.jjj.gt.1)
              jjj=jjj-1
            enddo
            if(find(b(1:10),label(j)(1:jjj+1),1))then
              i3=i3-1
              i4=i4+1
              nlabel=j-1
              goto 20
            endif
          enddo
        endif

        a=' '
        if(i1.lt.0.or.i2.lt.0.or.i3.lt.0.or.i4.lt.0)then
          PRINT*,'***ERROR--INVALID DIAGRAMMING INDEX line',form
          if(fout)WRITE(2,*)'***ERROR--INVALID DIAGRAMMING INDEX!***'
          print*,char(7)
          i1=max(i1,0)
          i2=max(i2,0)
          i3=max(i3,0)
          i4=max(i4,0)
        endif
        jj=max(1,min(16,2*i2-1))
        if(i2.gt.0)a=aa(1:jj)
        if(i4.ne.0)then
          jjj=1
          dowhile(jjj.lt.160.and.b(jjj:jjj).eq.' ')
            jjj=jjj+1
          enddo
          if(jjj.gt.1)b(1:jjj-1)=
     &     '---------------------------------------------------'
          a(jj:16)='-------------------------------'
        endif
        do i=0,i4-1
          a(max(1,min(15,jj-i*2)):max(1,min(15,jj+1-i*2)))='+-'
        enddo
        if(iline.ne.0.and.b(max(1,iline):160).eq.' ')then
          if(nline.eq.0)write(fm,'(i5)')nlinesave
          form=fm
          if(form(1:1).eq.' ')form(1:1)='|'
          b(iline:iline+4)=form
        endif
        n=LenA(b)
        if(igoto.ne.0)a(15:15)='*'
c last minute change to reduce spaces in diagram block:
        if(fout)write(2,2)(a(i:i),i=1,15,2),(b(i:i),i=1,n)
        if(.not.fout)write(*,2)' ',(a(i:i),i=1,15,2),(b(i:i),i=1,n)
2       format(132a1)
        i1=i3
        goto 10
99      if(iunit.eq.3)then
          iunit=1
          i1=i1-1
          close(3)
          nline=nlinesave
          goto 10
        endif
        if(i3.gt.0.or.InSub.ne.0)then
          PRINT*,'***WARNING--SOME NEST LEVELS LEFT HANGING AT END***'
          print*,char(7)
        endif
        end
c-----------------------------------------------------------------------
        logical function find(a,b,icond) !find b in a, subject to conditions:
                                        !icond=sum of the following:
                                        !2:  Must be first non-blank
                                        !4:  Prior character, if present,
                                        !    must not be alphanumeric.
                                        !8:  Prior character, if present,
                                        !    must be blank or )
                                        !16: Prior character, if present,
                                        !    must be blank or ,
                                        !32: Next character not alphanumeric
                                        !64: Next character not alphabetic
                                        !128:Next character must be blank or (
                                        !256:1st non-blank, possibly except for
                                        !    labels
c Program by Mitchell R Grunes, ATSC/NRL (grunes@nrlvax.nrl.navy.mil).
c Revision date: 5/9/95.
        character*(*) a,b
        character*1   c,cNext,c2
        common icol,icol1
        logical result

        ii=len(a)
        jj=len(b)
        result=.false.
        do i=1,ii-jj+1
          if(a(i:i+jj-1).eq.b)then      ! Found--Now do tests
            icol1=i                     ! icol1=column of item found
            icol =i+jj                  ! icol =colomn after item found

            c=' '
            cNext=' '
            if(icol1.gt.1)c=a(icol1-1:icol1-1)
            if(icol .le.ii)cNext=a(icol:icol)

            result=.true.
            if(result.and.iand(icond,2).ne.0.and.icol1.gt.1)then
              result=a(1:icol1-1).eq.' '
            endif

            if(result.and.iand(icond,4).ne.0)
     &       result=(c.lt.'0'.or.c.gt.'9').and.(c.lt.'a'.or.c.gt.'z')

            if(result.and.iand(icond,8).ne.0)result=c.eq.' '.or.c.eq.')'

            if(result.and.iand(icond,16).ne.0)
     &       result=c.eq.' '.or.c.eq.','

            if(result.and.iand(icond,32).ne.0)
     &       result=(cNext.lt.'0'.or.cNext.gt.'9').and.
     &              (cNext.lt.'a'.or.cNext.gt.'z')

            if(result.and.iand(icond,64).ne.0)
     &       result=(cNext.lt.'a'.or.cNext.gt.'z')

            if(result.and.iand(icond,128).ne.0)
     &       result=cNext.eq.' '.or.cNext.eq.'('

            if(result.and.iand(icond,256).ne.0.and.icol1.gt.1)then
              ii=1
              do iii=1,6
                c2=a(iii:iii)
                if(c2.ge.'0'.and.c2.le.'9')ii=iii+1
                if(c2.ne.' '.and.(c2.lt.'0'.or.c2.gt.'9'))goto 20
              enddo
20            if(ii.lt.icol1)then
                result=a(ii:icol1-1).eq.' '
              endif
            endif

            find=result
            if(result)return
          endif
        enddo
        find=result
        end
c-----------------------------------------------------------------------
        function LenA(a)                        ! Length of string, at least 1
c Program by Mitchell R Grunes, ATSC/NRL (grunes@nrlvax.nrl.navy.mil).
c Revision date: 5/9/95.
        character*(*) a
        n=len(a)
        dowhile(n.gt.1.and.a(n:n).eq.' ')
          n=n-1
        enddo
        LenA=n
        end
---diagramf.bat--------------CUT HERE-----------------------------
rem procedure to diagram a FORTRAN language program

rem  by Mitchell R Grunes.

rem I assume that the executable is in directory \gsar89\grunes on
rem  your PC.

rem Syntax:
rem     diagramf
rem to be prompted for input parameters.

rem Alternate Syntax:
rem     diagramf filename(s)
rem to append diagram of file(s) into diagram.out

if %1a == a  c:\gsar89\grunes\diagramf
if %1a == a  goto quit

echo off
:loop

  echo ========================-- %1 --========================

  echo %1          >  diagram.sc
  echo diagram2.sc >> diagram.sc
  echo 0           >> diagram.sc
  echo 1           >> diagram.sc
  \gsar89\grunes\diagramf < diagram.sc
  type diagram2.sc >> diagram.out
  del diagram.sc
  del diagram2.sc
  shift
  if not %1a == a goto loop

:quit
-----------------------------CUT HERE-----------------------------

-------------------------------------------------------------------------
  (Opinions are mine alone.)
  Mitchell R Grunes (grunes@nrlvax.nrl.navy.mil)
  Allied-Signal Technical Services


