Compiler Construction
Introduction to Compiler
What is Compiler?
A compiler is a computer program that transforms code written in a high-
level programming language into the machine code. It is a program
which translates the human-readable code to a language a computer
processor understands (binary 1 and 0 bits). The computer processes the
machine code to perform the corresponding tasks.
A compiler should comply with the syntax rule of that programming
language in which it is written. However, the compiler is only a program
and cannot fix errors found in that program. So, if you make a mistake,
you need to make changes in the syntax of your program. Otherwise, it
will not compile.
What is Interpreter?
An interpreter is a computer program, which coverts each
high-level program statement into the machine code. This
includes source code, pre-compiled code, and scripts. Both
compiler and interpreters do the same job which is
converting higher level programming language to machine
code. However, a compiler will convert the code into
machine code (create an exe) before program run.
Interpreters convert code into machine code when the
program is run.
Key Difference
Compiler transforms code written in a high-level programming language into the
machine code, at once, before program runs, whereas an Interpreter coverts each
high-level program statement, one by one, into the machine code, during program
run.
Compiled code runs faster while interpreted code runs slower.
Compiler displays all errors after compilation, on the other hand, 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.
Compiler takes an entire program whereas the Interpreter takes a single line of
code.
Role of Compiler
Compliers reads the source code, outputs executable code
Translates software written in a higher-level language into
instructions that computer can understand. It converts the text that a
programmer writes into a format the CPU can understand.
The process of compilation is relatively complicated. It spends a lot
of time analyzing and processing the program.
The executable result is some form of machine-specific binary code.
Role of Interpreter
The interpreter converts the source code line-by-line during RUN
Time.
Interpret completely translates a program written in a high-level
language into machine level language.
Interpreter allows evaluation and modification of the program while
it is executing.
Relatively less time spent for analyzing and processing the program
Program execution is relatively slow compared to compiler
HIGH-LEVEL LANGUAGES
High-level languages, like C, C++, JAVA, etc., are very
near to English. It makes programming process easy.
However, it must be translated into machine language
before execution. This translation process is either
conducted by either a compiler or an interpreter. Also
known as source code.
MACHINE CODE
Machine languages are very close to the hardware. Every
computer has its machine language. A machine language
programs are made up of series of binary pattern. (Eg.
110110) It represents the simple operations which should
be performed by the computer. Machine language
programs are executable so that they can be run directly.
Features of Compilers
Correctness
Speed of compilation
Preserve the correct the meaning of the code
The speed of the target code
Recognize legal and illegal program constructs
Good error reporting/handling
Code debugging help
Why use a Compiler?
Compiler verifies entire program, so there are no syntax or semantic errors
The executable file is optimized by the compiler, so it is executes faster
Allows you to create internal structure in memory
There is no need to execute the program on the same machine it was built
Translate entire program in other language
Generate files on disk
Link the files into an executable format
Check for syntax errors and data types
Helps you to enhance your understanding of language semantics
Helps to handle language performance issues
Opportunity for a non-trivial programming project
The techniques used for constructing a compiler can be useful for other purposes
as well
History of Compiler
Important Landmark of Compiler's history are as follows:
The "compiler" word was first used in the early 1950s by Grace Murray Hopper
The first compiler was build by John Backum and his group between 1954 and
1957 at IBM
COBOL was the first programming language which was compiled on multiple
platforms in 1960
The study of the scanning and parsing issues were pursued in the 1960s and 1970s
to provide a complete solution