After parsing, a compiler must verify meaning, manage runtime memory, produce intermediate code and optimize it. PSC questions often ask type checking, scope, activation record, symbol table, three-address code and optimization types.

Engineering Definitions

Type checking

Standard definition: Verification that operations are applied to compatible data types.

Exam meaning: Operation र operands type-compatible छन् कि छैनन् जाँच्ने process।

Activation record

Standard definition: Runtime stack frame storing function call information.

Exam meaning: Function call को parameters, locals, return address आदि store गर्ने stack frame।

Intermediate code

Standard definition: Machine-independent representation between source and target code.

Exam meaning: Source र target code बीचको abstract code representation।

Optimization

Standard definition: Transformation that improves code performance or size without changing meaning.

Exam meaning: Program meaning नबदली speed/size/resource improve गर्ने transformation।

Concept Teaching

Semantic analysis asks whether a syntactically valid program makes sense. Runtime storage explains how calls and variables live during execution. Intermediate code enables machine-independent optimization before target code generation.

Semantic Analysis and Type Checking

Syntax can be correct while meaning is wrong.

  • Check declared before use.
  • Check type compatibility in expressions.
  • Check function argument count/types.
  • Check return type consistency.
  • Check scope and visibility.
  • Implicit conversion/coercion rules depend on language.

Runtime Storage

Storage areas support different lifetimes.

Area Stores Lifetime
Code Program instructions Whole execution
Static/global Global/static variables Whole execution
Stack Activation records/local calls Function call lifetime
Heap Dynamic objects Until freed/garbage collected

Intermediate Representations

IR simplifies optimization and portability.

  • Three-address code uses operations with at most three addresses.
  • Quadruple stores op, arg1, arg2, result.
  • Triple omits explicit result names using position references.
  • Control-flow graph represents basic blocks and branches.
  • SSA form gives each variable one assignment version.

Optimization

Optimization can be local, global or loop-oriented.

  • Constant folding evaluates constants at compile time.
  • Common subexpression elimination reuses repeated computation.
  • Dead code elimination removes unused computations.
  • Strength reduction replaces expensive operation with cheaper one.
  • Loop invariant code motion moves stable computation outside loop.
  • Peephole optimization improves small instruction windows.

Engineering Mechanism

  • Traverse AST after parsing.
  • Use symbol table to resolve names and types.
  • Report semantic/type errors.
  • Generate IR such as three-address code.
  • Build control-flow/data-flow information.
  • Apply safe optimizations.
  • Generate target code with runtime storage conventions.

Diagrams / Models To Draw

  • Draw activation record layout.
  • Draw AST to three-address code transformation.
  • Draw control-flow graph.
  • Draw stack and heap memory regions.
  • Draw optimization before/after example.

Formulas, Algorithms and Rules

  • Three-address form: x = y op z.
  • Activation record includes parameters, return address, saved registers, locals and temporaries.
  • Constant folding example: t = 3*4 -> t = 12.
  • Optimization must preserve program semantics.
Concept Purpose Exam trap
Type checking Semantic correctness Not syntax parsing
Symbol table Name attributes Used beyond lexer
Stack Call frames LIFO lifetime
Heap Dynamic storage Manual/GC management
IR Machine-independent code Not final machine code
Optimization Improve code Must preserve meaning

Exam Point

  • Type checking occurs after parsing.
  • Activation record is high-yield diagram.
  • Three-address code is common IR example.
  • Optimization can change form but not meaning.
  • Stack vs heap lifetime is common MCQ.

Worked Example

Expression `a = b + c * d` can become three-address code: t1 = c * d; t2 = b + t1; a = t2. This makes order explicit and helps optimization/code generation.

Subjective Answer Pattern

  • Define semantic analysis/type checking.
  • Explain symbol table role.
  • Describe runtime storage and activation record.
  • Explain IR/three-address code.
  • Discuss optimization techniques and safety.

Common Engineering Mistakes

  • Calling type error a lexical error.
  • Forgetting heap for dynamic allocation.
  • Saying optimization can change output.
  • Confusing activation record with process PCB.
  • Not distinguishing IR and target code.

MCQ Revision

  • Which phase checks type compatibility?
  • What does activation record store?
  • Where are dynamic objects stored?
  • What is three-address code?
  • What is dead code elimination?
  • What is constant folding?

Final Summary

  • Semantic analysis validates program meaning.
  • Runtime storage manages lifetimes.
  • IR enables analysis and portability.
  • Optimization improves performance while preserving semantics.
  • Activation record and symbol table are key compiler structures.