program Autocoder_To_Txt

! Convert Autocoder listings to text.

! Before a line with 1 in column 1 is noticed, emit a blank line
! before lines that have zero in column 1 and then emit that line
! with a blank in column 1.

! When a line with 1 in column 1 is noticed, and thereafter until one
! is noticed that begins {\tt 0SYMBOL}, delete lines that don't begin
! with blank or that are entirely blank.  Go back to the first mode
! if a line is noticed that begins {\tt 0SYMBOL}.

  character(132) :: LINE
  logical :: Saw1 = .false. ! "Saw a line beginning with 1"

  write ( *, '(a)' ) '{\scriptsize\begin{verbatim}'

  do
    read ( *, '(a)', end=9 ) line
    if ( saw1 ) then
      if ( line(1:7) == '0SYMBOL' ) then
        saw1 = .false.
      else
        if ( line(1:1) /= '' ) cycle
        if ( line == '' ) cycle
        write ( *, '(a)' ) trim(line)
        cycle
      end if
    end if
    if ( line(1:1) == '1' ) then
      saw1 = .true.
      cycle
    end if
    if ( line(1:1) == '0' ) write ( *, * )
    line(1:1) = ''
    write ( *, '(a)' ) trim(line)
  end do

9 write ( *, '(a)' ) '\end{verbatim}}'

  end program
