SlideShare une entreprise Scribd logo
1  sur  76
ALF
Parser Bottom-Up
Bibliographie pour aujourd'hui
Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey
D. Ullman, Compilers: Principles, Techniques,
and Tools (2nd Edition)
– Chapitre 4
• 4.5
• 4.6
• 4.7.4
Contenu
• LR
• SLR
• LALR
Limor Fried
• Américain
• MIT
• Adafruit
• STEM
Slides
Partie de slides sont écrie par
Bogdan Nitulescu
Notation BNF
RFC 2616 HTTP/1.1 June 1999
HTTP-date = rfc1123-date | rfc850-date | asctime-date
rfc1123-date = wkday "," SP date1 SP time SP "GMT“
rfc850-date = weekday "," SP date2 SP time SP "GMT“
asctime-date = wkday SP date3 SP time SP 4DIGIT
date1 = 2DIGIT SP month SP 4DIGIT
; day month year (e.g., 02 Jun 1982)
date2 = 2DIGIT "-" month "-" 2DIGIT
; day-month-year (e.g., 02-Jun-82)
date3 = month SP ( 2DIGIT | ( SP 1DIGIT ))
; month day (e.g., Jun 2)
time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
; 00:00:00 - 23:59:59
wkday = "Mon" | "Tue" | "Wed“
| "Thu" | "Fri" | "Sat" | "Sun“
weekday = "Monday" | "Tuesday" | "Wednesday“
| "Thursday" | "Friday" | "Saturday" | "Sunday“
month = "Jan" | "Feb" | "Mar" | "Apr“
| "May" | "Jun" | "Jul" | "Aug"
| "Sep" | "Oct" | "Nov" | "Dec"
Arbre de dérivation / syntactique
E  E + E
E  E * E
E  n
n : [0-9]+
Lexer
2 * 3 + 4 * 5
n * n + n * n
2 3 4 5
parser
n n n n
2 3 4 5
* *
+
n n n n
2 3 4 5
E * E E * E
E + E
E
• jetons (tokens)
• Valeurs
• Grammaire
• Arbre de dérivation
• Arbre syntactique
Types d’analyse syntactique
 Descendent (top-down)
 Avec backtracking
 Prédictive
 Descendent récursive, LL avec un tableau
 Ascendant (bottom-up)
 Avec backtracking
 Shift-reduce
 LR(0),SLR,LALR, LR canonique
Parser LL, LR
 Nous devrions éviter backtracking
 Une grammaire qui permet le parser déterministe
 LL(k) lit left-to-right, dérivation left
 LR(k) lit left-to-right, dérivation right
 K – lookahead (combien de tokens sont lus)
 LL(k) < LR(k)
 L'algorithme est indépendant du langage, la
grammaire dépend du langage
Parser ascendant
Un analyseur vers le haut, ou shift-reduce commence à
« feuille » et construit vers le haut de l'arbre de dérivation
Étapes de réduction suivent une dérivation droite dans
l'ordre inverse
S  aABe
A  Abc | b
B  d
GRAMMAIRE:
Nous analysons la chaîne d'entrée abbcde.
(Aho,Sethi,Ullman, pp. 195)
Exemple de parser ascendant
a dbb cInput:
Parser ascendant
e Output:$
Production
S  aABe
A  Abc
A  b
B  d
(Aho,Sethi,Ullman, pp. 195)
S  aABe
Exemple de parser ascendant
a dbb c
Parser ascendant
e
A
b
$
S  aABe
A  Abc
A  b
B  d
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Exemple de parser ascendant
a dbA c
Parser ascendant
e
A
b
$
S  aABe
A  Abc
A  b
B  d
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Nous ne pouvons pas réduire, le parser s'arrêterait.
Exemple de parser ascendant
a dbA c
Parser ascendant
e
A
b
$
S  aABe
A  Abc
A  b
B  d
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Exemple de parser ascendant
a dbA c
Parser ascendant
e
A
b
$
S  aABe
A  Abc
A  b
B  d
c
A
b
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Exemple de parser ascendant
a dA
Parser ascendant
e
A c
A
b
$
S  aABe
A  Abc
A  b
B  d
b
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Exemple de parser ascendant
a dA
Parser ascendant
e
A c
A
b
$
S  aABe
A  Abc
A  b
B  d
b
B
d
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Exemple de parser ascendant
a BA
Parser ascendant
e
A c
A
b
$
S  aABe
A  Abc
A  b
B  d
b
B
d
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Exemple de parser ascendant
a BA
Parser ascendant
e
A c
A
b
$
S  aABe
A  Abc
A  b
B  d
b
B
d
a
S
e
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Exemple de parser ascendant
S
Parser ascendant A c
A
b
$
S  aABe
A  Abc
A  b
B  d
b
B
d
a
S
e
Parser LR parce que l'entrée este lu « De gauche à droite »,
et construit « dérivation plus à droite » dans l'ordre inverse.
(Aho,Sethi,Ullman, pp. 195)
Input: Output:
Production
Exemple de parser ascendant
• Analyse les productions pour le match
avec le texte d'entrée
• Backtracking
• Inefficace
• Pouvons-nous faire mieux?
Exemple de parser LR
Input
P
I
L
e
Algorithme de
parser LR
action goto
Output
(Aho,Sethi,Ullman, pp. 217)
Exemple de parser LR
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
GRAMMAIRE:
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
Il peut être parser avec les tableaux
‘action’ et ‘goto’
(Aho,Sethi,Ullman, pp. 219)
Exemple de parser LR
id idid+ Input: $
Pile: E0
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
Program de
parser LR
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
GRAMMAIRE:
Output:
(Aho,Sethi,Ullman, pp. 220)
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
E5
id
0
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
0
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
E3
F
0
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
0
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
E2
T
0
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
E7

2
T
0 T
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
E5
id
7

2
T
0
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
F
id
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
E7

2
T
0
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
F
id
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
E10
F
7

