DIZNR INTERNATIONAL

Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target

Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target

https://www.gyanodhan.com/video/7B2.%20GATE%20CSEIT/Compiler%20Design/273.%20Example%20of%20phase%20of%20compiler%20-%20Lexical%20%20%20Syntax%20l%20Semantic%20%20%20Intermediate%20%20%20Code%20%20%20Target.mp4

Phases of a Compiler – Examples for Each Phase

A compiler translates high-level code into machine code through multiple phases. These phases can be categorized into front-end (analysis) and back-end (synthesis) processes.

 1. Lexical Analysis (Tokenization)

Tool Used: Lexical Analyzer (Lexer)

 2. Syntax Analysis (Parsing)

Tool Used: Parser

 3. Semantic Analysis (Meaning Verification)

Tool Used: Semantic Analyzer

 4. Intermediate Code Generation

Tool Used: Intermediate Code Generator

 5. Target Code Generation

Tool Used: Code Generator

 Summary Table

Phase Purpose Example Output
Lexical Analysis Converts code into tokens int → Keyword, x → Identifier
Syntax Analysis Creates syntax tree Parses expressions a = b + c;
Semantic Analysis Checks for logical errors Type mismatch: int vs string
Intermediate Code Converts code to IR (Three-Address Code) t1 = c * d
Target Code Generation Generates machine/assembly code MOV R1, c

Would you like me to explain Compiler Optimizations as well?

Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target

Module 1 Compiler Phases of a Compiler ( Structure of …

Analysis of the source program, Phases of a compiler, …

Computer Science Compiler Design Module 2

Here is a simple and clear example of all major phases of a compiler, showing how one line of code is processed step by step:


Example Line of Code:

c
a = b + c;

Now let’s go through each phase of the compiler and see what it does with this line:


1. Lexical Analysis (Tokenization)

Breaks the code into tokens:

Token Type Token
Identifier a
Operator =
Identifier b
Operator +
Identifier c
Symbol ;

Output: Stream of tokens
Stored in symbol table (a, b, c)


2. Syntax Analysis (Parsing)

Checks grammar (structure) of the code.

  • Builds a parse tree or syntax tree:

markdown
=
/ \
a +
/ \
b c

Output: Syntax tree
If missing semicolon or wrong order, shows syntax error.


3. Semantic Analysis

Checks meaning of the code.

  • Is a declared before use?

  • Are b and c of same type?

  • Can we add them?

Output: Validated tree
If b is int and c is string, shows semantic error.


4. Intermediate Code Generation

Converts code into an intermediate form (machine-independent):

Example:

ini
t1 = b + c
a = t1

This makes optimization and translation easier.


5. Code Optimization (Optional)

Improves performance without changing meaning.

Before:

ini
t1 = b + c
a = t1

After:

ini
a = b + c // Direct assignment

6. Target Code Generation

Converts into machine code (assembly or binary):

Example (in simple assembly-like code):

css
LOAD R1, b
ADD R1, c
STORE R1, a

This is what your machine actually runs.


Summary Table:

Compiler Phase What it Does Output
Lexical Analysis Breaks into tokens Tokens
Syntax Analysis Checks structure Parse Tree
Semantic Analysis Checks meaning Annotated Tree / Errors
Intermediate Code Gen. Machine-independent code 3-address code
Code Optimization Improves code Optimized code
Target Code Generation Creates machine code Assembly / Binary Code

Would you like a diagram of the compiler phases, a PDF summary, or a quiz to test your understanding?

Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target

UNIT- I Introduction to Compiling

Compiler Design by Md. Ajij (5th Semester)