Académique Documents
Professionnel Documents
Culture Documents
Jean Privat
Université du Québec à Montréal
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 1/1
Conflit LR(0) (rappel)
s = a End ;
a = b c ;
b = b x | ;
c = y c | ;
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 2/1
Conflit LR(0) (rappel)
End 3 s=a End.
2 s=a.End
s=.a End a
a=.b c
1 5 a=b c.
b=.b x b c
a=b.c
b=.
b=b.x x
4 6 b=b x.
c=.y c
c=. y
y
c=y.c
c
7 c=.y c 8 c=y c.
c=.
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 3/1
Conflit LR(0) (rappel)
End 3 s=a End.
2 s=a.End
s=.a End a
a=.b c
1 5 a=b c.
b=.b x b c
a=b.c
b=.
b=b.x x
4 6 b=b x.
c=.y c
c=. y
y
c=y.c
c
7 c=.y c 8 c=y c.
c=.
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 3/1
Conflit LR(0)
Conflits gênants
En pratique, la plupart des grammaires ne sont pas LR(0)
Solution
Il faut une analyse plus forte : LR(1)
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 4/1
Analyse syntaxique LR(1)
Analyse ascendante
Déduit les productions a partir des jetons
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 5/1
Fonctions d’aide
Ensemble Nullable
Indique si une production peut n’englober aucun jeton
Exemple : b ∈ Nullable
Fonction Premier
Indique l’ensemble des jetons qui commencent (au sens large)
une production
Exemple : x ∈ Premier(a)
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 6/1
Ensemble Nullable
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 7/1
Exercice
s = a End ;
a = b c ;
b = b x | ;
c = y c | ;
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 8/1
Exercice
s = a End ;
a = b c ;
b = b x | ;
c = y c | ;
0 : Nullable = {}
1 : Nullable = {b, c}
2 : Nullable = {a, b, c}
3 : Nullable = {a, b, c} → Stabilité atteinte
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 8/1
Fonction Premier
Jean Privat (UQAM) 08—Analyse syntaxique LR(1) INF5000 — Automne 2011 9/1
Exercice
s = a End ;
a = b c ;
b = b x | ;
c = y c | ;
s = a End ;
a = b c ;
b = b x | ;
c = y c | ;
P(s)={ } ; P(a)={ } ; P(b)={ } ; P(c)={ }
P(s)={End} ; P(a)={ } ; P(b)={x} ; P(c)={y}
P(s)={End} ; P(a)={x, y} ; P(b)={x} ; P(c)={y}
P(s)={End, x, y} ; P(a)={x, y} ; P(b)={x} ; P(c)={y}
P(s)={End, x, y} ; P(a)={x, y} ; P(b)={x} ; P(c)={y} →
Stabilité atteinte
Item LR(1)
Une alternative
Une position (indiquée par le point)
Un jeton suivant la production (indiqué après la virgule)
Exemple : a = b . c , End
Item de départ
s = . a End , End
a = γ . b α k β , j → dérouler b, suivant = k
a = γ . b α , j → dérouler b, suivant = j
avec
α une séquence (possiblement vide) de productions nullables
β et γ deux séquences quelconques (possiblement vide) de
productions ou de jetons
c=y.c, End
c
7 c=.y c, End 8 c=y c., End
c=., End
c=y.c, End
c
7 c=.y c, End 8 c=y c., End
c=., End
Principe
On conserve le principe de LR(0)
L’action à effectuer dépend du prochain jeton
Concrètement
Regarder le prochain jeton : ne pas le consommer !
En déduire l’action pour l’état courant
Effectuer l’action : même comportement que pour LR(0)
Recommencer à la première étape
Évaluer l’automate
s=.a End, End
End 3 s=a End., End
a=.b c, End 2 s=a.End, End
b=.b x, y a
b=., y 5 a=b c., End
1
b=.b x, End a=b.c, End c
b
b=., End b=b.x, y
b=.b x, x b=b.x, End x b=b x., y
4 6 b=b x., End
b=., x b=b.x, x
c=.y c, End b=b x., x
c=.,End y
y
c=y.c, End
c
7 c=.y c, End 8 c=y c., End
c=., End
a = x b x | x c y | y b y | y c x ;
b = z ;
c = z ;
a = x b x | x c y | y b y | y c x ;
b = z ;
c = z ;
a x
5 a=x b.x, End 6 a=x b x., End
s=.a End, End b
a=x.b x, End
a=.x b x, End
x a=x.c y, End c y
1 a=.x c y, End 4 7 a=x c.y, End 8 a=x c y., End
b=.z, x z
a=.y b y, End
c=.z, y
a=.y c x, End
b=z., x
y 9
c=z., y
a=y.b y, End y
b 11 a=y b.y, End 12 a=y b y., End
a=y.c x, End
10 c
b=.z, y x
c=.z, x z 13 a=y c.x, End 14 a=y c x., End
b=z., y
15
c=z., x
a x
5 a=x b.x, End 6 a=x b x., End
s=.a End, End b
a=x.b x, End
a=.x b x, End
x a=x.c y, End c y
1 a=.x c y, End 4 7 a=x c.y, End 8 a=x c y., End
b=.z, x z
a=.y b y, End
c=.z, y
a=.y c x, End
b=z., x
y 9
c=z., y
a=y.b y, End y
b 11 a=y b.y, End 12 a=y b y., End
a=y.c x, End
10 c
b=.z, y x
c=.z, x z 13 a=y c.x, End 14 a=y c x., End
b=z., y
15
c=z., x
a x
5 a=x b.x, End 6 a=x b x., End
s=.a End, End b
a=x.b x, End
a=.x b x, End
x a=x.c y, End c y
1 a=.x c y, End 4 7 a=x c.y, End 8 a=x c y., End
b=.z, x z
a=.y b y, End
c=.z, y
a=.y c x, End
b=z., x
y 9
c=z., y
a=y.b y, End y
b 11 a=y b.y, End 12 a=y b y., End
a=y.c x, End
10 c
b=.z, y x
c=.z, x z 13 a=y c.x, End 14 a=y c x., End
b=z., y
15
c=z., x
Problème
L’algo est valide si une seule action est possible par jeton de
prévision par état
L’automate construit ne l’est pas forcément
Solutions
Utiliser une analyse plus forte
Réécrire la grammaire
e = e ’+ ’ e | i ;
e = f x x | g x y ;
f = z ;
g = z ;
e = x e x | ;