Schwannden 2020/8/19
Names, Bindings,
Scopes
A gentle introduction to the formal language
Formal Language
Formal Language
How to formally define language
Formal Language
How to formally define language
Formal Language
How to formally define grammar
Formal Language
How does grammar generate language
• Each language has different grammar that strictly defines its
syntax. This is one of the reason why complicated language can
still be designed with a correct syntax analyses.

• Human language usually can not be defined as the simple kind of
grammar mentioned (CFG), so human bran is actually amazing.

• Different type of grammar have different power to define
language with different capability (will not be discussed).

• Language design usually does not start from grammar design,
grammar design comes after.

• Knowing the grammar, with the concept of expression and value
help us learn and formalize one language really fast (TBD in the
future)
Formal Language
Implication
Binding
Binding
Definition
Binding
Definition
Binding
Definition
Binding
Type of Variable
Binding
Type of Variable
Binding
Type of Variable
Binding
Type of Variable
Binding
Type of Variable
Binding
Case Study - Javascript
Regardless of the programming language, the memory life cycle is pretty much always the same:
1. Allocate the memory you need
2. Use the allocated memory (read, write)
3. Release the allocated memory when it is not needed anymore
The second part is explicit in all languages. The first and last parts are explicit in low-level languages but are mostly implicit in high-
level languages
Binding
Case Study - Javascript
Binding
Case Study - Javascript
Binding
Case Study - Javascript GC (Reference Counting)
Binding
Case Study - Javascript GC (Reference Counting)
Binding
Case Study - Javascript GC (Mark-and-Sweep Algorithm)
Scope
Scope
Definition
Scope
Case Study - Javascript Scope
Scope
Case Study - Javascript Scope
To be clear, JavaScript does not, in fact, have dynamic scope. It has
lexical scope. Plain and simple. But the this mechanism is kind of like
dynamic scope.
Scope
Case Study - Javascript Scope
Scope
Case Study - Javascript Scope
Scope
Dynamic Scoping
Scope
Static Scoping
Scope
Static Scoping
Scope
Static Scoping
Scope
Static Scoping
Scope
Namespaces
Scope
Namespaces
Javascript
function f(x) {return x;}

var f = f // legal, because function and var are in different name space.

var g = g // undefined, binding does not happen after assignment

function fac(n) { return n <= 1 ? 1 : n * fac(n-1) } // legal, function name is visible to its body
Scala
def f(n: Int): Int = n

val f = f // illegal, because function and var are in the same name space.

val g = g // illegal, binding does not happen after assignment

PL Lecture 02 - Binding and Scope