camfort / fortran-src

Fortran parsing and static analysis infrastructure
https://hackage.haskell.org/package/fortran-src
Other
48 stars 20 forks source link

Unable to parse a procedure containing comments in its parameter list #256

Closed uNouss closed 1 year ago

uNouss commented 1 year ago

Hello everyone, I have an example of code in fortran77 in which in the declaration of the parameters of procedures over several lines using line continuation, there are comments as on the following example.

C234567890123456789012345678901234567890123456789012345678901234567890123
      subroutine hello(
C == fname: first name ==
     & fname,
C == lname: last name ==
     & lname)
         character fname*(*), lname*(*)
         write(*,*) 'Hello ' , fname , ', ' , lname
      end
      program main
         character *10 fname, lname
         fname = 'John'
         lname = 'Doe'
         call hello(fname, lname)
      end
C----------------------------------------------------------------------^

I can compile it, so parse it, with gfortran but not with fortran-src with the following error message.

You can see it here to reproduce the bug asciicast

RaoulHC commented 1 year ago

Looks like it does parse fine if you use the legacy flag, i.e. fortran-src -v77l test.f.

I'm not entirely sure why it's confined to the legacy parser, but there's logic in the fixed form lexer in alexGetByte for skipping comments that are before a continuation line. It might be that this isn't actually allowed in the Fortran 77 standard but I will check. If not this should be made more general to the different fortran 77 parsers.

uNouss commented 1 year ago

@RaoulHC Thank you for the answer.

yes indeed with the legacy flag in addition, the parsing works.

RaoulHC commented 1 year ago

Looks like the 77 standard explicitly says this should be allowed in section 3.2 here, so I'll look at making this behaviour a bit more general.

uNouss commented 1 year ago

Okay thanks

RaoulHC commented 1 year ago

FYI I was having a slightly deeper look at the Fortran standard and there's a few things in your example that are actually non-standard and so the Fortran 77 parser fails to compile: underscore in a symbolic name, symbolic names that are longer than 6 characters and end subroutine id. So a strict Fortran77 standard parser should actually still fail.

Given how little old Fortran is actually strictly standard compliant I wonder if we should change the default to Fortran 77 legacy for .f files. Any thoughts on this @raehik or @dorchard?

uNouss commented 1 year ago

okay I have edited the example to take into account your remarks.

raehik commented 1 year ago

Confirmed that #257 has fixed this.