← CSC 414 Compiler Construction
Semantic Analysis and Intermediate Code Generation
Notes
Scope and Type Checking in Semantic Analysis
Semantic analysis represents the phase where a compiler determines whether source code adheres to language consistency rules that syntax analysis cannot express. A program may be syntactically immaculate yet semantically invalid, such as attempting to divide a floating-point number by a string or invoking a method outside its declared scope. Static semantic checking involves type checking, type coercion, operator overloading verification, flow-of-control checks, and variable uniqueness constraints within lexical blocks.
The Symbol Table Structure
The central data structure underpinning semantic validation is the symbol table. It tracks identifiers along with attributes such as type, memory offset, scope level, and parameter signatures. As the compiler traverses lexical blocks, symbol tables are dynamically maintained as linked chains or scoped hash tables. When an identifier appears in an expression, the semantic analyzer performs lookup operations through parent scopes to confirm existence and retrieve typing constraints.
Three-Address Code Generation
Once semantic soundness is verified, the compiler converts the Abstract Syntax Tree into an Intermediate Representation (IR). Using an IR decouples source language dependencies from hardware architecture specifics, maximizing code optimization and retargetability across different processors like x86 and ARM. A prevalent intermediate representation is Three-Address Code (TAC), where every instruction contains at most three memory references: an operator, two source operands, and one destination operand. TAC linearizes nested tree structures into easily optimizable sequential quad-tuples.
Practical example
Imagine compiling payroll software configured for the Kenya Revenue Authority (KRA) tax system. The program must calculate a statutory deduction based on: netTax = grossPay - (pension + relief).
First, the semantic analyzer validates that grossPay, pension, and relief are declared as numeric types (float) within the symbol table. If relief was declared as a string, a type-mismatch semantic error halts the compilation process.
Following successful validation, the Intermediate Code Generator converts the expression into Three-Address Code instructions using generated temporary registers (t1, t2):
t1 = pension + relief
t2 = grossPay - t1
netTax = t2
In this TAC representation, each operator is evaluated individually. The compiler can now apply local optimizations, such as copy propagation, subexpression elimination, or register allocation, before targeting specific CPU instruction sets for local Kenyan servers running Linux on ARM64 architecture.
Quiz
1. What is the primary advantage of utilizing an Intermediate Representation like Three-Address Code?
2. In Three-Address Code generation, how many memory address references can appear at most in a single standard instruction?
3. Which compilation task falls outside the theoretical capability of a context-free parser and requires semantic analysis?
Sign in to submit and securely grade your quiz.