Looking for parsers for fortran77

Hello to all.

For my internship, I am looking for a parser able to parse fortran77 and to provide me with an AST.

Thank you for your suggestions of parser fulfilling this requirement.
I found this fortran-src parser which does about what I want but it is in haskell (unknown language for me).
I would like to know if there are others to compare them.

As a reminder, I am new with Fortran too.

Check out LLVM flang: Welcome to Flang’s documentation — The Flang Compiler

1 Like

Maybe not helpful, but a recently announced implementation of a Flex/Bison parser for Fortran 66 can be found here: GitHub - FrankLhota56/fortran66-flex: Flex grammar for Fortran 66. It shouldn’t be too hard to make it work for Fortran 77.

LFortran has a FORTRAN 77 parser GitHub - lfortran/lfortran: Official main repository for LFortran and you can get the AST or ASR quite easily with --show-ast and --show-asr.

As @kargl said we need more info. What do you plan to do with the AST? What is the end objective? If you want to use an existing parser get the AST and then do stuff with it I suspect it won’t be simple. There is no standard AST format so you’ll need to write a lot of glue code between the parser and your application.

1 Like

As @gnikit said, LFortran can parse F77 and provide you with a nice AST. Let me know what you need, or if you discover any bugs and we are happy to fix them.

Hello all: @gak @gnikit @certik @kargl
Thank you all for your answers, which I will be sure to watch carefully. I am sick right now and am taking time to recover and will see all your suggestions.

@gnikit , @certik For Lfortran, I already tested it around April and at that time, it didn’t allow me to get ASTs from the fortran77 files I submitted to it. I guess it has improved since then and is now better at handling fortran77. I will try again.

@kargl @gnikit @certik My goal is to be able to develop a tool that can analyze and then migrate fortran77 code to more modern fortran. So I need to get the ASTs to do my processing.

1 Like

Yes, we didn’t have a dedicated fixed-form parser back then. We have one now, it can parse all of SciPy for example. If it fails for your code, please report a bug and we’ll fix it.

Seems like it might be the outcome of this thread:

A preprint can be found here: Parsing Fortran-77 with proprietary extensions - Inria - Institut national de recherche en sciences et technologies du numérique

Quite an interesting read!

As mentioned in another thread, you might check out grammars-v4/fortran/fortran77 at 6d13b1068d0fc9eba30a3f291fe62026fbc71c2f · antlr/grammars-v4 · GitHub. This is an Antlr4 grammar, but I cannot attest to how good it is because there are only 5 files for testing. (A proper test-suite for f77 is needed.) I tested the grammar for ambiguity, and none is detected. Antlr4 parsers output CSTs, not ASTs. But, you can work with these easily in a visitor or listener tree walker. Or, if you want, use the Trash toolkit (GitHub - kaby76/Trash: Toolkit for grammars) to query parts of the tree using XPath expressions. E,g., find all variables assigned in an assignment statement.

$ dotnet trparse -- -l ../examples/*.txt | dotnet trxgrep -- ' //assignmentStatement/varRef/NAME' | dotnet trcaret
CSharp 0 ../examples/continue-example.txt success 0.0517991
CSharp 0 ../examples/elseIfExample.txt success 0.0810741
CSharp 0 ../examples/example1.txt success 0.0043722
CSharp 0 ../examples/example2.txt success 0.0087449
CSharp 0 ../examples/goto-example.txt success 0.0140336
../examples/elseIfExample.txt:L9:       M = 3
                                        ^
../examples/elseIfExample.txt:L13:             INFO = 1
                                               ^
../examples/elseIfExample.txt:L16:             INFO = 2
                                               ^
../examples/elseIfExample.txt:L19:             INFO = 3
                                               ^
../examples/elseIfExample.txt:L22:             INFO = 4
                                               ^
10/02-15:53:45 ~/issues/g4-current/fortran/fortran77/Generated-CSharp