Académique Documents
Professionnel Documents
Culture Documents
Notion de langage
Automates Finis Déterministes
Automates Finis non-déterministes
Ambiguïté Lexicale
Minimisation d'un AFD
Analyse Lexicale
Théorie des langages et de compilation
1
R. EZZAHIR
1 Département d'Informatique
Ecole Nationale des Sciences Apliquées
ENSA-Agadir
Université Ibn Zohr
ème
4 année Génie Informatique, 2013/2014
Sommaire
1 Introduction
2 Notion de langage
5 Ambiguïté Lexicale
Analyse lexicale
Analyse lexicale
Analyse lexicale
Analyse lexicale
Problématique
Problématique
Problématique
Alphabet Alphabets
Un alphabet Σ (ou vocbulaire, 1. Σ ={a, b} ⇒ Σ* ={a,b,
ou lexique) est un ensemble ni ab,..., aaaa, aabb, ...}
de symboles. On note Σ* 2. {if, then, else, begin,
l'ensemble des mots sur Σ. end, :=, ;,(, ), A, B, C, 1,2}
Mot Mots
Un mot (ou phrase) sur un 1. aaaa, aabb
Alphabet Alphabets
Un alphabet Σ (ou vocbulaire, 1. Σ ={a, b} ⇒ Σ* ={a,b,
ou lexique) est un ensemble ni ab,..., aaaa, aabb, ...}
de symboles. On note Σ* 2. {if, then, else, begin,
l'ensemble des mots sur Σ. end, :=, ;,(, ), A, B, C, 1,2}
Mot Mots
Un mot (ou phrase) sur un 1. aaaa, aabb
Alphabet Alphabets
Un alphabet Σ (ou vocbulaire, 1. Σ ={a, b} ⇒ Σ* ={a,b,
ou lexique) est un ensemble ni ab,..., aaaa, aabb, ...}
de symboles. On note Σ* 2. {if, then, else, begin,
l'ensemble des mots sur Σ. end, :=, ;,(, ), A, B, C, 1,2}
Mot Mots
Un mot (ou phrase) sur un 1. aaaa, aabb
Alphabet Alphabets
Un alphabet Σ (ou vocbulaire, 1. Σ ={a, b} ⇒ Σ* ={a,b,
ou lexique) est un ensemble ni ab,..., aaaa, aabb, ...}
de symboles. On note Σ* 2. {if, then, else, begin,
l'ensemble des mots sur Σ. end, :=, ;,(, ), A, B, C, 1,2}
Mot Mots
Un mot (ou phrase) sur un 1. aaaa, aabb
Union
Intersection
Complément
A ∪ B = {w |w ∈ A ou w ∈ B }
Produit
Ex. Soit Σ = {a, b },
Cartésien
L1 = {ε, a, aa} et L2 = {a, bb}.
Ensemble des
L1 ∪ L2 = {ε, a, aa, bb}
parties
Fermeture de
Kleene
Union
Intersection
Complément
A ∩ B = {w |w ∈ A et w ∈ B }
Produit
Ex. Soit Σ = {a, b }
Cartésien
L1 = {ε, a, aa} et L2 = {a, bb}.
Ensemble des
L1 ∩ L2 = {a}
parties
Fermeture de
Kleene
Union
Intersection A = {w |w ∈/ A}
Complément
plus préciséiment, soit U l'ensemble de réference,
Produit
Cartésien
alors A = {w |w ∈ U et w ∈/ A}
Σ = {a, b } et L1 = {ε, a, aa}
Ensemble des
Ex. Soit
parties
L 1 = {b , ab , ba, bb , aaa, aab , aba, baa, bba, ...}
Fermeture de
Kleene
Union
Intersection A × B = {x ∈ A et y ∈ B }
Complément
Note : A x ∅ =∅ ; |A x B| =|A||B|
Produit
Cartésien
Ex. Soit Σ = {a, b }, L1 = {ε, a, aa} et
Ensemble des
L 2 = { a , bb}.
parties
L 1 × L2 = {(ε, a), (a, a), (a, bb ), (aa, a), (aa, bb )}
Fermeture de
Kleene
Union
P(A) = {B |B ⊆ A}
Intersection
Complément
Note : |P(A)| = 2|A|
Produit
Union
Soit L un langage sur l'alphabet Σ alors
Intersection
L∗ = {w = x .x 1 2 ......xk |k ≥ 0 et xi ∈ L} est la
Complément fermeture de Kleene de L, dénotée par l'ER (a)* |
Ensemble des L∗ = {ε, a, bb, aa, abb, bba, bbbb, aaa, aabb, ...}
2
parties
Fermeture de
Note : pour tout langage L 6= ∅ on a |L∗ | = ∞
Kleene
Example :
Nombres paire (+|-| ) (0|1|2|3|4|5|6|7|8|9)* (0|2|4|6|8)
Example :
Nombres paire (+|-| ) (0|1|2|3|4|5|6|7|8|9)* (0|2|4|6|8)
Example :
Nombres paire (+|-| ) (0|1|2|3|4|5|6|7|8|9)* (0|2|4|6|8)
Example :
Nombres paire (+|-| ) (0|1|2|3|4|5|6|7|8|9)* (0|2|4|6|8)
Example :
Nombres paire (+|-| ) (0|1|2|3|4|5|6|7|8|9)* (0|2|4|6|8)
q 1
0 1
start q 0 q 3
0 0 0 1
1 0
q 2
q 1
0 1
start q 0 q 3
0 0 0 1
⇑
1 0
q 2
q 1
0 1
start q 0 q 3
0 0 0 1
⇑
1 0
q 2
q 1
0 1
start q 0 q 3
0 0 0 1
⇑
1 0
q 2
q 1
0 1
start q 0 q 3
0 0 0 1
⇑
1 0
q 2
q 1
0 1
start q 0 q 3
0 0 0 1
OK
1 0
q 2
q1 q0 q1
q0 : état d'acceptation
q1 q0 q1
q0 : état d'acceptation
q1 q0 q1
q0 : état d'acceptation
Langages réguliers
Soit A = (Q , Σ, δ, q , F ) un AFD
0
AF Non déterminisme
Déterministe
Un automate A est déterministe si b
Non déterministe
ε
Un automate estnon déterministe s'il start q0 q1
q2
AF Non déterminisme
Déterministe
Un automate A est déterministe si b
Non déterministe
ε
Un automate estnon déterministe s'il start q0 q1
q2
AF Non déterminisme
Déterministe
Un automate A est déterministe si b
Non déterministe
ε
Un automate estnon déterministe s'il start q0 q1
q2
a,b
a a,b
start q0 q1 q2
a b
→ q0 {q0,q1} {q0}
(Attention !) δ est donné formellement par :
q1 {q2} {q2}
← q2 ∅ ∅
Remarque
Un automate ni déterministe (AFD) est un cas particulier d'un AFN :
a,b
a a,b
start q0 q1 q2
a b
→ q0 {q0,q1} {q0}
(Attention !) δ est donné formellement par :
q1 {q2} {q2}
← q2 ∅ ∅
Remarque
Un automate ni déterministe (AFD) est un cas particulier d'un AFN :
(0, ababab)
2
(0, babab) (1, babab)
a,b
(0, abab) (2, abab)
1
(1, bab) (0, bab)
a
(2, ab) (0, ab)
(2, ε) (0, ε)
Lignes directrices
1 Introduction
2 Notion de langage
5 Ambiguïté Lexicale
Déterminisation
Théorème de Rabin-Scott
Considérons un automate ni non-déterministe An = (Qn , Σ, δn , q , Fn )
0
Déterminisation (Exemple)
non-déterministe reconnaissant b a b
les mots de l'alphabet a,b qui
terminent par bab. 0 1 2 3
Pour déterminiser A en
construisant un nouvel état à a
b a b
partir de chaque sous ensemble
d'état possible. {0 } {0 , 1 } {1 , 3 } {0 , 1 , 3 }
b a
a b
Remarquons que les états {1},
{2}, {3}, {0,2}, {0,3}, {1,2}, {1 } {2 } {3 } {0 , 2 } {0 , 3 } {1 , 2 }
{2,3}, {0,1,2}, {1,2,3}, {0,2,3}
sont inatteignables et peuvent
être "retirés" de l'automate. {2 , 3 } {0 , 1 , 2 } {1 , 2 , 3 } {0 , 2 , 3 }
Déterminisation (Exemple)
non-déterministe reconnaissant b a b
les mots de l'alphabet a,b qui
terminent par bab. 0 1 2 3
Pour déterminiser A en
construisant un nouvel état à a
b a b
partir de chaque sous ensemble
d'état possible. {0 } {0 , 1 } {1 , 3 } {0 , 1 , 3 }
b a
a b
Remarquons que les états {1},
{2}, {3}, {0,2}, {0,3}, {1,2}, {1 } {2 } {3 } {0 , 2 } {0 , 3 } {1 , 2 }
{2,3}, {0,1,2}, {1,2,3}, {0,2,3}
sont inatteignables et peuvent
être "retirés" de l'automate. {2 , 3 } {0 , 1 , 2 } {1 , 2 , 3 } {0 , 2 , 3 }
Déterminisation (Exemple)
non-déterministe reconnaissant b a b
les mots de l'alphabet a,b qui
terminent par bab. 0 1 2 3
Pour déterminiser A en
construisant un nouvel état à a
b a b
partir de chaque sous ensemble
d'état possible. {0 } {0 , 1 } {1 , 3 } {0 , 1 , 3 }
b a
a b
Remarquons que les états {1},
{2}, {3}, {0,2}, {0,3}, {1,2}, {1 } {2 } {3 } {0 , 2 } {0 , 3 } {1 , 2 }
{2,3}, {0,1,2}, {1,2,3}, {0,2,3}
sont inatteignables et peuvent
être "retirés" de l'automate. {2 , 3 } {0 , 1 , 2 } {1 , 2 , 3 } {0 , 2 , 3 }
Lignes directrices
1 Introduction
2 Notion de langage
5 Ambiguïté Lexicale
pour
pour a
Construction pour R*
Construction pour R*
Ambiguïté Lexicale
Ambiguïté Lexicale
T_For for
T_Identier [A-Za-z_][A-Za-z0-9_]*
Mot : fort
f o r t f o r t
f o r t f o r t
f o r t f o r t
f o r t f o r t
f o r t f o r t
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
⇓
D O U B D O U B L E
⇑
T_Do do
T_Double double
T_Mystere (inconnu) [A-Za-z]
d o
start
d o u b l e
start
Σ
start
D O U B D O U B L E
Petite Simplification
d o
d o u b l e
start
plainchar = charNoQuote.
1 ...
2 /** print current character and read next character */
3 rivate void nextCh () {
4 System . out . print ( ch );
5 try {
6 ch = ( char ) in . read ();
7 } catch ( IOException ex ) {
8 error ( " read failure : " + ex . toString ());
9 }
10 }
11 /** read next symbol */
12 ...
13 }
1
2 /** the string representation of a symbol */
3 public static String representation ( int sym ) {
4 switch ( sym ) {
5 case ERROR : return " < error > " ;
6 case EOF : return " <eof > " ;
7 case IDENT : return " identifier " ;
8 case LITERAL : return " literal " ;
9 case LPAREN : return " `( ' " ;
10 case RPAREN : return " `)' " ;
11 case EQL : return " `= ' " ;
12 case PERIOD : return " . ";
13 default : return " < unknown > " ;
14 }
15 }
16
17 public void close () throws IOException {
18 in . close (); }
19 } R. EZZAHIR Analyse Lexicale
Introduction
Notion de langage
Automates Finis Déterministes
Automates Finis non-déterministes
Ambiguïté Lexicale
Minimisation d'un AFD
Programme de teste
0 0 0 0 1 5 6 9 erreur erreur
1 4* 4* 4* 4* 2 3 4* 4* 4*
6 8* 8* 8* 8* 7 8* 8* 8* 8*
9 10* 10* 10* 10* 10* 10* 9 9 10
blanc
début < =
0 1 2 render INFEG
>
3 render DIFF
autre
4 render INF
=
5 render EGAL
> =
6 7 render SUPEG
autre
chire 8 render SUP
lettre autre
9 10 render IDENTIF
Dans cet exemple, on peut obtenir un analyseur qui peut être plus rapide
que celui de la première manière puisque l'essentiel du travail de
l'analyseur se réduira à répéter bêtement l'action état =
transit[état][nextCh( )] jusqu'à tomber sur un état nal.
Un tableau identié par terminal et indexé par les états, est déni par :
1 class ScannerAuomata {
2 static final int NBR_ETATS = 10
3 static final int NBR_CARS = 256
4 int transit [ NBR_ETATS ][ NBR_CARS ];
5 int terminal [ NBR_ETATS + 1];
6 public ScannerAuomata (){
7 transit = new int [ NBR_ETATS ][ NBR_CARS ];
8 terminal = new int [ NBR_ETATS +1];
9 int i , j ;
10 for ( i = 0; i < NBR_ETATS ; i ++)
11 terminal [ i ] = 0;
12
13 terminal [2] = INFEG + 1; terminal [3] = DIFF + 1;
14 terminal [4] = - ( INF + 1); terminal [5] = EGAL + 1;
15 terminal [7] = SUPEG + 1; terminal [8] = - ( SUP + 1);
16 terminal [10] = - ( IDENTIF + 1);
17 terminal [ NBR_ETATS ] = ERREUR + 1;
R. EZZAHIR Analyse Lexicale
Introduction
Notion de langage
Automates Finis Déterministes
Automates Finis non-déterministes
Ambiguïté Lexicale
Minimisation d'un AFD
Soit l'AFD M
1 = {1, 2, 3, 4, 5}; {a, b }; 1; {1, 34}; δ}
Soit l'AFD M
1 = {1, 2, 3, 4, 5}; {a, b }; 1; {1, 34}; δ}
Soit l'AFD M
1 = {1, 2, 3, 4, 5}; {a, b }; 1; {1, 34}; δ}
Regle 1
Soit M un AFD et soit M 0 l'AFD obtenu en enlevant tous les états
inaccessibles de M. Alors L(M) = L( M
0 ).
Regle 1
Soit M un AFD et soit M 0 l'AFD obtenu en enlevant tous les états
inaccessibles de M. Alors L(M) = L( M
0 ).
Regle 1
Soit M un AFD et soit M 0 l'AFD obtenu en enlevant tous les états
inaccessibles de M. Alors L(M) = L( M
0 ).
Remarque
si i et j sont deux états équivalents d'un automate donne, alors a chaque
fois que l'on se retrouve dans l'état i, on pourrait poursuivre l'éxecution à
partir de l'état j sans rien changer au resultat. Cela conduit a
l'observation suivante :
Observation
Il est toujours possible d'ajouter des ε−transitions entre deux états
équivalents sans rien changer au langage reconnu.
Remarque
si i et j sont deux états équivalents d'un automate donne, alors a chaque
fois que l'on se retrouve dans l'état i, on pourrait poursuivre l'éxecution à
partir de l'état j sans rien changer au resultat. Cela conduit a
l'observation suivante :
Observation
Il est toujours possible d'ajouter des ε−transitions entre deux états
équivalents sans rien changer au langage reconnu.
Remarque
si i et j sont deux états équivalents d'un automate donne, alors a chaque
fois que l'on se retrouve dans l'état i, on pourrait poursuivre l'éxecution à
partir de l'état j sans rien changer au resultat. Cela conduit a
l'observation suivante :
Observation
Il est toujours possible d'ajouter des ε−transitions entre deux états
équivalents sans rien changer au langage reconnu.
Remarque
L'automate obtenu n'est plus deterministe. Cependant, on peut obtenir
un AFD M 5 a partir de L'AFN M 4 en utilisant la methode de
détermination.
Remarque
L'automate obtenu n'est plus deterministe. Cependant, on peut obtenir
un AFD M 5 a partir de L'AFN M 4 en utilisant la methode de
détermination.
L'AFD M
5 obtenu a partir de L'AFN M4
La suite
Analyse Santaxique
Lectures complémentaires I