LFortran help: produce AST/ASR of a single file which contains #include

Dear LFortran users and experts,

I am new to LFortran. I think LFortran is an amazing project that could allow me to translate (the core part of) VASP into Julia. To do this, I need to do some experimental projects, starting with a single Fortran file from the VASP source code. However, most of the source files contains #include lines, which import definitions, macros from other files.

My question is, if I just wish to focus on a single Fortran file and want to use

lfortran --show-ast FORTRAN_SRC.F

to produce the AST and then feed that into the Julia AST processor, how can I instruct LFortran to properly include all necessary files? Currently I got an error message

(base) a@a:~/a/src$ lfortran --show-ast dmft.F
syntax error: Token 'GDEF' (of type 'identifier') is unexpected here
  --> dmft.F:37:3
   |
37 |   GDEF, ALLOCATABLE :: TWOE_4WANNIER(:,:,:,:,:,:,:)
   |   ^^^^ 


Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that must be fixed).

where the file dmft.F contains a line

(base) a@a:~/a/src$  cat dmft.F | grep "GDEF, ALLOCATABLE :: TWOE_4WANNIER" -C3
  ! 
  REAL(q),ALLOCATABLE :: WPOS(:,:)
  ! two electron four orbital integrals in a Wannier basis
  GDEF, ALLOCATABLE :: TWOE_4WANNIER(:,:,:,:,:,:,:)
 
  ! some parameter from ADD_XI routine, needed for PIJKL
  REAL(q), PARAMETER :: PIJKL_EMPTY_THRESHHOLD=0.00001

and I know that the first line in dmft.F is

(base) a@a:~/a/src$ head dmft.F 
#include "symbol.inc"
MODULE dmft 
...

and the file “symbol.inc” is a collection of macros, where I find

(base) a@a:~/a/src$  cat symbol.inc | grep "GDEF "
#define GDEF      REAL(q)
#define GDEF      COMPLEX(q)

In this particular case, how can I instruct LFortran to properly include "symbol.inc" when building the AST/ASR for the file dmft.F?

In general, how can I do this cleanly for each file in the source file folder?

You can use gfortran -E -cpp dmft.F firstly.

1 Like

Thank you very much!
BTW: to my suprise, there is an option --show-julia for LFortran!

You can do lfortran --cpp to enable the preprocessor. Please report all bugs that you find. Note that lfortran is still alpha, so it will almost for sure not work out of the box, for a large production code like Vasp. But many things will work.

Yes, once LFortran matures, it will be fun to translate many codes to Julia, C or C++ and compare the performance.

1 Like

That will actually be a very interesting use case. Writing an algorithm natively in Fortran, machine translating to C and C++, comparing, then doing the same thing in C and machine translating to Fortran or other languages.

2 Likes