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›Tasks of Semantic Analyzer and Types of Errors
    Compiler ConstructionTopic 23 of 32

    Tasks of Semantic Analyzer and Types of Errors

    4 minread
    714words
    Beginnerlevel

    🧠 Semantic Analyzer: Tasks and Types of Errors (Compiler Construction)

    Semantic analysis is one of the most important compiler phases. It checks whether the program is meaningful (logically correct) according to language rules, not just syntactically correct.


    📌 1. Semantic Analyzer

    📖 Definition

    A Semantic Analyzer is a phase of the compiler that checks the meaning and consistency of the program after syntax analysis.

    👉 It ensures that the program follows language rules related to meaning, such as:

    • Type correctness
    • Variable declarations
    • Function usage rules

    ⚙️ 2. Main Tasks of Semantic Analyzer

    🔹 1. Type Checking (Very Important)

    Ensures that operations are performed on compatible data types.

    Example:

    int a = 5;
    float b = a + 2.5;
    

    ✔ Valid because int can be converted to float

    ❌ Invalid:

    int a = "hello";
    

    🔹 2. Checking Variable Declaration

    Ensures variables are declared before use.

    Example:

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

    🔹 3. Scope Resolution

    Checks whether a variable/function is accessible in a given scope.

    Example:

    {
       int x = 10;
    }
    printf("%d", x);  // ❌ x out of scope
    

    🔹 4. Function Check (Arguments & Return Type)

    Ensures:

    • Correct number of arguments
    • Correct return type

    Example:

    int sum(int a, int b);
    
    sum(5);  // ❌ missing argument
    

    🔹 5. Array Index Checking (Static check if possible)

    Example:

    int a[5];
    a[10] = 20;  // ⚠ semantic error (out of range conceptually)
    

    🔹 6. Label and Jump Validation

    Checks correctness of goto, break, continue.


    🔹 7. Type Conversion (Type Casting)

    Handles implicit conversions.

    Example:

    float x = 10;  // int → float (automatic conversion)
    

    🔹 8. Symbol Table Management

    Maintains information about:

    • Variables
    • Functions
    • Data types
    • Scope

    👉 Very important for semantic analysis.


    📊 Flow of Semantic Analyzer

    Syntax Tree
         ↓
    Semantic Analyzer
         ↓
    Annotated Syntax Tree
         ↓
    Intermediate Code
    

    🚨 3. Types of Semantic Errors

    Semantic errors occur when syntax is correct but meaning is wrong.


    🔴 1. Type Mismatch Errors (MOST IMPORTANT)

    Occurs when incompatible types are used.

    Example:

    int x = "hello";
    

    🔴 2. Undeclared Variable Error

    Variable used without declaration.

    Example:

    sum = a + b;  // a, b not declared
    

    🔴 3. Multiple Declaration Error

    Same variable declared more than once in same scope.

    Example:

    int a;
    int a;  // ❌ error
    

    🔴 4. Function Related Errors

    Examples:

    • Wrong number of arguments
    • Wrong return type
    int add(int a, int b);
    add(10);  // ❌ error
    

    🔴 5. Scope Violation Error

    Using variable outside its scope.

    {
       int x = 5;
    }
    x = 10;  // ❌ error
    

    🔴 6. Operator Incompatibility

    Using invalid operations on types.

    int x = 10;
    int y = x + "abc";  // ❌ error
    

    🔴 7. Array Index Error (Semantic Level)

    int a[5];
    a[100] = 10;
    

    🔴 8. Return Type Error

    Function returns wrong type.

    int f() {
       return "hello";  // ❌ error
    }
    

    📌 4. Key Exam Points (VERY IMPORTANT)

    ✔ Semantic analysis checks meaning, not grammar ✔ Uses syntax tree + symbol table ✔ Performs type checking and scope checking ✔ Detects errors like:

    • Undeclared variables
    • Type mismatch
    • Wrong function usage

    📊 5. Quick Revision Table

    Task Description Example
    Type checking Ensures compatible types int + float OK
    Declaration check Variable must be declared x not declared
    Scope checking Valid access region out of block error
    Function checking Correct arguments/return missing arguments
    Symbol table Stores identifiers variables/functions
    Array check Logical index validation a[100] in a[5]

    Error Type Meaning Example
    Type mismatch Wrong data types int = "abc"
    Undeclared variable Not defined x used without declaration
    Multiple declaration Same variable twice int a; int a;
    Function error Wrong call wrong arguments
    Scope error Outside block variable out of scope
    Operator error Invalid operation int + string
    Return error Wrong return type int returns string

    🎯 Final Exam Definition

    The Semantic Analyzer is a compiler phase that checks the meaningfulness of the source program by verifying type correctness, variable declarations, scope rules, and function usage. It detects semantic errors such as type mismatch, undeclared variables, and scope violations using the symbol table and annotated syntax tree.


    Previous topic 22
    Three Address Code
    Next topic 24
    Type Checking and Environments

    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 count714
      Code examples0
      DifficultyBeginner