0% found this document useful (0 votes)
3 views

Compiler_Concepts

The document provides an overview of compilers and related topics, detailing the functions and processes involved in compilation, such as lexical analysis, preprocessing, assembly, interpretation, linking, and loading. It compares compilers and interpreters, highlighting their differences in execution speed, debugging ease, and output generation. Additionally, it distinguishes between source code and object code, explaining their definitions and formats.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Compiler_Concepts

The document provides an overview of compilers and related topics, detailing the functions and processes involved in compilation, such as lexical analysis, preprocessing, assembly, interpretation, linking, and loading. It compares compilers and interpreters, highlighting their differences in execution speed, debugging ease, and output generation. Additionally, it distinguishes between source code and object code, explaining their definitions and formats.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Compiler and Related Topics - 7 Mark Answers

1. Compiler
A compiler is a system software that translates high-level programming language (HLL) code into machine
code (object code) before execution.
It processes the entire source code at once, generating an executable file.

Working of a Compiler:
1. Lexical Analysis - Converts the code into tokens.
2. Syntax Analysis - Checks for grammatical errors.
3. Semantic Analysis - Ensures logical correctness.
4. Intermediate Code Generation - Converts to an intermediate representation.
5. Optimization - Improves efficiency.
6. Code Generation - Produces machine code.
7. Code Linking & Loading - Produces the final executable.

Examples: GCC (C Compiler), Java Compiler, Turbo C.

Advantages:
- Faster execution since the entire code is compiled at once.
- Optimized performance due to code optimization.

Disadvantages:
- Debugging is difficult as all errors are shown after compilation.

2. Lexical Analysis
Lexical Analysis is the first phase of compilation, where the source code is converted into tokens. It removes
comments and whitespace while grouping
characters into meaningful sequences.

Steps in Lexical Analysis:


1. Tokenization - Breaks the code into tokens (keywords, identifiers, operators, etc.).
2. Symbol Table Generation - Stores variable and function names.
3. Error Handling - Identifies illegal tokens.

Example:
For the code:
int a = 10;

Lexical Analysis generates tokens:


- int -> Keyword
- a -> Identifier
- = -> Assignment Operator
- 10 -> Constant

Role of Lexical Analyzer:


- Improves readability for the parser.
- Detects lexical errors (e.g., incorrect identifiers).

3. Preprocessor
A preprocessor processes the source code before actual compilation. It handles macros, file inclusion, and
conditional compilation.

Functions of Preprocessor:
1. File Inclusion (#include) - Adds library files.
2. Macro Expansion (#define) - Replaces macros with values.
3. Conditional Compilation (#ifdef) - Allows selective code compilation.
4. Removing Comments - Simplifies compilation.

Example:
#include <stdio.h>
#define PI 3.14

- #include <stdio.h> adds standard I/O functions.


- #define PI 3.14 replaces PI with 3.14 in the code.

4. Assembler
An assembler converts assembly language into machine code (binary).

Steps in Assembly Process:


1. Converts mnemonics (e.g., MOV, ADD) into binary.
2. Resolves memory addresses.
3. Generates object code.
Example:
Assembly Code:
MOV AL, 02H
ADD AL, 03H

Machine Code:
B0 02
04 03

Types of Assemblers:
- One-Pass Assembler - Translates in a single pass.
- Two-Pass Assembler - Resolves addresses in two passes.

5. Interpreter
An interpreter translates and executes code line-by-line instead of compiling the whole program at once.

Examples: Python, JavaScript, MATLAB.

Advantages:
- Easy debugging as errors are detected immediately.
- No separate compilation step required.

Disadvantages:
- Slower execution than compiled programs.

Example:
Python Code:
print("Hello, World!")
The interpreter executes each line directly.

6. Linker
A linker combines multiple object files into a single executable file. It resolves references and addresses.

Functions of a Linker:
1. External Symbol Resolution - Links function calls.
2. Address Relocation - Adjusts memory addresses.
3. Library Linking - Adds standard functions like printf().
Types:
- Static Linker - Combines all files permanently.
- Dynamic Linker - Uses external libraries at runtime.

7. Loader
A loader loads the compiled program into memory for execution.

Steps in Loading Process:


1. Allocates Memory - Assigns space in RAM.
2. Relocates Addresses - Adjusts absolute addresses.
3. Starts Execution - Transfers control to the program.

Types:
- Absolute Loader - Uses fixed memory addresses.
- Relocatable Loader - Adjusts addresses dynamically.

8. Compiler vs Interpreter
Feature | Compiler | Interpreter
------------------ | -------------------------------- | --------------------------------
Execution | Translates entire code at once | Translates line-by-line
Speed | Faster | Slower
Debugging | Difficult | Easier
Output | Generates an executable file | No separate executable file
Example | C Compiler (GCC) | Python Interpreter

9. Source Code vs Object Code


Feature | Source Code | Object Code
--------------- | ----------------------------- | -----------------------------
Definition | Human-readable program in HLL | Machine-readable binary code
Processing | Written by programmers | Generated by compilers
Format | Text format | Binary format
Example | int a = 5; | 1010110 00000001

You might also like