WHAT IS A COMPILER?
It's a computer program translating a high-level programming language into the machine code. Basically, it helps to transform the human-readable code to a binary (1 and 0 bits) language that a computer processor can read. After the computer gets the idea, it can perform all the tasks written in machine code.
But there is a condition. It must comply with the syntax rule of the code’s programming language. Moreover, the compiler is only a program and cannot fix errors made in a source language. That’s why, if you make a coding mistake, you’ll need to go back to your language syntax and make changes. Otherwise, compiler will not compile.
What a compiler can do is let the programmer know if there are any errors, ranges, limits, etc., especially the syntactical errors in the program. The analyses of the program are made before converting it into machine code.
HOW IT WORKS
A compiler takes the entire program, written in a source code, and converts it into object code typically stored as a file. The object code is also referred to as binary code and can be directly executed by the machine.
EXAMPLES of compiled programming languages are C and C++.
COMPILER PROCESS PHASES:
Lexical analysis: Splitting of source code into an abstract fragment known as lexeme. A token is generated for each of the lexemes, referring to whether it is a keyword, a string, or some other variable.
Syntax Analysis: The tokens assigned are structured to form an Abstract Syntax Tree (AST) and checked for syntax errors.
Semantic Analysis: The AST is checked for semantic errors like the wrong variable assigned, using an undeclared variable, using keywords as variable, etc.
Intermediate Code Generation: The process of compilation generates two or more intermediate code forms.
Optimization: The compilation process looks for multiple ways through which the task can be enhanced.
Code generation: The compiler converts the optimized intermediate code into a machine code transforming a source program to an object program.
TYPES OF COMPILERS
All compilers are translation programs from High-level program code to machine code so that it can be understood by the computer. Depending on how the translation process takes place, Compilers are divided into the following types:
Single Pass Compilers
WHAT IS AN INTERPRETER?
It is a computer program, translating each high-level program statement into the machine code. This includes source code, pre-compiled code, and scripts. Both compiler and interpreter are similar in what they do – converting higher level programming language to machine code. However, a compiler will convert the source code into machine code (and create an exe file) BEFORE the program run. Interpreters convert code into machine code on the go WHEN the program is run.
So, it is safe to say that an Interpreter directly executes instructions written in a programming/scripting language.
EXAMPLES of interpreted languages are Perl, Python, Matlab and Ruby.
HOW IT WORKS
An interpreter works more or less similar to a compiler. The only difference between them is that the interpreter does not generate any intermediate code forms. Instead it reads the program line to line checking for errors, and runs the program simultaneously.
INTERESTING FACTS ABOUT INTERPRETERS AND COMPILERS.
1) Both compilers and interpreters transform source code (text files) into tokens, both may generate a parse tree, and both may generate immediate instructions. The basic difference is that a compiler system, including a (built in or separate) linker, generates a standalone machine code program, while an interpreter system instead performs the actions described by the high-level program.
2) Once a program is compiled, its source code is not needed for running. For interpreted programs, the source code is needed to run the program every time.
3) In general, interpreted programs run slower than the compiled programs.
4) Java programs are first compiled to an intermediate form, then interpreted by the interpreter.
KEY DIFFERENCES OF COMPILER AND INTERPRETER
- Compiler converts code written in a high-level programming language into the machine code at once, while an Interpreter converts each high-level program statement, one by one.
- Compiled code runs faster while interpreted code is slower.
- Compiler displays all errors after compilation, on the flip side, the Interpreter displays errors of each line one by one.
- Compiler is based on translation linking-loading model, whereas Interpreter is based on Interpretation Method.
- A Compiler can work with an entire program while an Interpreter can work with a single line of code at a time.
IN A NUTSHELL
Metaphorically speaking, the compilers and the interpreters are like an oven and induction stove. Both of them are used with the same purpose: to “cook” the source code into machine code so the computer can digest it. But there are different programming languages requiring different “cooking”. Compilers and interpreters' usage depends on the language being used and its various purposes.