SlideShare a Scribd company logo
1 of 61
Download to read offline
IN4303 2016-2017
Compiler Construction
Introduction to Static Semantics
static analysis and error checking
Guido Wachsmuth, Eelco Visser
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
DynSem
NaBL2
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
DynSem
NaBL2
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! = %dn”, 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 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
t1 ≅ t2
E ⊢ if e1 then e2 else e3: 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 := 0
in
x := 42 ;
let
type u = t
var y: u := 0
in
y := 42
end
end
Static Analysis and Error Checking 22
Tiger
scoping
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
end
end
Static Analysis and Error Checking 22
Tiger
scoping
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
end
end
Static Analysis and Error Checking 22
Tiger
scoping
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
end
end
Static Analysis and Error Checking 22
Tiger
scoping
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
end
end
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
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
end
end
]] 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
end
end
]] 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]]
end
end
]] 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]]
end
end
]] 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]]
end
end
]] 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]]
end
end
]] 1 error
Testing
constraints
Static Analysis and Error Checking 29
context-free superset
Testing
static semantics
language
Static Analysis and Error Checking 30
Next
Imperative and Object-Oriented Languages
Next
Week 5
• lexical analysis: from regular grammar to DFA
31
Imperative and Object-Oriented Languages
Next
Week 5
• lexical analysis: from regular grammar to DFA
Week 6
• name binding rules
• name resolution with scope graphs
31
Imperative and Object-Oriented Languages
Next
Week 5
• lexical analysis: from regular grammar to DFA
Week 6
• name binding rules
• name resolution with scope graphs
Week 8
• constraint-based type analysis
31
Imperative and Object-Oriented Languages
Next
Week 5
• lexical analysis: from regular grammar to DFA
Week 6
• name binding rules
• name resolution with scope graphs
Week 8
• constraint-based type analysis
Q2
• dynamic semantics, code generation, optimization
• parsing algorithms
31
Static Analysis and Error Checking 32
Spoofax
annotated terms
t{t1, ..., tn}
add additional information to a term but preserve its signature
Static Analysis and Error Checking 33
Except where otherwise noted, this work is licensed under
Static Analysis and Error Checking 34
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

More Related Content

What's hot

Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
Eelco Visser
 

What's hot (20)

Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
 
LL Parsing
LL ParsingLL Parsing
LL Parsing
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
 
Declare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesDeclare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) Services
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Declare Your Language: Type Checking
Declare Your Language: Type CheckingDeclare Your Language: Type Checking
Declare Your Language: Type Checking
 
Type analysis
Type analysisType analysis
Type analysis
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
Ch04
Ch04Ch04
Ch04
 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
 
5 top-down-parsers
5  top-down-parsers 5  top-down-parsers
5 top-down-parsers
 
Regular expression examples
Regular expression examplesRegular expression examples
Regular expression examples
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Ch03
Ch03Ch03
Ch03
 
"That scripting language called Prolog"
"That scripting language called Prolog""That scripting language called Prolog"
"That scripting language called Prolog"
 

Viewers also liked (9)

Programming languages
Programming languagesProgramming languages
Programming languages
 
Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7
 
Ti1220 Lecture 1: Programming Linguistics
Ti1220 Lecture 1: Programming LinguisticsTi1220 Lecture 1: Programming Linguistics
Ti1220 Lecture 1: Programming Linguistics
 
Planetary geodesy
Planetary geodesyPlanetary geodesy
Planetary geodesy
 
LR Parsing
LR ParsingLR Parsing
LR Parsing
 
LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation ppt
 
Tree diagram
Tree diagramTree diagram
Tree diagram
 
Introduction to aerial photography and photogrammetry.ppt
Introduction to aerial photography and photogrammetry.pptIntroduction to aerial photography and photogrammetry.ppt
Introduction to aerial photography and photogrammetry.ppt
 
Syntax
SyntaxSyntax
Syntax
 

Similar to Static Analysis

Towards advanced data retrieval from learning objects repositories
Towards advanced data retrieval from learning objects repositoriesTowards advanced data retrieval from learning objects repositories
Towards advanced data retrieval from learning objects repositories
Valentina Paunovic
 
Container and algorithms(C++)
Container and algorithms(C++)Container and algorithms(C++)
Container and algorithms(C++)
JerryHe
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
Software Guru
 

Similar to Static Analysis (20)

Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
 
Theory of computation:Finite Automata, Regualr Expression, Pumping Lemma
Theory of computation:Finite Automata, Regualr Expression, Pumping LemmaTheory of computation:Finite Automata, Regualr Expression, Pumping Lemma
Theory of computation:Finite Automata, Regualr Expression, Pumping Lemma
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode Compiler
 
Ch6.ppt
Ch6.pptCh6.ppt
Ch6.ppt
 
Ch3
Ch3Ch3
Ch3
 
Ch2 (1).ppt
Ch2 (1).pptCh2 (1).ppt
Ch2 (1).ppt
 
defense
defensedefense
defense
 
Towards advanced data retrieval from learning objects repositories
Towards advanced data retrieval from learning objects repositoriesTowards advanced data retrieval from learning objects repositories
Towards advanced data retrieval from learning objects repositories
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Ch6
Ch6Ch6
Ch6
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...
 
Container and algorithms(C++)
Container and algorithms(C++)Container and algorithms(C++)
Container and algorithms(C++)
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
 
C program compiler presentation
C program compiler presentationC program compiler presentation
C program compiler presentation
 

More from Eelco Visser

Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
Eelco Visser
 

More from Eelco Visser (20)

CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
 
A Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesA Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation Rules
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionCompiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler Construction
 
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone Frameworks
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static Analysis
 
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing
 
Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor Services
 

Recently uploaded

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Static Analysis

  • 1. IN4303 2016-2017 Compiler Construction Introduction to Static Semantics static analysis and error checking Guido Wachsmuth, Eelco Visser
  • 2. Static Analysis and Error Checking 2 source code
  • 3. Static Analysis and Error Checking 2 source code parse
  • 4. Static Analysis and Error Checking 2 source code errors parse check
  • 5. Static Analysis and Error Checking 2 source code errors parse generate check machine code
  • 6. Static Analysis and Error Checking 3 source code
  • 7. Static Analysis and Error Checking 3 source code parse
  • 8. Static Analysis and Error Checking 3 source code parse check
  • 9. Static Analysis and Error Checking 3 source code parse check
  • 10. Static Analysis and Error Checking 3 source code parse generate machine code check
  • 11. Static Analysis and Error Checking 4 static checking Name Analysis name binding and scope
  • 12. Static Analysis and Error Checking 4 static checking editor services Name Analysis name binding and scope
  • 13. Static Analysis and Error Checking 4 static checking editor services transformation Name Analysis name binding and scope
  • 14. Static Analysis and Error Checking 4 static checking editor services transformation refactoring Name Analysis name binding and scope
  • 15. Static Analysis and Error Checking 4 static checking editor services transformation refactoring code generation Name Analysis name binding and scope
  • 16. Static Analysis and Error Checking Stratego DynSem NaBL2 SDF3 ESV editor SPT tests 5 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
  • 17. Static Analysis and Error Checking Stratego DynSem NaBL2 SDF3 ESV editor SPT tests 6 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
  • 18. Static Analysis and Error Checking 7 formal semantics type system name binding testing name binding type system constraints specification name binding type system constraints
  • 19. Static Analysis and Error Checking 8 Formal Semantics static semantics
  • 20. Static Analysis and Error Checking 9 word problem χL: Σ* → {0,1} w → 1, if w∈L w → 0, else Theoretical Computer Science decidability & complexity
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. Static Analysis and Error Checking 10 formal grammars context-sensitive context-free regular theoretical computer science decidability & complexity
  • 25. Static Analysis and Error Checking 10 formal grammars context-sensitive context-free regular theoretical computer science decidability & complexity
  • 26. Static Analysis and Error Checking 10 formal grammars context-sensitive context-free regular theoretical computer science decidability & complexity
  • 27. 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
  • 28. 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! = %dn”, 10, fac(10)); return 0; }
  • 29. 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; } }
  • 30. Static Analysis and Error Checking 14 static semantics restricting context-free languages context-sensitive language context-free grammar L(G) = {w∈Σ* | S G * w}
  • 31. 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}
  • 32. 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}
  • 33. 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}
  • 34. 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
  • 35. Static Analysis and Error Checking 15 Formal Semantics type systems
  • 36. Static Analysis and Error Checking 16 Tiger type system E ⊢ i : int E ⊢ s : string E ⊢ nil : ⊥
  • 37. Static Analysis and Error Checking 17 Tiger type system E ⊢ () : ∅ E ⊢ e1 : t1 E ⊢ e2 : t2 E ⊢ e1 ; e2 : t2
  • 38. 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
  • 39. 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
  • 40. 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 t1 ≅ t2 E ⊢ if e1 then e2 else e3: t2
  • 41. Static Analysis and Error Checking 21 Formal Semantics name binding
  • 42. Static Analysis and Error Checking 22 Tiger scoping 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 end end
  • 43. Static Analysis and Error Checking 22 Tiger scoping 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 end end
  • 44. Static Analysis and Error Checking 22 Tiger scoping 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 end end
  • 45. Static Analysis and Error Checking 22 Tiger scoping 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 end end
  • 46. Static Analysis and Error Checking 22 Tiger scoping 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 end end
  • 47. 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
  • 48. 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
  • 49. Static Analysis and Error Checking 25 Testing Static Analysis
  • 50. 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 end end ]] 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 end end ]] resolve #2 to #1 Testing name binding
  • 51. 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]] end end ]] 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]] end end ]] run get-type to IntTy() Testing type system
  • 52. 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]] end end ]] 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]] end end ]] 1 error Testing constraints
  • 53. Static Analysis and Error Checking 29 context-free superset Testing static semantics language
  • 54. Static Analysis and Error Checking 30 Next
  • 55. Imperative and Object-Oriented Languages Next Week 5 • lexical analysis: from regular grammar to DFA 31
  • 56. Imperative and Object-Oriented Languages Next Week 5 • lexical analysis: from regular grammar to DFA Week 6 • name binding rules • name resolution with scope graphs 31
  • 57. Imperative and Object-Oriented Languages Next Week 5 • lexical analysis: from regular grammar to DFA Week 6 • name binding rules • name resolution with scope graphs Week 8 • constraint-based type analysis 31
  • 58. Imperative and Object-Oriented Languages Next Week 5 • lexical analysis: from regular grammar to DFA Week 6 • name binding rules • name resolution with scope graphs Week 8 • constraint-based type analysis Q2 • dynamic semantics, code generation, optimization • parsing algorithms 31
  • 59. Static Analysis and Error Checking 32 Spoofax annotated terms t{t1, ..., tn} add additional information to a term but preserve its signature
  • 60. Static Analysis and Error Checking 33 Except where otherwise noted, this work is licensed under
  • 61. Static Analysis and Error Checking 34 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