CSS Prepare

optional · CSS study notes

Semantic Analysis

Definition

Semantic analysis checks that a syntactically valid program is also meaningful — respecting type rules, variable declarations, and scope — using information collected in the symbol table.

Key Points

  • Symbol Table: a data structure (often hash table) storing each identifier's name, type, scope, memory location; built and consulted throughout compilation. Supports operations: insert, lookup, delete (on scope exit).
  • Type Expressions: a notation to represent types, e.g. int, array(10, int), int → int (function type), built recursively from basic and constructed types.
  • Type Checking: verifying operator/operand type compatibility per language rules.
    • Type coercion: automatic conversion between compatible types (e.g., int → float)
    • Type error: incompatible types used together (e.g., adding a string to a boolean) — flagged at compile time in statically typed languages
  • Also handles: scope resolution, checking for undeclared variables, duplicate declarations, function-argument count/type matching.

Example

Declaring int x; float y; z = x + y; → semantic analyzer inserts x, y into symbol table with types; on x + y, applies coercion rule int → float, so result type is float.

📌 CSS Frequency: High — often paired with symbol table structure diagrams and a small type-checking trace.

Model Answer (short)

"Semantic analysis verifies that a syntactically correct program is meaningful, primarily through type checking and scope resolution, using the symbol table built during earlier phases to track each identifier's type and location. Type expressions represent types recursively (basic types plus constructors like arrays and functions), and type checking flags incompatible operations while applying automatic coercion where the language rules allow it."