Back to series
· 11 min read

Semantics


1. What Are Semantics?

Semantics is the set of rules in a programming language that describes the meaning of a program, its execution behavior, and its computed results.

Put simply:

  • Syntax: how the code should be written and whether its structure is legal.
  • Semantics: what the code means and what should happen when it executes.

For example:

x = 10
y = 20
z = x + y

This code conforms to Python's syntax rules.

From a semantic perspective:

  • x = 10: binds the integer value 10 to the variable name x.
  • y = 20: binds the integer value 20 to the variable name y.
  • x + y: performs an addition operation.
  • z = x + y: binds the result 30 to the variable name z.

Semantics describes not only the result of evaluating an expression, but also:

  • Data types and the rules for operating on them.
  • The scope and lifetime of variables.
  • Name binding and reference relationships.
  • Function call and return behavior.
  • Control flow and execution order.
  • Memory access and state changes.
  • Exception handling and program termination.
  • Concurrent execution and the memory model.

Semantics is the foundation for understanding how a program really works.

2. The Difference Between Syntax and Semantics

Syntax and semantics are two distinct but closely related concepts in programming languages.

1. Syntax focuses on code structure

For example:

if age >= 18:
    print("成年人")

This code conforms to Python's syntax rules.

If it were written as:

if age >= 18
    print("成年人")

The colon is missing, which is a syntax error.

A parser typically cannot build the corresponding complete program structure according to Python's syntax rules.

2. Semantics focuses on code meaning

The following code:

age = "18"
result = age + 10

is syntactically legal.

But Python does not allow a string and an integer to be added directly in this way, so a TypeError is raised at runtime.

This shows that:

Correct syntax does not guarantee correct semantics.

Consider another example:

price = 100
discount = 0.2

final_price = price * discount

The code is syntactically correct and the types match, but if the business requirement is to compute the discounted price, then the calculation logic here may be wrong.

The correct calculation should probably be:

final_price = price * (1 - discount)

This is a semantic problem at the level of business logic.

3. Syntax Errors, Semantic Errors, and Logic Errors

Error type Core issue Example
Syntax error Code structure does not conform to language rules Missing parentheses or colon
Static semantic error Code structure is legal but violates static semantic constraints Static type mismatch
Runtime semantic error Execution violates operating rules Division by zero, illegal type operation
Business logic error Program behavior does not match business expectations Incorrect discount calculation formula

Different languages check for errors at different times.

For example, Java checks a large number of type issues at compile time, whereas Python, as a dynamically typed language, can only discover some type issues when the corresponding code is executed.

4. The Relationship Between Syntax and Semantics

flowchart TB
    A["Source code"] --> B["Syntax analysis"]
    B --> C{"Is the syntax valid?"}
    C -->|"No"| D["Syntax error"]
    C -->|"Yes"| E["Semantic checking"]
    E --> F{"Are semantic constraints satisfied?"}
    F -->|"No"| G["Semantic error"]
    F -->|"Yes"| H["Execute the program"]
    H --> I["Produce runtime result"]

    style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style D fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Note that the diagram illustrates conceptual processing relationships. In actual language implementations, semantic checking may be distributed across compile time, load time, and runtime, and is not necessarily a strictly separate phase.

III. Three Main Approaches to Describing Programming Language Semantics

In programming language theory, there are three common approaches to describing semantics:

  1. Operational Semantics.
  2. Denotational Semantics.
  3. Axiomatic Semantics.

They describe the meaning of a program from different perspectives.

1. Operational Semantics

Operational semantics defines the meaning of a program by describing the state changes during program execution.

It focuses on:

What changes occur with each execution step of the program?

For example:

x = 10
x = x + 5

The program execution process can be abstracted as:

  • Initial state: variable x is not yet bound.
  • Execute the first statement: x is bound to 10.
  • Execute the second statement: read the value of x and compute 10 + 5.
  • Update the state: x is bound to 15.

The program state can be represented as:

\sigma = \{x \mapsto 10\}

Where:

  • \sigma represents the program state.
  • x represents a variable.
  • 10 represents the value currently associated with the variable.

After executing the second statement:

\sigma' = \{x \mapsto 15\}

The entire process can be represented as a state transition:

\sigma \rightarrow \sigma'

Operational semantics is commonly used to describe:

  • Expression evaluation.
  • Variable assignment.
  • Function calls.
  • Control flow execution.
  • Exception handling.
  • Concurrent operations.

