SlideShare a Scribd company logo
IN4303 2016-2017
Compiler Construction
Programming Languages
imperative & object-oriented
Guido Wachsmuth, Eelco Visser
Imperative and Object-Oriented Languages 2
Imperative and Object-Oriented Languages 3
interpreter
software
language
compiler
software
language
machine
language
Imperative and Object-Oriented Languages
natural
language
natural
language
translator
4
compiler
software
language
machine
language
Imperative and Object-Oriented Languages 5
Traditional compilers
architecture
source
code
errors
parse generate
check
machine
code
Imperative and Object-Oriented Languages 6
Modern compilers in IDEs
architecture
source
code
parse generate
machine
code
check
Imperative and Object-Oriented Languages 7
Compilers are inverted abstractions
architecture
source
code
machine
code
abstract
Imperative and Object-Oriented Languages
Compilers Invert Abstractions
Computers speak machine language
‱ basic instructions to move around and combine small things
Humans do not speak machine language very well
‱ “mov; loadi; jump” anyone?
Programming languages abstract from machine language
‱ capture common programming patterns in language constructs
‱ abstract from uninteresting variation in machines
Compilers invert the abstractions of programming languages
8
Imperative and Object-Oriented Languages 9
Portability: Platform Independence
architecture
source
code
parse
generate
GPU
code
check
generate
x86
code
Imperative and Object-Oriented Languages
Understanding Compilers Requires 

Understanding machine languages
‱ machine architecture, instruction sets
Understanding programming language (abstraction)s
‱ memory, control-flow, procedures, modules, 

‱ safety mechanisms: type systems & other static analyses
Understanding how to define mapping from PL to ML
‱ semantics of such mappings
‱ techniques to implement such mappings
10
Imperative and Object-Oriented Languages 11
Imperative Languages
state & statements
procedures
types
Object-Oriented Languages
objects & messages
types
Imperative and Object-Oriented Languages 12
Imperative Languages
state & statements
Imperative and Object-Oriented Languages 13
machine code
basic concepts
Imperative and Object-Oriented Languages
State
Machine state
‱ a pile of data stored in memory
‱ memory hierarchy: registers, RAM, disk, network, 

