UNIT 4 — Interpreters & Introduction of Compilers
1. Interpreters
An interpreter is a system software that reads the source program, analyzes it,
and executes it line-by-line or statement-by-statement.
Unlike a compiler (which translates the entire source code into machine code
before execution), an interpreter performs translation and execution
simultaneously.
Working :
1. Reads one statement at a time
The interpreter takes the first line/statement of the source code.
2. Performs syntax analysis
It checks if the statement follows the correct grammar of the
programming language.
3. Performs semantic analysis
It checks whether the statement makes logical sense
(e.g., variables must be declared before use, types must match).
4. Immediately executes the statement
If the statement is correct, the interpreter directly executes it on the
system.
5. Moves to the next statement
This cycle continues until the entire program is executed.
There is NO separate object code or machine code file generated.
Translation + Execution happen together.
1|Page
Diagram: Interpreter
Advantages of Interpreters :
1. Easier Debugging
Since execution is line-by-line:
Errors are detected immediately at the point where they occur.
The programmer knows exactly which line caused the problem.
Very helpful for students and beginners.
2. Faster Start-Up Time
Execution begins immediately because:
There is no need to translate the whole program first.
Useful for scripting and testing.
3. Good for Rapid Application Development
Because of immediate execution:
You can test code quickly.
Makes programming interactive and flexible.
4. Portable Programs
Many interpreted languages are platform-independent because:
The interpreter handles machine differences internally.
2|Page
Disadvantages of Interpreters :
1. Slower Execution
Since translation happens every time the program runs,
Each line is re-analyzed again and again,
It becomes slower than compiled programs.
2. No Permanent Machine Code
No object file is created.
Code must be interpreted every time, making execution repeatedly
slower.
3. More Memory During Execution
Interpreter, source code, and runtime environment must stay in memory
together.
Uses of Interpreters :
1. Learning Programming
Interpreters show errors immediately, so beginners can learn easily.
2. Quick Testing of Code
You can write a line of code and run it instantly without compiling the
whole program.
3. Scripting and Automation
Used to make small scripts for tasks like file handling, system tasks,
and automation.
4. Web Development
JavaScript and PHP (interpreted languages) are used to make
interactive websites.
5. Data Science and Research
Python and MATLAB (interpreted) help in testing ideas quickly with
instant results.
6. Platform Independent Programs
The same code can run on any system where the interpreter is available.
3|Page
7. Interactive Use (REPL)
You can run commands one by one in shells like Python shell, Node.js,
MATLAB, etc.
Examples of Interpreted Languages:
Python
Most popular interpreted language.
Uses CPython interpreter by default.
JavaScript
Runs inside web browsers.
Executed by JavaScript engines like V8 or SpiderMonkey.
PHP
Server-side scripting language, interpreted by PHP engine.
Ruby
Interpreted by Ruby MRI engine.
MATLAB
Executes mathematical statements line-by-line.
Early BASIC and LISP
Classic examples of old interpreted languages used in educational computers.
2. Pure and Impure Interpreters
I. Pure Interpreter:
A pure interpreter executes the program directly from the source code.
It does not create any intermediate code like bytecode.
Characteristics of Pure Interpreter:
No intermediate code is generated
Executes code exactly as it is written
4|Page
Simple design and easy to implement
Slower, because it parses (reads and analyzes) the program every time it runs
Examples:
Early BASIC
Early LISP systems
How Pure Interpreter Works:
Source Code → Interpreter → Execution
The interpreter reads each line
Checks it
Executes it immediately
No extra files, no stored bytecode
II. Impure Interpreter
An impure interpreter first converts the program into an intermediate
representation (IR) like bytecode, and then executes that IR.
Characteristics of Impure Interpreter:
Faster, because intermediate code is easier to execute
Can perform optimizations
Supports hybrid models (like JIT compilation)
More efficient than pure interpreters
Examples:
Python (creates bytecode .pyc files)
JavaScript V8 engine (uses JIT)
Ruby MRI interpreter
How Impure Interpreter Works:
Source Code → Intermediate Code (Bytecode) → Interpreter → Execution
First step: generate IR
Second step: interpret and execute IR
Makes the program run faster
5|Page
3. Introduction of Compilers
A compiler is a system software that reads the entire source code of a
program and converts it into machine code (binary code) that the
computer can directly execute.
Important Features of a Compiler
Translation happens only once
Execution becomes very fast because machine code is ready
Produces an executable file like .exe, .out
Performs optimization to make the program faster and efficient
Working:
Examples of Compiled Languages
C
C++
Rust
Go
Swift
6|Page
4. Phases of the Compiler
A compiler works in a sequence of steps called phases.
Each phase receives input from the previous phase and gives output to
the next.
1. Lexical Analysis (Scanner)
Reads the program character by character
Groups them into tokens (keywords, identifiers, numbers, symbols)
Removes spaces and comments
Output: Token list
2. Syntax Analysis (Parser)
Checks if the tokens form a correct sentence according to grammar
Builds a tree structure of the program (Parse Tree / AST)
Finds errors like missing ; or wrong brackets
3. Semantic Analysis
Checks the meaning of the code
Ensures variables are declared
Checks data types
Checks function arguments
Output: AST with semantic info
4. Intermediate Code Generation (ICG)
Converts the AST into simple, machine-independent code
Usually generates three-address code
5. Code Optimization
Makes the code faster and better
Removes unnecessary statements
Reduces calculations
Improves loops
7|Page
6. Target Code Generation
Converts optimized code into real machine code
Assigns registers
Generates executable file (like .exe)
8|Page
5. Introduction to Scanning & Parsing
A. Scanning (Lexical Analysis)
Scanning is the first phase of a compiler/interpreter.
It reads the program character by character and converts them into
tokens.
What it does
Converts characters → Tokens
Removes spaces and comments
Identifies keywords, identifiers, numbers, operators, symbols
B. Parsing (Syntax Analysis)
Parsing is the second phase of the compiler.
It checks whether the sequence of tokens follows the grammar (rules)
of the programming language.
What it does
Checks correctness of token arrangement
Follows grammar rules
Builds Parse Tree / Syntax Tree
Detects syntax errors
6. Aspects of Compilation (Detailed + Simple)
These are the important qualities a good compiler must have.
1. Correctness
The compiler must generate correct and accurate machine code.
The output program should always work exactly as the source code
intends.
9|Page
2. Efficiency
The generated code should run fast.
It should use minimum memory and system resources.
3. Error Handling
Compiler should detect errors and show clear, easy-to-understand
messages.
It should help programmers identify where the mistake is.
4. Portability
A compiler should work on different systems and platforms.
Example: A C compiler available on Windows, Linux, and macOS.
5. Optimization
Compiler should improve code performance by:
o Removing unnecessary instructions
o Making loops faster
o Reducing memory usage
6. Reliability
Compiler should work consistently without crashing.
It should handle wrong inputs and bad code gracefully.
7. Maintainability
The compiler should be easy to update, modify, and extend.
New features or optimizations should be simple to add.
10 | P a g e