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 Analyzer Task…
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 23 of 32
    Topic Study Note Illustration
    23

    Tasks of Semantic Analyzer and Types of Errors

    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...

    🧠 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 view and download past papers

    Click on Show Past Papers to load available exam papers.

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

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction