SlideShare a Scribd company logo
1 of 55
ALF
Parser Top-Down
Bibliographie pour aujourd'hui
Keith Cooper, Linda Torczon, Engineering a
Compiler
– Chapitre 3
• 3.3
Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D.
Ullman, Compilers: Principles, Techniques, and Tools
(2nd Edition)
– Chapitre 4
• 4.4
Contenu
• Les parser
• LL
Alexander Aiken
• Américain
• Stanford
• LL(*)
• MOSS
• ANTLR
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
–Instr
–id = Expr ;
–id = ( Expr ) ;
–id = ( Expr + Expr ) ;
–id = ( id + Expr ) ;
–id = ( id + id ) ;
id = ( id + id ) ;
id = ( id + id ) ;
id = ( id + id ) ;
id = ( id + id ) ;
id = ( id + id ) ;
id = ( id + id ) ;
• LL: La chaîne de jetons est itérée à partir du côté
gauche (L)
• Le non-terminal le plus à gauche est dérivé (L)
Dérivation gauche, top down
–Instr
–id = Expr ;
–id = Expr + Expr ;
–id = ( Expr ) + Expr ;
–id = ( id ) + Expr ;
–id = ( id ) + ( Expr ) ;
–id = ( id + id ) ;
id = ( id ) + ( id ) ;
id = ( id ) + ( id ) ;
id = ( id ) + ( id ) ;
id = ( id ) + ( id ) ;
id = ( id ) + ( id ) ;
id = ( id ) + ( id ) ;
id = ( id ) + ( id ) ;
• Comment choisir la production utilisée pour la
dérivation?
• Backtracking?
Dérivation gauche, top down
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
Analyse descendent récursive
 Non-terminal -> fonction
 Si le symbole apparaît dans la partie droite
de production -> appel la fonction
 Si le symbole apparaît dans la partie
gauche de production – la production est
choisi en fonction des jetons (tokens)
suivants (lookahead)
MatchToken (token) {
if (lookahead != token) throw error();
lookahead = lexer.getNextToken();
}
rfc850-date = weekday "," SP date2 SP time SP "GMT“
ParseRFC850Date() {
ParseWeekDay();
MatchToken(",");
MatchToken(SP);
ParseDate2();
MatchToken(SP);
ParseTime();
MatchToken(SP);
MatchToken("GMT“);
}
Fonction pour parser le non-
terminal rfc850-date
Analyse descendent récursive
Avec la grammaire
E  E + T | T
T  T  F | F
F  ( E ) | id
Un parser descendant entre dans une boucle infinie
lorsque vous essayez de parser cette grammaire
E E
+E T
E
+E T
+E T
E
+E T
+E T
+E T
(Aho,Sethi,Ullman, pp. 176)
Récursivité gauche
Grammaire des
expression
E  E + T | T
T  T  F | F
F  ( E ) | id
Peut être écrive sans la récursivité gauche
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
(Aho,Sethi,Ullman, pp. 176)
ε – string vide
Récursivité gauche
Exemple de parser récursive
ParseE() {
ParseT(); ParseE1();
}
ParseE1() {
if (lookahead==“+”)
{
MatchToken(“+”);
ParseT();
ParseE1();
}
}
ParseT() {
ParseF(); ParseT1();
}
ParseT1() {
if (lookahead==“*”)
{
MatchToken(“*”);
ParseF();
ParseT1();
}
}
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
ParseF() {
if (lookahead == “(“) {
MatchToken(“(“); ParseE(); MatchToken(“)”);
}
else
MatchToken(T_ID);
}
Comment choisir entre deux productions?
Comment pouvons-nous savoir quelles conditions de poser
a if?
Lorsque nous émettons des erreurs?
ParseF() {
if (lookahead == “(“) {
MatchToken(“(“);
ParseE();
MatchToken(“)”);
}
else if (lookahead == T_ID)
{
MatchToken(T_ID);
}
else throw error();
}
F  ( E )
F  id
T’  *FT’
T’  ε
ParseT1() {
if (lookahead==“*”) {
MatchToken(“*”);
ParseF();
ParseT1();
}
else if (lookahead == “+”) { }
else if (lookahead == “)”) { }
else if (lookahead == T_EOF) { }
else throw error();
}
Analyse descendent récursive
Les conditions pour if
• FIRST
– Ensemble de terminaux-préfixées pour le non-terminal
• FOLLOW
– Ensemble de terminaux suivantes pour le non-terminal
• NULLABLE
– Ensemble de non-terminaux qui peut etre derive en ε
FIRST
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
1. If X is a terminal, FIRST(X) = {X}
FIRST(id) = {id}
FIRST() = {}
FIRST(+) = {+}
ENSEBLES:
2. If X   , then   FIRST(X)
4. If X  Y1 Y2 ••• Yk
FIRST(() = {(}
FIRST()) = {)}
FIRST (pseudocode):
and a FIRST(Yi)
then a  FIRST(X)
FIRST(F) = {(, id}
FIRST(T) = FIRST(F) = {(, id}
FIRST(E) = FIRST(T) = {(, id}
FIRST(E’) = {} {+, }
FIRST(T’) = {} {, }
(Aho,Sethi,Ullman, pp. 189)
*
3. If X  Y1 Y2 ••• Yk
and Y1••• Yi-1 
and a FIRST(Y1)
then a  FIRST(X)
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
1. If S is the start symbol, then $  FOLLOW(S)
FOLLOW(E) = {$}
FOLLOW(E’) = { ), $}
ENSEBLES:
2. If A  B,
and a  FIRST()
and a  
then a  FOLLOW(B)
3. If A  B
and a  FOLLOW(A)
then a  FOLLOW(B)
FOLLOW – pseudocode:
{ ), $}
3a. If A  B
and
and a  FOLLOW(A)
then a  FOLLOW(B)
*  
FOLLOW(T) = { ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
 et  - string de terminaux et non-
terminaux
A et B – non-terminaux,
$ - fin du text
(Aho,Sethi,Ullman,
pp. 189)
FOLLOW
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
1. If S is the start symbol, then $  FOLLOW(S)
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
3. If A  B
and a  FOLLOW(A)
then a  FOLLOW(B)
3a. If A  B
and
and a  FOLLOW(A)
then a  FOLLOW(B)
*  
FOLLOW(T) = { ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
2. If A  B,
and a  FIRST()
and a  
then a  FOLLOW(B)
{+, ), $}
(Aho,Sethi,Ullman, pp. 189)
GRAMMAIRE:
ENSEBLES:
FOLLOW – règles:
FOLLOW
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
1. If S is the start symbol, then $  FOLLOW(S)
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW(T) = {+, ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
2. If A  B,
and a  FIRST()
and a  
then a  FOLLOW(B)
3. If A  B
and a  FOLLOW(A)
then a  FOLLOW(B)
FOLLOW(T’) = {+, ), $}
3a. If A  B
and
and a  FOLLOW(A)
then a  FOLLOW(B)
*  
(Aho,Sethi,Ullman, pp. 189)
GRAMMAIRE:
ENSEBLES:
FOLLOW – règles:
FOLLOW
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
1. If S is the start symbol, then $  FOLLOW(S)
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW(T) = {+, ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
2. If A  B,
and a  FIRST()
and a  
then a  FOLLOW(B)
3. If A  B
and a  FOLLOW(A)
then a  FOLLOW(B)
FOLLOW(T’) = {+, ), $}
3a. If A  B
and
and a  FOLLOW(A)
then a  FOLLOW(B)
*  
FOLLOW(F) = {+, ), $}
(Aho,Sethi,Ullman, pp. 189)
GRAMMAIRE:
ENSEBLES:
FOLLOW – règles:
FOLLOW
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
1. If S is the start symbol, then $  FOLLOW(S)
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW(T) = {+, ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
3. If A  B
and a  FOLLOW(A)
then a  FOLLOW(B)
FOLLOW(T’) = {+, ), $}
3a. If A  B
and
and a  FOLLOW(A)
then a  FOLLOW(B)
*  
FOLLOW(F) = {+, ), $}
2. If A  B,
and a  FIRST()
and a  
then a  FOLLOW(B)
{+, , ), $}
(Aho,Sethi,Ullman, pp. 189)
GRAMMAIRE:
ENSEBLES:
FOLLOW – règles:
FOLLOW
L’algo générique récursive LL(1)
A  a B … x
A  C D … y
…
ParseA() {
if (lookahead in FIRST(a B … x FOLLOW(A)) {
MatchToken(a); ParseB(); … MatchToken(x);
}
else if (lookahead in FIRST(C D … y FOLLOW(A))
{
ParseC(); ParseD(); … MatchToken(y);
}
…
else throw error();
}
• Pour chaque non-terminal crée une fonction de parser.
• Pour chaque règle Aα ajouter un test
if (lookahead in FIRST(αFOLLOW(A)) )
• Pour chaque non-terminal dans a appeler la fonction de parser.
• Pour chaque terminal dans a, vérifier le lookahead(match)
Récursivité gauche
Quand une grammaire a au moins une forme de production
A  Aα
nous disons qu'il est une grammaire récursive gauche.
Le parsers descendent ne fonctionnent pas (sans
backtracking) sur les grammaire récursives gauche.
(Aho,Sethi,Ullman, pp. 176)
Récursivité peut ne pas être immédiat
A  Bα
B  A β
Elimination récursivité gauche
• Cela se fait par la réécriture de la
grammaire
E  E + T | T
T  T  F | F
F  ( E ) | id
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
(Aho,Sethi,Ullman, pp. 176)
List  List Item | Item
List  Item List’
List’  Item List’ | ε
Cas général (récursivité immédiat):
A → Aβ1 |Aβ2 | ... |Aβm | α1 | α2 | ... | αn
A → α1A' | α2A' | ... | αnA‘
A' → β1A' | β2A' | ... | βmA'| ε
E  E + T | T
T  T  F | F
F  ( E ) | id
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
Elimination récursivité gauche
• Pour une instruction if:
• Pour parser avec LL, elle doit être factorise:
Factorisation gauche
 Cas général:
A → αβ1 | αβ2 | ... | αβn | δ
 Factorise:
A → αA' | δ
A' → β1 | β2 | ... | βn
Factorisation gauche
Elimination des ambiguïtés
 Ambigu: Ε → Ε + Ε | Ε * Ε | a | ( E )
1. Ε → Ε + T | T
T → T * F | F
F → a | ( E )
2. Ε → T + E | T
T → F * T | F
F → a | ( E )
 La précédence des operateurs
 La associativité gauche ou droite
 Productions qui peuvent produire l'ambiguïté:
X → aAbAc
 Cas général:
A → A B A | α1 | α2 | ... | αn
 Désambiguïsât:
A → A' B A | A‘
A' → α1 | α2 | ... | αn
Elimination des ambiguïtés
Parser automatique
• Automate push-down
• Le parser est fait avec un automate est un
tableau
• Langage LL(1) si il n'a pas de conflits dans
le tableau
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
Grammaire:
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’+TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
Tableau
de
Parsing:
(Aho,Sethi,Ullman, pp. 188)
Exemple de parser LL
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
PILE:
id idid+ INPUT:
Predictive Parsing
Program
E
$
$ OUTPUT:
E
T
E’
$
T E’
TABLEAU
DE
PARSING:
Exemple de parser LL
T
E’
$
T
E’
$
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
id idid+ INPUT:
Predictive Parsing
Program
$ OUTPUT:
E
F
T’
E’
$
F T’
T E’
(Aho,Sethi,
Ullman,
pp. 186)
PILE:
TABLEAU
DE
PARSING:
Exemple de parser LL
(Aho,Sethi,
Ullman,
pp. 188)
T
E’
$
T
E’
$
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
id idid+ INPUT:
Predictive Parsing
Program
$ OUTPUT:
E
F
T’
E’
$
F T’
T E’
id
T’
E’
$
id
PILE:
TABLEAU
DE
PARSING:
Exemple de parser LL
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
id idid+ INPUT:
Predictive Parsing
Program
$ OUTPUT:
E
T’
E’
$
F T’
T E’
id
Quand l’action c’est Top(Pile) = input ≠ $ : ‘Pop’ de la pile, avance la bande de input.
(Aho,Sethi,
Ullman,
pp. 188)
PILE:
TABLEAU
DE
PARSING:
Exemple de parser LL
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
id idid+ INPUT:
Predictive Parsing
Program
$ OUTPUT:
E
F T’
T E’
id 
T’
E’
$
E’
$
(Aho,Sethi,
Ullman,
pp. 188)
PILE:
TABLEAU
DE
PARSING:
Exemple de parser LL
E
F T’
T E’
id 
T+ E’
F T’
id F T’
id 

Et ainsi, il construit
l’arbre de dérivation:
E’  +TE’
T  FT’
F  id
T’   FT’
F  id
T’  
E’  
Quand Top(Pile) = input = $
Le parser arrêt et accepte l’input
(Aho,Sethi,
Ullman,
pp. 188)
Exemple de parser LL
Remplir de tableau
• FIRST
– Ensemble de terminaux-préfixées pour le non-terminal
• FOLLOW
– Ensemble de terminaux suivantes pour le non-terminal
• NULLABLE
– Ensemble de non-terminaux qui peut etre derive en ε
Reguli pentru construit tabela de parsare
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW SETS:
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, , ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
FIRST SETS:
TABLEAU
DE
PARSING:
1. If A  :
if a  FIRST(), add A   to M[A, a]
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
(Aho,Sethi,Ullman, pp. 190)
1. If A  :
if a  FIRST(), add A   to M[A, a]
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
(Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW SETS:
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, , ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
FIRST SETS:
TABLEAU
DE
PARSING:
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW SETS:
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, , ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
FIRST SETS:
1. If A  :
if a  FIRST(), add A   to M[A, a]
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
(Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare
TABLEAU
DE
PARSING:
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW SETS:
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, , ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
FIRST SETS:
TABLEAU
DE
PARSING:
1. If A  :
if a  FIRST(), add A   to M[A, a]
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
(Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW SETS:
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, , ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
FIRST SETS:
1. If A  :
if a  FIRST(), add A   to M[A, a]
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
(Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare
TABLEAU
DE
PARSING:
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW SETS:
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, , ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
FIRST SETS:
1. If A  :
if a  FIRST(), add A   to M[A, a]
2. If A  :
if   FIRST(), add A   to M[A, b]
for each terminal b  FOLLOW(A),
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
(Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare
TABLEAU
DE
PARSING:
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW SETS:
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, , ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
FIRST SETS:
1. If A  :
if a  FIRST(), add A   to M[A, a]
2. If A  :
if   FIRST(), add A   to M[A, b]
for each terminal b  FOLLOW(A),
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
(Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare
TABLEAU
DE
PARSING:
E  TE’
E’  +TE’ | 
T  FT’
T’  FT’ | 
F  ( E ) | id
GRAMMAIRE:
FOLLOW(E) = {), $}
FOLLOW(E’) = { ), $}
FOLLOW SETS:
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {+, , ), $}
FIRST(F) = {(, id}
FIRST(T) = {(, id}
FIRST(E) = {(, id}
FIRST(E’) = {+, }
FIRST(T’) = { , }
FIRST SETS:
1. If A  :
if a  FIRST(), add A   to M[A, a]
2. If A  :
if   FIRST(), add A   to M[A, b]
for each terminal b  FOLLOW(A),
3. If A  :
if   FIRST(), and $  FOLLOW(A),
add A   to M[A, $]
INPUTSYMBOLNON-
TERMINAL id + * ( ) $
E ETE’ ETE’
E’ E’ +TE’ E’  E’ 
T TFT’ TFT’
T’ T’ T’ *FT’ T’  T’ 
F Fid F(E)
(Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare
TABLEAU
DE
PARSING:
Utilisation de parser LL(1)
 Grammaires
 Non ambigu
 Factorise
 Non récursive a gauche
 On peut montrer que la grammaire G est LL (1) si et
seulement si pour deux productions de la forme
A  , A  , avec    les conditions suivantes
sont satisfaites:
 FIRST()  FIRST() = 
 Si  * ε alors FIRST()  FOLLOW(A) = et si  * ε
alors FIRST()  FOLLOW(A) = .
Avantage/désavantage LL(1)
 Facile de écrive ‘aux main’
 Vite, facile de comprendre
 La grammaire doit être transforme
 L’arbre de dérivation et diffèrent de l’arbre sémantique
E
F T’
T E’
id 
T+ E’
F T’
id F T’
id 

E
F
T
TE
id
+
F
id
F
id
T
Parser LL
• ANTLR
– Java
– LL (*)
– Factorisation
Règles EBNF
Something?
Something*
Something+
SomethingQ -> ε
| Something
SomethingStar -> ε
| Something SomethingStar
SomethingPlus ->
Something SomethingStar
Sujets
• Les parser
• LL
– Eviter l’ambiguïté
– Factorisation
– Eviter la récursivité gauche
• Algorithme général récursive LL
Questions

More Related Content

What's hot

Java 8, lambdas, generics: How to survive? - NYC Java Meetup Group
Java 8, lambdas, generics: How to survive? - NYC Java Meetup GroupJava 8, lambdas, generics: How to survive? - NYC Java Meetup Group
Java 8, lambdas, generics: How to survive? - NYC Java Meetup GroupHenri Tremblay
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayKevlin Henney
 
Generics and Lambdas cocktail explained - Montreal JUG
Generics and Lambdas cocktail explained  - Montreal JUGGenerics and Lambdas cocktail explained  - Montreal JUG
Generics and Lambdas cocktail explained - Montreal JUGHenri Tremblay
 
Lambdas and Generics (long version) - Bordeaux/Toulouse JUG
Lambdas and Generics (long version) - Bordeaux/Toulouse JUGLambdas and Generics (long version) - Bordeaux/Toulouse JUG
Lambdas and Generics (long version) - Bordeaux/Toulouse JUGHenri Tremblay
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingGuido Wachsmuth
 
Hello, Type Systems! - Introduction to Featherweight Java
Hello, Type Systems! - Introduction to Featherweight JavaHello, Type Systems! - Introduction to Featherweight Java
Hello, Type Systems! - Introduction to Featherweight Javay_taka_23
 
T3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerT3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerDavid Muñoz Díaz
 
GUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programmingGUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programmingDavid Muñoz Díaz
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeKevlin Henney
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationEelco Visser
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1Hackraft
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 
The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.9 book - Part 31 of 210The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.9 book - Part 31 of 210Mahmoud Samir Fayed
 
The Next Great Functional Programming Language
The Next Great Functional Programming LanguageThe Next Great Functional Programming Language
The Next Great Functional Programming LanguageJohn De Goes
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaDaniel Cukier
 
Why The Free Monad isn't Free
Why The Free Monad isn't FreeWhy The Free Monad isn't Free
Why The Free Monad isn't FreeKelley Robinson
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick referenceilesh raval
 

What's hot (20)

Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
 
Java 8, lambdas, generics: How to survive? - NYC Java Meetup Group
Java 8, lambdas, generics: How to survive? - NYC Java Meetup GroupJava 8, lambdas, generics: How to survive? - NYC Java Meetup Group
Java 8, lambdas, generics: How to survive? - NYC Java Meetup Group
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went Away
 
Generics and Lambdas cocktail explained - Montreal JUG
Generics and Lambdas cocktail explained  - Montreal JUGGenerics and Lambdas cocktail explained  - Montreal JUG
Generics and Lambdas cocktail explained - Montreal JUG
 
Lambdas and Generics (long version) - Bordeaux/Toulouse JUG
Lambdas and Generics (long version) - Bordeaux/Toulouse JUGLambdas and Generics (long version) - Bordeaux/Toulouse JUG
Lambdas and Generics (long version) - Bordeaux/Toulouse JUG
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
 
Hello, Type Systems! - Introduction to Featherweight Java
Hello, Type Systems! - Introduction to Featherweight JavaHello, Type Systems! - Introduction to Featherweight Java
Hello, Type Systems! - Introduction to Featherweight Java
 
T3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerT3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmer
 
GUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programmingGUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programming
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.9 book - Part 31 of 210The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.9 book - Part 31 of 210
 
The Next Great Functional Programming Language
The Next Great Functional Programming LanguageThe Next Great Functional Programming Language
The Next Great Functional Programming Language
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Why The Free Monad isn't Free
Why The Free Monad isn't FreeWhy The Free Monad isn't Free
Why The Free Monad isn't Free
 
Core csharp and net quick reference
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
 

Similar to ALF 5 - Parser Top-Down (20)

Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
LL(1) Parsers
 
Left factor put
Left factor putLeft factor put
Left factor put
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx
 
Ch5b
Ch5bCh5b
Ch5b
 
Ch8b
Ch8bCh8b
Ch8b
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
11CS10033.pptx
11CS10033.pptx11CS10033.pptx
11CS10033.pptx
 
UNIT 2 (1).pptx
UNIT 2 (1).pptxUNIT 2 (1).pptx
UNIT 2 (1).pptx
 
Ch8a
Ch8aCh8a
Ch8a
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 

More from 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
 
ALF 6 - Parser
ALF 6 - ParserALF 6 - Parser
ALF 6 - Parser
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

ALF 5 - Parser Top-Down

  • 2. Bibliographie pour aujourd'hui Keith Cooper, Linda Torczon, Engineering a Compiler – Chapitre 3 • 3.3 Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, Compilers: Principles, Techniques, and Tools (2nd Edition) – Chapitre 4 • 4.4
  • 4. Alexander Aiken • Américain • Stanford • LL(*) • MOSS • ANTLR
  • 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. –Instr –id = Expr ; –id = ( Expr ) ; –id = ( Expr + Expr ) ; –id = ( id + Expr ) ; –id = ( id + id ) ; id = ( id + id ) ; id = ( id + id ) ; id = ( id + id ) ; id = ( id + id ) ; id = ( id + id ) ; id = ( id + id ) ; • LL: La chaîne de jetons est itérée à partir du côté gauche (L) • Le non-terminal le plus à gauche est dérivé (L) Dérivation gauche, top down
  • 10. –Instr –id = Expr ; –id = Expr + Expr ; –id = ( Expr ) + Expr ; –id = ( id ) + Expr ; –id = ( id ) + ( Expr ) ; –id = ( id + id ) ; id = ( id ) + ( id ) ; id = ( id ) + ( id ) ; id = ( id ) + ( id ) ; id = ( id ) + ( id ) ; id = ( id ) + ( id ) ; id = ( id ) + ( id ) ; id = ( id ) + ( id ) ; • Comment choisir la production utilisée pour la dérivation? • Backtracking? Dérivation gauche, top down
  • 11. 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
  • 12. Analyse descendent récursive  Non-terminal -> fonction  Si le symbole apparaît dans la partie droite de production -> appel la fonction  Si le symbole apparaît dans la partie gauche de production – la production est choisi en fonction des jetons (tokens) suivants (lookahead)
  • 13. MatchToken (token) { if (lookahead != token) throw error(); lookahead = lexer.getNextToken(); } rfc850-date = weekday "," SP date2 SP time SP "GMT“ ParseRFC850Date() { ParseWeekDay(); MatchToken(","); MatchToken(SP); ParseDate2(); MatchToken(SP); ParseTime(); MatchToken(SP); MatchToken("GMT“); } Fonction pour parser le non- terminal rfc850-date Analyse descendent récursive
  • 14. Avec la grammaire E  E + T | T T  T  F | F F  ( E ) | id Un parser descendant entre dans une boucle infinie lorsque vous essayez de parser cette grammaire E E +E T E +E T +E T E +E T +E T +E T (Aho,Sethi,Ullman, pp. 176) Récursivité gauche
  • 15. Grammaire des expression E  E + T | T T  T  F | F F  ( E ) | id Peut être écrive sans la récursivité gauche E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id (Aho,Sethi,Ullman, pp. 176) ε – string vide Récursivité gauche
  • 16. Exemple de parser récursive ParseE() { ParseT(); ParseE1(); } ParseE1() { if (lookahead==“+”) { MatchToken(“+”); ParseT(); ParseE1(); } } ParseT() { ParseF(); ParseT1(); } ParseT1() { if (lookahead==“*”) { MatchToken(“*”); ParseF(); ParseT1(); } } E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id ParseF() { if (lookahead == “(“) { MatchToken(“(“); ParseE(); MatchToken(“)”); } else MatchToken(T_ID); }
  • 17. Comment choisir entre deux productions? Comment pouvons-nous savoir quelles conditions de poser a if? Lorsque nous émettons des erreurs? ParseF() { if (lookahead == “(“) { MatchToken(“(“); ParseE(); MatchToken(“)”); } else if (lookahead == T_ID) { MatchToken(T_ID); } else throw error(); } F  ( E ) F  id T’  *FT’ T’  ε ParseT1() { if (lookahead==“*”) { MatchToken(“*”); ParseF(); ParseT1(); } else if (lookahead == “+”) { } else if (lookahead == “)”) { } else if (lookahead == T_EOF) { } else throw error(); } Analyse descendent récursive
  • 18. Les conditions pour if • FIRST – Ensemble de terminaux-préfixées pour le non-terminal • FOLLOW – Ensemble de terminaux suivantes pour le non-terminal • NULLABLE – Ensemble de non-terminaux qui peut etre derive en ε
  • 19. FIRST E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: 1. If X is a terminal, FIRST(X) = {X} FIRST(id) = {id} FIRST() = {} FIRST(+) = {+} ENSEBLES: 2. If X   , then   FIRST(X) 4. If X  Y1 Y2 ••• Yk FIRST(() = {(} FIRST()) = {)} FIRST (pseudocode): and a FIRST(Yi) then a  FIRST(X) FIRST(F) = {(, id} FIRST(T) = FIRST(F) = {(, id} FIRST(E) = FIRST(T) = {(, id} FIRST(E’) = {} {+, } FIRST(T’) = {} {, } (Aho,Sethi,Ullman, pp. 189) * 3. If X  Y1 Y2 ••• Yk and Y1••• Yi-1  and a FIRST(Y1) then a  FIRST(X)
  • 20. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: 1. If S is the start symbol, then $  FOLLOW(S) FOLLOW(E) = {$} FOLLOW(E’) = { ), $} ENSEBLES: 2. If A  B, and a  FIRST() and a   then a  FOLLOW(B) 3. If A  B and a  FOLLOW(A) then a  FOLLOW(B) FOLLOW – pseudocode: { ), $} 3a. If A  B and and a  FOLLOW(A) then a  FOLLOW(B) *   FOLLOW(T) = { ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , }  et  - string de terminaux et non- terminaux A et B – non-terminaux, $ - fin du text (Aho,Sethi,Ullman, pp. 189) FOLLOW
  • 21. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id 1. If S is the start symbol, then $  FOLLOW(S) FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} 3. If A  B and a  FOLLOW(A) then a  FOLLOW(B) 3a. If A  B and and a  FOLLOW(A) then a  FOLLOW(B) *   FOLLOW(T) = { ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } 2. If A  B, and a  FIRST() and a   then a  FOLLOW(B) {+, ), $} (Aho,Sethi,Ullman, pp. 189) GRAMMAIRE: ENSEBLES: FOLLOW – règles: FOLLOW
  • 22. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id 1. If S is the start symbol, then $  FOLLOW(S) FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW(T) = {+, ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } 2. If A  B, and a  FIRST() and a   then a  FOLLOW(B) 3. If A  B and a  FOLLOW(A) then a  FOLLOW(B) FOLLOW(T’) = {+, ), $} 3a. If A  B and and a  FOLLOW(A) then a  FOLLOW(B) *   (Aho,Sethi,Ullman, pp. 189) GRAMMAIRE: ENSEBLES: FOLLOW – règles: FOLLOW
  • 23. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id 1. If S is the start symbol, then $  FOLLOW(S) FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW(T) = {+, ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } 2. If A  B, and a  FIRST() and a   then a  FOLLOW(B) 3. If A  B and a  FOLLOW(A) then a  FOLLOW(B) FOLLOW(T’) = {+, ), $} 3a. If A  B and and a  FOLLOW(A) then a  FOLLOW(B) *   FOLLOW(F) = {+, ), $} (Aho,Sethi,Ullman, pp. 189) GRAMMAIRE: ENSEBLES: FOLLOW – règles: FOLLOW
  • 24. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id 1. If S is the start symbol, then $  FOLLOW(S) FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW(T) = {+, ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } 3. If A  B and a  FOLLOW(A) then a  FOLLOW(B) FOLLOW(T’) = {+, ), $} 3a. If A  B and and a  FOLLOW(A) then a  FOLLOW(B) *   FOLLOW(F) = {+, ), $} 2. If A  B, and a  FIRST() and a   then a  FOLLOW(B) {+, , ), $} (Aho,Sethi,Ullman, pp. 189) GRAMMAIRE: ENSEBLES: FOLLOW – règles: FOLLOW
  • 25. L’algo générique récursive LL(1) A  a B … x A  C D … y … ParseA() { if (lookahead in FIRST(a B … x FOLLOW(A)) { MatchToken(a); ParseB(); … MatchToken(x); } else if (lookahead in FIRST(C D … y FOLLOW(A)) { ParseC(); ParseD(); … MatchToken(y); } … else throw error(); } • Pour chaque non-terminal crée une fonction de parser. • Pour chaque règle Aα ajouter un test if (lookahead in FIRST(αFOLLOW(A)) ) • Pour chaque non-terminal dans a appeler la fonction de parser. • Pour chaque terminal dans a, vérifier le lookahead(match)
  • 26. Récursivité gauche Quand une grammaire a au moins une forme de production A  Aα nous disons qu'il est une grammaire récursive gauche. Le parsers descendent ne fonctionnent pas (sans backtracking) sur les grammaire récursives gauche. (Aho,Sethi,Ullman, pp. 176) Récursivité peut ne pas être immédiat A  Bα B  A β
  • 27. Elimination récursivité gauche • Cela se fait par la réécriture de la grammaire E  E + T | T T  T  F | F F  ( E ) | id E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id (Aho,Sethi,Ullman, pp. 176) List  List Item | Item List  Item List’ List’  Item List’ | ε
  • 28. Cas général (récursivité immédiat): A → Aβ1 |Aβ2 | ... |Aβm | α1 | α2 | ... | αn A → α1A' | α2A' | ... | αnA‘ A' → β1A' | β2A' | ... | βmA'| ε E  E + T | T T  T  F | F F  ( E ) | id E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id Elimination récursivité gauche
  • 29. • Pour une instruction if: • Pour parser avec LL, elle doit être factorise: Factorisation gauche
  • 30.  Cas général: A → αβ1 | αβ2 | ... | αβn | δ  Factorise: A → αA' | δ A' → β1 | β2 | ... | βn Factorisation gauche
  • 31. Elimination des ambiguïtés  Ambigu: Ε → Ε + Ε | Ε * Ε | a | ( E ) 1. Ε → Ε + T | T T → T * F | F F → a | ( E ) 2. Ε → T + E | T T → F * T | F F → a | ( E )  La précédence des operateurs  La associativité gauche ou droite
  • 32.  Productions qui peuvent produire l'ambiguïté: X → aAbAc  Cas général: A → A B A | α1 | α2 | ... | αn  Désambiguïsât: A → A' B A | A‘ A' → α1 | α2 | ... | αn Elimination des ambiguïtés
  • 33. Parser automatique • Automate push-down • Le parser est fait avec un automate est un tableau • Langage LL(1) si il n'a pas de conflits dans le tableau
  • 34. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id Grammaire: INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’+TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) Tableau de Parsing: (Aho,Sethi,Ullman, pp. 188) Exemple de parser LL
  • 35. INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) PILE: id idid+ INPUT: Predictive Parsing Program E $ $ OUTPUT: E T E’ $ T E’ TABLEAU DE PARSING: Exemple de parser LL
  • 36. T E’ $ T E’ $ INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) id idid+ INPUT: Predictive Parsing Program $ OUTPUT: E F T’ E’ $ F T’ T E’ (Aho,Sethi, Ullman, pp. 186) PILE: TABLEAU DE PARSING: Exemple de parser LL
  • 37. (Aho,Sethi, Ullman, pp. 188) T E’ $ T E’ $ INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) id idid+ INPUT: Predictive Parsing Program $ OUTPUT: E F T’ E’ $ F T’ T E’ id T’ E’ $ id PILE: TABLEAU DE PARSING: Exemple de parser LL
  • 38. INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) id idid+ INPUT: Predictive Parsing Program $ OUTPUT: E T’ E’ $ F T’ T E’ id Quand l’action c’est Top(Pile) = input ≠ $ : ‘Pop’ de la pile, avance la bande de input. (Aho,Sethi, Ullman, pp. 188) PILE: TABLEAU DE PARSING: Exemple de parser LL
  • 39. INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) id idid+ INPUT: Predictive Parsing Program $ OUTPUT: E F T’ T E’ id  T’ E’ $ E’ $ (Aho,Sethi, Ullman, pp. 188) PILE: TABLEAU DE PARSING: Exemple de parser LL
  • 40. E F T’ T E’ id  T+ E’ F T’ id F T’ id   Et ainsi, il construit l’arbre de dérivation: E’  +TE’ T  FT’ F  id T’   FT’ F  id T’   E’   Quand Top(Pile) = input = $ Le parser arrêt et accepte l’input (Aho,Sethi, Ullman, pp. 188) Exemple de parser LL
  • 41. Remplir de tableau • FIRST – Ensemble de terminaux-préfixées pour le non-terminal • FOLLOW – Ensemble de terminaux suivantes pour le non-terminal • NULLABLE – Ensemble de non-terminaux qui peut etre derive en ε
  • 42. Reguli pentru construit tabela de parsare E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW SETS: FOLLOW(T) = {+, ), $} FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, , ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } FIRST SETS: TABLEAU DE PARSING: 1. If A  : if a  FIRST(), add A   to M[A, a] INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) (Aho,Sethi,Ullman, pp. 190)
  • 43. 1. If A  : if a  FIRST(), add A   to M[A, a] INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) (Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW SETS: FOLLOW(T) = {+, ), $} FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, , ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } FIRST SETS: TABLEAU DE PARSING:
  • 44. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW SETS: FOLLOW(T) = {+, ), $} FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, , ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } FIRST SETS: 1. If A  : if a  FIRST(), add A   to M[A, a] INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) (Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare TABLEAU DE PARSING:
  • 45. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW SETS: FOLLOW(T) = {+, ), $} FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, , ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } FIRST SETS: TABLEAU DE PARSING: 1. If A  : if a  FIRST(), add A   to M[A, a] INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) (Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare
  • 46. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW SETS: FOLLOW(T) = {+, ), $} FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, , ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } FIRST SETS: 1. If A  : if a  FIRST(), add A   to M[A, a] INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) (Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare TABLEAU DE PARSING:
  • 47. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW SETS: FOLLOW(T) = {+, ), $} FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, , ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } FIRST SETS: 1. If A  : if a  FIRST(), add A   to M[A, a] 2. If A  : if   FIRST(), add A   to M[A, b] for each terminal b  FOLLOW(A), INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) (Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare TABLEAU DE PARSING:
  • 48. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW SETS: FOLLOW(T) = {+, ), $} FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, , ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } FIRST SETS: 1. If A  : if a  FIRST(), add A   to M[A, a] 2. If A  : if   FIRST(), add A   to M[A, b] for each terminal b  FOLLOW(A), INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) (Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare TABLEAU DE PARSING:
  • 49. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  ( E ) | id GRAMMAIRE: FOLLOW(E) = {), $} FOLLOW(E’) = { ), $} FOLLOW SETS: FOLLOW(T) = {+, ), $} FOLLOW(T’) = {+, ), $} FOLLOW(F) = {+, , ), $} FIRST(F) = {(, id} FIRST(T) = {(, id} FIRST(E) = {(, id} FIRST(E’) = {+, } FIRST(T’) = { , } FIRST SETS: 1. If A  : if a  FIRST(), add A   to M[A, a] 2. If A  : if   FIRST(), add A   to M[A, b] for each terminal b  FOLLOW(A), 3. If A  : if   FIRST(), and $  FOLLOW(A), add A   to M[A, $] INPUTSYMBOLNON- TERMINAL id + * ( ) $ E ETE’ ETE’ E’ E’ +TE’ E’  E’  T TFT’ TFT’ T’ T’ T’ *FT’ T’  T’  F Fid F(E) (Aho,Sethi,Ullman, pp. 190)Reguli pentru construit tabela de parsare TABLEAU DE PARSING:
  • 50. Utilisation de parser LL(1)  Grammaires  Non ambigu  Factorise  Non récursive a gauche  On peut montrer que la grammaire G est LL (1) si et seulement si pour deux productions de la forme A  , A  , avec    les conditions suivantes sont satisfaites:  FIRST()  FIRST() =   Si  * ε alors FIRST()  FOLLOW(A) = et si  * ε alors FIRST()  FOLLOW(A) = .
  • 51. Avantage/désavantage LL(1)  Facile de écrive ‘aux main’  Vite, facile de comprendre  La grammaire doit être transforme  L’arbre de dérivation et diffèrent de l’arbre sémantique E F T’ T E’ id  T+ E’ F T’ id F T’ id   E F T TE id + F id F id T
  • 52. Parser LL • ANTLR – Java – LL (*) – Factorisation
  • 53. Règles EBNF Something? Something* Something+ SomethingQ -> ε | Something SomethingStar -> ε | Something SomethingStar SomethingPlus -> Something SomethingStar
  • 54. Sujets • Les parser • LL – Eviter l’ambiguïté – Factorisation – Eviter la récursivité gauche • Algorithme général récursive LL