Compilers

While high-level languages are human-friendly and simplify their task of writing computer programs, processors only understand machine language. In short, any program written in a high-level language must be translated to machine language. This translation is done by programs known as compilers.

Program instructions in a high-level language, such as C, C++, C#, and Java, are referred to as the source code, which is then translated by a compiler to machine language to create what is known as an executable program, which is a set of instructions for the computer processor to execute. Machine code is specific to the type of processor and the operating system. Thus, while the source code remains the same, you need to use a compiler according to the type of processor and operating system on which you intend to run your program.

Program instructions written in simple text files, with an extension, specific to the programming language (such as .cs for C#, .cpp for C++, .java for Java, .js for JavaScript, .ino for Arduino, and so on). When the source code file in any of these languages is translated to machine code, an executable file is created with an extension that depends on the operating system (such as .exe for Windows, .asm for machine code, etc.). Compilers also perform syntax and semantic checks on the program and in case they find any errors, then the compilation fails and provides the programmer feedback on what needs to be corrected. It is useful, so the incorrect code is not allowed to run on computers.

There are a variety of compilers that translate code in intermediate stages or even do a reverse translation. For example:

  • An interpreter translates code from a higher-level language to a lower-level language by scanning it line-by-line but does not convert them into the machine code file.
  • An assembler translates assembly code to machine code.
  • A decompiler translates code from a lower-level language to a higher-level one.
  • A transpiler is a source-to-source compiler that takes the source code of a program written in one programming language as its input and produces the equivalent source code in a different programming language. As one example, a transpiler translates TypeScript, which is a superset of JavaScript, to JavaScript. With this approach, you can use TypeScript or JavaScript to write the program, and both will work in the same environment. While this is not very common, we will be using TypeScript or JavaScript extensively, and therefore it is good to know about transpilers.

An interesting conundrum that should come to mind is this: If a compiler is a program written in a high-level language, then how is it compiled into a compiler? The answer is that, initially, it is necessary to write some low-level machine code that further compiles the compiler code into an executable. Once a minimal compiler was programmed this way, it could be executed to create another one written in the language it compiles. That again can be used to create a higher level one and so forth.

Today enough high-level languages exist to allow new compilers to be created easily relatively. It does require an extraordinary level of programming skills since other programmers trust that their code will be compiled correctly and their program will execute as intended.

Language Translation