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

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

play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

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)

  • Purpose: Converts source code into a sequence of tokens.

  • Example:
    Input Code (C language):

    int x = 10;

    Output (Tokens):

    Keyword: int
    Identifier: x
    Operator: =
    Number: 10
    Symbol: ;

Tool Used: Lexical Analyzer (Lexer)

 2. Syntax Analysis (Parsing)

  • Purpose: Checks if tokens form a valid syntax according to the language grammar.

  • Example:
    Input Tokens:

    int x = 10;

    Parsing Tree (Abstract Syntax Tree – AST):

    =
    / \
    int 10
    \
    x

    Error Detected: Missing semicolon (;) or incorrect placement of keywords.

Tool Used: Parser

 3. Semantic Analysis (Meaning Verification)

  • Purpose: Ensures code makes logical sense (type checking, scope checking, etc.).

  • Example:
    Input Code:

    int x;
    x = "Hello"; // Error: Type mismatch

    Error: x is declared as an integer, but assigned a string value.

Tool Used: Semantic Analyzer

 4. Intermediate Code Generation

  • Purpose: Converts the code into an intermediate representation (IR) that is independent of machine architecture.

  • Example (Three-Address Code Representation):
    Input Code:

    a = b + c * d;

    Intermediate Code (Three-Address Code – TAC):

    t1 = c * d
    t2 = b + t1
    a = t2

Tool Used: Intermediate Code Generator

 5. Target Code Generation

  • Purpose: Converts intermediate code into machine code (assembly language) for execution.

  • Example (x86 Assembly Code):

    MOV R1, c
    MUL R1, d
    MOV R2, b
    ADD R2, R1
    MOV a, R2

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)



Leave a Reply

Your email address will not be published. Required fields are marked *

error: