3 avril 2014 : Écriture en C++ d'un analyseur ascendant
- La notation polonaise postfixée
- La grammaire proposée par Louis
Lecailliez
Mise en œuvre en C++ de la stratégie d'analyse
ascendante,
sous la forme d'un programme C++ qui produit
- l'inverse de la dérivation droite associée au mot candidat, si ce
mot
appartient au langage
- une erreur dans le cas contraire.
avec les mêmes éléments (pile et lexeur) et la même organisation que
pour l'écriture d'un
analyseur
descendant.
-
S -> S S op
S -> cte
S -> var
Exemple : chaîne "12 xxx 89+* 678 yyy++
"
dans le fichier "e.txt
"
jfp$ ./pp < e.txt
xxx -- cte
xxx -- S
89 -- var S
89 -- S S
+ -- cte S S
+ -- S S S
* -- op S S S
* -- S S
678 -- op S S
678 -- S
yyy -- cte S
yyy -- S S
+ -- var S S
+ -- S S S
+ -- op S S S
+ -- S S
$ -- op S S
$ -- S
OK : 2 3 2 1 1 2 3 1 1
-
S -> ( op2 S S )
S -> ( op1 S )
S -> chn
avec op2
= '||
' ou '&&
',
et op1
= '~
'.
Exemple : chaîne "(~(|| xxx (&& (~ vvv) yyy)) )
"
dans le fichier "e.txt
"
jfp$ ./pp < e.txt
~ -- po
( -- op1 po
|| -- po op1 po
xxx -- op2 po op1 po
( -- chn op2 po op1 po
( -- S op2 po op1 po
&& -- po S op2 po op1 po
( -- op2 po S op2 po op1 po
~ -- po op2 po S op2 po op1 po
vvv -- op1 po op2 po S op2 po op1 po
) -- chn op1 po op2 po S op2 po op1 po
) -- T op1 po op2 po S op2 po op1 po
yyy -- pf T op1 po op2 po S op2 po op1 po
yyy -- S op2 po S op2 po op1 po
) -- chn S op2 po S op2 po op1 po
) -- S S op2 po S op2 po op1 po
) -- pf S S op2 po S op2 po op1 po
) -- S S op2 po op1 po
) -- pf S S op2 po op1 po
) -- T op1 po
$ -- pf T op1 po
$ -- S
OK : 3 3 2 3 1 1 2