Semantic Analysis – Compiler



Syntax analyzer will just create parse tree. Semantic Analyzer will check actual meaning of the statement parsed in parse tree. Semantic analysis can compare information in one part of a parse tree to that in another part (e.g., compare reference to variable agrees with its declaration, or that parameters to a function call match the function definition).

Semantic Analysis is used for the following –

  1. Maintaining the Symbol Table for each block.
  2. Check Source Program for Semantic Errors.
  3. Collect Type Information for Code Generation.
  4. Reporting compile-time errors in the code (except syntactic errors, which are caught by syntactic analysis)
  5. Generating the object code (e.g., assembler or intermediate code)

Sematic Analyzer in Compiler Design

Now In the Semantic Analysis Compiler Will Check –

  1. Data Type of First Operand
  2. Data Type of Second Operand
  3. Check Whether + is Binary or Unary.
  4. Check for Number of Operands Supplied to Operator Depending on Type of Operator (Unary | Binary | Ternary)