Small-Step and Big-Step Semantics

Operational semantics can be further divided into two common description styles.

Small-Step Operational Semantics

It describes the step-by-step changes of a program from one computational state to the next.

For example:

1 + 2 \times 3

It can be reduced step by step:

1 + 2 \times 3 \rightarrow 1 + 6 \rightarrow 7

It focuses on each step of the computation process.

Big-Step Operational Semantics

It directly describes what final result an expression or program evaluates to.

For example:

1 + 2 \times 3 \Downarrow 7

The symbol \Downarrow indicates that the expression ultimately evaluates to 7.

The difference between the two is:

Comparison aspect Small-step semantics Big-step semantics
Description style Execute step by step Directly describe the final result
Focus Intermediate states Final evaluation result
Suitable for analyzing Execution details, state changes Overall computation result
Characteristics More detailed More concise

2. Denotational Semantics

Denotational semantics assigns mathematical meaning to program constructs through mathematical objects and mathematical functions.

It focuses on:

What does a program construct represent mathematically?

For example:

def square(x):
    return x * x

The square function can be represented as a mathematical function:

\llbracket square \rrbracket = f

where:

f(x) = x^2

The double brackets here:

\llbracket \cdot \rrbracket

are typically used to denote the semantic interpretation of a program construct.

For example:

\llbracket 1 + 2 \rrbracket = 3

means that the mathematical meaning corresponding to the expression 1 + 2 is 3.

For variable assignment, program semantics can be understood as a mapping between states:

\llbracket x = x + 1 \rrbracket : \Sigma \rightarrow \Sigma

where \Sigma denotes the set of program states.

This means that an assignment statement can be viewed as a state transition function.

Denotational semantics is often used in:

  • Programming language theory research.
  • Mathematical modeling of language features.
  • Functional language research.
  • Formal description of program behavior.
  • Language design and correctness analysis.

3. Axiomatic Semantics

Axiomatic semantics describes the conditional relationships before and after program execution through logical rules.

It focuses on:

What conditions must be satisfied before program execution, and what results can be guaranteed after execution?

The most classic method is Hoare logic.

Hoare logic uses triples to express:

\{P\}\ C\ \{Q\}

where:

  • P: precondition.
  • C: command.
  • Q: postcondition.

The meaning is:

If condition P is satisfied before program execution and program C terminates normally, then condition Q should be satisfied after execution.

For example:

{x = 10}
x = x + 5
{x = 15}

This can be expressed as:

\{x = 10\}\ x := x + 5\ \{x = 15\}

This describes the logical relationship before and after program execution, rather than specific execution steps.

Axiomatic semantics is often used in:

  • Program correctness proofs.
  • Algorithm verification.
  • Safety-critical systems.
  • Formal verification.
  • Program logic research.

4. Differences Among the Three Semantic Description Approaches

Comparison aspect Operational semantics Denotational semantics Axiomatic semantics
English Operational Denotational Axiomatic
Core idea Describe the execution process Establish mathematical mappings Describe logical conditions
Focus How states change What the program represents Whether the program satisfies conditions
Main tools State transition rules Mathematical functions and domains Logical inference rules
Common applications Language execution model Formal language definition Program correctness proof

5. Overall Perspective on the Three Semantics

flowchart TB
    A["Program Semantics"] --> B["Operational Semantics"]
    A --> C["Denotational Semantics"]
    A --> D["Axiomatic Semantics"]
    B --> E["Execution Steps"]
    B --> F["State Transitions"]
    C --> G["Mathematical Mapping"]
    C --> H["Functions and Domains"]
    D --> I["Preconditions"]
    D --> J["Postconditions"]
    E --> K["Describe Program Behavior"]
    F --> K
    G --> K
    H --> K
    I --> K
    J --> K

    style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style I fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style J fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style K fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

These three approaches are not mutually exclusive. They can describe the same language or program from three perspectives: execution, mathematical abstraction, and logical proof.

IV. Static Semantics and Dynamic Semantics

In addition to operational semantics, denotational semantics, and axiomatic semantics, programming language design also often distinguishes static semantics from dynamic semantics based on when checks occur and the phase of behavior.

1. Static Semantics

Static semantics describes the constraints that a program must satisfy before execution.

It is usually checked by compilers or static analysis tools.

