Declarative Semantics Definition - Static Analysis and Error Checking
Post on 01-Dec-2014
2067 Views
Preview:
DESCRIPTION
Transcript
IN4303 2014/15 Compiler Construction
Declarative Semantics Definition static analysis and error checking
Guido Wachsmuth
Static Analysis and Error Checking 2
source code
Static Analysis and Error Checking 2
source code
parse
Static Analysis and Error Checking 2
source code
errors
parse
check
Static Analysis and Error Checking 2
source code
errors
parse generate
check
machine code
Static Analysis and Error Checking 3
source code
Static Analysis and Error Checking 3
source code
parse
Static Analysis and Error Checking 3
source code
parse
check
Static Analysis and Error Checking 3
source code
parse
check
Static Analysis and Error Checking 3
source code
parse generate
machine code
check
Static Analysis and Error Checking 4
static checking
name analysis name binding and scope
Static Analysis and Error Checking 4
static checking
editor services
name analysis name binding and scope
Static Analysis and Error Checking 4
static checking
editor services
transformation
name analysis name binding and scope
Static Analysis and Error Checking 4
static checking
editor services
transformation
refactoring
name analysis name binding and scope
Static Analysis and Error Checking 4
static checking
editor services
transformation
refactoring
code generation
name analysis name binding and scope
Static Analysis and Error Checking
Stratego
NaBL TS
SDF3
ESV editor
SPT tests
5
syntax definition
concrete syntax
abstract syntax
static semantics
name binding
type system
dynamic semantics
translation
interpretation
Static Analysis and Error Checking
Stratego
NaBL TS
SDF3
ESV editor
SPT tests
6
syntax definition
concrete syntax
abstract syntax
static semantics
name binding
type system
dynamic semantics
translation
interpretation
Static Analysis and Error Checking 7
formal semantics
type system
name binding
testing
name binding
type system
constraints
specification
name binding
type system
constraints
Static Analysis and Error Checking 8
formal semantics static semantics
Static Analysis and Error Checking 9
word problem χL: Σ*→ {0,1}
w → 1, if w∈L
w → 0, else
theoretical computer science decidability & complexity
Static Analysis and Error Checking 9
word problem χL: Σ*→ {0,1}
w → 1, if w∈L
w → 0, else
decidability
type-0: semi-decidable
type-1, type-2, type-3: decidable
theoretical computer science decidability & complexity
Static Analysis and Error Checking 9
word problem χL: Σ*→ {0,1}
w → 1, if w∈L
w → 0, else
decidability
type-0: semi-decidable
type-1, type-2, type-3: decidable
complexity
type-1: PSPACE-complete
type-2, type-3: P
theoretical computer science decidability & complexity
Static Analysis and Error Checking 9
word problem χL: Σ*→ {0,1}
w → 1, if w∈L
w → 0, else
decidability
type-0: semi-decidable
type-1, type-2, type-3: decidable
complexity
type-1: PSPACE-complete
type-2, type-3: P
theoretical computer science decidability & complexity
PSPACE⊇NP⊇P
Static Analysis and Error Checking 10
formal grammars
context-sensitive
context-free
regular
theoretical computer science decidability & complexity
Static Analysis and Error Checking 10
formal grammars
context-sensitive
context-free
regular
theoretical computer science decidability & complexity
Static Analysis and Error Checking 10
formal grammars
context-sensitive
context-free
regular
theoretical computer science decidability & complexity
Static Analysis and Error Checking 11
/* factorial function */!let ! var x := 0! function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1))!in ! for i := 1 to 3 do ( x := x + fact(i); printint(x); print(" ") )!end
Static Analysis and Error Checking 12
#include <stio.h>!/* factorial function */!int fac(int num) { if (num < 1) return 1; else return num * fac(num - 1);}!int main() { printf(“%d! = %d\n”, 10, fac(10)); return 0;}
Static Analysis and Error Checking 13
class Main {! public static void main(String[] args) { System.out.println(new Fac().fac(10)); }}!class Fac {! public int fac(int num) { int num_aux; if (num < 1) num_aux = 1; else num_aux = num * this.fac(num - 1); return num_aux; }}
Static Analysis and Error Checking 14
static semantics restricting context-free languages
context-sensitive language
context-free grammar
L(G) = {w∈Σ* | S ⇒G* w}
Static Analysis and Error Checking 14
context-free superset
static semantics restricting context-free languages
context-sensitive language
context-free grammar
L(G) = {w∈Σ* | S ⇒G* w}
Static Analysis and Error Checking 14
context-free superset
static semantics restricting context-free languages
context-sensitive language
context-free grammar
L(G) = {w∈Σ* | S ⇒G* w}
Static Analysis and Error Checking 14
context-free superset
static semantics restricting context-free languages
context-sensitive language
context-free grammar
L(G) = {w∈Σ* | S ⇒G* w}
static semantics
L = {w∈ L(G) | ⊢ w}
Static Analysis and Error Checking 14
context-free superset
static semantics restricting context-free languages
context-sensitive language
context-free grammar
L(G) = {w∈Σ* | S ⇒G* w}
static semantics
L = {w∈ L(G) | ⊢ w}
judgements
well-formed ⊢ w
well-typed E ⊢ e : t
Static Analysis and Error Checking 14
context-free superset
static semantics restricting context-free languages
context-sensitive language
context-free grammar
L(G) = {w∈Σ* | S ⇒G* w}
static semantics
L = {w∈ L(G) | ⊢ w}
judgements
well-formed ⊢ w
well-typed E ⊢ e : t
Static Analysis and Error Checking 15
formal semantics type systems
Static Analysis and Error Checking 16
Tiger type system
E ⊢ i : int
E ⊢ s : string
E ⊢ nil : ⊥
Static Analysis and Error Checking 17
Tiger type system
E ⊢ () : ∅
E ⊢ e1 : t1 E ⊢ e2 : t2
E ⊢ e1 ; e2 : t2
Static Analysis and Error Checking 18
Tiger type system
E ⊢ e1 : int E ⊢ e2 : int
E ⊢ e1 + e2 : int
E ⊢ e1 : int E ⊢ e2 : int
E ⊢ e1 < e2 : int
E ⊢ e1 : string E ⊢ e2 : string
E ⊢ e1 < e2 : int
E ⊢ e1 : array of t E ⊢ e2 : int
E ⊢ e1[e2] : t
Static Analysis and Error Checking 19
Tiger type system
E ⊢ e1 : t1 E ⊢ e2 : t2 t1 ≅ t2
E ⊢ e1 = e2 : int
t1 <: t2
t1 ≅ t2
t2 <: t1
t1 ≅ t2
t ≠ ∅
t ≅ t
⊥<: {f1, …, fn}
⊥<: array of t
Static Analysis and Error Checking 20
Tiger type system
E ⊢ e1 : t1 E ⊢ e2 : t2 t1 ≅ t2
E ⊢ e1 := e2 : ∅
E ⊢ e1 : int E ⊢ e2 : t1 E ⊢ e3 : t2
E ⊢ if e1 then e2 else e3: sup<: {t1, t2}
Static Analysis and Error Checking 21
formal semantics name binding
Static Analysis and Error Checking 22
Tiger scoping
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 22
Tiger scoping
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 22
Tiger scoping
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 22
Tiger scoping
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 22
Tiger scoping
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 23
Tiger variable names
E ⊢ e1 : t1 t ≅ t1 E ⊕ v ↦ t ⊢ e2 : t2
E ⊢ let var v : t = e1 in e2: t2
E(v) = t
E ⊢ v : t
Static Analysis and Error Checking 24
Tiger function names
E ⊕ v1 ↦ t1 ,…, vn ↦ tn ⊢ e1 : tf E ⊕ f ↦ t1 × … × tn → tf ⊢ e2 : t
E ⊢ let function f (v1 : t1, …, vn : tn) = e1 in e2: t
E(f) = t1 × … × tn → tf e1 : t1 … en : tn
E ⊢ f (e1, …, en) : t
Static Analysis and Error Checking 25
testing
Static Analysis and Error Checking 26
test outer name [[ let type t = u type [[u]] = int var x: [[u]] := 0 in x := 42 ; let type u = t var y: u := 0 in y := 42 endend]] resolve #2 to #1
test inner name [[ let type t = u type u = int var x: u := 0 in x := 42 ; let type [[u]] = t var y: [[u]] := 0 in y := 42 endend]] resolve #2 to #1
Testing name binding
Static Analysis and Error Checking 27
test integer constant [[ let type t = u type u = int var x: u := 0 in x := 42 ; let type u = t var y: u := 0 in y := [[42]] endend]] run get-type to IntTy()
test variable reference [[ let type t = u type u = int var x: u := 0 in x := 42 ; let type u = t var y: u := 0 in y := [[x]] endend]] run get-type to IntTy()
Testing type system
Static Analysis and Error Checking 28
test undefined variable [[ let type t = u type u = int var x: u := 0 in x := 42 ; let type u = t var y: u := 0 in y := [[z]] endend]] 1 error
test type error [[ let type t = u type u = string var x: u := 0 in x := 42 ; let type u = t var y: u := 0 in y := [[x]] endend]] 1 error
Testing constraints
Static Analysis and Error Checking 29
context-free superset
testing static semantics
language
Static Analysis and Error Checking 30
specification name binding
Static Analysis and Error Checking 31
defines !
refers!
namespaces!
scopes!
imports
Name Binding Language concepts
Static Analysis and Error Checking 32
Name Binding Language definitions and references
TypeDec(t, _): defines Type t Tid(t) : refers to Type t
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 33
Name Binding Language unique definitions
TypeDec(t, _): defines unique Type t Tid(t) : refers to Type t
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 34
Name Binding Language namespaces
let type mt = int type rt = {f1: string, f2: int} type at = array of int! var x := 42 var y: int := 42! function p() = print("foo") function sqr(x: int): int = x*xin … end
namespaces Type Variable Function!TypeDec(t, _): defines unique Type t!FunDec(f, _, _): defines unique Function fFunDec(f, _, _, _): defines unique Function fCall(f, _) : refers to Function f!VarDec(v, _): defines unique Variable vFArg(a, _): defines unique Variable aVar(v): refers to Variable v
Static Analysis and Error Checking 35
Name Binding Language scopes
FunDec(f, _, _): defines unique Function f scopes Variable!FunDec(f, _, _, _): defines unique Function f scopes Variable Let(_, _): scopes Type, Function, Variable
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 35
Name Binding Language scopes
FunDec(f, _, _): defines unique Function f scopes Variable!FunDec(f, _, _, _): defines unique Function f scopes Variable Let(_, _): scopes Type, Function, Variable
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 35
Name Binding Language scopes
FunDec(f, _, _): defines unique Function f scopes Variable!FunDec(f, _, _, _): defines unique Function f scopes Variable Let(_, _): scopes Type, Function, Variable
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 35
Name Binding Language scopes
FunDec(f, _, _): defines unique Function f scopes Variable!FunDec(f, _, _, _): defines unique Function f scopes Variable Let(_, _): scopes Type, Function, Variable
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 35
Name Binding Language scopes
FunDec(f, _, _): defines unique Function f scopes Variable!FunDec(f, _, _, _): defines unique Function f scopes Variable Let(_, _): scopes Type, Function, Variable
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
Static Analysis and Error Checking 36
Name Binding Language definition scopes
for x := 0 to 42 do x;For(v, start, end, body): defines Variable v in body
Static Analysis and Error Checking 37
Spoofax bound renaming
let type t = u type u = int var x: u := 0in x := 42 ; let type u = t var y: u := 0 in y := 42 endend
let type t0 = u0 type u0 = int var x: u0 := 0in x := 42 ; let type u1 = t0 var y: u1 := 0 in y := 42 endend
Static Analysis and Error Checking 38
Spoofax annotated terms
t{t1, ..., tn}!!!
add additional information to a term but preserve its signature
Static Analysis and Error Checking 39
specification type system
Static Analysis and Error Checking 40
TS axioms
type rules! Int(_) : IntTy() String(_): StringTy()!signatures! NilTy: Type type rules! Nil(): NilTy()
E ⊢ i : int
E ⊢ s : string
E ⊢ nil : ⊥
Static Analysis and Error Checking 41
TS inference rules
type rules! Add(e1,e2): IntTy() where e1: ty1 and ty1 == IntTy() and e2: ty2 and ty2 == IntTy()
E ⊢ e1 : int E ⊢ e2 : int
E ⊢ e1 + e2 : int
Static Analysis and Error Checking 42
TS inference rules
type rules! Lt(e1,e2): IntTy() where e1: ty1 and e2: ty2 and ( ( ty1 == IntTy() and ty2 == IntTy() ) or ( ty1 == StringTy() and ty2 == StringTy() ) )
E ⊢ e1 : int E ⊢ e2 : int
E ⊢ e1 < e2 : int
E ⊢ e1 : string E ⊢ e2 : string
E ⊢ e1 < e2 : int
Static Analysis and Error Checking 43
NaBL and TS interaction
type rules! Var(x): ty where definition of x: ty
binding rules! VarDec(x, ty): defines unique Variable x of type ty
Static Analysis and Error Checking 44
NaBL and TS interaction
FArg(a, t): defines unique Variable a of type t!FunDec(f, a*, e): defines unique Function f of type (t*, t) where a* has type t* and e has type t!Call(f, a*) : refers to Function f of type (t*, _) where a* has type t*
Static Analysis and Error Checking 45
specification constraints
Static Analysis and Error Checking 46
TS type errors
type rules! Add(e1,e2): IntTy() where e1: ty1 and ty1 == IntTy() else error "…" on e1 and e2: ty2 and ty2 == IntTy() else error "…" on e2
E ⊢ e1 : int E ⊢ e2 : int
E ⊢ e1 + e2 : int
Static Analysis and Error Checking 47
TS missing definitions
type rules! Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking48 48
Spoofax origin tracking
let var x := 21 in y * 2 end
Let([VarDec("x", Int("21"))], [Times(Var("y"), Int("2"))])
desugar: Times(e1, e2) -> Bop(MUL(), e1, e2)
Let([VarDec("x", Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Let([VarDec("x"{"…"}, Int("21"))], [Bop(MUL(), Var("y"), Int("2"))])
Var(x): ty where definition of x: ty else error "…" on x
Static Analysis and Error Checking 49
derivation of editor services
error checking
reference resolution
code completion
Spoofax static analysis
Static Analysis and Error Checking 49
derivation of editor services
error checking
reference resolution
code completion
multi-file analysis
Spoofax static analysis
Static Analysis and Error Checking 49
derivation of editor services
error checking
reference resolution
code completion
multi-file analysis
parallel analysis
Spoofax static analysis
Static Analysis and Error Checking 49
derivation of editor services
error checking
reference resolution
code completion
multi-file analysis
parallel analysis
incremental analysis
Spoofax static analysis
Static Analysis and Error Checking 50
Except where otherwise noted, this work is licensed under
Static Analysis and Error Checking 51
attribution
slide title author license
1 Inspection Kent Wien CC BY-NC 2.0
2, 3 PICOL icons Melih Bilgil CC BY 3.0
10 Noam Chomsky Maria Castelló Solbes CC BY-NC-SA 2.0
11, 16-20, 22-24 Tiger Bernard Landgraf CC BY-SA 3.0
12 The C Programming Language Bill Bradford CC BY 2.0
13 Italian Java book cover
top related