Newby Coder header banner

Semantics

What is Semantics

Semantics reveals the meaning of syntactically valid strings in a language

It describes the behavior that a computer follows when executing a program in the language

This behaviour might be disclosed by describing the relationship between the input and output of a program or by a step-by-step explanation of how a program executes on a real or an abstract machine

It has links with other areas of computer science such as programming language design, type theory, compilers and interpreters, program verification and model checking

A semantic error might be required to be spotted by the programmer

An example of semantic error can be usage of an uninitialised variable, which might not be easily detected during parsing

Semantic Analysis

During Semantic analysis, the compiler adds semantic information to the parse tree and builds the symbol table

This phase performs semantic checks such as

Semantic analysis usually requires a complete parse tree, meaning that this phase logically follows the parsing phase, and logically precedes the code generation phase, though it is often possible to fold multiple phases into one pass over the code in a compiler implementation