Jaideep M. More

software engineer


Semantic Analysis

Parse Trees and AST:

Parse trees contains terminals that are used to make the grammar non-ambiguous. Ex: semicolons are used to make the grammar unambiguous.

We also remove non-terminals for chain productions. A→B⋯→C→a⟹A→aA \rightarrow B \cdots \rightarrow C \rightarrow a \Longrightarrow A \rightarrow a

Semantic Analysis:

  1. Refute at compile time the programs that potentially get stuck
  2. Infer information about program helpful for compiler

Name Analysis:

Ensures each occurrence of an entity refers to a unique defining occurrence.

Every entity (variable, function, data-type) has a defining occurrence which defines the name, properties and scope (lifetime) / validity of that entity.

Validity:

The validity/scope of a definition of an identifier is the part of the program in which an applied occurrence of that identifier refers to it.

Qualification:

Some entities are composed of other entities. Their definition opens scope for other entities.

WE use qualification (.) to refer to the constituents.

Multiple NameSpaces:

Programming languages allow multiple namespaces where more than one definition of an identifier can be valid and visible at a time. Must be cleared from the context which occurrence is used.

Symbol Definition Table:

Typically, a stack of environments is used to handle visibility and detect valid occurrences for applied occurrences.

Type Inference:

Often types of variables are uniquely identified by the way the variable are used. We use this information to infer types and avoid declarations.

We define a relation:

Γ⊢e:t⊆(Id→type)×Expr×Type \Gamma \vdash e:t \subseteq (\text{Id} \rightarrow \text{type}) \times \text{Expr} \times \text{Type}

this indicates that under the environment Γ\Gamma which maps identifiers to types, an expressions Expr\text{Expr} maps to type Type\text{Type}

We then define the type rules over the Syntax that we can use inductively to infer the types of the expressions.

Ex:

Γ⊢c:tc\Gamma \vdash c : t_{c}

Γ⊢[]:t list\Gamma \vdash []: t \ \text{list}

Γ⊢x:(Γx)   lookup the type of x in Γ\Gamma \vdash x: (\Gamma x) \ \ \ \text{lookup the type of }x \text{ in } \Gamma

Given an environment Γ\Gamma, the rules can be used to check if the environment leads to correct typing

How do we find all valid types for an expression?

We build an equation system to describe all valid types for an expression.

We introduce type variable (tbt_{b}) for the types of expression with unknown types.

Let α[e]\alpha[e] be the type variable for expression ee.

A solution σ\sigma is a substitution/unifier (map of type variable to concrete types)

Correctness:

Let ee be an expression, VV be the set of variables in that expression and EE be the equation system for ee, then

  1. If σ\sigma is a solution of EE, then there is a derivation Γ⊢e:t\Gamma \vdash e:t with Γ={x↦σ(α[x])∣x∈V}\Gamma = \{ x\mapsto \sigma(\alpha[x]) | x \in V \}

Computing the most general unifier: (Using Algorithm WW):

The algorithm WW traverses the expression tree in a post-order fashion adding entries to the candidate unifier for the subexpressions that it visits.