Common checks include:

  • Whether types are valid.
  • Whether variables have been declared.
  • Whether identifiers are in a valid scope.
  • Whether function parameters and return types match.
  • Whether access permissions comply with the rules.
  • Whether control structures satisfy language constraints.

For example, in Java:

int age = 18;

String name = age;

The code is syntactically valid, but a value of type int cannot be assigned directly to a variable of type String.

The Java compiler will detect this type mismatch during compilation.

This is a violation of a static semantic constraint.

2. Dynamic Semantics

Dynamic semantics describes the behavior of a program at runtime.

It focuses on:

  • How expressions are evaluated.
  • How functions are called.
  • How variables are updated.
  • How objects are created.
  • How memory is allocated and freed.
  • How exceptions propagate.
  • How concurrent tasks execute.

For example:

numbers = [1, 2, 3]
print(numbers[10])

The code passes Python's syntax analysis and may also pass the corresponding static syntax checks.

However, when executed, accessing a nonexistent list index raises an IndexError.

This is a runtime behavior issue.

3. Differences Between Static Semantics and Dynamic Semantics

Comparison Dimension Static Semantics Dynamic Semantics
English Static Semantics Dynamic Semantics
Stage of Focus Before program execution During program execution
Main Content Types, scopes, declaration constraints Evaluation, state changes, control flow
Common Implementation Compiler checks, static analysis Interpreters, runtime systems
Typical Issues Type mismatch, undeclared variables Out-of-bounds access, division-by-zero exceptions

Note that static semantics and dynamic semantics are conceptual distinctions based on program constraints and execution behavior; they are not completely equivalent to compile time and runtime.

Some languages perform type checking at runtime, and some static analysis tools can detect some potential runtime errors without executing the program.

V. Type Systems and Semantics

A type system is an important component of programming language semantics.

It specifies which operations can be performed on data of different types, and how types can be converted.

1. Types Determine Whether Operations Are Valid

For example:

a = 10
b = 20

result = a + b

Here, a and b are both integers, so addition can be performed.

But:

a = "Hello" b = 10 result = a + b ```

In Python, this produces a type error.

Because strings and integers do not support this kind of direct addition operation.

A type system can help a language define:

  • The categorization of data.
  • The set of legal operations.
  • Type compatibility rules.
  • Type conversion rules.
  • Constraints on function parameters and return values.
  • Relationships between generics and composite types.

2. Static Typing vs. Dynamic Typing

Statically typed languages

Type constraints on expressions and variables are usually determined during compilation or during an analysis phase before execution.

For example, Java:

```java int number = 10; String name = "Alice"; ```

Dynamically typed languages

Type checking is usually tied to runtime values, and a variable name does not necessarily have a fixed static type.

For example, Python:

```python value = 10 value = "Hello" ```

The same variable name can be bound to objects of different types at different times.

3. Type Checking and Program Semantics

```mermaid flowchart TB A["Expression"] --> B["Determine operand types"] B --> C["Check operation rules"] C --> D{"Are the types compatible"} D -->|"Yes"| E["Execute the corresponding operation"] D -->|"No"| F["Report a type error"] E --> G["Obtain the result"] style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155 style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976 style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155 style D fill:#FFF5DE,stroke:#E8D29A,color:#334155 style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155 style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155 style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155 linkStyle default stroke:#A3B8AC,stroke-width:1.5px ```

The type system not only affects error checking, but also affects how a program is executed, memory layout, operation rules, and opportunities for optimization.

VI. Scope, Binding, and Lifetime

The semantics of a variable include not only the value it currently holds, but also how a name is associated with data, and where and when the name is valid.

1. Scope

Scope is the region of a program in which a name can be accessed.

Common scopes include:

  • Global scope.
  • Local scope.
  • Function scope.
  • Block scope.
  • Class scope.

For example:

```python name = "Global" def show(): name = "Local" print(name) show() print(name) ```

Output:

```text Local Global ```

The name inside the function and the name in the global scope are different local bindings.

Scope determines where in the program a name is visible.

2. Lexical Scope vs. Dynamic Scope

This is a very important distinction for understanding the semantics of programming languages.

Lexical Scope

Also called static scope. The rules for looking up a name are determined by the lexical structure of the code.

For example:

```javascript const name = "Global"; function outer() { const name = "Outer"; function inner() { console.log(name); } return inner; } const fn = outer(); fn(); ```

Output:

```text Outer ```

