stfc / fparser

This project maintains and develops a Fortran parser called fparser2 written purely in Python which supports Fortran 2003 and some Fortran 2008. A legacy parser fparser1 is also available but is not supported. The parsers were originally part of the f2py project by Pearu Peterson.
https://fparser.readthedocs.io
Other
61 stars 29 forks source link

FortranSyntaxError in Fparser2 when `kind =` not present #110

Closed sethrj closed 5 years ago

sethrj commented 5 years ago

The following fails to compile with fparser2 (but it works correctly with intel and gfortran):

module mymod
    use, intrinsic :: ISO_C_BINDING
    implicit none
    private

    integer(C_INT), parameter, public :: MY_SPECIAL_NUMBER = 5_C_INT
    interface
        subroutine fails(farg1)
            integer(kind(MY_SPECIAL_NUMBER)), intent(in) :: farg1
        end subroutine
    end interface
end module

Error:

$ python test.py temp.f90
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/fparser-0.0.7-py3.6.egg/fparser/two/Fortran2003.py", line 209, in __new__
    return Base.__new__(cls, string)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/fparser-0.0.7-py3.6.egg/fparser/two/utils.py", line 257, in __new__
    raise NoMatchError(errmsg)
fparser.two.utils.NoMatchError: at line 12
>>>            integer(kind(MY_SPECIAL_NUMBER)), intent(in) :: farg1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    PROGRAM = F2008_PARSER(READER)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/fparser-0.0.7-py3.6.egg/fparser/two/Fortran2003.py", line 211, in __new__
    raise FortranSyntaxError(error)
fparser.two.utils.FortranSyntaxError: at line 12
>>>            integer(kind(MY_SPECIAL_NUMBER)), intent(in) :: farg1

The module compiles with both intel 18 and GCC 7 (although Intel emits a warning that I don't quite understand):

$ gfortran-mp-7 -std=f2003 -c -Wall -Wextra temp.f90
$ ifort -stand f03 -warn stderrors -c temp.f90
temp.f90(9): warning #6717: This name has not been given an explicit type.   [MY_SPECIAL_NUMBER]
            integer(kind(MY_SPECIAL_NUMBER)), intent(in) :: farg1
-------------------------^

It looks like the parser doesn't understand the definition of a type without the kind = in this circumstance for some reason. The following routines all compile with Intel and GCC, but the ones marked as failing do not compile in fparser2:

subroutine works(farg1)
    integer(kind = kind(MY_SPECIAL_NUMBER)), intent(in) :: farg1
end subroutine
subroutine fails(farg1)
    integer(kind(MY_SPECIAL_NUMBER)), intent(in) :: farg1
end subroutine
subroutine also_works(farg1)
    integer, parameter :: my_kind = kind(MY_SPECIAL_NUMBER)
    integer(kind = my_kind), intent(in) :: farg1
end subroutine
subroutine also_fails(farg1)
    integer, parameter :: my_kind = kind(MY_SPECIAL_NUMBER)
    integer(my_kind), intent(in) :: farg1
end subroutine
rupertford commented 5 years ago

Thanks @sethrj. I'll take a look.

rupertford commented 5 years ago

I can reproduce the error and have found the problem.

rupertford commented 5 years ago

Created branch kind_error_fix

rupertford commented 5 years ago

There is a fix on the branch if you want to try it out.

sethrj commented 5 years ago

Nice! It works, thanks :)

arporter commented 5 years ago

111 has been merged to master. Closing issue.