Imperative program
‱ computation is series of changes to memory
‱ basic operations on memory (increment register)
‱ controlling such operations (jump, return address, 
)
‱ control represented by state (program counter, stack, 
)
14
registers
Imperative and Object-Oriented Languages
x86 family
general purpose registers
‱ accumulator AX - arithmetic operations
‱ counter CX - shift/rotate instructions, loops
‱ data DX - arithmetic operations, I/O
‱ base BX - pointer to data
‱ stack pointer SP, base pointer BP - top and base of stack
‱ source SI, destination DI - stream operations
15
registers
Imperative and Object-Oriented Languages
x86 family
general purpose registers
‱ accumulator AX - arithmetic operations
‱ counter CX - shift/rotate instructions, loops
‱ data DX - arithmetic operations, I/O
‱ base BX - pointer to data
‱ stack pointer SP, base pointer BP - top and base of stack
‱ source SI, destination DI - stream operations
special purpose registers
‱ segments SS, CS, DS, ES, FS, GS
‱ flags EFLAGS
15
mov AX [1]
mov CX AX
L: dec CX
mul CX
cmp CX 1
ja L
mov [2] AX
basic concepts
Imperative and Object-Oriented Languages
Example: x86 Assembler
16
read memorymov AX [1]
mov CX AX
L: dec CX
mul CX
cmp CX 1
ja L
mov [2] AX
basic concepts
Imperative and Object-Oriented Languages
Example: x86 Assembler
16
read memorymov AX [1]
mov CX AX
L: dec CX
mul CX
cmp CX 1
ja L
mov [2] AX
basic concepts
Imperative and Object-Oriented Languages
Example: x86 Assembler
16
write memory
read memorymov AX [1]
mov CX AX
L: dec CX
mul CX
cmp CX 1
ja L
mov [2] AX
basic concepts
Imperative and Object-Oriented Languages
Example: x86 Assembler
16
write memory
calculation
read memorymov AX [1]
mov CX AX
L: dec CX
mul CX
cmp CX 1
ja L
mov [2] AX
basic concepts
Imperative and Object-Oriented Languages
Example: x86 Assembler
16
write memory
calculation
jump
.method static public m(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Math/m(I)I
imul
ireturn
basic concepts
Imperative and Object-Oriented Languages
Example: Java Bytecode
17
.method static public m(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Math/m(I)I
imul
ireturn
basic concepts
Imperative and Object-Oriented Languages
Example: Java Bytecode
17
read memory
.method static public m(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Math/m(I)I
imul
ireturn
basic concepts
Imperative and Object-Oriented Languages
Example: Java Bytecode
17
read memory
calculation
.method static public m(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Math/m(I)I
imul
ireturn
basic concepts
Imperative and Object-Oriented Languages
Example: Java Bytecode
17
read memory
calculation
jump
basic concepts
Imperative and Object-Oriented Languages
Memory & Control Abstractions
Memory abstractions
‱ variables: abstract over data storage
‱ expressions: combine data into new data
‱ assignment: abstract over storage operations
Control-flow abstractions
‱ structured control-flow: abstract over unstructured jumps
‱ ‘go to statement considered harmful’ Edgser Dijkstra, 1968
18
states & statements
Imperative and Object-Oriented Languages
Example: C
int f = 1
int x = 5
int s = f + x
while (x > 1) {
f = x * f ;
x = x - 1
}
19
states & statements
Imperative and Object-Oriented Languages
Example: C
int f = 1
int x = 5
int s = f + x
while (x > 1) {
f = x * f ;
x = x - 1
}
19
variable
states & statements
Imperative and Object-Oriented Languages
Example: C
int f = 1
int x = 5
int s = f + x
while (x > 1) {
f = x * f ;
x = x - 1
}
19
variable
expression
states & statements
Imperative and Object-Oriented Languages
Example: C
int f = 1
int x = 5
int s = f + x
while (x > 1) {
f = x * f ;
x = x - 1
}
19
variable
expression
assignment
states & statements
Imperative and Object-Oriented Languages
Example: C
int f = 1
int x = 5
int s = f + x
while (x > 1) {
f = x * f ;
x = x - 1
}
19
variable
expression
assignment
control flow
states & statements
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
var f := 1
var x := 5
var s := f + x
in
while x > 1 do (
f := x * f ;
x := x - 1
)
end
20
states & statements
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
var f := 1
var x := 5
var s := f + x
in
while x > 1 do (
f := x * f ;
x := x - 1
)
end
20
variable
states & statements
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
var f := 1
var x := 5
var s := f + x
in
while x > 1 do (
f := x * f ;
x := x - 1
)
end
20
variable
expression
states & statements
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
var f := 1
var x := 5
var s := f + x
in
while x > 1 do (
f := x * f ;
x := x - 1
)
end
20
variable
expression
assignment
states & statements
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
var f := 1
var x := 5
var s := f + x
in
while x > 1 do (
f := x * f ;
x := x - 1
)
end
20
variable
expression
assignment
control flow
Imperative and Object-Oriented Languages 21
Imperative Languages
procedures
push 21
push 42
call _f
add SP 8
push BP
mov BP SP
mov AX [BP + 8]
mov DX [BP + 12]
add AX DX
pop BP
ret
Imperative and Object-Oriented Languages
Example: x86 Assembler
22
modularity
push 21
push 42
call _f
add SP 8
push BP
mov BP SP
mov AX [BP + 8]
mov DX [BP + 12]
add AX DX
pop BP
ret
pass parameter
Imperative and Object-Oriented Languages
Example: x86 Assembler
22
modularity
push 21
push 42
call _f
add SP 8
push BP
mov BP SP
mov AX [BP + 8]
mov DX [BP + 12]
add AX DX
pop BP
ret
pass parameter
Imperative and Object-Oriented Languages
Example: x86 Assembler
22
new stack frame
modularity
push 21
push 42
call _f
add SP 8
push BP
mov BP SP
mov AX [BP + 8]
mov DX [BP + 12]
add AX DX
pop BP
ret
pass parameter
Imperative and Object-Oriented Languages
Example: x86 Assembler
22
access parameter
new stack frame
modularity
push 21
push 42
call _f
add SP 8
push BP
mov BP SP
mov AX [BP + 8]
mov DX [BP + 12]
add AX DX
pop BP
ret
pass parameter
Imperative and Object-Oriented Languages
Example: x86 Assembler
22
access parameter
new stack frame
modularity
old stack frame
push 21
push 42
call _f
add SP 8
push BP
mov BP SP
mov AX [BP + 8]
mov DX [BP + 12]
add AX DX
pop BP
ret
pass parameter
Imperative and Object-Oriented Languages
Example: x86 Assembler
22
access parameter
new stack frame
modularity
old stack frame
free parameters
procedures
Imperative and Object-Oriented Languages
Example: C
#include <stio.h>
/* factorial function */
int fac( int num ) {
if (num < 1)
return 1;
else
return num * fac(num - 1) ;
}
int main() {
int x = 10 ;
int f = fac( x ) ;
int x printf(“%d! = %dn”, x, f);
return 0;
}
23
procedures
Imperative and Object-Oriented Languages
Example: C
#include <stio.h>
/* factorial function */
int fac( int num ) {
if (num < 1)
return 1;
else
return num * fac(num - 1) ;
}
int main() {
int x = 10 ;
int f = fac( x ) ;
int x printf(“%d! = %dn”, x, f);
return 0;
}
23
formal parameter
procedures
Imperative and Object-Oriented Languages
Example: C
#include <stio.h>
/* factorial function */
int fac( int num ) {
if (num < 1)
return 1;
else
return num * fac(num - 1) ;
}
int main() {
int x = 10 ;
int f = fac( x ) ;
int x printf(“%d! = %dn”, x, f);
return 0;
}
23
formal parameter
actual parameter
procedures
Imperative and Object-Oriented Languages
Example: C
#include <stio.h>
/* factorial function */
int fac( int num ) {
if (num < 1)
return 1;
else
return num * fac(num - 1) ;
}
int main() {
int x = 10 ;
int f = fac( x ) ;
int x printf(“%d! = %dn”, x, f);
return 0;
}
23
formal parameter
actual parameter
local variable
procedures
Imperative and Object-Oriented Languages
Example: C
#include <stio.h>
/* factorial function */
int fac( int num ) {
if (num < 1)
return 1;
else
return num * fac(num - 1) ;
}
int main() {
int x = 10 ;
int f = fac( x ) ;
int x printf(“%d! = %dn”, x, f);
return 0;
}
23
formal parameter
actual parameter
local variable
recursive call
procedures
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
function fac( n: int ) : int =
let
var f := 1
in
if n < 1 then
f := 1
else
f := (n * fac(n - 1) );
f
end
var f := 0
var x := 5
in
f := fac( x )
end
24
procedures
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
function fac( n: int ) : int =
let
var f := 1
in
if n < 1 then
f := 1
else
f := (n * fac(n - 1) );
f
end
var f := 0
var x := 5
in
f := fac( x )
end
24
formal parameter
procedures
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
function fac( n: int ) : int =
let
var f := 1
in
if n < 1 then
f := 1
else
f := (n * fac(n - 1) );
f
end
var f := 0
var x := 5
in
f := fac( x )
end
24
formal parameter
actual parameter
procedures
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
function fac( n: int ) : int =
let
var f := 1
in
if n < 1 then
f := 1
else
f := (n * fac(n - 1) );
f
end
var f := 0
var x := 5
in
f := fac( x )
end
24
formal parameter
actual parameter
local variable
procedures
Imperative and Object-Oriented Languages
Example: Tiger
/* factorial function */
let
function fac( n: int ) : int =
let
var f := 1
in
if n < 1 then
f := 1
else
f := (n * fac(n - 1) );
f
end
var f := 0
var x := 5
in
f := fac( x )
end
24
formal parameter
actual parameter
local variable
recursive call
call by value vs. call by reference
Imperative and Object-Oriented Languages
Example: Tiger
let
type vector = array of int
function init(v: vector) =
v := vector[5] of 0
function upto(v: vector, l: int) =
for i := 0 to l do
v[i] := i
var v : vector := vector[5] of 1
in
init(v) ;
upto(v, 5)
end
25
Imperative and Object-Oriented Languages 26
Imperative Languages
types
Subtitle Text
Imperative and Object-Oriented Languages
Types & Type Checking
Type
‱ type is category of values
‱ operation can be applied to all values in some category
Type checking
‱ safety: ensure that operation is applied to right input values
‱ static: check during compile time
‱ dynamic: check during execution
27
dynamic & static typing
Imperative and Object-Oriented Languages
Type Systems
Machine code
‱ memory: no type information
‱ instructions: assume values of certain types
28
dynamic & static typing
Imperative and Object-Oriented Languages
Type Systems
Machine code
‱ memory: no type information
‱ instructions: assume values of certain types
Dynamically typed languages
‱ typed values
‱ run-time checking & run-time errors
28
dynamic & static typing
Imperative and Object-Oriented Languages
Type Systems
Machine code
‱ memory: no type information
‱ instructions: assume values of certain types
Dynamically typed languages
‱ typed values
‱ run-time checking & run-time errors
Statically typed languages
‱ typed expressions
‱ compile-time checking & compile-time errors
28
compatibility
Imperative and Object-Oriented Languages
Type Systems
Type compatibility
‱ value/expression: actual type
‱ context: expected type
29
compatibility
Imperative and Object-Oriented Languages
Type Systems
Type compatibility
‱ value/expression: actual type
‱ context: expected type
Type equivalence
‱ structural type systems
‱ nominal type systems
29
compatibility
Imperative and Object-Oriented Languages
Type Systems
Type compatibility
‱ value/expression: actual type
‱ context: expected type
Type equivalence
‱ structural type systems
‱ nominal type systems
Subtyping
‱ relation between types
‱ value/expression: multiple types
29
type compatibility
Imperative and Object-Oriented Languages
Example: Tiger
let
type A = int
type B = int
type V = array of A
type W = V
type X = array of A
type Y = array of B
var a: A := 42
var b: B := a
var v: V := V[42] of b
var w: W := v
var x: X := w
var y: Y := x
in
y
end
30
record types
Imperative and Object-Oriented Languages
Type Systems
Record
‱ consecutively stored values
‱ fields accessible via different offsets
Record type
‱ fields by name, type, position in record
‱ structural subtyping: width vs. depth
31
type R1 = {f1 : int, f2 : int}
type R2 = {f1 : int, f2 : int, f3 : int}
type R3 = {f1 : byte, f2 : byte}
biology
Imperative and Object-Oriented Languages
Polymorphism
32
biology
Imperative and Object-Oriented Languages
Polymorphism
32
biology
Imperative and Object-Oriented Languages
Polymorphism
32
biology
Imperative and Object-Oriented Languages
Polymorphism
32
the occurrence of more than one form
biology
Imperative and Object-Oriented Languages
Polymorphism
33
biology
Imperative and Object-Oriented Languages
Polymorphism
34
can give blood to can give plasma to
programming languages
Imperative and Object-Oriented Languages
Polymorphism
35
programming languages
Imperative and Object-Oriented Languages
Polymorphism
21 + 21
35
programming languages
Imperative and Object-Oriented Languages
Polymorphism
21 + 21
21.0 + 21.0
35
programming languages
Imperative and Object-Oriented Languages
Polymorphism
21 + 21
21.0 + 21.0
"foo" + "bar"
35
programming languages
Imperative and Object-Oriented Languages
Polymorphism
print(42)
print(42.0)
print("foo")
36
programming languages
Imperative and Object-Oriented Languages
Polymorphism
21 + 21
21.0 + 21.0
21 + 21.0
21 + "bar"
37
polymorphism
Imperative and Object-Oriented Languages
Type Systems
Ad-hoc polymorphism
overloading
‱ same name, different types, same operation
‱ same name, different types, different operations
type coercion
‱ implicit conversion
Universal polymorphism
subtype polymorphism
‱ substitution principle
parametric polymorphism
38
21 + 21
21.0 + 21.0
print(42)
print(42.0)
"foo" + "bar"
21 + "bar"
Imperative and Object-Oriented Languages 39
Object-Oriented Languages
objects & messages
objects & messages
Imperative and Object-Oriented Languages
Modularity
Objects
‱ generalization of records
‱ identity
‱ state
‱ behavior
40
objects & messages
Imperative and Object-Oriented Languages
Modularity
Objects
‱ generalization of records
‱ identity
‱ state
‱ behavior
Messages
‱ objects send and receive messages
‱ trigger behavior
‱ imperative realization: method calls
40
Imperative and Object-Oriented Languages 41
Object-Oriented Languages
types
classes
Imperative and Object-Oriented Languages
Modularity
Classes
‱ generalization of record types
‱ characteristics of objects: attributes, fields, properties
‱ behavior of objects: methods, operations, features
42
public class C {
public int f1;
private int f2;
public void m1() { return; }
private C m2(C c) { return c; }
}
classes
Imperative and Object-Oriented Languages
Modularity
Classes
‱ generalization of record types
‱ characteristics of objects: attributes, fields, properties
‱ behavior of objects: methods, operations, features
Encapsulation
‱ interface exposure
‱ hide attributes & methods
‱ hide implementation
42
public class C {
public int f1;
private int f2;
public void m1() { return; }
private C m2(C c) { return c; }
}
inheritance vs. interfaces
Imperative and Object-Oriented Languages
Modularity
Inheritance
‱ inherit attributes & methods
‱ additional attributes & methods
‱ override behavior
‱ nominal subtyping
43
public class C {
public int f1;
public void m1() {
}
public void m2() {
}
}
public class D extends C {
public int f2;
public void m2() {
}
public void m3() {
}
}
public interface I {
public int f;
public void m();
}
public class E implements I {
public int f;
public void m() {
}
public void m’() {
}
}
inheritance vs. interfaces
Imperative and Object-Oriented Languages
Modularity
Inheritance
‱ inherit attributes & methods
‱ additional attributes & methods
‱ override behavior
‱ nominal subtyping
Interfaces
‱ avoid multiple inheritance
‱ interface: contract for attributes & methods
‱ class: provides attributes & methods
‱ nominal subtyping
43
public class C {
public int f1;
public void m1() {
}
public void m2() {
}
}
public class D extends C {
public int f2;
public void m2() {
}
public void m3() {
}
}
public interface I {
public int f;
public void m();
}
public class E implements I {
public int f;
public void m() {
}
public void m’() {
}
}
polymorphism
Imperative and Object-Oriented Languages
Type Systems
44
polymorphism
Imperative and Object-Oriented Languages
Type Systems
Ad-hoc polymorphism
overloading
‱ same method name, independent classes
‱ same method name, same class, different parameter types
overriding
‱ same method name, subclass, compatible types
44
polymorphism
Imperative and Object-Oriented Languages
Type Systems
Ad-hoc polymorphism
overloading
‱ same method name, independent classes
‱ same method name, same class, different parameter types
overriding
‱ same method name, subclass, compatible types
Universal polymorphism
subtype polymorphism
‱ inheritance, interfaces
parametric polymorphism
44
static vs. dynamic dispatch
Imperative and Object-Oriented Languages
Type Systems
Dispatch
‱ link method call to method
45
static vs. dynamic dispatch
Imperative and Object-Oriented Languages
Type Systems
Dispatch
‱ link method call to method
Static dispatch
‱ type information at compile-time
45
static vs. dynamic dispatch
Imperative and Object-Oriented Languages
Type Systems
Dispatch
‱ link method call to method
Static dispatch
‱ type information at compile-time
Dynamic dispatch
‱ type information at run-time
‱ single dispatch: one parameter
‱ multiple dispatch: more parameters
45
single dispatch
Imperative and Object-Oriented Languages
Example: Java
public class A {} public class B extends A {} public class C extends B {}
public class D {
public A m(A a) { System.out.println("D.m(A a)"); return a; }
public A m(B b) { System.out.println("D.m(B b)"); return b; }
}
public class E extends D {
public A m(A a) { System.out.println("E.m(A a)"); return a; }
public B m(B b) { System.out.println("E.m(B b)"); return b; }
}
46
A a = new A(); B b = new B(); C c = new C(); D d = new D(); E e = new E();
A ab = b; A ac = c; D de = e;
d. m(a); d. m(b); d. m(ab); d. m(c); d. m(ac);
e. m(a); e. m(b); e. m(ab); e. m(c); e. m(ac);
de.m(a); de.m(b); de.m(ab); de.m(c); de.m(ac);
overloading vs. overriding
Imperative and Object-Oriented Languages
Example: Java
public class F {
public A m(B b) { System.out.println("F.m(B b)"); return b; }
}
public class G extends F {
public A m(A a) { System.out.println("G.m(A a)"); return a; }
}
public class H extends G {
public B m(B b) { System.out.println("H.m(B b)"); return b; }
}
47
A a = new A(); B b = new B(); F f = new F(); G g = new G(); H h = new H();
A ab = b;
f.m(b);
g.m(a); g.m(b); g.m(ab);
h.m(a); h.m(b); h.m(ab);
Imperative and Object-Oriented Languages 48
Except where otherwise noted, this work is licensed under
copyrights
Imperative and Object-Oriented Languages
Pictures
Slide 1: Popular C++ by Itkovian, some rights reserved
Slide 4: PICOL icons by Melih Bilgil, some rights reserved
Slides 7, 9, 13: Dual Processor Module by roobarb!, some rights reserved
Slides 11, 14: The C Programming Language by Bill Bradford, some rights reserved
Slides 12, 15, 16, 19: Tiger by Bernard Landgraf, some rights reserved
Slide 21: Adam and Eva by Albrecht DĂŒrer, public domain
Slide 22: ABO blood type by InvictaHOG, public domain
Slide 23: Blood Compability and Plasma donation compatibility path by InvictaHOG, public domain
Slide 28: Delice de France by Dominica Williamson, some rights reserved
Slide 46: Nieuwe Kerk by Arne Kuilman, some rights reserved
49

More Related Content

PDF
Declare Your Language: Syntax Definition
PDF
Introduction - Imperative and Object-Oriented Languages
PDF
Compiler Construction | Lecture 3 | Syntactic Editor Services
PDF
CS4200 2019 | Lecture 4 | Syntactic Services
PDF
Compiler Construction | Lecture 12 | Virtual Machines
PDF
Compiler Construction | Lecture 8 | Type Constraints
PDF
Compiler Construction | Lecture 6 | Introduction to Static Analysis
PDF
CS4200 2019 | Lecture 3 | Parsing
Declare Your Language: Syntax Definition
Introduction - Imperative and Object-Oriented Languages
Compiler Construction | Lecture 3 | Syntactic Editor Services
CS4200 2019 | Lecture 4 | Syntactic Services
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 6 | Introduction to Static Analysis
CS4200 2019 | Lecture 3 | Parsing

What's hot (20)

PPT
Lex (lexical analyzer)
PDF
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
PDF
Declarative Semantics Definition - Term Rewriting
PPTX
Introduction of flex
 
PDF
Compiler Construction | Lecture 9 | Constraint Resolution
PPTX
Introduction of bison
 
PDF
Writing Parsers and Compilers with PLY
PDF
Compiler Construction | Lecture 13 | Code Generation
PDF
CS4200 2019 | Lecture 2 | syntax-definition
PDF
regular expressions (Regex)
PPT
Lexyacc
PPTX
More on Lex
PPT
Chapter Eight(3)
 
PPT
Logic Programming and Prolog
PDF
C programming_MSBTE_Diploma_Pranoti Doke
PDF
Compiler Components and their Generators - Traditional Parsing Algorithms
PDF
Lecture 3 RE NFA DFA
PPTX
LISP: Introduction to lisp
PDF
Static name resolution
PPTX
System Programming Unit IV
Lex (lexical analyzer)
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Declarative Semantics Definition - Term Rewriting
Introduction of flex
 
Compiler Construction | Lecture 9 | Constraint Resolution
Introduction of bison
 
Writing Parsers and Compilers with PLY
Compiler Construction | Lecture 13 | Code Generation
CS4200 2019 | Lecture 2 | syntax-definition
regular expressions (Regex)
Lexyacc
More on Lex
Chapter Eight(3)
 
Logic Programming and Prolog
C programming_MSBTE_Diploma_Pranoti Doke
Compiler Components and their Generators - Traditional Parsing Algorithms
Lecture 3 RE NFA DFA
LISP: Introduction to lisp
Static name resolution
System Programming Unit IV
Ad

Viewers also liked (11)

PDF
Lexical Analysis
PDF
Syntax Definition
PDF
Language
PDF
Register Allocation
PDF
Dynamic Semantics
PDF
Software languages
PDF
LR Parsing
PDF
LL Parsing
PDF
Type analysis
PDF
Garbage Collection
PDF
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Lexical Analysis
Syntax Definition
Language
Register Allocation
Dynamic Semantics
Software languages
LR Parsing
LL Parsing
Type analysis
Garbage Collection
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Ad

Similar to Programming languages (20)

PPTX
Programming paradigms Techniques_part2.pptx
PPTX
Imperative programming
PDF
Programing paradigm &amp; implementation
PPTX
Define Computer language, Translator, Standard input out C
PPT
Programing Language
PDF
Lecture_HPC_7_ProgrammingCSI32_Paradigms.pdf
PPTX
Introduction to programming
PPT
PPL unit 1 syntax and semantics- evolution of programming language lexical an...
PPTX
Plc part 1
PPTX
Imperative Programing engineering ppt.pptx
PDF
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
PDF
Declare Your Language: Virtual Machines & Code Generation
PPTX
Programming Language
PPTX
Introduction to Programming kkkkkkkkkkkkk
PPTX
Computer Science ACW Intro to OOP L7.pptx
PPTX
Lec.01-03.pptx for object oriented programming language
PDF
Introduction
PDF
PDF
Object Oriented Programming -- Dr Robert Harle
 
PDF
Programming In Ada 2012 1st Edition Barnes John Gilbert Presslie
Programming paradigms Techniques_part2.pptx
Imperative programming
Programing paradigm &amp; implementation
Define Computer language, Translator, Standard input out C
Programing Language
Lecture_HPC_7_ProgrammingCSI32_Paradigms.pdf
Introduction to programming
PPL unit 1 syntax and semantics- evolution of programming language lexical an...
Plc part 1
Imperative Programing engineering ppt.pptx
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Declare Your Language: Virtual Machines & Code Generation
Programming Language
Introduction to Programming kkkkkkkkkkkkk
Computer Science ACW Intro to OOP L7.pptx
Lec.01-03.pptx for object oriented programming language
Introduction
Object Oriented Programming -- Dr Robert Harle
 
Programming In Ada 2012 1st Edition Barnes John Gilbert Presslie

More from Eelco Visser (19)

PDF
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
PDF
CS4200 2019 Lecture 1: Introduction
PDF
A Direct Semantics of Declarative Disambiguation Rules
PDF
Declarative Type System Specification with Statix
PDF
Compiler Construction | Lecture 17 | Beyond Compiler Construction
PDF
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
PDF
Compiler Construction | Lecture 15 | Memory Management
PDF
Compiler Construction | Lecture 14 | Interpreters
PDF
Compiler Construction | Lecture 11 | Monotone Frameworks
PDF
Compiler Construction | Lecture 10 | Data-Flow Analysis
PDF
Compiler Construction | Lecture 7 | Type Checking
PDF
Compiler Construction | Lecture 4 | Parsing
PDF
Compiler Construction | Lecture 2 | Declarative Syntax Definition
PDF
Compiler Construction | Lecture 1 | What is a compiler?
PDF
Declare Your Language: Dynamic Semantics
PDF
Declare Your Language: Constraint Resolution 2
PDF
Declare Your Language: Constraint Resolution 1
PDF
Declare Your Language: Type Checking
PDF
Declare Your Language: Name Resolution
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 Lecture 1: Introduction
A Direct Semantics of Declarative Disambiguation Rules
Declarative Type System Specification with Statix
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 1 | What is a compiler?
Declare Your Language: Dynamic Semantics
Declare Your Language: Constraint Resolution 2
Declare Your Language: Constraint Resolution 1
Declare Your Language: Type Checking
Declare Your Language: Name Resolution

Recently uploaded (20)

PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Digital Strategies for Manufacturing Companies
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
System and Network Administration Chapter 2
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
AI in Product Development-omnex systems
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
System and Network Administraation Chapter 3
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Design an Analysis of Algorithms II-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
VVF-Customer-Presentation2025-Ver1.9.pptx
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Wondershare Filmora 15 Crack With Activation Key [2025
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
System and Network Administration Chapter 2
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
AI in Product Development-omnex systems
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
System and Network Administraation Chapter 3
Operating system designcfffgfgggggggvggggggggg
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Design an Analysis of Algorithms I-SECS-1021-03
Design an Analysis of Algorithms II-SECS-1021-03

Programming languages

  • 1. IN4303 2016-2017 Compiler Construction Programming Languages imperative & object-oriented Guido Wachsmuth, Eelco Visser
  • 3. Imperative and Object-Oriented Languages 3 interpreter software language compiler software language machine language
  • 4. Imperative and Object-Oriented Languages natural language natural language translator 4 compiler software language machine language
  • 5. Imperative and Object-Oriented Languages 5 Traditional compilers architecture source code errors parse generate check machine code
  • 6. Imperative and Object-Oriented Languages 6 Modern compilers in IDEs architecture source code parse generate machine code check
  • 7. Imperative and Object-Oriented Languages 7 Compilers are inverted abstractions architecture source code machine code abstract
  • 8. Imperative and Object-Oriented Languages Compilers Invert Abstractions Computers speak machine language ‱ basic instructions to move around and combine small things Humans do not speak machine language very well ‱ “mov; loadi; jump” anyone? Programming languages abstract from machine language ‱ capture common programming patterns in language constructs ‱ abstract from uninteresting variation in machines Compilers invert the abstractions of programming languages 8
  • 9. Imperative and Object-Oriented Languages 9 Portability: Platform Independence architecture source code parse generate GPU code check generate x86 code
  • 10. Imperative and Object-Oriented Languages Understanding Compilers Requires 
 Understanding machine languages ‱ machine architecture, instruction sets Understanding programming language (abstraction)s ‱ memory, control-flow, procedures, modules, 
 ‱ safety mechanisms: type systems & other static analyses Understanding how to define mapping from PL to ML ‱ semantics of such mappings ‱ techniques to implement such mappings 10
  • 11. Imperative and Object-Oriented Languages 11 Imperative Languages state & statements procedures types Object-Oriented Languages objects & messages types
  • 12. Imperative and Object-Oriented Languages 12 Imperative Languages state & statements
  • 13. Imperative and Object-Oriented Languages 13 machine code
  • 14. basic concepts Imperative and Object-Oriented Languages State Machine state ‱ a pile of data stored in memory ‱ memory hierarchy: registers, RAM, disk, network, 
 Imperative program ‱ computation is series of changes to memory ‱ basic operations on memory (increment register) ‱ controlling such operations (jump, return address, 
) ‱ control represented by state (program counter, stack, 
) 14
  • 15. registers Imperative and Object-Oriented Languages x86 family general purpose registers ‱ accumulator AX - arithmetic operations ‱ counter CX - shift/rotate instructions, loops ‱ data DX - arithmetic operations, I/O ‱ base BX - pointer to data ‱ stack pointer SP, base pointer BP - top and base of stack ‱ source SI, destination DI - stream operations 15
  • 16. registers Imperative and Object-Oriented Languages x86 family general purpose registers ‱ accumulator AX - arithmetic operations ‱ counter CX - shift/rotate instructions, loops ‱ data DX - arithmetic operations, I/O ‱ base BX - pointer to data ‱ stack pointer SP, base pointer BP - top and base of stack ‱ source SI, destination DI - stream operations special purpose registers ‱ segments SS, CS, DS, ES, FS, GS ‱ flags EFLAGS 15
  • 17. mov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16
  • 18. read memorymov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16
  • 19. read memorymov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16 write memory
  • 20. read memorymov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16 write memory calculation
  • 21. read memorymov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16 write memory calculation jump
  • 22. .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn basic concepts Imperative and Object-Oriented Languages Example: Java Bytecode 17
  • 23. .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn basic concepts Imperative and Object-Oriented Languages Example: Java Bytecode 17 read memory
  • 24. .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn basic concepts Imperative and Object-Oriented Languages Example: Java Bytecode 17 read memory calculation
  • 25. .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn basic concepts Imperative and Object-Oriented Languages Example: Java Bytecode 17 read memory calculation jump
  • 26. basic concepts Imperative and Object-Oriented Languages Memory & Control Abstractions Memory abstractions ‱ variables: abstract over data storage ‱ expressions: combine data into new data ‱ assignment: abstract over storage operations Control-flow abstractions ‱ structured control-flow: abstract over unstructured jumps ‱ ‘go to statement considered harmful’ Edgser Dijkstra, 1968 18
  • 27. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19
  • 28. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19 variable
  • 29. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19 variable expression
  • 30. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19 variable expression assignment
  • 31. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19 variable expression assignment control flow
  • 32. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20
  • 33. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20 variable
  • 34. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20 variable expression
  • 35. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20 variable expression assignment
  • 36. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20 variable expression assignment control flow
  • 37. Imperative and Object-Oriented Languages 21 Imperative Languages procedures
  • 38. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret Imperative and Object-Oriented Languages Example: x86 Assembler 22 modularity
  • 39. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 modularity
  • 40. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 new stack frame modularity
  • 41. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 access parameter new stack frame modularity
  • 42. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 access parameter new stack frame modularity old stack frame
  • 43. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 access parameter new stack frame modularity old stack frame free parameters
  • 44. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23
  • 45. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23 formal parameter
  • 46. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23 formal parameter actual parameter
  • 47. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23 formal parameter actual parameter local variable
  • 48. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23 formal parameter actual parameter local variable recursive call
  • 49. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24
  • 50. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24 formal parameter
  • 51. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24 formal parameter actual parameter
  • 52. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24 formal parameter actual parameter local variable
  • 53. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24 formal parameter actual parameter local variable recursive call
  • 54. call by value vs. call by reference Imperative and Object-Oriented Languages Example: Tiger let type vector = array of int function init(v: vector) = v := vector[5] of 0 function upto(v: vector, l: int) = for i := 0 to l do v[i] := i var v : vector := vector[5] of 1 in init(v) ; upto(v, 5) end 25
  • 55. Imperative and Object-Oriented Languages 26 Imperative Languages types
  • 56. Subtitle Text Imperative and Object-Oriented Languages Types & Type Checking Type ‱ type is category of values ‱ operation can be applied to all values in some category Type checking ‱ safety: ensure that operation is applied to right input values ‱ static: check during compile time ‱ dynamic: check during execution 27
  • 57. dynamic & static typing Imperative and Object-Oriented Languages Type Systems Machine code ‱ memory: no type information ‱ instructions: assume values of certain types 28
  • 58. dynamic & static typing Imperative and Object-Oriented Languages Type Systems Machine code ‱ memory: no type information ‱ instructions: assume values of certain types Dynamically typed languages ‱ typed values ‱ run-time checking & run-time errors 28
  • 59. dynamic & static typing Imperative and Object-Oriented Languages Type Systems Machine code ‱ memory: no type information ‱ instructions: assume values of certain types Dynamically typed languages ‱ typed values ‱ run-time checking & run-time errors Statically typed languages ‱ typed expressions ‱ compile-time checking & compile-time errors 28
  • 60. compatibility Imperative and Object-Oriented Languages Type Systems Type compatibility ‱ value/expression: actual type ‱ context: expected type 29
  • 61. compatibility Imperative and Object-Oriented Languages Type Systems Type compatibility ‱ value/expression: actual type ‱ context: expected type Type equivalence ‱ structural type systems ‱ nominal type systems 29
  • 62. compatibility Imperative and Object-Oriented Languages Type Systems Type compatibility ‱ value/expression: actual type ‱ context: expected type Type equivalence ‱ structural type systems ‱ nominal type systems Subtyping ‱ relation between types ‱ value/expression: multiple types 29
  • 63. type compatibility Imperative and Object-Oriented Languages Example: Tiger let type A = int type B = int type V = array of A type W = V type X = array of A type Y = array of B var a: A := 42 var b: B := a var v: V := V[42] of b var w: W := v var x: X := w var y: Y := x in y end 30
  • 64. record types Imperative and Object-Oriented Languages Type Systems Record ‱ consecutively stored values ‱ fields accessible via different offsets Record type ‱ fields by name, type, position in record ‱ structural subtyping: width vs. depth 31 type R1 = {f1 : int, f2 : int} type R2 = {f1 : int, f2 : int, f3 : int} type R3 = {f1 : byte, f2 : byte}
  • 65. biology Imperative and Object-Oriented Languages Polymorphism 32
  • 66. biology Imperative and Object-Oriented Languages Polymorphism 32
  • 67. biology Imperative and Object-Oriented Languages Polymorphism 32
  • 68. biology Imperative and Object-Oriented Languages Polymorphism 32 the occurrence of more than one form
  • 69. biology Imperative and Object-Oriented Languages Polymorphism 33
  • 70. biology Imperative and Object-Oriented Languages Polymorphism 34 can give blood to can give plasma to
  • 71. programming languages Imperative and Object-Oriented Languages Polymorphism 35
  • 72. programming languages Imperative and Object-Oriented Languages Polymorphism 21 + 21 35
  • 73. programming languages Imperative and Object-Oriented Languages Polymorphism 21 + 21 21.0 + 21.0 35
  • 74. programming languages Imperative and Object-Oriented Languages Polymorphism 21 + 21 21.0 + 21.0 "foo" + "bar" 35
  • 75. programming languages Imperative and Object-Oriented Languages Polymorphism print(42) print(42.0) print("foo") 36
  • 76. programming languages Imperative and Object-Oriented Languages Polymorphism 21 + 21 21.0 + 21.0 21 + 21.0 21 + "bar" 37
  • 77. polymorphism Imperative and Object-Oriented Languages Type Systems Ad-hoc polymorphism overloading ‱ same name, different types, same operation ‱ same name, different types, different operations type coercion ‱ implicit conversion Universal polymorphism subtype polymorphism ‱ substitution principle parametric polymorphism 38 21 + 21 21.0 + 21.0 print(42) print(42.0) "foo" + "bar" 21 + "bar"
  • 78. Imperative and Object-Oriented Languages 39 Object-Oriented Languages objects & messages
  • 79. objects & messages Imperative and Object-Oriented Languages Modularity Objects ‱ generalization of records ‱ identity ‱ state ‱ behavior 40
  • 80. objects & messages Imperative and Object-Oriented Languages Modularity Objects ‱ generalization of records ‱ identity ‱ state ‱ behavior Messages ‱ objects send and receive messages ‱ trigger behavior ‱ imperative realization: method calls 40
  • 81. Imperative and Object-Oriented Languages 41 Object-Oriented Languages types
  • 82. classes Imperative and Object-Oriented Languages Modularity Classes ‱ generalization of record types ‱ characteristics of objects: attributes, fields, properties ‱ behavior of objects: methods, operations, features 42 public class C { public int f1; private int f2; public void m1() { return; } private C m2(C c) { return c; } }
  • 83. classes Imperative and Object-Oriented Languages Modularity Classes ‱ generalization of record types ‱ characteristics of objects: attributes, fields, properties ‱ behavior of objects: methods, operations, features Encapsulation ‱ interface exposure ‱ hide attributes & methods ‱ hide implementation 42 public class C { public int f1; private int f2; public void m1() { return; } private C m2(C c) { return c; } }
  • 84. inheritance vs. interfaces Imperative and Object-Oriented Languages Modularity Inheritance ‱ inherit attributes & methods ‱ additional attributes & methods ‱ override behavior ‱ nominal subtyping 43 public class C { public int f1; public void m1() {
} public void m2() {
} } public class D extends C { public int f2; public void m2() {
} public void m3() {
} } public interface I { public int f; public void m(); } public class E implements I { public int f; public void m() {
} public void m’() {
} }
  • 85. inheritance vs. interfaces Imperative and Object-Oriented Languages Modularity Inheritance ‱ inherit attributes & methods ‱ additional attributes & methods ‱ override behavior ‱ nominal subtyping Interfaces ‱ avoid multiple inheritance ‱ interface: contract for attributes & methods ‱ class: provides attributes & methods ‱ nominal subtyping 43 public class C { public int f1; public void m1() {
} public void m2() {
} } public class D extends C { public int f2; public void m2() {
} public void m3() {
} } public interface I { public int f; public void m(); } public class E implements I { public int f; public void m() {
} public void m’() {
} }
  • 86. polymorphism Imperative and Object-Oriented Languages Type Systems 44
  • 87. polymorphism Imperative and Object-Oriented Languages Type Systems Ad-hoc polymorphism overloading ‱ same method name, independent classes ‱ same method name, same class, different parameter types overriding ‱ same method name, subclass, compatible types 44
  • 88. polymorphism Imperative and Object-Oriented Languages Type Systems Ad-hoc polymorphism overloading ‱ same method name, independent classes ‱ same method name, same class, different parameter types overriding ‱ same method name, subclass, compatible types Universal polymorphism subtype polymorphism ‱ inheritance, interfaces parametric polymorphism 44
  • 89. static vs. dynamic dispatch Imperative and Object-Oriented Languages Type Systems Dispatch ‱ link method call to method 45
  • 90. static vs. dynamic dispatch Imperative and Object-Oriented Languages Type Systems Dispatch ‱ link method call to method Static dispatch ‱ type information at compile-time 45
  • 91. static vs. dynamic dispatch Imperative and Object-Oriented Languages Type Systems Dispatch ‱ link method call to method Static dispatch ‱ type information at compile-time Dynamic dispatch ‱ type information at run-time ‱ single dispatch: one parameter ‱ multiple dispatch: more parameters 45
  • 92. single dispatch Imperative and Object-Oriented Languages Example: Java public class A {} public class B extends A {} public class C extends B {} public class D { public A m(A a) { System.out.println("D.m(A a)"); return a; } public A m(B b) { System.out.println("D.m(B b)"); return b; } } public class E extends D { public A m(A a) { System.out.println("E.m(A a)"); return a; } public B m(B b) { System.out.println("E.m(B b)"); return b; } } 46 A a = new A(); B b = new B(); C c = new C(); D d = new D(); E e = new E(); A ab = b; A ac = c; D de = e; d. m(a); d. m(b); d. m(ab); d. m(c); d. m(ac); e. m(a); e. m(b); e. m(ab); e. m(c); e. m(ac); de.m(a); de.m(b); de.m(ab); de.m(c); de.m(ac);
  • 93. overloading vs. overriding Imperative and Object-Oriented Languages Example: Java public class F { public A m(B b) { System.out.println("F.m(B b)"); return b; } } public class G extends F { public A m(A a) { System.out.println("G.m(A a)"); return a; } } public class H extends G { public B m(B b) { System.out.println("H.m(B b)"); return b; } } 47 A a = new A(); B b = new B(); F f = new F(); G g = new G(); H h = new H(); A ab = b; f.m(b); g.m(a); g.m(b); g.m(ab); h.m(a); h.m(b); h.m(ab);
  • 94. Imperative and Object-Oriented Languages 48 Except where otherwise noted, this work is licensed under
  • 95. copyrights Imperative and Object-Oriented Languages Pictures Slide 1: Popular C++ by Itkovian, some rights reserved Slide 4: PICOL icons by Melih Bilgil, some rights reserved Slides 7, 9, 13: Dual Processor Module by roobarb!, some rights reserved Slides 11, 14: The C Programming Language by Bill Bradford, some rights reserved Slides 12, 15, 16, 19: Tiger by Bernard Landgraf, some rights reserved Slide 21: Adam and Eva by Albrecht DĂŒrer, public domain Slide 22: ABO blood type by InvictaHOG, public domain Slide 23: Blood Compability and Plasma donation compatibility path by InvictaHOG, public domain Slide 28: Delice de France by Dominica Williamson, some rights reserved Slide 46: Nieuwe Kerk by Arne Kuilman, some rights reserved 49