0% found this document useful (0 votes)
16 views4 pages

Compiler Design Notes

The document provides an overview of compiler design, detailing its phases including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It explains the role of a compiler as a translator from high-level to low-level programming languages while preserving program meaning and reporting errors. Additionally, it outlines the supporting components such as the symbol table and error handler.

Uploaded by

yugaldesktop
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)
16 views4 pages

Compiler Design Notes

The document provides an overview of compiler design, detailing its phases including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It explains the role of a compiler as a translator from high-level to low-level programming languages while preserving program meaning and reporting errors. Additionally, it outlines the supporting components such as the symbol table and error handler.

Uploaded by

yugaldesktop
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

Compiler Design Notes

Unit I: Overview & Phases of Compiler

Design by Yugal
[Link]. (Hons) Computer Science

November 26, 2025

Contents
1 Introduction and Syllabus Scope 1
1.1 What is a Compiler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Language Processing System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 The Phases of a Compiler 2


2.1 Visualizing the Phases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 Detailed Example: The Running Example 3


3.1 1. Lexical Analysis (Scanning) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 2. Syntax Analysis (Parsing) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.3 3. Semantic Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.4 4. Intermediate Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.5 5. Code Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.6 6. Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

4 Supporting Components 4

1 Introduction and Syllabus Scope


Syllabus Reference: Unit I covers Overview of compiler and Phases of compiler (Topics 1.1,
1.2 from the Textbook).

1.1 What is a Compiler?


A Compiler is a translator program. It accepts a program written in a high-level language
(called the Source Program) and translates it into an equivalent program in a low-level language
(called the Target Program), usually Machine Language or Assembly.
Compiler
Source Program −−−−−−→ Target Program
Key Function:

• It must preserve the meaning of the program.

• It must report errors (syntax or semantic) to the user.

1
Compiler Design (NEP-UGCF 2022) Unit I Notes

1.2 Language Processing System


Before the compiler executes, other system software prepares the code:

1. Preprocessor: Handles macros (e.g., #include).

2. Compiler: Generates assembly code.

3. Assembler: Converts assembly to relocatable machine code.

4. Linker/Loader: Links libraries and loads the executable into memory.

2 The Phases of a Compiler


The compilation process is divided into two main stages:

1. Analysis (Front End): Breaks the source program into pieces and creates an interme-
diate representation.

2. Synthesis (Back End): Constructs the target program from the intermediate represen-
tation.

2.1 Visualizing the Phases


Below is the architectural flow of a compiler:

Source Program Symbol Table Manager

1. Lexical Analysis

2. Syntax Analysis

3. Semantic Analysis
Error Handler

4. Intermediate Code Gen

5. Code Optimization

6. Code Generation

Target Machine Code

Figure 1: Phases of a Compiler

Department of Computer Science Design by Yugal


Compiler Design (NEP-UGCF 2022) Unit I Notes

3 Detailed Example: The Running Example


To understand the phases, we will trace the translation of the following statement:
position = initial + rate ∗ 60

3.1 1. Lexical Analysis (Scanning)


Task: Reads the stream of characters and groups them into meaningful sequences called Tokens.
• position → id1
• initial → id2
• rate → id3
• 60 → num (integer)
Output Stream:
id1 = id2 + id3 ∗ 60

3.2 2. Syntax Analysis (Parsing)


Task: Checks if tokens form a valid structure based on grammar rules. Constructs a Syntax
Tree. Note that due to precedence, multiplication (∗) is nested deeper than addition (+).

id1 +

id2 *

id3 60

Figure 2: Syntax Tree for: position = initial + rate * 60

3.3 3. Semantic Analysis


Task: Checks for semantic consistency (meaning), such as Type Checking.
• The compiler notices rate (id3 ) is a float, but 60 is an integer.
• It inserts a conversion node: inttofloat.
Tree Update: The node 60 becomes inttofloat(60).

3.4 4. Intermediate Code Generation


Task: Generates machine-independent code (e.g., Three-Address Code).
t1 = inttofloat(60)
t2 = id3 * t1
t3 = id2 + t2
id1 = t3

Department of Computer Science Design by Yugal


Compiler Design (NEP-UGCF 2022) Unit I Notes

3.5 5. Code Optimization


Task: Improving the intermediate code for speed or space.

• The compiler detects that inttofloat(60) is a constant calculation (60.0).

Optimized Code:

t1 = id3 * 60.0
id1 = id2 + t1

(Reduced from 4 lines to 2 lines).

3.6 6. Code Generation


Task: Mapping to target Assembly/Machine code (e.g., x86 logic).

LDF R2, id3 ; Load rate into Register 2


MULF R2, R2, #60.0 ; Multiply by 60.0
LDF R1, id2 ; Load initial into Register 1
ADDF R1, R1, R2 ; Add result to R1
STF id1, R1 ; Store R1 into position

4 Supporting Components
• Symbol Table: A database that stores variable names (identifiers), their types, and
memory locations. It is accessed by almost all phases.

• Error Handler: Detects problems (e.g., a typo in lexical analysis, a missing bracket in
syntax analysis) and reports them to the user.

Notes prepared by Professor (AI) for Yugal based on NEP-UGCF 2022 Guidelines.

Department of Computer Science Design by Yugal

Common questions

Powered by AI

Code optimization improves the efficiency of the target program by reducing execution time and/or the space it occupies. It identifies redundancies or constants in intermediate code, performing optimizations such as constant propagation. For instance, a calculation identified as a constant can be simplified, reducing the number of generated instructions .

The symbol table supports different phases of the compiler by storing variable names, their types, and memory locations. It acts as a centralized database accessed throughout the compilation process, aiding in tasks like type checking during semantic analysis and memory allocation in code generation .

The error handler in the compilation process detects and reports issues such as typographical errors during lexical analysis or syntax errors like missing brackets during parsing. It ensures that such errors are communicated to the user promptly and explicitly, thereby facilitating debugging .

Lexical analysis is fundamental because it reads the input stream of characters and groups them into meaningful sequences called tokens, which are the basic building blocks for syntax analysis. For example, during lexical analysis, individual components such as variable names and constants are identified and categorized, forming the basis for subsequent parsing .

Intermediate code generation is significant because it represents the source code in a machine-independent form, facilitating optimization and correctness checking before generating machine-specific code. This abstracts away platform-specific details, providing a common ground for further optimizations .

The primary role of a compiler is to translate a program written in a high-level language (the source program) into an equivalent program in a low-level language (the target program), typically machine language or assembly, while preserving the program's meaning and reporting errors .

Semantic analysis is crucial because it checks for semantic consistency and meaning within the program, such as type checking. This step ensures that operations are performed on compatible types and inserts necessary type conversions. For example, a float-integer operation would necessitate inserting a conversion node like inttofloat during semantic analysis .

The analysis stage, or front end, breaks down the source program into components and creates an intermediate representation. This includes lexical, syntax, and semantic analysis. The synthesis stage, or back end, constructs the target program from this intermediate representation, including intermediate code generation, code optimization, and code generation .

The conversion from high-level to low-level language is necessary to make the high-level instructions understandable by the machine. High-level languages are designed for human readability and logic, while low-level languages, such as machine code, are needed for execution by the computer's processor, making this conversion essential .

Syntax analysis determines if the structure of the code is valid by checking against predefined grammar rules. This phase constructs a syntax tree that represents the hierarchical structure of the code, ensuring correct precedence and associativity of operations, such as nesting multiplication operations deeper than addition .

You might also like