# Looking for parsers for fortran77

**URL:** <https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606>\
**Category:** Help\
**Created:** [October 24, 2022, 12:46pm UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606 "2022-10-24T12:46:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![uNouss](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/unouss/32/2575_2.png) [@uNouss](https://fortran-lang.discourse.group/u/uNouss)\
**Post date:** [October 24, 2022, 12:46pm UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/1 "2022-10-24T12:46:07Z")

</div>

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](https://github.com/camfort/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.

---

<div class="post-metadata">

**Author:** ![eugene\_epshteyn](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@eugene\_epshteyn](https://fortran-lang.discourse.group/u/eugene_epshteyn)\
**Post date:** [October 24, 2022, 1:50pm UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/3 "2022-10-24T13:50:56Z")

</div>

Check out LLVM flang: [Welcome to Flang’s documentation — The Flang Compiler](https://flang.llvm.org/docs/)

---

<div class="post-metadata">

**Author:** ![gak](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gak/32/146_2.png) [@gak](https://fortran-lang.discourse.group/u/gak)\
**Post date:** [October 25, 2022, 3:34pm UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/4 "2022-10-25T15:34:43Z")

</div>

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](https://github.com/FrankLhota56/fortran66-flex). It shouldn’t be too hard to make it work for Fortran 77.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [October 25, 2022, 6:40pm UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/5 "2022-10-25T18:40:06Z")

</div>

LFortran has a FORTRAN 77 parser [GitHub - lfortran/lfortran: Official main repository for LFortran](https://github.com/lfortran/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.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 29, 2022, 1:43am UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/6 "2022-10-29T01:43:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![uNouss](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/unouss/32/2575_2.png) [@uNouss](https://fortran-lang.discourse.group/u/uNouss)\
**Post date:** [November 2, 2022, 10:38am UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/7 "2022-11-02T10:38:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 2, 2022, 4:57pm UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/8 "2022-11-02T16:57:29Z")

</div>

> [@uNouss](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [October 2, 2024, 2:00pm UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/9 "2024-10-02T14:00:43Z")

</div>

Seems like it might be the outcome of this thread:

> **[Parsing Fortran-77 with proprietary extensions](https://ieeexplore.ieee.org/abstract/document/10336352)**
>
> Far from the latest innovations in software development, many organizations still rely on old code written in "obsolete" programming languages. Because this source code is old and proven it often contributes significantly to the continuing success of...

A preprint can be found here: [Parsing Fortran-77 with proprietary extensions - Inria - Institut national de recherche en sciences et technologies du numérique](https://inria.hal.science/hal-04205262/)

Quite an interesting read!

---

<div class="post-metadata">

**Author:** ![kaby76](https://avatars.discourse-cdn.com/v4/letter/k/aeb1de/32.png) [@kaby76](https://fortran-lang.discourse.group/u/kaby76)\
**Post date:** [October 2, 2024, 8:04pm UTC](https://fortran-lang.discourse.group/t/looking-for-parsers-for-fortran77/4606/10 "2024-10-02T20:04:10Z")

</div>

As mentioned in another thread, you might check out [grammars-v4/fortran/fortran77 at 6d13b1068d0fc9eba30a3f291fe62026fbc71c2f · antlr/grammars-v4 · GitHub](https://github.com/antlr/grammars-v4/tree/6d13b1068d0fc9eba30a3f291fe62026fbc71c2f/fortran/fortran77). 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](https://github.com/kaby76/Trash)) to query parts of the tree using XPath expressions. E,g., find all variables assigned in an assignment statement.

```auto
$ 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

```
