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
64 stars 29 forks source link

fparser2 incorrectly reports SyntaxError for format statement #129

Closed arporter closed 5 years ago

arporter commented 5 years ago

This can be reproduced with the following test code:

from fparser.api import get_reader
from fparser.two.Fortran2003 import Format_Stmt, Program

def test_format_rxxx(f2003_create):

    reader = get_reader(
        'program just_a_test\n'
        '      my_int = 10\n'
        '      100 FORMAT(a19,3(a18,"=",es14.7,x,a2,:,","),a12,i8)'
        'end program just_a_test\n')
    ast = Program(reader)

For which fparser reports a syntax error with the format statement. Strangely, if I just do:

reader = get_reader( '      100 FORMAT(a19,3(a18,"=",es14.7,x,a2,:,","),a12,i8)')
ast = Format_Stmt(reader)

then everything is fine.

arporter commented 5 years ago

I thought this might have been something to do with whether or not the reader thought it was free-form source but I've tried explicitly setting the mode to free-form and that didn't alter the behaviour.

rupertford commented 5 years ago

I can reproduce this error in fparser and confirm that it compiles fine with gfortran

rupert@ubuntu:~/proj/fparser/src/fparser/two$ cat tmp.f90
program just_a_test
100 FORMAT(a19,3(a18,"=",es14.7,x,a2,:,","),a12,i8)
end program just_a_test
rupert@ubuntu:~/proj/fparser/src/fparser/two$ ../scripts/fparser2.py tmp.f90 
Syntax error: at line 2
>>>100 FORMAT(a19,3(a18,"=",es14.7,x,a2,:,","),a12,i8)
at line 2
>>>100 FORMAT(a19,3(a18,"=",es14.7,x,a2,:,","),a12,i8)

parsing 'tmp.f90' failed at line #2 100 'FORMAT(a19,3(a18,"=",es14.7,x,a2,:,","),a12,i8)'
started at line #1'program just_a_test'
rupert@ubuntu:~/proj/fparser/src/fparser/two$ gfortran tmp.f90 
rupert@ubuntu:~/proj/fparser/src/fparser/two$ 
rupertford commented 5 years ago

Created branch format_error

rupertford commented 5 years ago

If you check the return value from

reader = get_reader( '      100 FORMAT(a19,3(a18,"=",es14.7,x,a2,:,","),a12,i8)')
ast = Format_Stmt(reader)

you should see that ast is None so it does not match and is consistent with the reported syntax error.

rupertford commented 5 years ago

I've found the root of the problem. fparser2 does not like the x in the format statement. If we remove it then the line is parsed, whereas this simple example fails ...

program just_a_test
100 FORMAT(x)
end program just_a_test
rupertford commented 5 years ago

Reading the spec for fortran2003 and fortran2008 it seems to me that fparser2 is correct. The use of x must be preceded by a positive integer.

In f2003

R1013 position-edit-desc is T n
or TL n
or TR n
or n X

In f2008

R1015 position-edit-desc is T n
 or TL n
 or TR n
 or n X
rupertford commented 5 years ago

And here we have it ...

rupert@ubuntu:~/proj/fparser/src/fparser/two$ gfortran -std=f2003 tmp.f90 
tmp.f90:5:12:

 100 FORMAT(X)
            1
Error: GNU Extension: X descriptor requires leading space count at (1)
rupertford commented 5 years ago

Created PR #141 from format_error branch to address this issue.

arporter commented 5 years ago

PR #141 has been merged. Closing issue.