TP de compilation : une solution de l'exercice 3 du TP 2

3.1. 3.2.
...

3.3.
 
%{
int ferm=0;
int ouvr=0;
%}
%s anomalie
%%
[ \t]
<INITIAL>[0-9]+   {printf(" nombre %s\n",yytext);}
<INITIAL>"("      { printf("parenthese %s\n",yytext); ouvr++; }
<INITIAL>")"      { printf("parenthese %s\n",yytext);
                    ferm++;
                    if (ferm>ouvr)
                      { printf("trop de parenthèses fermantes\n"); BEGIN(anomalie); }
                  }
<INITIAL>"+"|"-"|"div"|"mod"   {printf(" operateur %s\n",yytext);}
<INITIAL>.   { printf("anomalie!\n"); BEGIN(anomalie); }
^FIN$       {return 0;}
<anomalie>.  
\n       { if (ferm<ouvr) {printf("trop de parentheses ouvrantes\n");} ferm=0; ouvr=0; BEGIN(INITIAL); }

© 2000, 2017 – A. Sigayret & al.