GFortran releases

Just hypothetically, what would one need to study or look through to be able to write a compiler in Fortran ?

You need to have some knowledge of how compilers are engineered: lexer, parser, semantic analysis but not necessarily be an expert on that.
If you are developing a new compiler you will most likely want llvm bindings (probably the C api at least until we have better generics support).
Most parser and lexer generators target C and C++ so you would need to add a Fortran target or more likely write the parser and lexer yourself.

Rustc was initially written in Ocaml, maybe LFortran could be rewritten in Fortran in some very distant future.

1 Like

Thanks, for posting this. I almost feel like this (or some version of it) should be a separate post for people to find easily i.e. “Contributing to GFortran”. Do you have any other bits of advice on getting new contributors started with gfortran?


I’ve been thinking about the idea a bit: Brad Richardson / lets-build-a-compiler · GitLab . I don’t expect to have real time to devote to the effort any time soon though.

As @meow464 said, you’ll likely have to write the parser manually, rather than be able to use any of the existing generator tools.


Another question:
@everythingfunctional @RonShepard @meow464
What would be the benefit of eating our own dog food “Cutting out the middle man” (I like this quote better) and writing the compiler in pure Fortran ?

(forgive my naive question)

Here is a general article on the subject.

In general, in our case it would mean one less language to master before being able to contribute to future compiler developments. Other benefits are that the tools that are developed to support the compiler can also be used for other unrelated projects.

1 Like

There are now three different free modern Fortran compilers (gfortran, lfortran and flang, which exists in a traditional and a “from scratch” version). One the one hand this is good, on the other hand, each of these compilers would profit from more manpower. So I don’t think a fouth free modern Fortran compiler would really be beneficial.

Furthermore these compilers profit from the existing llvn/gcc infrastructure, which has matured for at least a decade (or 3 in the case of gcc).

So while a Fortran compiler that can compile itself sounds like a nice idea, I do not really believe the Fortran community should increase the number of free compilers.

Disclaimer: My practical experience in building compilers is exactly zero, so take everything I wrote with a large grain of salt.


I appreciate and respect your opinion none the less.

However, I must push back.
The argument I have against this is that there is one common lisp implementation, yet there are different common lisp compilers each offering different things.

At one point in time there were more than three Fortran compilers, why then did those compiler manufacturers not use your argument ?

My point is how long do we have to wait until the proverbial well runs dry of gfortran devs ?
If we make a compiler in Fortran, we reduce the overhead required to contribute to the compiler.

1 Like

I would also add intel’s ifort and ifx compilers to that list. They are now also freely available, so that makes five compilers, not three.

And one reason for a fortran-based fortran compiler is exactly to address the manpower issue.

As for whether reading the K&R C book is sufficient to contribute to the gfortran effort, consider the following. Here is the very first program in that book, on page 6:

  printf("hello, world\n");

Here is what happens when you try to run it.

$ gcc hello.c
hello.c:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
hello.c:3:3: error: implicitly declaring library function 'printf' with type 'int (const char *, ...)' [-Werror,-Wimplicit-function-declaration]
  printf("hello, world\n");
hello.c:3:3: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
1 warning and 1 error generated.

I do know enough C to know how to fix these problems, but my point is that the C language has changed over the last four decades, just as fortran has changed, and it is sometimes difficult to keep up with all of the changes.


I know that’s not the main point of your post, but I really had to double check. In my book, there is an extra #include <stdio.h> before main.
Maybe that’s from the first version of the book (I have the second)?

Thank you for explaing the motivation

One question: Do you think of a pure frontend, or also a backend (that cares about CPU-specific optimizations ect)?

I am not opposed to writing a compiler in Fortran and actually believe the fields where Fortran is used should not become to specialized.
And I do not want to be the one that contributes nothing, but tells people what can/should not be done

I am just a bit concerned wheter the intersection of people interested in Fortran (most of them are rather natural than computer scientists) and people able to and willing to contribute to writing a compiler (for free) is limited and a fully standard- conforming compiler requires years of work.

The development of gfortran to the current state took about 20 years.

By the way, while Steve is concerned about the current state of gfortran, I still want to thank the developers (like him) for providing a free, and platform-dependent optimizing Compiler for modern Fortran. I am not sure whether this community would exist without gfortran.

I can not judge whether a switch to Fortran to write a compiler would motivate more people.
I agree that a lot of compilers (e.g. Free Pascal ect.) aim to be self-hosting.


With “free” I actually meant open-source and not bound the a vendor company, so that (in theory) everyone who is interested can contribute.

Still it is great that Intel provides a high-quality Fortran compiler and there is still competition like NAG.

I am not sure about this, I have to read up a lot more on compilers to answer this.

Again, I don’t know why it took this long. Only the compiler devs know why.

1 Like

We asked the same question when contemplating whether to switch fpm development to Fortran. It turns out it was a great decision. I’m not saying that means a Fortran compiler written in Fortran will necessarily turn out the same way, but it could.

Don’t take my suggestion to mean I’ve thought very long or hard about it, but I think the experience from fpm (the conventional wisdom would have been you’re crazy to try writing a package manger in Fortran) demonstrates that maybe the conventional wisdom isn’t still true that Fortran would not be well suited to writing a compiler.


Just to avoid misunderstandings: Mentioning that it took 20 years was not at all meant in a negative way. I rather wanted to express how much work and experience went into this projects.

It is the same for commercial compulers like the ones of Intel and NAG that have a even longer history.

1 Like

So you’re saying there’s a chance… :slight_smile:

That is exactly the point I was making. I also have several editions and printings of this book, and they are all different because the language changed over time in the 1980s and 1990s. And the language has changed even further after the last K&R printings up to the present.

1 Like

I understood :+1: . My point still stands, whereby we shouldn’t get to a point where there is no one to carry the torch of those 20 years of hard labour, and lower the contribution barrier so that people’s work is preserved and lives on. :muscle:

1 Like

I just want to say I’m sympathetic to this argument. I don’t think it would be a good idea to start yet another Fortran compiler project if it came at the expense of any of the existing projects. But I don’t think it necessarily would.

My suggestion was primarily that, if there are Fortran developers out there that aren’t really proficient in C or C++ and don’t want to take the time to learn LLVM or GCC, that they shouldn’t feel discouraged from playing around with writing a Fortran compiler in Fortran.


Considering the following discussion I weaken my statement from “I don’t think that it is beneficial” to “I am not sure whether it is beneficial due to concerns about the number of available contributors for free Fortran compilers”.

1 Like