Académique Documents
Professionnel Documents
Culture Documents
1
Plan
Introduction
Approche descendante (Top-down)
Approche ascendante (Bottom-up)
2
Introduction
4
Introduction
5
Introduction
⚫ Structure générale d’un parser
6
Introduction
Approches de
construction d’un arbre
de dérivation
7
Approche descendante
(Top-down)
8
Approche descendante (Top-down)
Principe
w=acb
10
Approche descendante (Top-down)
⚫ Exemple
S->aTb
T->cd|c
Avec le mot w= acb
w=acb
11
Approche descendante (Top-down)
⚫ Exemple
S->aTb
T->cd|c
Avec le mot w= acb
w=acb
12
Approche descendante (Top-down)
Conclusion
⚫ Ce qui serait pratique, ça serait d'avoir une table qui
indique : quand on lit tel caractère et qu’on en est à
dériver tel symbole non-terminal, alors on applique telle
règle et on ne se pose pas de questions.
⚫ Ça existe, et ça s'appelle une table d'analyse.
13
Approche descendante (Top-down)
Analyseur LL(1)
⚫ L'analyseur LL(1) est un type d'analyseur syntaxique qui est utilisé pour
analyser des langages de programmation ou des langues naturelles.
⚫ Le terme LL(1) signifie que l'analyseur LL(1) utilise une méthode d'analyse
"left-to-right, leftmost-derivation" (de gauche à droite, dérivation la plus à
gauche) et qu'il utilise un seul symbole de lecture à chaque étape de
l'analyse.
14
Approche descendante (Top-down)
Analyseur LL(1)
15
Approche descendante (Top-down)
Analyseur LL(1)
⚫L'analyseur LL(1) effectue une analyse syntaxique en suivant
une stratégie de lecture anticipée de gauche à droite.
16
Approche descendante (Top-down)
Analyseur LL(1)
⚫ L'analyseur LL(1) a l'avantage d'être facile à implémenter et de
générer des messages d'erreur clairs lorsqu'il rencontre des
erreurs de syntaxe.
17
Approche descendante (Top-down)
Analyseur LL(1)
⚫Pour construire une table d'analyse, on a besoin de calculer
deux ensembles de terminaux :
⚫ PREMIER “First()”
⚫ SUIVANT “Follow()”
18
Approche descendante (Top-down)
Analyseur LL(1)
⚫ Follow(A) : l'ensemble Follow(A) contient les terminaux qui
peuvent suivre A dans une chaîne de symboles. Par exemple, si
A est suivi par B dans une production, et si B peut être suivi par
le terminal c, alors Follow(A) contiendra c.
19
Approche descendante (Top-down)
Analyseur LL(1)
En résumé
20
Approche descendante
(Top-down)
Calcul des premiers
21
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
22
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
Algorithme de calcul des ensembles PREMIER(X)
X est composé de terminaux et de non-terminaux :
1. Si X est un non-terminal et X → Y1Y2..Yn est une production de la grammaire
(avec Yi symbole terminal ou non-terminal) alors
23
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
24
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
25
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
premier(S)= {a,b,c,d}
26
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
27
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
Si on rajoute ε à la règle de production de C
premier(A)= {a, ε}
premiers (B)={b, c, ε}
premiers (C)={d, ε}
premier(S)= {a,b,c,d,ε}
28
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
29
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
30
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
31
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
32
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
33
Approche descendante (Top-down)
Analyseur LL(1): Calcul des premiers
34
Approche descendante
(Top-down)
Calcul des suivants
35
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
• Pour tout non-terminal A, on cherche SUIVANT(A) :
SUIVANT(A) = l'ensemble de tous les symboles terminaux a qui peuvent
apparaître immédiatement à droite de A dans une dérivation S → αAaβ
36
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
Algorithme de calcul des ensembles SUIVANT(X)
1. Ajouter un marqueur de fin de chaîne (symbole $ par exemple) à
SUIVANT(S) (où S est l'axiome de départ de la grammaire)
▪ Suivant(C)=Suivant(A)+Suivant(C)=Suivant(A)={σ, $}
38
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
▪ Suivant(A)={σ, $}
39
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
40
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
▪ Suivant(C)=Suivant(A)+Suivant(C)={σ, $}
41
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
42
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
▪ Suivant(E)=Suivant(B)+Suivant(E)
=Suivant(B)
={α , σ, $}
43
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
44
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
▪ Suivant(A)={σ, $}
▪ Suivant(B)= {α , σ, $}
▪ Suivant(C)={σ, $}
▪ Suivant(D)={β, α , σ, $}
▪ Suivant(E)= {α , σ, $}
45
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
Suivant(S)={$}
Suivant(A)=Premier(B)-ε+ Premier(C si B=ε)-ε +Suivant(A) +Suivant(C)={b,c,d,$}
Suivant(C)=Suivant(S)+Suivant(C)={$}
Suivant(B)= Premier(C)-ε+Suivant(B)={d}
46
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
47
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
Exemple
S → iEtSS’|a
S’ → eS|ε
E → bb
PREMIER SUIVANT
S
S’
E
48
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
Exemple
S → iEtSS’|a
S’ → eS|ε
E → bb
PREMIER SUIVANT
S ia $e
S’ eε $e
E b t
49
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
50
Approche descendante (Top-down)
Analyseur LL(1): Calcul des suivants
51
Approche descendante
(Top-down)
Construction de la table
d’analyse
52
Approche descendante (Top-down)
Analyseur LL(1): Construction de la table d’analyse
53
Approche descendante (Top-down)
Analyseur LL(1): Construction de la table d’analyse
54
Approche descendante (Top-down)
Analyseur LL(1): Construction de la table d’analyse
55
Approche descendante (Top-down)
Analyseur LL(1)
56
Approche descendante (Top-down)
Analyseur LL(1)
57
Approche descendante (Top-down)
Analyseur LL(1)
58
Approche descendante (Top-down)
Analyseur LL(1)
59
Approche descendante (Top-down)
Analyseur LL(1)
60
Approche descendante (Top-down)
Analyseur LL(1)
PREMIER(E)=PREMIER(T)={(,nb} SUIVANT(E)={$,)}
PREMIER(E’)={+,-,ε} SUIVANT(E’)={$,)}
PREMIER(T)=PREMIER(F)={(,nb} SUIVANT(T)={+,-,),$}
PREMIER(T’)={*,/,ε} SUIVANT(T’)={+,-,),$}
PREMIER(F)={(,nb} SUIVANT(F)={*,/,),+,-,$}
62
Approche descendante (Top-down)
Analyseur LL(1)
⚫Construction
⚫ de la table d’analyse : Exemple
PREMIER(E)=PREMIER(T)={(,nb} SUIVANT(E)={$,)}
PREMIER(E’)={+,-,ε} SUIVANT(E’)={$,)}
PREMIER(T)=PREMIER(F)={(,nb} SUIVANT(T)={+,-,),$}
PREMIER(T’)={*,/,ε} SUIVANT(T’)={+,-,),$}
PREMIER(F)={(,nb} SUIVANT(F)={*,/,),+,-,$}
63
Approche descendante (Top-down)
Analyseur LL(1)
64
Approche descendante (Top-down)
Analyseur LL(1)
65
Approche descendante
(Top-down)
En pratique
66
Approche descendante (Top-down)
Analyseur LL(1)
67
Approche descendante (Top-down)
Analyseur LL(1)
68
Approche descendante (Top-down)
Analyseur LL(1)
69
Approche descendante (Top-down)
Analyseur LL(1)
70
Approche descendante (Top-down)
Analyseur LL(1)
71
Approche descendante (Top-down)
Analyseur LL(1) E→TE’
T
E’
72
Approche descendante (Top-down)
Analyseur LL(1) E→TE’
T
E’
73
Approche descendante (Top-down)
Analyseur LL(1)
E→TE’
T→FT’
F
T’
E’
74
Approche descendante (Top-down)
Analyseur LL(1) Depiler
E→TE’
T→FT’
F→id
dépiler
id
T’
E’
75
Approche descendante (Top-down)
Analyseur LL(1)
E→TE’
T→FT’
F→id
dépiler
T’
E’
76
Approche descendante (Top-down)
Analyseur LL(1)
E→TE’
T→FT’
F→id
dépiler
T’→ε
E’→+TE’
E’
77
Approche descendante (Top-down)
Analyseur LL(1) Depiler
E→TE’
T→FT’
F→id
dépiler
T’→ε
+
E’→+TE’
+
T dépiler
E’
78
Approche descendante (Top-down)
Analyseur LL(1)
E→TE’
T→FT’
F→id
dépiler
T’→ε
E’→+TE’
* dépiler
id T→FT’
F→id
dépiler
T’→*FT’
dépiler
F→id
dépiler
T’→ε
E’→ε
79
Approche descendante (Top-down)
Analyseur LL(1)
Algorithme
Répéter
Soit X le symbole en sommet de pile
Soit a la lettre pointée par ps
Si X est un non terminal alors
Si M[X,a]= X → Y1..Yn alors
enlever X de la pile
mettre Yn puis Yn-1 puis ...puis Y1 dans la pile
émettre en sortie la production X → Y1..Yn
Sinon
ERREUR
Finsi
Sinon
Si X=$ alors
Si a=$ alors ACCEPTER
Sinon ERREUR
Finsi
Sinon
Si X=a alors
enlever X de la pile
avancer ps
Sinon
ERREUR
Finsi
Finsi
Finsi
jusqu'à ERREUR ou ACCEPTER 80
Approche descendante (Top-down)
Analyseur LL(1)
81
Approche descendante (Top-down)
Analyseur LL(1) Pile Entrée Sortie
⚫Accepter/Rejeter un mot $E 3+4*5$ E→TE’
$ E'T 3+4*5$ T→FT’
m=3+4*5 $ E'T'F 3+4*5$ F→nb
$ E'T'3 3+4*5$
$ E'T' +4*5$ T’ →ε
$ E' +4*5$ E’ →+TE’
$ E'T+ +4*5$
$ E'T 4*5$ T→FT’
$ E'T'F 4*5$ F→nb
$ E'T'4 4*5$
$ E'T' *5$ T’ →*FT’
$ E'T'F* *5$
$ E'T'F 5$ F→nb
$ E'T'5 5$
$ E'T' $ T’ →ε
$ E' $ E’ →ε
analyse
$ $ syntaxique
réussie
82
Approche descendante (Top-down)
Analyseur LL(1) Pile Entrée Sortie
⚫Accepter/Rejeter un mot $E 3+4*5$ E→TE’
$ E'T 3+4*5$ T→FT’
m=3+4*5 $ E'T'F 3+4*5$ F→nb
$ E'T'3 3+4*5$
⚫Analyse syntaxique réussie : on $ E'T' +4*5$ T’ →ε
obtient l’arbre syntaxique suivant
$ E' +4*5$ E’ →+TE’
$ E'T+ +4*5$
$ E'T 4*5$ T→FT’
$ E'T'F 4*5$ F→nb
$ E'T'4 4*5$
$ E'T' *5$ T’ →*FT’
$ E'T'F* *5$
$ E'T'F 5$ F→nb
$ E'T'5 5$
$ E'T' $ T’ →ε
$ E' $ E’ →ε
analyse
$ $ syntaxique
réussie
83
Approche descendante (Top-down)
Analyseur LL(1)
84
Approche descendante (Top-down)
Analyseur LL(1)
85
Approche descendante (Top-down)
Analyseur LL(1)
86
Approche descendante (Top-down)
Exercice 1
Considérons la grammaire suivante:
S -> aX
X -> B | CD | E
B -> bB | ε
C -> cC | ε
D -> dDd | ε
E -> FG
F-> aF|cF|ε
G -> g | h
⚫ Calculer les ensembles premier et suivant.
⚫ Construire la table d'analyse LL de cette grammaire.
87
Approche descendante (Top-down)
Exercice 1 Ensembles premiers :
Premier(S) = {a}
S -> aX Premier(X) = {b, c, d, g, h, ε}
X -> B | CD | E Premier(B) = {b, ε}
B -> bB | ε Premier(C) = {c, ε}
C -> cC | ε Premier(D) = {d, ε}
D -> dDd | ε Premier(E) = {a, c, g, h}
E -> FG Premier(F) = {a, c, ε}
F-> aF|cF|ε Premier(G) = {g, h}
G -> g | h
88
Approche descendante (Top-down)
Exercice 1 Suivants(S) = {$}
Suivants(X) = {$}
S -> aX Suivants(B) = {$}
X -> B | CD | E Suivants(C) = {d, $}
B -> bB | ε Suivants(D) = {d, $}
C -> cC | ε Suivants(E) = {$}
D -> dDd | ε Suivants(F) = {g, h}
E -> FG Suivants(G) = {$}
F-> aF|cF|ε
G -> g | h
89
Approche descendante (Top-down)
Exercice 1 Premier(S) = {a} Suivants(S) = {$}
Premier(X) = {b, c, d, g, h, ε} Suivants(X) = {$}
S -> aX Premier(B) = {b, ε} Suivants(B) = {$}
X -> B | CD | E Premier(C) = {c, ε} Suivants(C) = {d, $}
B -> bB | ε Premier(D) = {d, ε} Suivants(D) = {d, $}
C -> cC | ε Premier(E) = {a, c, g, h} Suivants(E) = {$}
D -> dDd | ε Premier(F) = {a, c, ε} Suivants(F) = {g, h}
E -> FG Premier(G) = {g, h} Suivants(G) = {$}
F-> aF|cF|ε
G -> g | h
90
Approche descendante (Top-down)
Exercice 1 S -> aX
Premier(S) = {a} Suivants(S) = {$}
X -> B | CD | E Premier(X) = {b, c, d, g, h, ε} Suivants(X) = {$}
B -> bB | ε Premier(B) = {b, ε} Suivants(B) = {$}
C -> cC | ε Premier(C) = {c, ε} Suivants(C) = {d, $}
D -> dDd | ε Premier(D) = {d, ε} Suivants(D) = {d, $}
E -> FG Premier(E) = {a, c, g, h} Suivants(E) = {$}
Premier(F) = {a, c, ε} Suivants(F) = {g, h}
F-> aF|cF|ε
Premier(G) = {g, h} Suivants(G) = {$}
G -> g | h
a b c d g h $
S S -> aX
X X -> B X -> CD X -> CD X -> E X -> E X -> ε
B B -> bB B -> ε
C C -> cC C -> ε
D D -> dDd D -> ε
E E -> FG E -> FG E -> FG E -> FG
F F -> aF F -> cF F -> ε F -> ε
G G -> g G -> h
91
Approche descendante (Top-down)
Exercice 1
acdd
Etape Pile Entrée Action
1 $S acdd$ S->aX
3 $X cdd$ X -> E
4 $E cdd$ E -> FG
5 $GF cdd$ F -> cF
6 $GFc cdd$ depiler
Erreur
7 $GF dd$
syntaxique
92
Approche descendante (Top-down)
Exercice 1
acaah
Etape Pile Entrée Action
1 $S acaah$ S->aX
3 $X caah$ X -> E
4 $E caah$ E -> FG
5 $GF caah$ F -> cF
6 $GFc caah$ depiler
7 $GF aah$ F -> aF
8 $GFa aah$ depiler
9 $GF ah$ F -> aF
10 $GFa ah$ depiler
11 $GF h$ F -> ε
12 $G h$ G -> h
13 $h h$ depiler
14 $ $ succès
93
Approche descendante (Top-down)
Exercice 1
aghac
Etape Pile Entrée Action
1 $S aghac$ S->aX
3 $X ghac$ X -> E
4 $E ghac$ E -> FG
6 $G ghac$ G -> g
7 $g ghac$ depiler
ERREUR
8 $ hac$
syntaxique
94
Approche descendante (Top-down)
Analyseur LL(1)
⚫Remarque
95
Approche descendante (Top-down)
Une grammaire ambigüe
Définition
On appelle grammaire LL(1) une grammaire pour laquelle la table d'analyse
(décrite précédemment) n'a aucune case définie de façon multiple.
Théorème
Une grammaire ambigüe ou récursive à gauche ou non factorisée à gauche
n'est pas LL(1).
Définition
Une grammaire est dite ambigüe s'il existe un mot de L(G) ayant
plusieurs arbres syntaxiques.
96
Approche descendante (Top-down)
Grammaire LL(1)
⚫Grammaire ambigüe
Exemple
Cette grammaire est ambiguë car le mot m= if (x>10 ) if (y<0) a=1 else a=0
possède deux arbres syntaxiques différents :
97
Approche descendante (Top-down)
Grammaire LL(1)
⚫Grammaire ambigüe
⚫Exemple
98
Approche descendante (Top-down)
Grammaire LL(1)
⚫Grammaire ambigüe
⚫Exemple
car il y a deux interprétations syntaxiques possibles :
if (x>10) if (x>10)
if (y<0) if (y<0)
a=1 a=1
else ; ; //
finsi
a=0 else
// finsi ; a=0
// finsi // finsi
;
99
Approche descendante (Top-down)
Une grammaire ambigüe
Théorème
Une grammaire ambigüe ou récursive à gauche ou non factorisée à gauche
n'est pas LL(1).
Factoriser à gauche
100
Approche descendante (Top-down)
Elimination de la récursive à gauche
⚫ Une grammaire est immédiatement récursive à
gauche si elle contient un non-terminal A tel qu'il existe
une production A-> Aα où α est une chaîne quelconque.
101
Approche descendante (Top-down)
Elimination de la récursive à gauche
⚫ Une grammaire est récursive à gauche si elle
contient un non-terminal A tel qu'il existe une dérivation
+ Aα où α est une chaîne quelconque.
A->
S->Aa|b
A->Ac|Sd|ε
102
Approche descendante (Top-down)
Elimination de la récursive à gauche
103
Approche descendante (Top-down)
Elimination de la récursive à gauche
104
Approche descendante (Top-down)
Elimination de la récursive à gauche
105
Approche descendante (Top-down)
Elimination de la récursive à gauche
106
Approche descendante (Top-down)
Elimination de la récursive à gauche
107
Approche descendante (Top-down)
Elimination de la récursive à gauche
108
Approche descendante (Top-down)
Elimination de la récursive à gauche
109
Approche descendante (Top-down)
Factorisation à gauche
110
Approche descendante (Top-down)
Factorisation à gauche
111
Approche descendante (Top-down)
Factorisation à gauche
112
Approche descendante (Top-down)
Factorisation à gauche
113
Approche descendante (Top-down)
Factorisation à gauche
114
Approche descendante (Top-down)
Factorisation à gauche
115
Approche descendante (Top-down)
Exercice
S → ScA|B
A → Aa|ε
B → Bb|d|e
116
Approche descendante (Top-down)
Exercice
S → ScA|B
A → Aa|ε
B → Bb|d|e
117
Approche descendante (Top-down)
Exercice
S -> aEbS|aEbSeB|a
E -> bcB|bca
B -> ba
Exercice
S -> aEbS|aEbSeB|a
E -> bcB|bca
B -> ba
Conclusion
Exemple : grammaire des expressions arithmétiques avec les opérateurs + et *
E->E+E|E*E|(E)|nb
Mais elle est ambiguë. Pour lever l'ambiguïté, on considère les priorités
classiques des opérateurs et on obtient la grammaire non ambiguë :
E->E+T|T
T->T*F|F
F->(E)|nb
120
Approche descendante (Top-down)
Conclusion
Exemple : grammaire des expressions arithmétiques avec les opérateurs + et *
E->E+E|E*E|(E)|nb
Mais elle est ambiguë. Pour lever l'ambiguïté, on considère les priorités
classiques des opérateurs et on obtient la grammaire non ambiguë :
E->E+T|T
T->T*F|F
F->(E)|nb
121
Approche descendante (Top-down)
Grammaire LL(1)
Conclusion
Exemple : grammaire des expressions arithmétiques avec les opérateurs + et *
E->E+T|T
T->T*F|F
F->(E)|nb
Après suppression de la récursivité à gauche, on obtient
122
Approche ascendante
(Bottom-up)
123
Approche ascendante (Bottom-up)
124
Approche ascendante (Bottom-up)
Analyse SLR
125
Approche ascendante (Bottom-up)
Analyse SLR
126
Approche ascendante (Bottom-up)
Analyse SLR
127
Approche ascendante (Bottom-up)
Analyse SLR
128
Approche ascendante (Bottom-up)
Analyse SLR
129
Approche ascendante (Bottom-up)
Analyse SLR
130
Approche ascendante (Bottom-up)
Analyse SLR
131
Approche ascendante (Bottom-up)
Analyse SLR
132
Approche ascendante (Bottom-up)
Analyse SLR
133
Approche ascendante (Bottom-up)
Analyse SLR
134
Approche ascendante (Bottom-up)
Analyse SLR
135
Approche ascendante (Bottom-up)
Analyse SLR
136
Approche ascendante (Bottom-up)
Analyse SLR
137
Approche ascendante (Bottom-up)
Analyse SLR
138
Approche ascendante (Bottom-up)
Analyse SLR
139
Approche ascendante (Bottom-up)
Analyse SLR
140
Approche ascendante (Bottom-up)
Analyse SLR
141
Approche ascendante (Bottom-up)
Analyse SLR
142
Approche ascendante (Bottom-up)
Analyse SLR
143
Approche ascendante (Bottom-up)
Analyse SLR: Construction de la table d’analyse SLR
144
Approche ascendante (Bottom-up)
Analyse SLR: Construction de la table d’analyse SLR
145
Approche ascendante (Bottom-up)
Analyse SLR: Construction de la table d’analyse SLR
146
Approche ascendante (Bottom-up)
Analyse SLR: Construction de la table d’analyse SLR
147
Approche ascendante (Bottom-up)
Analyse SLR: Construction de la table d’analyse SLR
148
Approche ascendante (Bottom-up)
Analyse SLR: Construction de la table d’analyse SLR
149