What does the interpreter do
Compilation vs. Interpretation
When you write a program, it exists as a computer file, typically a piece of text placed in text files. Let's explore how this source code is executed through compilation and interpretation, and the advantages and disadvantages of each method.
Pure Text Source Code
The source code must be pure text, without any decorations such as different fonts, colors, embedded images, or other media.
Invoking the Interpreter
- The interpreter reads the source file from top to bottom and from left to right.
- It checks if all subsequent lines are correct using four aspects: alphabetically, lexically, syntactically, and semantically.
Error Handling by the Interpreter
- If the compiler finds an error, it finishes its work immediately, resulting in an error message.
- The interpreter informs you where the error is located and what caused it, but the messages may be misleading as the interpreter may detect errors at a distance from their real causes.
Example:
Using an entity of an unknown name causes an error discovered at the usage point, not at the introduction point.
Execution by the Interpreter
- If the line looks good, the interpreter tries to execute it.
- Each line is usually executed separately, and the "read-check-execute" process can be repeated multiple times.
- A significant part of the code may be executed successfully before the interpreter finds an error.
Compilation vs. Interpretation: Which is Better?
There is no definitive answer. Both methods have their advantages and disadvantages.
Advantages and Disadvantages of Each Model:
Compilation:
- Translates the source program once.
- Efficient execution.
- Requires recompilation for any changes in the source code.
Interpretation:
- Translates the source program each time it runs.
- Flexible for testing and debugging.
- Slower execution speed compared to compiled programs.
Short Questions and Answers
A computer program is a piece of text usually placed in text files.
The interpreter reads the source code from top to bottom and from left to right.
The compiler finishes its work immediately, resulting in an error message.
The interpreter informs you where the error is located and what caused it, but the messages may be misleading.
Multiple Choice Questions (MCQs)
- a) Decorated text
- b) Pure text
- c) Encrypted text
- d) Compiled text
Answer: b) Pure text
- a) Bottom to top
- b) Left to right
- c) Top to bottom, left to right
- d) Right to left
Answer: c) Top to bottom, left to right
- a) Skips it
- b) Modifies it
- c) Executes it
- d) Deletes it
Answer: c) Executes it
- a) Requires recompilation
- b) Slower execution speed
- c) Inflexible testing
- d) Platform dependency
Answer: b) Slower execution speed