Un compilateur... comment ça marche?
-
Upload
dinesh-bolkensteyn -
Category
Technology
-
view
181 -
download
0
description
Transcript of Un compilateur... comment ça marche?
Les compilateurs... comment ça marche ?
Dinesh Bolkensteyn@dbolkensteyn
14 janvier 2014
La face visible
public static <T> Collection<T>
identity(Collection<?> param) {
return param;
}
La face visible
public static <T> Collection<T>
identity(Collection<??> param) {
return param;
}
incompatible types
found : java.util.Collection<capture#42 of ?>
required: java.util.Collection<T>
Ou moins visible...
Ou moins visible...
Architecture
Front End
● Syntaxe● Sémantique
Back End
● Optimisation● Génération de code
Front End
1. Lexer
2. Parser
3. Résolution des noms
4. Vérification des types
Front End : Lexer
foo + 42 * bar
Front End : Lexer
foo + 42 * bar
1. ID(« foo »)
2. PLUS(« + »)
3. INTEGER(« 42 »)
4. STAR(« * »)
5. ID(« bar »)
Front End : Lexer
foo + 42 * bar
1. ID(« foo »)
2. PLUS(« + »)
3. INTEGER(« 42 »)
4. STAR(« * »)
5. ID(« bar »)
Expressions régulières
● ID : [a-zA-Z][a-zA-Z0-9]*+
● INTEGER : [0-9]++
● PLUS : \+
● STAR : \*
● WHITESPACES : [ \r\n]++
Front End : Parser
foo + 42 * bar
Front End : Parser
foo + 42 * bar
Front End : Parser
foo + 42 * bar Grammaire formelle
● ADD :=
MUL {PLUS MUL}
MUL
● MUL :=
PRIM {STAR PRIM}
PRIM
● PRIM := ID | INTEGER
Front End : Parser
foo + 42 * bar Fonctions récursives
add() {
Expr left = mul() ;
accept(PLUS) ;
Expr right = mul() ;
return new Add(
left, right)
}
Front End : Résolution des noms
class Example {
public Example(
int foo) {
this.foo =
foo;
}
int foo;
}
Front End : Résolution des noms
class Example {
public Example(
int foo) {
this.foo =
foo;
}
int foo;
}
Phase 1:
Class(
fields => { foo }
constructors = {
Constructor(
parameters = { foo }
)
}
)
Front End : Résolution des noms
class Example {
public Example(
int foo) {
this.foo = // field
foo; // parameter
}
int foo;
}
Résultat phase 1:
Class(
fields => { foo }
constructors = {
Constructor(
parameters = { foo }
)
}
)
Front End : Vérifications des types
int a = 0 ; // OK
int a = 10 / 3 ; // OK
int a = 10.0 / 3 ; // KO
Front End : Vérifications des types
int a = 0 ; // OK
int a = 10 / 3 ; // OK
int a = 10.0 / 3 ; // KO
Règles de type● e1 : int, e2 : int
-> e1 + e2 : int● e1 : double, e2 : int
-> e1 + e2 : double● etc.
Back End
1. Optimisation
2. Génération de code
Et ça va bien plus loin...
● Inférence des types: val foo = ... ;● C++ template « meta-programming » :
template <int I>
int recursive(void) {
return recursive<I - 1>();
}
Les compilateurs... comment ça marche ?
Dinesh Bolkensteyn@dbolkensteyn
Merci !