ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Compiler Construction
    COMP3149
    Progress0 / 32 topics
    Topics
    1. Introduction to interpreter and compiler2. Structure of a Compiler and its Phases3. Lexical Analyzer and Input Buffering4. Specifications and Recognitions of Tokens5. Regular Expressions and Finite Automata6. Transition Table and Transition Graph7. Definitions of Grammars, Derivations, and Parse Trees8. Ambiguity, Associativity, and Precedence of Operators9. Syntax Analysis and Role of the Parser10. Eliminating Ambiguity, Left Recursion, and Left Factoring11. Top-Down Parsing and Recursive-Descent Parsing12. First and Follow Sets13. LL(1) Grammars and Non-recursive Predictive Parsing14. Bottom-Up Parsing: Reductions and Shift-Reduce Parsing15. LR Parsing and LR(0) Parsers16. LR(0) Automaton and Parsing Table17. Shift-Reduce Conflicts18. SLR(1) Parsers: Automaton and Parsing Table19. LR(1) Parsers: Automaton and Parsing Table20. LALR Parsing: Automaton and Parsing Table21. Semantic Analysis and Intermediate Code Generation22. Three Address Code23. Tasks of Semantic Analyzer and Types of Errors24. Type Checking and Environments25. Type Conversions: Implicit vs Explicit26. Back Patching and Switch Statements27. Storage Organization and Stack Allocation of Space28. Heap Management and Optimization29. Code Generation: Design of a Code Generator30. Target Language and Addresses in Target Code31. Basic Blocks and Flow Graphs32. Optimization of Basic Blocks
    COMP3149›Semantic Analysis and Intermediate Code Generation
    Compiler ConstructionTopic 21 of 32

    Semantic Analysis and Intermediate Code Generation

    4 minread
    690words
    Beginnerlevel

    📘 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count690
      Code examples0
      DifficultyBeginner