fparser icon indicating copy to clipboard operation
fparser copied to clipboard

stop with string concatentaion

Open bblay opened this issue 2 years ago • 8 comments

Hi, fparser reports a syntax error if you use string concatenation in a stop statement, despite the code compiling and running fine. It's fine with just a string. Tested with 2003 and 2008 standards.

program foo
    implicit none

    character(len = 20) :: str1
    character(len = 20) :: str2

    str1 = "Hello"
    str2 = " World!"

    print "(A)", str1//str2

    stop str1//str2

end program foo

bblay avatar Apr 27 '22 14:04 bblay

Just to pitch in with some information from the Fortran standard. I'm working from BS ISO/IEC 1539-1:2010

Section 8.4 tells us that both STOP and ERROR STOP accept a "stop code." This "stop code" is defined as a "scalar default character constant expression" or a "scalar integer constant expression."

Section 7.1.12 gives a list of features an expression must have to be considered "constant." It is written in dense specification speak but it is not clear to me that the use of non-parameter variables in the concatenation above is legal. Compilers apparently accept it so better insight will be needed.

Regardless concatenation and other operations should be accepted after a STOP or ERROR STOP statement in some circumstances.

"Modern Fortran explained" is a bit more readable. It seems to confirm the above but also notes that as of Fortran 2018 the requirement for the expression to be constant has been removed. That makes the above valid.

IBM's on-line Fortran documentation suggests that ERROR STOP was introduced with Fortran 2008. It also thinks that STOP started accepting expressions with Fortran 2008.

MatthewHambley avatar Apr 28 '22 06:04 MatthewHambley

Support for ERROR STOP was only recently added in #315. Hopefully, all that is required to fix this is a tweak to the list of subclasses that are matched against for the "stop code".

arporter avatar Apr 28 '22 08:04 arporter

Currently, Stop_Code is implemented as:

subclass_names = ['Scalar_Char_Constant']

@staticmethod
def match(string):
    return StringBase.match(pattern.abs_label, string)

while string concatenation is a level-3-expr (R710). Therefore, I think this is probably non-standard behaviour but our general philosophy is that "if compilers typically support it then so should fparser."

arporter avatar Apr 28 '22 09:04 arporter

Having a concatenation works fine with both gfortran and nvfortran, even when I specify strict conformance to the F95 standard. I found https://stackoverflow.com/questions/17446557/stop-code-in-variable which agrees with Matthew's comments above. It seems that it is non-standard to have an expression on the RHS but the compilers we've tested against don't even notice!

arporter avatar Apr 28 '22 10:04 arporter

@bblay and @MatthewHambley, which compilers have you tested with?

arporter avatar Apr 28 '22 10:04 arporter

@bblay and @MatthewHambley, which compilers have you tested with?

(Apologies for the delay) I've seen the code compile with gfortran from v4 to v11, and believe it also compiles on several other platforms/compilers including HPC.

bblay avatar May 31 '22 08:05 bblay

A related problem.

This program gives a syntax error with fparser (0.0.16) with both the 2003 and 2008 options.

program testprog
  stop -1
end program

The intel compiler with -std03 reports

testprog.f90(2): warning #8430: Using constant expression as a stop code is an extension of Standard F2003.

No warning with -std08.

No warnings from gfortran with -std=f2003

MartinDix avatar Nov 02 '22 21:11 MartinDix

Marking this as 'release', since it would be nice if we could remove the special handling in LFRic with the next fparser release.

hiker avatar Jul 23 '24 13:07 hiker