3 avril 2014 : Écriture en C++ d'un analyseur ascendant

Jean-François Perrot

  1. La notation polonaise postfixée
  2. 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
avec les mêmes éléments (pile et lexeur) et la même organisation que pour l'écriture d'un analyseur descendant.

  1. La notation polonaise postfixée

    1. S -> S S op
    2. S -> cte
    3. 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


     
  2. La grammaire proposée par Louis Lecailliez

    1. S -> ( op2 S S )
    2. S -> ( op1 S )
    3. 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