How is a programming language programmed?

How is a programming language programmed? There are roughly two types of languages in terms of execution:

  • Compiled languages
  • Interpreted languages

If I could, I would add a third type: intermediate languages that are compiled for a virtual machine, like Java and its bytecode designed for the JVM. Although, now, the bytecode is translated to machine code via the JIT (Just In Time) compiler. Besides, we tend to classify Java in the first group, but it is not that simple.

The former use a compiler that translates the source code into a particular machine code. The latter use an interpreter that will read the source code on the fly and execute the program in the process. Compiled languages cannot produce programs that can be modified on the fly, you have to go through the compilation step to get an execution. Interpreted languages, like Python, can produce programs that can be modified at runtime. It is enough to modify a source code file (under certain conditions) and when the program executes its content again, it is again analyzed and executed.

To produce a compiler, it must be programmed. It is a software that has the particularity to produce an executable. There are different methods to develop one. In fact, in general, a developer does not write software for a machine or a system (except in specific cases), but for a compiler that will do it for him. In general, a compiler includes several modules: the scanner, the parser and the syntactic analysis, the semantic analysis, an abstract code generator (tree generation), the optimizer, the machine code generator. You will have to add the linker which allows you to manage the code dependencies and so on. It is quite complex to write and the number of engineers able to write one is quite limited.

To produce an interpreter, it is a bit simpler in theory because it is not necessary in this case to generate a binary code. The interpreter is usually a compiled program. This is not always the case, because you can write an interpreter with an interpreted language, a kind of Russian doll.

So it is possible to write a compiler or an interpreter with any language, in principle. You can find courses on the Internet to program a C compiler in Java, for example. But some languages are not adapted. Java and Python for example are not adapted. Many are written in C or C++ because they date from a time when these languages were the simplest or rather the most practical. It is better to use languages like Ocaml, Rust, Scala, etc. These languages implement the functional paradigm and as such offer adapted syntactic sugar and features that ensure a certain execution safety, and this is very important when writing a compiler. Then, it is necessary to choose the language according to the platform targeted for the compilation of the programs which will be produced with the language, although it is possible to make cannadian compilation (cross compilation). Moreover it is necessary to go through this way when building a new platform. Moreover, if we take the Java virtual machine, its reference compiler is written in C++ for the most part.

If we take the example of Rust, the first version of its compiler was written with Ocaml. Afterwards, the compiler was written again in Rust and then compiled with the original compiler to make it self-porting.

Programming, as you say, or rather designing, a programming language is not a trivial exercise. You will find below a link to a simple tutorial to help you to understand the process. You have to define a grammar anyway and you can find tools on the Internet that allow you to create a small language without having to code too much, but you shouldn’t expect it to be very subtle.

The question leads us to another one. Who came first: the machine or the software? I lean more towards the software because it took machines to run algorithms quickly. Without the need for software, the machine would not be of much use. Other people think the opposite. It’s a bit of a chicken and egg situation, although if you read the history of mathematics…