fortran-src icon indicating copy to clipboard operation
fortran-src copied to clipboard

Extraneous brackets cause parsing for arithmetic operator error

Open RaoulHC opened this issue 2 years ago • 5 comments

The following valid fortran fails to be parsed

      program main
      integer*4 bar
      call foo((bar)*2)
      end program main

Giving:

ProgramFilefortran-src: test.f, 3:22: test.f: parsing failed. 
Last parsed token: TStar (3:21)-(3:21).
CallStack (from HasCallStack):
  error, called at app/Main.hs:108:36 in main:Main

This seems to apply to all arithmetic operators (haven't tried other operators) and only if the brackets are on the left side.

RaoulHC avatar Oct 17 '22 18:10 RaoulHC

Argh. This is surely caused by b335ac595b32fd784f2e9970325eb36bea44baa7 . I imagine it gets to the '(' VARIABLE ')' line in the CALLABLE_EXPRESSION rule:

https://github.com/camfort/fortran-src/blob/70c04566e72135cfefe37e75bc0ea8d587f333b7/src/Language/Fortran/Parser/Fixed/Fortran77.y#L750-L753

But (bar)*2 is an expression, not a variable. So it erroneously parses (bar), then has nowhere to go for the rest of the expression.

raehik avatar Oct 18 '22 16:10 raehik

Works fine on commit 58b0de3d0612680b661fa3fddb24d7f044450cc1 (immediately before the arg expr change)

raehik avatar Oct 18 '22 16:10 raehik

Why exactly is that parsed differently? Is there anything significant about bracketed arguments?

RaoulHC avatar Oct 18 '22 16:10 RaoulHC

In https://github.com/camfort/fortran-src/issues/203 we found some surprising cases where bracketing variables in call() argument lists produced different behaviour. We inserted an intermediate data type to enable disambiguating whether an arg was bracketed or not and adding parsing for it, above.

Not too sure how to approach this immediately. Do we have to refactor to handle bracketed expressions in a more complex way? may I ask @dorchard if he has any insights...?

raehik avatar Oct 18 '22 16:10 raehik

Oh I was unaware of that too, that's a feature that introduces some unfortunate syntactical ambiguity.

Not sure what the best way to handle that is. Obviously foo((var)*2) should still be valid so the parser might have some nastiness to deal with both these cases.

RaoulHC avatar Oct 18 '22 17:10 RaoulHC