When we write a program using languages such as C, C++, Java or Python, the text we type is called source code. A computer's processor cannot execute source code directly because it understands only machine language (binary instructions). The process of converting source code into a form the computer can run is done by two main kinds of system software: compilers and interpreters. This chapter explains what each does, how they work, their advantages and limitations, and how they differ in practice.
Program Compilation
A compiler is a program that translates source code written in a high‐level programming language into machine code or an intermediate code that can be executed by a computer. The act of translation performed by a compiler is called compilation.
Output of a compiler: native machine code (binary) such as a Windows .exe or UNIX a.out, or an intermediate format such as Java bytecode (.class files).
Whole-program translation: a typical compiler reads the entire source (or modules) and translates them into an executable file before execution.
Error detection: compilers perform syntax and many semantic checks during translation and report compile‐time errors.
Optimisation: compilers apply optimisations to improve runtime speed and reduce memory use; optimisation may be at source, intermediate or machine‐code level.
Independence at run time: once compiled, the executable runs without the compiler being present.
Need to recompile: if the source code changes, the affected parts (or the entire program) must be recompiled to produce an updated executable.
Typical languages: C, C++, Rust, Go and many system-level languages are commonly compiled to native code.
Phases of Compilation
Preprocessing: handles directives such as #include and macro expansion (C/C++).
Lexical analysis: the source text is split into tokens (identifiers, keywords, literals, operators).
Syntax analysis (parsing): tokens are checked against language grammar to build a parse tree or abstract syntax tree (AST).
Semantic analysis: type checking, scope resolution, and other semantic rules are enforced.
Intermediate code generation: an internal representation of the program is produced for optimisation.
Optimisation: transformations are applied to the intermediate code to improve performance or reduce resource use.
Code generation: the intermediate code is translated into assembly or machine code for a target architecture.
Assembly & linking: assembler produces object files; a linker combines object files and libraries into a final executable.
Examples and Notes
gcc compiles C programs to native machine code (on Linux typically creating an a.out or named executable).
Java is compiled by javac into bytecode (.class), which is platform independent; the bytecode is executed by the Java Virtual Machine (JVM).
Advantages of compiled programs: generally faster at runtime and can access low‐level system or hardware features; suitable for large software and performance‐critical applications.
Disadvantages: compilation time can be significant; executables are platform dependent (unless an intermediate, portable format is used).
Relevance to engineering disciplines: compiled languages are widely used where performance and direct hardware access are required - for example, embedded systems in Electrical Engineering, numerical simulation in Civil Engineering, and system software in Computer Science.
MULTIPLE CHOICE QUESTION
Try yourself: What must happen to source code before a computer can execute it?
A
It must be edited.
B
It must be saved as a text file.
C
It must be compiled into machine code.
D
It must be printed on paper.
Correct Answer: C
- Source code is written in a high-level programming language that is understandable by humans, like C or Java. - Computers cannot run this high-level code directly; they need it in a low-level language known as machine code. - Compiling is the process that converts source code into machine code so that the computer's CPU can execute it. - Therefore, before the computer can execute the program, the source code must go through the compiling process. - Options A, B, and D do not relate to the necessary step of preparing the code for execution by a computer.
Report a problem
Program Interpreter
An interpreter is a program that reads source code (or another high‐level representation) and executes it directly, translating and running it statement by statement or construct by construct without producing a separate standalone native executable file.
Direct execution: interpreters parse and execute code on the fly; common interpreters are those for Perl, Python, and Ruby.
Single-step processing: interpreters perform lexical analysis, parsing, and execution in a single runtime pass or in small incremental passes.
Useful for scripting: interpreters are convenient for writing and running scripts, quick tests, and programs that change frequently because there is no separate compile step to perform before running.
REPL support: many interpreted language implementations offer a REPL (Read-Eval-Print Loop) that aids rapid development and debugging.
Portability: interpreted source code can run on any platform that has the appropriate interpreter installed.
Dependency: interpreted code requires the interpreter at run time; without it the source remains plain text and not an executable program.
Limitations: interpreted programs are typically slower than compiled native code and may have restricted access to low‐level system or hardware features compared with compiled programs.
Examples: executing a Python script with the command python script.py runs the program under the interpreter without producing a separate binary.
Hybrid and Advanced Approaches
Bytecode and virtual machines: some languages (for example, Java and C#) use a hybrid approach - source is compiled into portable bytecode which is then interpreted or JIT‐compiled by a virtual machine.
Just‐In‐Time (JIT) compilation: modern interpreters or virtual machines use JIT to compile frequently executed code paths into native code at runtime, improving performance (examples: Java HotSpot, V8 for JavaScript, PyPy for Python).
Trace and dynamic optimisation: runtime systems can profile program behaviour and perform optimisations dynamically based on actual execution patterns.
Difference between Interpreter and Compiler
Translation vs execution: a compiler translates the whole program into machine code before execution; an interpreter translates and executes the program step by step at run time.
Output: compilers produce a separate executable file; interpreters do not produce a standalone executable (unless a separate packaging step is used).
Execution speed: compiled code generally runs faster; interpreted programs are usually slower unless JIT compilation or other optimisations are used.
Error detection: compilers detect many errors at compile time and report them before running; interpreters detect and report errors at the point of execution.
Portability: interpreted programs are portable across systems with the same interpreter; compiled programs are platform dependent unless compiled to an intermediate portable format.
Use cases: compilers are preferred for system software, high‐performance applications and embedded systems; interpreters are preferred for scripting, rapid prototyping, educational use, and interactive tasks.
Development cycle: interpreted languages allow quicker edit‐run cycles; compiled languages may require longer build times but often provide better runtime performance.
Access to system/hardware: compiled programs can more easily access low‐level system and hardware interfaces; interpreted programs may be restricted by the runtime environment.
Hybrid cases: languages and environments often combine techniques (bytecode + VM, interpreter + JIT) to get benefits of both approaches.
Practical Examples
C program: source written in C is compiled by gcc to a native executable that runs directly on the processor.
Python script: source is interpreted by the Python interpreter; the same script can run on any machine with a compatible Python interpreter installed.
Java: source is compiled into bytecode which is portable; the JVM interprets or JIT‐compiles the bytecode to native code at run time.
Summary and Applications
Compilers are used where performance, resource efficiency and direct hardware access are required - examples include operating systems, compilers for embedded controllers, scientific computing, and performance‐critical libraries. Interpreters are valuable for rapid development, scripting, automation, server‐side scripting on web servers, education and interactive development tools. Many modern systems combine compilation and interpretation techniques (bytecode, virtual machines, JIT) to balance portability and performance.
FAQs on Program Compiler and Program Interpreter - Computer Software, Computer Awareness
1. What is a program compiler?
Ans. A program compiler is a computer software that translates the source code of a program written in a high-level programming language into machine code or a lower-level language that can be directly executed by the computer's hardware. It performs a series of complex processes such as lexical analysis, syntax analysis, semantic analysis, and code generation to convert the code into a form that can be understood and executed by the computer.
2. What is a program interpreter?
Ans. A program interpreter is a software that directly executes the source code of a program written in a high-level programming language without explicitly converting it into machine code. Instead of translating the entire code before execution, an interpreter reads and executes the code line by line or statement by statement. It analyzes and executes each instruction in real-time, which makes it slower compared to a compiler but provides greater flexibility and ease of debugging.
3. What are the advantages of using a program compiler?
Ans. There are several advantages of using a program compiler: - Compiled programs generally execute faster as the code is translated into machine code beforehand. - Once compiled, the program can be executed multiple times without the need for recompilation, which saves time. - Compilers perform optimization techniques to improve the efficiency of the code. - Compiled programs are less dependent on the presence of the compiler, making them more portable. - Compilation helps in detecting errors and bugs in the code early on, enabling developers to fix them before execution.
4. What are the advantages of using a program interpreter?
Ans. Some advantages of using a program interpreter include: - Interpreted programs are platform-independent as they are executed by the interpreter, which abstracts the underlying hardware. - Interpreters provide a dynamic execution environment where code can be modified and executed interactively. - Debugging is easier with an interpreter as it allows developers to execute code step-by-step and analyze the intermediate results. - Interpreted programs are more flexible and can be easily modified without the need for recompilation. - Interpreters are useful for scripting languages, where rapid development and ease of use are prioritized over execution speed.
5. Can a program be both compiled and interpreted?
Ans. Yes, a program can be both compiled and interpreted. This is known as hybrid or just-in-time (JIT) compilation. In this approach, the program is initially compiled into an intermediate representation, which is then interpreted by an interpreter or a runtime system. The interpreter executes the intermediate code and can also dynamically compile certain parts of the code into machine code for better performance. This combination allows for a balance between execution speed and flexibility, as well as the ability to optimize code during runtime.
Previous Year Questions with Solutions, Extra Questions, Computer Awareness, Summary, Semester Notes, Objective type Questions, Free, study material, Program Compiler and Program Interpreter - Computer Software, practice quizzes, MCQs, shortcuts and tricks, Program Compiler and Program Interpreter - Computer Software, Sample Paper, mock tests for examination, pdf , ppt, video lectures, Viva Questions, Exam, Computer Awareness, Program Compiler and Program Interpreter - Computer Software, Important questions, Computer Awareness, past year papers;