ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3149Semantic Analysis Inte…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3149

    Compiler Construction

    Progress0 / 32 done
    Topics32
    1Introduction to interpreter and compiler2Structure of a Compiler and its Phases3Lexical Analyzer and Input Buffering4Specifications and Recognitions of Tokens5Regular Expressions and Finite Automata6Transition Table and Transition Graph7Definitions of Grammars, Derivations, and Parse Trees8Ambiguity, Associativity, and Precedence of Operators9Syntax Analysis and Role of the Parser10Eliminating Ambiguity, Left Recursion, and Left Factoring11Top-Down Parsing and Recursive-Descent Parsing12First and Follow Sets13LL(1) Grammars and Non-recursive Predictive Parsing
    14Reductions and Shift-Reduce Parsing15LR Parsing and LR(0) Parsers16LR(0) Automaton and Parsing Table17Shift-Reduce Conflicts
    18Automaton and Parsing Table
    19Automaton and Parsing Table
    20Automaton and Parsing Table21Semantic Analysis and Intermediate Code Generation22Three Address Code23Tasks of Semantic Analyzer and Types of Errors24Type Checking and Environments
    25Implicit vs Explicit26Back Patching and Switch Statements27Storage Organization and Stack Allocation of Space28Heap Management and Optimization
    29Design of a Code Generator30Target Language and Addresses in Target Code31Basic Blocks and Flow Graphs32Optimization of Basic Blocks
    4 min read
    Topic 21 of 32
    Topic Study Note Illustration
    21

    Semantic Analysis and Intermediate Code Generation

    (Compiler Construction — exam-ready, simple explanation with examples, diagrams, and key points)

    📘 Semantic Analysis and Intermediate Code Generation

    (Compiler Construction — exam-ready, simple explanation with examples, diagrams, and key points)


    🧠 1. Semantic Analysis

    ✅ Definition

    Semantic Analysis is the phase of the compiler that checks whether the program has a meaningful (logically correct) structure according to the language rules.

    👉 It is done after syntax analysis and ensures:

    “The program is syntactically correct AND makes sense.”


    📌 Position in Compiler

    Source Code → Lexical → Syntax → Semantic → Intermediate Code
    

    🎯 2. Purpose of Semantic Analysis

    It checks:

    ✔ Type checking ✔ Variable declaration ✔ Function correctness ✔ Scope rules ✔ Expression validity


    🔧 3. Main Tasks of Semantic Analysis


    🔹 1. Type Checking (Very Important)

    Ensures correct data types are used.

    📌 Example:

    int x = 10;
    float y = x + 2.5;
    

    ✔ Valid (implicit conversion)


    ❌ Invalid Example:

    int x = "hello";
    

    👉 Error: type mismatch


    🔹 2. Scope Checking

    Ensures variables are used within valid scope.

    Example:

    {
        int x = 10;
    }
    print(x);  // ❌ Error
    

    🔹 3. Function Checking

    Checks:

    • Correct number of arguments
    • Correct return type

    Example:

    int add(int a, int b);
    add(5);  // ❌ Error
    

    🔹 4. Declaration Checking

    Every variable must be declared before use.


    Example:

    x = 10;  // ❌ Error (not declared)
    

    🌳 4. Symbol Table (Very Important)

    ✅ Definition

    A symbol table stores information about identifiers.


    📊 Contains:

    • Variable name
    • Data type
    • Scope
    • Memory location

    📌 Example Table:

    Name Type Scope
    x int global
    y float local

    🧠 5. Semantic Errors

    Error Type Example
    Type mismatch int x = "abc"
    Undeclared variable x = 10
    Wrong function call add(5)
    Scope error using local variable outside block

    📌 6. Difference: Syntax vs Semantic

    Feature Syntax Analysis Semantic Analysis
    Checks Grammar rules Meaning rules
    Errors Syntax errors Logical errors
    Example missing ; type mismatch
    Stage Before semantic After syntax

    🧠 7. Intermediate Code Generation (ICG)

    ✅ Definition

    Intermediate Code Generation converts source code into an intermediate representation (IR) that is:

    • Independent of machine
    • Easier to optimize
    • Easier to translate to machine code

    🎯 Purpose

    ✔ Bridge between source code and machine code ✔ Helps optimization ✔ Makes compiler portable


    📊 8. Forms of Intermediate Code


    🔹 1. Three Address Code (TAC) ⭐ Most Important

    Each instruction has at most 3 operands.


    📌 Example:

    a = b + c * d
    

    TAC:

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

    🔹 2. Quadruples

    Format:

    (op, arg1, arg2, result)
    

    Example:

    op arg1 arg2 result
    * c d t1
    + b t1 t2
    = t2 a

    🔹 3. Triples

    Format:

    (index) op arg1 arg2
    

    Example:

    i op arg1 arg2
    0 * c d
    1 + b (0)
    2 = (1) a

    🔹 4. Indirect Triples

    • Uses pointer table to refer triples
    • Easier optimization

    🧠 9. Expression Translation (Important Exam Part)


    📌 Example:

    x = a + b * c
    

    Step 1: Convert multiplication first

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

    🌳 10. Syntax Tree vs Intermediate Code

    Feature Syntax Tree Intermediate Code
    Form Tree Linear code
    Use Structure Execution steps
    Level High Medium
    Machine dependency No No

    🔄 11. Flow of Semantic + ICG

    Syntax Tree → Semantic Analysis → Annotated Tree → Intermediate Code
    

    ⚠️ 12. Common Exam Points

    👉 Frequently asked:

    • Define semantic analysis
    • Role of symbol table
    • Type checking with example
    • What is intermediate code?
    • Three address code with example
    • Quadruples vs triples
    • Difference between syntax and semantic analysis
    • Translate expression into TAC

    📝 13. Short Notes (Quick Revision)

    🔵 Semantic Analysis

    • Checks meaning
    • Uses symbol table
    • Detects logical errors

    🔵 Intermediate Code

    • Machine independent
    • Easy for optimization
    • Common form: TAC

    📊 14. Final Summary Table

    Aspect Semantic Analysis Intermediate Code
    Purpose Check meaning Generate IR code
    Input Syntax tree Annotated tree
    Output Annotated tree Intermediate code
    Focus Correctness Translation
    Tool Symbol table TAC / Quadruples
    Errors Semantic errors None (after generation)

    ✅ Final Conclusion

    • Semantic Analysis ensures program meaning is correct
    • It uses symbol tables and type checking
    • Intermediate Code Generation converts program into machine-independent form
    • Both are essential steps between syntax analysis and code optimization

    ← Previous topic 20
    LALR Parsing: Automaton and Parsing Table
    Next topic 22 →
    Three Address Code

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    690words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction