Semantic Analysis in Compiler Design - GeeksforGeeks (2024)

Improve

Semantic Analysis is the third phase of Compiler. Semantic Analysis makes sure that declarations and statements of program are semantically correct. It is a collection of procedures which is called by parser as and when required by grammar. Both syntax tree of previous phase and symbol table are used to check the consistency of the given code. Type checking is an important part of semantic analysis where compiler makes sure that each operator has matching operands.

Semantic Analyzer:
It uses syntax tree and symbol table to check whether the given program is semantically consistent with language definition. It gathers type information and stores it in either syntax tree or symbol table. This type information is subsequently used by compiler during intermediate-code generation.

Semantic Errors:
Errors recognized by semantic analyzer are as follows:

  • Type mismatch
  • Undeclared variables
  • Reserved identifier misuse

Functions of Semantic Analysis:

  1. Type Checking –
    Ensures that data types are used in a way consistent with their definition.
  2. Label Checking –
    A program should contain labels references.
  3. Flow Control Check –
    Keeps a check that control structures are used in a proper manner.(example: no break statement outside a loop)

Example:

float x = 10.1;float y = x*30; 

In the above example integer 30 will be typecasted to float 30.0 before multiplication, by semantic analyzer.

Static and Dynamic Semantics:

  1. Static Semantics –
    It is named so because of the fact that these are checked at compile time. The static semantics and meaning of program during execution, are indirectly related.
  2. Dynamic Semantic Analysis –
    It defines the meaning of different units of program like expressions and statements. These are checked at runtime unlike static semantics.

Last Updated : 22 Apr, 2020

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Semantic Analysis in Compiler Design - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5293

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.