inner() is defined inside outer(), so it can access the name in the outer scope at its definition site.

Even after outer() has finished executing, the returned inner() can still access the relevant lexical environment.

This is an important foundation of closures.

Dynamic Scope

Dynamic scope determines where a name is looked up based on the call chain at the time a function is called.

In other words, variable lookup depends on the current call path, rather than being determined solely by the function's definition site.

Dynamic scope is relatively rare in modern mainstream general-purpose programming languages, but similar ideas can be seen in some language features, macro systems, and dynamic binding mechanisms.

3. Name Binding

Binding is the process of associating a name with some entity.

For example:

```python age = 18

Here, the variable name age is bound to the integer object 18.

In different languages, the objects and timing of binding may differ:

  • Variable names are bound to values.
  • Function names are bound to function implementations.
  • Type names are bound to type definitions.
  • Method calls are bound to concrete implementations.
  • Symbol references are bound to modules or link targets.

Name binding can occur at compile time, link time, or runtime.

4. Lifetime

Lifetime is the period during program execution in which an object or data remains valid.

For example:

def create_user():
    user = {"name": "Alice"}
    return user

The scope of the local name user is only inside the function.

But the returned dictionary object can still be used by the function's caller.

This shows that:

Variable scope and object lifetime are not the same concept.

In Python, objects are usually managed by reference counting and garbage collection; in Rust, lifetimes and ownership rules participate in compile-time safety checks; in C and C++, concepts such as automatic storage duration, static storage duration, and dynamic storage duration also need to be considered.

5. Differences Between Scope, Binding, and Lifetime

Concept Main question it answers
Scope Where can a name be accessed?
Binding Which object or entity is a name associated with?
Lifetime When is an object or data created, and when does it become invalid?

These three concepts are interrelated, but they cannot be conflated.

VII. Expression Evaluation and Execution Order

Expressions in a program not only have syntactic structure but also have explicit evaluation rules.

1. Expression Evaluation

For example:

result = 1 + 2 * 3

Semantic rules specify:

  • Multiplication is calculated first.
  • Addition is calculated next.
  • The final result is 7.

The evaluation of an expression is usually determined jointly by operator precedence, associativity, operand types, and the evaluation rules defined by the language.

2. Short-Circuit Evaluation

Short-circuit evaluation is an evaluation strategy for logical expressions.

When the left side of an expression is already sufficient to determine the overall logical result, the right-side expression may not be executed.

For example, in Python:

x = 0

result = x != 0 and 10 / x > 1

print(result)

Output:

False

Since x != 0 is False, the and expression can already determine the result, so the division on the right side is not executed.

This avoids the problem of division by zero.

For logical OR:

result = True or some_function()

When the left side is True, the right side is usually not evaluated.

Short-circuit evaluation is part of language semantics, not just a compiler optimization.

3. Evaluation Order and Side Effects

The following code:

let count = 0;

function increment() {
    count++;
    return count;
}

const result = increment() + increment();

Both function calls modify external state.

Therefore, the evaluation order of expressions may affect the final result.

Different languages have different rules for the evaluation order of operands:

  • Java specifies left-to-right evaluation order for the operands of most operators.
  • Python generally evaluates subexpressions in an expression from left to right.
  • In C and C++, the evaluation order of operands for some operators is not guaranteed, and some expressions may also involve undefined behavior.

You cannot infer, merely from the order in which the code is written, that all languages will execute all subexpressions in the same order.

4. The Process of Expression Evaluation

flowchart TB
    A["Expression"] --> B["Identify operators and operands"]
    B --> C["Determine evaluation rules"]
    C --> D["Execute subexpression evaluation"]
    D --> E["Apply operation rules"]
    E --> F["Produce expression result"]
    F --> G["Produce state changes when necessary"]

    style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

VIII. Function Calls and Return Semantics

Function calls are a very important semantic behavior during program execution.

They not only mean executing another piece of code, but also involve parameter passing, establishing the execution environment, handling return values, and transferring control flow.

1. The Basic Process of a Function Call

For example:

def add(a, b):
    result = a + b
    return result


value = add(10, 20)

From a semantic perspective:

  1. Evaluate the call arguments.
  2. Establish the correspondence between the actual arguments and the formal parameters.
  3. Establish the local environment required for function execution.
  4. Execute the function body.
  5. Execute the return.
  6. Hand the return value back to the caller.
  7. Continue executing the program after the call site.