The document discusses compiler design options and the differences between compilers and interpreters. It states that a compiler converts a high-level language program into machine code all at once, while an interpreter converts the program line-by-line at runtime. Compilers generally execute programs faster but take longer to compile, while interpreters execute more slowly but can compile incrementally and debug line-by-line. The document also covers pure and impure interpreters, p-code compilers, and the roles of compilers and interpreters.
2. INTRODUCTION
A compiler is a translator that converts the high-level language into the machine language.
High-level language is written by a developer and machine language can be understood by the processor.
Compiler is used to show errors to the programmer.
The main purpose of compiler is to change the code written in one language without changing the meaning of the
program.
When you execute a program which is written in HLL programming language then it executes into two parts.
In the first part, the source program compiled and translated into the object program (low level language).
In the second part, object program translated into the target program through the assembler.
A compiler is a language processor that converts a users program written in a programming language into a machine
language program which can execute on the computer, while an Interpreter is a language processor that itself performs
the action indicated in a user program without generating a machine language program.
3. We have learnt that any computer system is made of hardware and
software. The hardware understands a language, which humans cannot
understand. So we write programs in high-level language, which is easier
for us to understand and remember. These programs are then fed into a
series of tools and OS components to get the desired code that can be used
by the machine. This is known as Language Processing System.
Let us first understand how a program, using C compiler, is executed on a
host machine.
User writes a program in C language (high-level language).
The C compiler, compiles the program and translates it to assembly
program (low-level language).
An assembler then translates the assembly program into machine code
(object).
A linker tool is used to link all the parts of the program together for
execution (executable machine code).
A loader loads all of them into memory and then the program is executed.
LANGUAGE PROCESSING SYSTEM
Pre
Processor
Compiler
Assembler
Linker
Loader
Memory
4. A compiler can broadly be divided into two phases based on the way they compile.
1. Analysis Phase
Known as the front-end of the compiler, the analysis phase of the compiler reads the source program, divides it into core parts
and then checks for lexical, grammar and syntax errors. The analysis phase generates an intermediate representation of the
source program and symbol table, which should be fed to the Synthesis phase as input.
2. Synthesis Phase
Known as the back-end of the compiler, the synthesis phase generates the target program with the help of intermediate source
code representation and symbol table.
COMPILER DESIGN- ARCHITECTURE
Analysis
Phase
Synthesis
Phase
Intermediate code
Source code Machine code
5. INTERPRETER
An interpreter is a computer program that is used to directly execute program instructions written using one of the many
high-level programming languages.
The interpreter transforms the high-level program into an intermediate language that it then executes, or it could parse the
high-level source code and then performs the commands directly, which is done line by line or statement by statement.
The interpreter reads each statement of code and then converts or executes it directly. In contrast, an assembler or a
compiler converts a high-level source code into native (compiled) code that can be executed directly by the operating
system (e.g. by creating a .exe program).
This feature makes easy for programmers to check any particular line at the time of debugging, but slows down the overall
time of execution of the entire program.
Data
Interpreter
Source
code
Machine
code
Advantage Disadvantage
The program code is already translat
ed into machine code. Thus, it code
execution time is less.
Interpreters are easier to use, especially
for beginners.
You can't change the program withou
t going back to the source code.
Interpreted programs can run on compu
ters that have the
corresponding interpreter.
6. 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
ROLE OF COMPILER & INTERPRETER
7. Interpreter Compiler
Translates program one statement at a time.
Scans the entire program and translates it as a whole into
machine code.
Interpreters usually take less amount of time to analyze the
source code. However, the overall execution time is
comparatively slower than compilers.
Compilers usually take a large amount of time to analyze the
source code. However, the overall execution time is
comparatively faster than interpreters.
No intermediate object code is generated, hence are memory
efficient.
Generates intermediate object code which further requires
linking, hence requires more memory.
Errors are displayed for every instruction interpreted. Errors are displayed after entire program is checked.
Programming languages like JavaScript, Python, Ruby use
interpreters.
Programming languages like C, C++, Java use compilers.
COMPARISION OF INTERPRETER & COMPILER
8. 1. Pure Interpreters:
In a pure interpreter, the source program is maintained in the source form throughout its interpretation. Each
statement of source program is analyzed while interpreting. This arrangement acquires substantial analysis
overheads when interpreting a statement.
2. Impure Interpreters:
An impure interpreter carries out some preliminary processing of the source program to decrease the
analysis overheads during interpretation. To reduce the analysis overhead, some initial processing of the
source program is done by the impure interpreter during interpretation.
The source program is given to the preprocessor and the preprocessor transforms the program to an
intermediate representation (IR) that is used during interpretation.
This speeds up interpretation since the code component of the IR that is the IC, can be analyzed more
resourcefully than the source form of the program.
PURE AND IMPUTER INTERPRETER
9. Like interpreter, p-code compilers are also analyzed and convert
source program into an intermediate code(IC).
P-code compilers are also called bytecode compilers & are very
similar in concept to interpreter. The source program is analyzed and
converted into an intermediate form, which is then executed
interpretively.
With a P-Code compiler, this intermediate forms is the machine
language for a hypothetical machine, often call pseudo-machine. P-
code object programs can be executed on any machine that has a P-
Code interpreter.
P-CODE COMPILERS
Compiler
Object Program
p-code
P-code
interpreter
Source Program
Advantage Disadvantage
1. Portability 1. Slower execution of program
2. Saves memory