fparser icon indicating copy to clipboard operation
fparser copied to clipboard

Can't parse preprocessed files

Open ZedThree opened this issue 3 years ago • 2 comments
trafficstars

Because fparser doesn't do any preprocessing, some files need to be preprocessed first in order to get valid Fortran that can then be parsed. Unfortunately, fparser can't handle the output of either cpp or gfortran -E -cpp:

$ echo "end" | gfortran -E -cpp - > mvce.f90
$ cat mvce.f90
# 0 "<stdin>"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "<stdin>"
end
$ fparser2 --task=none mvce.f90
File: 'mvce.f90'
Syntax error: at line 1
>>># 0 "<stdin>"

ZedThree avatar Apr 28 '22 15:04 ZedThree

This is because the annotations inserted by gfortran look like PP macros. To parse them successfully the existing preprocessor nodes would require to be extended.

We have had good experiences using pcpp to preprocess files before parsing with fparser, which might be a viable alternative for you?

reuterbal avatar Apr 29 '22 10:04 reuterbal

This turns out to be basically equivalent to the #line directive, but is from the output of the preprocessor.

Playing about with different compilers on godbolt, it looks like this is fairly standard preprocessor output, so should probably be supported in fparser.

Yes, pcpp would also work for my use case too.

ZedThree avatar Apr 29 '22 12:04 ZedThree