2
T
0
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
0
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
2
T
0 T
T F
F
id
id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
E
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
0
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
E
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
1
E
0
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
E
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
E6
+
1
E
0
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
E5
id
6
+
1
E
0
F
id
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
T
T F
F
id
id
E6
+
1
E
0
F
id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5 (Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
E3
F
6
+
1
E
0
F
id
T
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
T
T F
F
id
id
E6
+
1
E
0
F
id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5 (Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
E9
T
6
+
1
E
0
F
id
T
E
+
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
0
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
E
F
id
T
E
+
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
id idid + $
(1) E  E + T
(2) E’  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
T
T F
F
id
id
E1
E
0 F
id
T
E
+
(Aho,Sethi,Ullman, pp. 220)
Exemple de parser LR
Input:
Pile:
Program de
parser LR
GRAMMAIRE:
Output:
Construction de tableaux de parser
• Tous les analyseurs utilisent le même algorithme
• La différence est dans l'action et goto
• Simple LR (SLR) se poursuit moins la grammaire, mais il est le plus
facile à mettre en œuvre
• Canonique LR allez sur la plupart de grammaire, mais il est plus
difficile à mettre en œuvre.
• LR Lookahead (LALR) va sur la plupart des constructions syntaxiques
utilisées dans les langages de programmation, mais produire des
tableaux beaucoup plus petites que Canonique LR.
Shift-Reduce
 Actions:
 Une séquence de shift et reduce
 Pile
 Règles et jetons
 Etat
 Etat suivante
 Pile et input
Actions Shift-Reduce
 Shift: push lookahead
 Reduce: pop , push X
pile input action
( 1+2+(3+4))+5 shift 1
(1 +2+(3+4))+5
pile input action
(S+E +(3+4))+5 reduce S  S+ E
(S +(3+4))+5
Analyse Shift-Reduce
derivation pile input action
(1+2+(3+4))+5 (1+2+(3+4))+5 shift
(1+2+(3+4))+5 ( 1+2+(3+4))+5 shift
(1+2+(3+4))+5 (1 +2+(3+4))+5 reduce Enum
(E+2+(3+4))+5 (E +2+(3+4))+5 reduce SE
(S+2+(3+4))+5 (S +2+(3+4))+5 shift
(S+2+(3+4))+5 (S+ 2+(3+4))+5 shift
(S+2+(3+4))+5 (S+2 +(3+4))+5 reduce Enum
(S+E+(3+4))+5 (S+E +(3+4))+5 reduce SS+E
(S+(3+4))+5 (S +(3+4))+5 shift
(S+(3+4))+5 (S+ (3+4))+5 shift
(S+(3+4))+5 (S+( 3+4))+5 shift
(S+(3+4))+5 (S+(3 +4))+5 reduce E num
...
S  S + E | E
E  num | (S)
Sélection de l'action
 Comment sélectionnons-nous l'action?
 shift ou reduce?
 Quelle règle s'appliquons-nous?
 Éviter le blocage
 La pile peut être réduite de plusieurs façons
Sélection de l'action
 Starea curenta:
 Pile 
 Symbole look-ahead b
 existe la production X  , et la pile est de forme  = 
 Le parser devrait:
 Shift b sur la pile, b ?
 Reduce avec la production X  , si la pile est  = , et
transformer la pile en X ?
 Décision fondée su b et le préfix 
  est différent pour différentes productions, parce que le
côté droit de productions (les ) peuvent avoir des
longueurs différentes
Algorithme de parser LR
action gotoState
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
GRAMMAIRE:
(1) E  E + T
(2) E  T
(3) T  T  F
(4) T  F
(5) F  ( E )
(6) F  id
Tableau de parser LR
 Algorithme: état curent S si token C au input
 Si Action[S,C] = s(S’) shift et aller sur S’:
 push(C), push(S’)
 Si Action[S,C] = X  reduce:
 pop(2*||), S’= top(), push(X), push(Goto[S’,X])
Action et
etat prochaine
Etat prochaine
Jeton (Token) Regle
Etat
Tableau ‘action’ Tableau‘Goto’
Element LR(0)
Règle
E  num | (S)
Deux elements LR(0)
E  num .
E  ( . S )
Exemple de etat LR(0)
 Un element LR(0) est une production avec un “.”
dans la partie droite
 Les éléments avant “.” sont sur la pile
 Les elements apres “.” nous dire ce que nous
pouvions lire suivante
E  num .
E  ( . S)
etat
element
Grammaire LR(0)
 Grammaire de liste
 S  (L) | id
 L  S | L,S
 Exemple:
 (a,b,c)
 ((a,b), (c,d), (e,f))
 (a, (b,c,d), ((f,g)))
S
( L )
L , S
L , S
( L )S
a L , S
S
b
c
d
Arbre de derivation pour
(a, (b,c), d)
Etat de start et Closure
 Etat de start
 Production S’  S
 Etat start: Closure (S’  . S )
 Closure pour l’ensable des éléments LR(0):
 Closure (S) = S
 Pour toutes les éléments en S:
 X   . Y 
 Toutes les éléments LR(0) de forme Y  . , pour
toutes les production Y   de la grammaire
Exemple
S  (L) | id
L  S | L,S
Elément LR(0) “start”
S’  . S
Closure
S’  . S
S  . (L)
S  . id
Goto
 Transitions entre les états (ensembles des éléments
LR(0) )
S’  . S
S  . (L)
S  . id
Goto(I, ‘(‘) Closure ( { S  ( . L) } )
Questions
I = { [E’  . E]}, Closure (I) = ??
I = { [E’  E . ], [E  E . + T] }, Goto(I,+) = ??
E’  E
E  E + T | T
T  T * F | F
F  (E) | id
Goto: tokens
S’  . S
S  . (L)
S  . id
S  ( . L)
L  . S
L  . L, S
S  . (L)
S  . id
S  id .
id
(
id (
Grammaire
S  (L) | id
L  S | L,S
Goto: règles
S’  . S
S  . (L)
S  . id
S  ( . L)
L  . S
L  . L, S
S  . (L)
S  . id
S  id .
id
(
id (
Grammaire
S  (L) | id
L  S | L,S
S  (L . )
L  L . , S
L  S .
L
S
Reduce
S’  . S
S  . (L)
S  . id
S  ( . L)
L  . S
L  . L, S
S  . (L)
S  . id
S  id .
id
(
id (
Grammaire
S  (L) | id
L  S | L,S
S  (L . )L
L  L . , S
L  S .
L
S
Etats de reduce
(le point e a la fin!)
Automate LR
S’  . S
S  . (L)
S  . id
S  ( . L)
L  . S
L  . L, S
S  . (L)
S  . id
S  id .id
(
id
(
S  (L . )
L  L . , S
L  S .
S
L  L , . S
S  . (L)
S  . id
L  L,S .
S  (L) .
S’  S .
Etat finale / accepte
1 2 8 9
6
5
3
74
S
,
)
S
$
id
L
Grammaire
S  (L) | id
L  S | L,S
(
Exemple de parser ((a),b)
dérivation pile input action
((a),b)  1 ((a),b) shift, goto 3
((a),b)  1(3 (a),b) shift, goto 3
((a),b)  1(3(3 a),b) shift, goto 2
((a),b)  1(3(3a2 ),b) reduce Sid
((S),b)  1(3(3(S7 ),b) reduce LS
((L),b)  1(3(3(L5 ),b) shift, goto 6
((L),b)  1(3(3L5)6 ,b) reduce S(L)
(S,b)  1(3S7 ,b) reduce LS
(L,b)  1(3L5 ,b) shift, goto 8
(L,b)  1(3L5,8 b) shift, goto 9
(L,b)  1(3L5,8b2 ) reduce Sid
(L,S)  1(3L8,S9 ) reduce LL,S
(L)  1(3L5 ) shift, goto 6
(L)  1(3L5)6 reduce S(L)
S  1S4 $ accepte
S  (L) | id
L  S | L,S
Tableau de parser LR(0)
 Etats = états de Automate LR
 S  S’ avec jeton C:
 Action[S,C] += Shift(S’)
 S  S’ avec la règle N:
 Goto[S,N] += S’
 Si S est état de réduction X  .:
 Action[S,*] += Reduce(X  )
Exemple de tableau
( ) id , $ S L
1 s3 s2 g4
2 Sid Sid Sid Sid Sid
3 s3 s2 g7 g5
4 accept
5 s6 s8
6 S(L) S(L) S(L) S(L) S(L)
7 LS LS LS LS LS
8 s3 s2 g9
9 LL,S LL,S LL,S LL,S LL,S
Etat
Jetons Regles
reduceshift
Limites LR(0)
 Une action pou
L  L , S .
L  L , S .
S  S . , L
L  S , L .
L  S .
OK shift/reduce reduce/reduce
num + $ E S
1 s4 g2 g6
2 SE s3/SE SE
Exemple de shift/reduce
S’  . S
S  .E + S
S  . E
E  .num
E  num .
S  E . +S
S  E .
E
num
+
S  E + S .
accept
S
S  E + . S
S  . E + S
S  . E
E  . num
S’  S .
1 2
5
3
7
4
S Grammaire
S  E + S | E
E  num
$
E
num
Shift ou
reduce
etat 2?
Parser SLR
 SLR =“Simple LR”= extension simple LR(0)
 Pour X  , le prochain jeton C
 reduce seulement si C est de FOLLOW(X)
 Résoudre partial les shift/reduce
 Identique avec LR(0) sauf ‘reduction’
 Réduction X   seulement les symboles de FOLLOW(X)
num + $ E S
1 s4 g2 g6
2 s3 SE
Exemple: FOLLOW(S) = {$}
Exemple de parser SLR
num + $ E S
1 s4 g2 g6
2 s3 SE
3 s4 g2 g5
4 Enum Enum
5 SE+S
6 s7
7 accept
Grammaire
S  E + S | E
E  num
Automate SLR
S’  . S
S  .E + S
S  . E
E  .num
E  num .
“+” “$” : reduce
S  E . +S
S  E .
“$” : reduce
E
num
+
S  E + S .
“$” : reduce
accept
S
S  E + . S
S  . E + S
S  . E
E  . num
S’  S .
1 2
5
3
7
4 S Grammaire
S  E + S | E
E  num
$
E
num
num + $ E S
1 s4 g2 g6
2 s3 SE
LALR(1)
 SLR: Decizia de reduce pentru “X->” se face doar daca urmatorul
simbol e in FOLLOW(X)
 LALR(1): Fiecare productie are un set de simboli care ii pot urma.
“X-> , S” se reduce doar daca urmatorul simbol e in S.
 Seturile se pot calcula prin aplicarea recursiva a regulilor:
S’ .S, {$}
B α.Aβ
c in FIRST(β)
A  .Xδ , {c}
B  α.Aβ , {c}
c  FOLLOW(β)
β * ε
A  .Xδ , {c}
X α.Xβ , c
X  αX.β , c
S’  .S , {$}
S  .L=R
S  .R
L  .*R
L  .ident
R  .L
S’  .S , {$}
S  .L=R
S  .R
L  .*R , {=}
L  .ident , {=}
R  .L
S’  .S , {$}
S  .L=R , {$}
S  .R , {$}
L  .*R , {= $}
L  .ident , {=
$}
R  .L , {$}
L  .ident, {= $}
L  ident. , {= $}
DFA pentru LALR
•
S  L = R
S  R
L  *R
L  ident
R  L
S’  .S {$}
S  .L=R {$}
S  .R {$}
L  .*R {= $}
L  .ident {= $}
R  .L {$}
S’  S. {$}
“$” : accept
S  L.=R {$}
R  L. {$}
“$” : reduce
S  R. {$}
“$” : reduce
L  *.R {=$}
L  .*R {=$}
L  .ident {=$}
R  .L {=$}
L  ident. {$ =}
“$” “=“ : reduce
S  L=.R {$}
L  .*R {$}
L  .ident {$}
R  .L {$}
L  *R. {$ =}
“$” “=“ : reduce
R  L. {=$}
“$” “=“ : reduce
S  L=R. {$}
“$” : reduce
S
L
L
L
R
R
R
*
*
ident
ident
*
ident
=
Sujets
• LR
• SLR
• LALR
Questions

Contenu connexe

Tendances

ALF 10 - Convention d'appel de fonction
ALF 10 - Convention d'appel de fonctionALF 10 - Convention d'appel de fonction
ALF 10 - Convention d'appel de fonctionAlexandru Radovici
 
ALF 7 - Arbre de syntaxe abstraite
ALF 7 - Arbre de syntaxe abstraiteALF 7 - Arbre de syntaxe abstraite
ALF 7 - Arbre de syntaxe abstraiteAlexandru Radovici
 
ALF 3 - Expressions régulières (2018)
ALF 3 - Expressions régulières (2018)ALF 3 - Expressions régulières (2018)
ALF 3 - Expressions régulières (2018)Alexandru Radovici
 
ALF 7 - Arbre de syntaxe abstraite
ALF 7 - Arbre de syntaxe abstraiteALF 7 - Arbre de syntaxe abstraite
ALF 7 - Arbre de syntaxe abstraiteAlexandru Radovici
 
ALF 11 - Diagramme de flux de contrôle et WebAssembly
ALF 11 - Diagramme de flux de contrôle et WebAssemblyALF 11 - Diagramme de flux de contrôle et WebAssembly
ALF 11 - Diagramme de flux de contrôle et WebAssemblyAlexandru Radovici
 
Le décodage Kaldi/FST pour la reconnaissance de l'écriture
Le décodage Kaldi/FST pour la reconnaissance de l'écriture Le décodage Kaldi/FST pour la reconnaissance de l'écriture
Le décodage Kaldi/FST pour la reconnaissance de l'écriture Wassim Swaileh
 
Monitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaMonitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaPatrick Allaert
 
Coat::Persistent at FPW2009
Coat::Persistent at FPW2009Coat::Persistent at FPW2009
Coat::Persistent at FPW2009Alexis Sukrieh
 
ALF 8 - Représentation des données
ALF 8 - Représentation des donnéesALF 8 - Représentation des données
ALF 8 - Représentation des donnéesAlexandru Radovici
 
Annotation Java vs. Decorator Python
Annotation Java vs. Decorator PythonAnnotation Java vs. Decorator Python
Annotation Java vs. Decorator PythonDidier Plaindoux
 

Tendances (20)

ALF 8 - Generation du code
ALF 8 - Generation du codeALF 8 - Generation du code
ALF 8 - Generation du code
 
ALF 11 - WebAssembly
ALF 11 - WebAssemblyALF 11 - WebAssembly
ALF 11 - WebAssembly
 
ALF 8 - Generation de code
ALF 8 - Generation de codeALF 8 - Generation de code
ALF 8 - Generation de code
 
ALF 12 - Optimisations
ALF 12 - OptimisationsALF 12 - Optimisations
ALF 12 - Optimisations
 
ALF 10 - Convention d'appel de fonction
ALF 10 - Convention d'appel de fonctionALF 10 - Convention d'appel de fonction
ALF 10 - Convention d'appel de fonction
 
ALF 4 - Grammaires
ALF 4 - GrammairesALF 4 - Grammaires
ALF 4 - Grammaires
 
ALF 7 - Arbre de syntaxe abstraite
ALF 7 - Arbre de syntaxe abstraiteALF 7 - Arbre de syntaxe abstraite
ALF 7 - Arbre de syntaxe abstraite
 
ALF 3 - Expressions régulières (2018)
ALF 3 - Expressions régulières (2018)ALF 3 - Expressions régulières (2018)
ALF 3 - Expressions régulières (2018)
 
ALF 7 - Arbre de syntaxe abstraite
ALF 7 - Arbre de syntaxe abstraiteALF 7 - Arbre de syntaxe abstraite
ALF 7 - Arbre de syntaxe abstraite
 
ALF 1 - Automates finis
ALF 1 - Automates finis ALF 1 - Automates finis
ALF 1 - Automates finis
 
ALF 11 - Diagramme de flux de contrôle et WebAssembly
ALF 11 - Diagramme de flux de contrôle et WebAssemblyALF 11 - Diagramme de flux de contrôle et WebAssembly
ALF 11 - Diagramme de flux de contrôle et WebAssembly
 
ALF 9 - Generation de code
ALF 9 - Generation de codeALF 9 - Generation de code
ALF 9 - Generation de code
 
ALF 2 - Automates Fini (2018)
ALF 2 - Automates Fini (2018)ALF 2 - Automates Fini (2018)
ALF 2 - Automates Fini (2018)
 
Le décodage Kaldi/FST pour la reconnaissance de l'écriture
Le décodage Kaldi/FST pour la reconnaissance de l'écriture Le décodage Kaldi/FST pour la reconnaissance de l'écriture
Le décodage Kaldi/FST pour la reconnaissance de l'écriture
 
R for data analysis
R for data analysisR for data analysis
R for data analysis
 
Monitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et PinbaMonitoring d'applications/environnements PHP: APM et Pinba
Monitoring d'applications/environnements PHP: APM et Pinba
 
Coat::Persistent at FPW2009
Coat::Persistent at FPW2009Coat::Persistent at FPW2009
Coat::Persistent at FPW2009
 
ALF 8 - Représentation des données
ALF 8 - Représentation des donnéesALF 8 - Représentation des données
ALF 8 - Représentation des données
 
Annotation Java vs. Decorator Python
Annotation Java vs. Decorator PythonAnnotation Java vs. Decorator Python
Annotation Java vs. Decorator Python
 
ALF - Introduction (2018)
ALF - Introduction (2018)ALF - Introduction (2018)
ALF - Introduction (2018)
 

Plus de Alexandru Radovici (20)

SdE2 - Pilot Tock
SdE2 - Pilot TockSdE2 - Pilot Tock
SdE2 - Pilot Tock
 
SdE2 - Systèmes embarquées
SdE2 - Systèmes embarquéesSdE2 - Systèmes embarquées
SdE2 - Systèmes embarquées
 
SdE2 - Planification, IPC
SdE2 - Planification, IPCSdE2 - Planification, IPC
SdE2 - Planification, IPC
 
ALF1 - Introduction
ALF1 - IntroductionALF1 - Introduction
ALF1 - Introduction
 
SdE2 - Introduction
SdE2 - IntroductionSdE2 - Introduction
SdE2 - Introduction
 
MDAD 6 - AIDL and Services
MDAD 6 - AIDL and ServicesMDAD 6 - AIDL and Services
MDAD 6 - AIDL and Services
 
MDAD 5 - Threads
MDAD 5 - ThreadsMDAD 5 - Threads
MDAD 5 - Threads
 
MDAD 4 - Lists, adapters and recycling
MDAD 4 - Lists, adapters and recyclingMDAD 4 - Lists, adapters and recycling
MDAD 4 - Lists, adapters and recycling
 
MDAD 3 - Basics of UI Applications
MDAD 3 - Basics of UI ApplicationsMDAD 3 - Basics of UI Applications
MDAD 3 - Basics of UI Applications
 
MDAD 2 - Introduction to the Android Framework
MDAD 2 - Introduction to the Android FrameworkMDAD 2 - Introduction to the Android Framework
MDAD 2 - Introduction to the Android Framework
 
MDAD 1 - Hardware
MDAD 1 - HardwareMDAD 1 - Hardware
MDAD 1 - Hardware
 
MDAD 0 - Introduction
MDAD 0 - IntroductionMDAD 0 - Introduction
MDAD 0 - Introduction
 
SdE 11 - Reseau
SdE 11 - ReseauSdE 11 - Reseau
SdE 11 - Reseau
 
SdE 10 - Threads
SdE 10 - ThreadsSdE 10 - Threads
SdE 10 - Threads
 
SdE 8 - Synchronisation de execution
SdE 8 - Synchronisation de executionSdE 8 - Synchronisation de execution
SdE 8 - Synchronisation de execution
 
SdE 8 - Memoire Virtuelle
SdE 8 - Memoire VirtuelleSdE 8 - Memoire Virtuelle
SdE 8 - Memoire Virtuelle
 
SdE 7 - Gestion de la Mémoire
SdE 7 - Gestion de la MémoireSdE 7 - Gestion de la Mémoire
SdE 7 - Gestion de la Mémoire
 
SdE 6 - Planification
SdE 6 - PlanificationSdE 6 - Planification
SdE 6 - Planification
 
SdE 5 - Planification
SdE 5 - PlanificationSdE 5 - Planification
SdE 5 - Planification
 
SdE2 4 - Processus
SdE2 4 - ProcessusSdE2 4 - Processus
SdE2 4 - Processus
 

Dernier

A3iFormations, organisme de formations certifié qualiopi.
A3iFormations, organisme de formations certifié qualiopi.A3iFormations, organisme de formations certifié qualiopi.
A3iFormations, organisme de formations certifié qualiopi.Franck Apolis
 
Annie Ernaux Extérieurs. pptx. Exposition basée sur un livre .
Annie   Ernaux  Extérieurs. pptx. Exposition basée sur un livre .Annie   Ernaux  Extérieurs. pptx. Exposition basée sur un livre .
Annie Ernaux Extérieurs. pptx. Exposition basée sur un livre .Txaruka
 
Cours SE Gestion des périphériques - IG IPSET
Cours SE Gestion des périphériques - IG IPSETCours SE Gestion des périphériques - IG IPSET
Cours SE Gestion des périphériques - IG IPSETMedBechir
 
Formation M2i - Comprendre les neurosciences pour développer son leadership
Formation M2i - Comprendre les neurosciences pour développer son leadershipFormation M2i - Comprendre les neurosciences pour développer son leadership
Formation M2i - Comprendre les neurosciences pour développer son leadershipM2i Formation
 
Saint Georges, martyr, et la lègend du dragon.pptx
Saint Georges, martyr, et la lègend du dragon.pptxSaint Georges, martyr, et la lègend du dragon.pptx
Saint Georges, martyr, et la lègend du dragon.pptxMartin M Flynn
 
systeme expert_systeme expert_systeme expert
systeme expert_systeme expert_systeme expertsysteme expert_systeme expert_systeme expert
systeme expert_systeme expert_systeme expertChristianMbip
 
7 PPT sue le project de fin d'étude.pptx
7 PPT sue le project de fin d'étude.pptx7 PPT sue le project de fin d'étude.pptx
7 PPT sue le project de fin d'étude.pptxrababouerdighi
 
Fondation Louis Vuitton. pptx
Fondation      Louis      Vuitton.   pptxFondation      Louis      Vuitton.   pptx
Fondation Louis Vuitton. pptxTxaruka
 
Evaluation du systeme d'Education. Marocpptx
Evaluation du systeme d'Education. MarocpptxEvaluation du systeme d'Education. Marocpptx
Evaluation du systeme d'Education. MarocpptxAsmaa105193
 
Guide Final de rédaction de mémoire de fin d'étude
Guide Final de rédaction de mémoire de fin d'étudeGuide Final de rédaction de mémoire de fin d'étude
Guide Final de rédaction de mémoire de fin d'étudeBenamraneMarwa
 
Cours SE Le système Linux : La ligne de commande bash - IG IPSET
Cours SE Le système Linux : La ligne de commande bash - IG IPSETCours SE Le système Linux : La ligne de commande bash - IG IPSET
Cours SE Le système Linux : La ligne de commande bash - IG IPSETMedBechir
 
Présentation_ Didactique 1_SVT (S4) complet.pptx
Présentation_ Didactique 1_SVT (S4) complet.pptxPrésentation_ Didactique 1_SVT (S4) complet.pptx
Présentation_ Didactique 1_SVT (S4) complet.pptxrababouerdighi
 
666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx
666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx
666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptxSAID MASHATE
 

Dernier (15)

A3iFormations, organisme de formations certifié qualiopi.
A3iFormations, organisme de formations certifié qualiopi.A3iFormations, organisme de formations certifié qualiopi.
A3iFormations, organisme de formations certifié qualiopi.
 
Annie Ernaux Extérieurs. pptx. Exposition basée sur un livre .
Annie   Ernaux  Extérieurs. pptx. Exposition basée sur un livre .Annie   Ernaux  Extérieurs. pptx. Exposition basée sur un livre .
Annie Ernaux Extérieurs. pptx. Exposition basée sur un livre .
 
Cours SE Gestion des périphériques - IG IPSET
Cours SE Gestion des périphériques - IG IPSETCours SE Gestion des périphériques - IG IPSET
Cours SE Gestion des périphériques - IG IPSET
 
Evaluación Alumnos de Ecole Victor Hugo
Evaluación Alumnos de Ecole  Victor HugoEvaluación Alumnos de Ecole  Victor Hugo
Evaluación Alumnos de Ecole Victor Hugo
 
Formation M2i - Comprendre les neurosciences pour développer son leadership
Formation M2i - Comprendre les neurosciences pour développer son leadershipFormation M2i - Comprendre les neurosciences pour développer son leadership
Formation M2i - Comprendre les neurosciences pour développer son leadership
 
Saint Georges, martyr, et la lègend du dragon.pptx
Saint Georges, martyr, et la lègend du dragon.pptxSaint Georges, martyr, et la lègend du dragon.pptx
Saint Georges, martyr, et la lègend du dragon.pptx
 
systeme expert_systeme expert_systeme expert
systeme expert_systeme expert_systeme expertsysteme expert_systeme expert_systeme expert
systeme expert_systeme expert_systeme expert
 
Pâques de Sainte Marie-Euphrasie Pelletier
Pâques de Sainte Marie-Euphrasie PelletierPâques de Sainte Marie-Euphrasie Pelletier
Pâques de Sainte Marie-Euphrasie Pelletier
 
7 PPT sue le project de fin d'étude.pptx
7 PPT sue le project de fin d'étude.pptx7 PPT sue le project de fin d'étude.pptx
7 PPT sue le project de fin d'étude.pptx
 
Fondation Louis Vuitton. pptx
Fondation      Louis      Vuitton.   pptxFondation      Louis      Vuitton.   pptx
Fondation Louis Vuitton. pptx
 
Evaluation du systeme d'Education. Marocpptx
Evaluation du systeme d'Education. MarocpptxEvaluation du systeme d'Education. Marocpptx
Evaluation du systeme d'Education. Marocpptx
 
Guide Final de rédaction de mémoire de fin d'étude
Guide Final de rédaction de mémoire de fin d'étudeGuide Final de rédaction de mémoire de fin d'étude
Guide Final de rédaction de mémoire de fin d'étude
 
Cours SE Le système Linux : La ligne de commande bash - IG IPSET
Cours SE Le système Linux : La ligne de commande bash - IG IPSETCours SE Le système Linux : La ligne de commande bash - IG IPSET
Cours SE Le système Linux : La ligne de commande bash - IG IPSET
 
Présentation_ Didactique 1_SVT (S4) complet.pptx
Présentation_ Didactique 1_SVT (S4) complet.pptxPrésentation_ Didactique 1_SVT (S4) complet.pptx
Présentation_ Didactique 1_SVT (S4) complet.pptx
 
666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx
666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx
666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx
 

ALF 6 - Parser Bottom-Up

  • 2. Bibliographie pour aujourd'hui Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, Compilers: Principles, Techniques, and Tools (2nd Edition) – Chapitre 4 • 4.5 • 4.6 • 4.7.4
  • 4. Limor Fried • Américain • MIT • Adafruit • STEM
  • 5. Slides Partie de slides sont écrie par Bogdan Nitulescu
  • 6. Notation BNF RFC 2616 HTTP/1.1 June 1999 HTTP-date = rfc1123-date | rfc850-date | asctime-date rfc1123-date = wkday "," SP date1 SP time SP "GMT“ rfc850-date = weekday "," SP date2 SP time SP "GMT“ asctime-date = wkday SP date3 SP time SP 4DIGIT date1 = 2DIGIT SP month SP 4DIGIT ; day month year (e.g., 02 Jun 1982) date2 = 2DIGIT "-" month "-" 2DIGIT ; day-month-year (e.g., 02-Jun-82) date3 = month SP ( 2DIGIT | ( SP 1DIGIT )) ; month day (e.g., Jun 2) time = 2DIGIT ":" 2DIGIT ":" 2DIGIT ; 00:00:00 - 23:59:59 wkday = "Mon" | "Tue" | "Wed“ | "Thu" | "Fri" | "Sat" | "Sun“ weekday = "Monday" | "Tuesday" | "Wednesday“ | "Thursday" | "Friday" | "Saturday" | "Sunday“ month = "Jan" | "Feb" | "Mar" | "Apr“ | "May" | "Jun" | "Jul" | "Aug" | "Sep" | "Oct" | "Nov" | "Dec"
  • 7. Arbre de dérivation / syntactique E  E + E E  E * E E  n n : [0-9]+ Lexer 2 * 3 + 4 * 5 n * n + n * n 2 3 4 5 parser n n n n 2 3 4 5 * * + n n n n 2 3 4 5 E * E E * E E + E E • jetons (tokens) • Valeurs • Grammaire • Arbre de dérivation • Arbre syntactique
  • 8. Types d’analyse syntactique  Descendent (top-down)  Avec backtracking  Prédictive  Descendent récursive, LL avec un tableau  Ascendant (bottom-up)  Avec backtracking  Shift-reduce  LR(0),SLR,LALR, LR canonique
  • 9. Parser LL, LR  Nous devrions éviter backtracking  Une grammaire qui permet le parser déterministe  LL(k) lit left-to-right, dérivation left  LR(k) lit left-to-right, dérivation right  K – lookahead (combien de tokens sont lus)  LL(k) < LR(k)  L'algorithme est indépendant du langage, la grammaire dépend du langage
  • 10. Parser ascendant Un analyseur vers le haut, ou shift-reduce commence à « feuille » et construit vers le haut de l'arbre de dérivation Étapes de réduction suivent une dérivation droite dans l'ordre inverse S  aABe A  Abc | b B  d GRAMMAIRE: Nous analysons la chaîne d'entrée abbcde. (Aho,Sethi,Ullman, pp. 195)
  • 11. Exemple de parser ascendant a dbb cInput: Parser ascendant e Output:$ Production S  aABe A  Abc A  b B  d (Aho,Sethi,Ullman, pp. 195) S  aABe
  • 12. Exemple de parser ascendant a dbb c Parser ascendant e A b $ S  aABe A  Abc A  b B  d (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 13. Exemple de parser ascendant a dbA c Parser ascendant e A b $ S  aABe A  Abc A  b B  d (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 14. Nous ne pouvons pas réduire, le parser s'arrêterait. Exemple de parser ascendant a dbA c Parser ascendant e A b $ S  aABe A  Abc A  b B  d (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 15. Exemple de parser ascendant a dbA c Parser ascendant e A b $ S  aABe A  Abc A  b B  d c A b (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 16. Exemple de parser ascendant a dA Parser ascendant e A c A b $ S  aABe A  Abc A  b B  d b (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 17. Exemple de parser ascendant a dA Parser ascendant e A c A b $ S  aABe A  Abc A  b B  d b B d (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 18. Exemple de parser ascendant a BA Parser ascendant e A c A b $ S  aABe A  Abc A  b B  d b B d (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 19. Exemple de parser ascendant a BA Parser ascendant e A c A b $ S  aABe A  Abc A  b B  d b B d a S e (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 20. Exemple de parser ascendant S Parser ascendant A c A b $ S  aABe A  Abc A  b B  d b B d a S e Parser LR parce que l'entrée este lu « De gauche à droite », et construit « dérivation plus à droite » dans l'ordre inverse. (Aho,Sethi,Ullman, pp. 195) Input: Output: Production
  • 21. Exemple de parser ascendant • Analyse les productions pour le match avec le texte d'entrée • Backtracking • Inefficace • Pouvons-nous faire mieux?
  • 22. Exemple de parser LR Input P I L e Algorithme de parser LR action goto Output (Aho,Sethi,Ullman, pp. 217)
  • 23. Exemple de parser LR action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 GRAMMAIRE: (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id Il peut être parser avec les tableaux ‘action’ et ‘goto’ (Aho,Sethi,Ullman, pp. 219)
  • 24. Exemple de parser LR id idid+ Input: $ Pile: E0 (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id Program de parser LR action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 GRAMMAIRE: Output: (Aho,Sethi,Ullman, pp. 220)
  • 25. id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id E5 id 0 action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 26. 0 id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 27. E3 F 0 id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 28. 0 id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 29. id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id E2 T 0 action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 30. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 E7  2 T 0 T F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 31. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id E5 id 7  2 T 0 action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T F id F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 32. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id E7  2 T 0 action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T F id F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 33. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id E10 F 7  2 T 0 action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 34. 0 id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 35. id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id 2 T 0 T T F F id id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 E (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 36. 0 id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id E (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 37. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id 1 E 0 action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id E (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 38. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id E6 + 1 E 0 (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 39. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id E5 id 6 + 1 E 0 F id (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 40. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id T T F F id id E6 + 1 E 0 F id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 41. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id E3 F 6 + 1 E 0 F id T (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 42. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id T T F F id id E6 + 1 E 0 F id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 43. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id E9 T 6 + 1 E 0 F id T E + (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 44. id idid + $ (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id 0 action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id E F id T E + (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 45. id idid + $ (1) E  E + T (2) E’  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 T T F F id id E1 E 0 F id T E + (Aho,Sethi,Ullman, pp. 220) Exemple de parser LR Input: Pile: Program de parser LR GRAMMAIRE: Output:
  • 46. Construction de tableaux de parser • Tous les analyseurs utilisent le même algorithme • La différence est dans l'action et goto • Simple LR (SLR) se poursuit moins la grammaire, mais il est le plus facile à mettre en œuvre • Canonique LR allez sur la plupart de grammaire, mais il est plus difficile à mettre en œuvre. • LR Lookahead (LALR) va sur la plupart des constructions syntaxiques utilisées dans les langages de programmation, mais produire des tableaux beaucoup plus petites que Canonique LR.
  • 47. Shift-Reduce  Actions:  Une séquence de shift et reduce  Pile  Règles et jetons  Etat  Etat suivante  Pile et input
  • 48. Actions Shift-Reduce  Shift: push lookahead  Reduce: pop , push X pile input action ( 1+2+(3+4))+5 shift 1 (1 +2+(3+4))+5 pile input action (S+E +(3+4))+5 reduce S  S+ E (S +(3+4))+5
  • 49. Analyse Shift-Reduce derivation pile input action (1+2+(3+4))+5 (1+2+(3+4))+5 shift (1+2+(3+4))+5 ( 1+2+(3+4))+5 shift (1+2+(3+4))+5 (1 +2+(3+4))+5 reduce Enum (E+2+(3+4))+5 (E +2+(3+4))+5 reduce SE (S+2+(3+4))+5 (S +2+(3+4))+5 shift (S+2+(3+4))+5 (S+ 2+(3+4))+5 shift (S+2+(3+4))+5 (S+2 +(3+4))+5 reduce Enum (S+E+(3+4))+5 (S+E +(3+4))+5 reduce SS+E (S+(3+4))+5 (S +(3+4))+5 shift (S+(3+4))+5 (S+ (3+4))+5 shift (S+(3+4))+5 (S+( 3+4))+5 shift (S+(3+4))+5 (S+(3 +4))+5 reduce E num ... S  S + E | E E  num | (S)
  • 50. Sélection de l'action  Comment sélectionnons-nous l'action?  shift ou reduce?  Quelle règle s'appliquons-nous?  Éviter le blocage  La pile peut être réduite de plusieurs façons
  • 51. Sélection de l'action  Starea curenta:  Pile   Symbole look-ahead b  existe la production X  , et la pile est de forme  =   Le parser devrait:  Shift b sur la pile, b ?  Reduce avec la production X  , si la pile est  = , et transformer la pile en X ?  Décision fondée su b et le préfix    est différent pour différentes productions, parce que le côté droit de productions (les ) peuvent avoir des longueurs différentes
  • 52. Algorithme de parser LR action gotoState id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 GRAMMAIRE: (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  ( E ) (6) F  id
  • 53. Tableau de parser LR  Algorithme: état curent S si token C au input  Si Action[S,C] = s(S’) shift et aller sur S’:  push(C), push(S’)  Si Action[S,C] = X  reduce:  pop(2*||), S’= top(), push(X), push(Goto[S’,X]) Action et etat prochaine Etat prochaine Jeton (Token) Regle Etat Tableau ‘action’ Tableau‘Goto’
  • 54. Element LR(0) Règle E  num | (S) Deux elements LR(0) E  num . E  ( . S )
  • 55. Exemple de etat LR(0)  Un element LR(0) est une production avec un “.” dans la partie droite  Les éléments avant “.” sont sur la pile  Les elements apres “.” nous dire ce que nous pouvions lire suivante E  num . E  ( . S) etat element
  • 56. Grammaire LR(0)  Grammaire de liste  S  (L) | id  L  S | L,S  Exemple:  (a,b,c)  ((a,b), (c,d), (e,f))  (a, (b,c,d), ((f,g))) S ( L ) L , S L , S ( L )S a L , S S b c d Arbre de derivation pour (a, (b,c), d)
  • 57. Etat de start et Closure  Etat de start  Production S’  S  Etat start: Closure (S’  . S )  Closure pour l’ensable des éléments LR(0):  Closure (S) = S  Pour toutes les éléments en S:  X   . Y   Toutes les éléments LR(0) de forme Y  . , pour toutes les production Y   de la grammaire
  • 58. Exemple S  (L) | id L  S | L,S Elément LR(0) “start” S’  . S Closure S’  . S S  . (L) S  . id
  • 59. Goto  Transitions entre les états (ensembles des éléments LR(0) ) S’  . S S  . (L) S  . id Goto(I, ‘(‘) Closure ( { S  ( . L) } )
  • 60. Questions I = { [E’  . E]}, Closure (I) = ?? I = { [E’  E . ], [E  E . + T] }, Goto(I,+) = ?? E’  E E  E + T | T T  T * F | F F  (E) | id
  • 61. Goto: tokens S’  . S S  . (L) S  . id S  ( . L) L  . S L  . L, S S  . (L) S  . id S  id . id ( id ( Grammaire S  (L) | id L  S | L,S
  • 62. Goto: règles S’  . S S  . (L) S  . id S  ( . L) L  . S L  . L, S S  . (L) S  . id S  id . id ( id ( Grammaire S  (L) | id L  S | L,S S  (L . ) L  L . , S L  S . L S
  • 63. Reduce S’  . S S  . (L) S  . id S  ( . L) L  . S L  . L, S S  . (L) S  . id S  id . id ( id ( Grammaire S  (L) | id L  S | L,S S  (L . )L L  L . , S L  S . L S Etats de reduce (le point e a la fin!)
  • 64. Automate LR S’  . S S  . (L) S  . id S  ( . L) L  . S L  . L, S S  . (L) S  . id S  id .id ( id ( S  (L . ) L  L . , S L  S . S L  L , . S S  . (L) S  . id L  L,S . S  (L) . S’  S . Etat finale / accepte 1 2 8 9 6 5 3 74 S , ) S $ id L Grammaire S  (L) | id L  S | L,S (
  • 65. Exemple de parser ((a),b) dérivation pile input action ((a),b)  1 ((a),b) shift, goto 3 ((a),b)  1(3 (a),b) shift, goto 3 ((a),b)  1(3(3 a),b) shift, goto 2 ((a),b)  1(3(3a2 ),b) reduce Sid ((S),b)  1(3(3(S7 ),b) reduce LS ((L),b)  1(3(3(L5 ),b) shift, goto 6 ((L),b)  1(3(3L5)6 ,b) reduce S(L) (S,b)  1(3S7 ,b) reduce LS (L,b)  1(3L5 ,b) shift, goto 8 (L,b)  1(3L5,8 b) shift, goto 9 (L,b)  1(3L5,8b2 ) reduce Sid (L,S)  1(3L8,S9 ) reduce LL,S (L)  1(3L5 ) shift, goto 6 (L)  1(3L5)6 reduce S(L) S  1S4 $ accepte S  (L) | id L  S | L,S
  • 66. Tableau de parser LR(0)  Etats = états de Automate LR  S  S’ avec jeton C:  Action[S,C] += Shift(S’)  S  S’ avec la règle N:  Goto[S,N] += S’  Si S est état de réduction X  .:  Action[S,*] += Reduce(X  )
  • 67. Exemple de tableau ( ) id , $ S L 1 s3 s2 g4 2 Sid Sid Sid Sid Sid 3 s3 s2 g7 g5 4 accept 5 s6 s8 6 S(L) S(L) S(L) S(L) S(L) 7 LS LS LS LS LS 8 s3 s2 g9 9 LL,S LL,S LL,S LL,S LL,S Etat Jetons Regles reduceshift
  • 68. Limites LR(0)  Une action pou L  L , S . L  L , S . S  S . , L L  S , L . L  S . OK shift/reduce reduce/reduce
  • 69. num + $ E S 1 s4 g2 g6 2 SE s3/SE SE Exemple de shift/reduce S’  . S S  .E + S S  . E E  .num E  num . S  E . +S S  E . E num + S  E + S . accept S S  E + . S S  . E + S S  . E E  . num S’  S . 1 2 5 3 7 4 S Grammaire S  E + S | E E  num $ E num Shift ou reduce etat 2?
  • 70. Parser SLR  SLR =“Simple LR”= extension simple LR(0)  Pour X  , le prochain jeton C  reduce seulement si C est de FOLLOW(X)  Résoudre partial les shift/reduce  Identique avec LR(0) sauf ‘reduction’  Réduction X   seulement les symboles de FOLLOW(X) num + $ E S 1 s4 g2 g6 2 s3 SE Exemple: FOLLOW(S) = {$}
  • 71. Exemple de parser SLR num + $ E S 1 s4 g2 g6 2 s3 SE 3 s4 g2 g5 4 Enum Enum 5 SE+S 6 s7 7 accept Grammaire S  E + S | E E  num
  • 72. Automate SLR S’  . S S  .E + S S  . E E  .num E  num . “+” “$” : reduce S  E . +S S  E . “$” : reduce E num + S  E + S . “$” : reduce accept S S  E + . S S  . E + S S  . E E  . num S’  S . 1 2 5 3 7 4 S Grammaire S  E + S | E E  num $ E num num + $ E S 1 s4 g2 g6 2 s3 SE
  • 73. LALR(1)  SLR: Decizia de reduce pentru “X->” se face doar daca urmatorul simbol e in FOLLOW(X)  LALR(1): Fiecare productie are un set de simboli care ii pot urma. “X-> , S” se reduce doar daca urmatorul simbol e in S.  Seturile se pot calcula prin aplicarea recursiva a regulilor: S’ .S, {$} B α.Aβ c in FIRST(β) A  .Xδ , {c} B  α.Aβ , {c} c  FOLLOW(β) β * ε A  .Xδ , {c} X α.Xβ , c X  αX.β , c S’  .S , {$} S  .L=R S  .R L  .*R L  .ident R  .L S’  .S , {$} S  .L=R S  .R L  .*R , {=} L  .ident , {=} R  .L S’  .S , {$} S  .L=R , {$} S  .R , {$} L  .*R , {= $} L  .ident , {= $} R  .L , {$} L  .ident, {= $} L  ident. , {= $}
  • 74. DFA pentru LALR • S  L = R S  R L  *R L  ident R  L S’  .S {$} S  .L=R {$} S  .R {$} L  .*R {= $} L  .ident {= $} R  .L {$} S’  S. {$} “$” : accept S  L.=R {$} R  L. {$} “$” : reduce S  R. {$} “$” : reduce L  *.R {=$} L  .*R {=$} L  .ident {=$} R  .L {=$} L  ident. {$ =} “$” “=“ : reduce S  L=.R {$} L  .*R {$} L  .ident {$} R  .L {$} L  *R. {$ =} “$” “=“ : reduce R  L. {=$} “$” “=“ : reduce S  L=R. {$} “$” : reduce S L L L R R R * * ident ident * ident =