%option nounput noinput %{ void echo(char *lex_cat) { fprintf(stderr,"[%s:%s]", lex_cat, yytext); } void echonl() { fprintf(stderr,"[\\n]\n"); } int nbmot=0, nbcar=0, nbligne=0; %} %s CORPS %s HEAD /* Etat INITIAL -> parsing des entetes RFC822 */ /* Etat HEAD -> Interieur d'une entete RFC822 */ /* Etat CORPS -> Corps du message */ %option 7bit ASCII7 [\x00-\x7F] HEADCAR [\x21-\x39\x3B-\x7E] HNAME {HEADCAR}* HLINE .* HCONT \n[ \t] MOT [a-zA-Z]+ SEP [^a-zA-Z\n] /* definition simpliste pour les mots, alphabetique only */ /* de facon generale on doit avoir MOT + SEP + '\n' == ASCII7 */ /* mais on peut aussi utiliser une regle balai pour se passer de SEP+ */ %% ^{HNAME} {BEGIN(HEAD);printf("Champs = %s; ",yytext);} {MOT} {nbcar+=yyleng; nbmot++;} {SEP}+ {nbcar+=yyleng;} {HCONT} {nbcar+=yyleng;nbligne++;} \n\n {BEGIN(CORPS); /* end of headers */ printf("HEAD-WC : %d %d %d \n",nbligne+1,nbmot,nbcar); nbligne=nbmot=nbcar=0; } \n {BEGIN(INITIAL); /*next header*/ printf("HEAD-WC : %d %d %d \n",nbligne+1,nbmot,nbcar); nbligne=nbmot=nbcar=0; } {MOT} {nbcar+=yyleng; nbmot++;} {SEP}+ {nbcar+=yyleng;} \n {nbcar+=yyleng;nbligne++;} .|\n echo("INVALID"); %% int yywrap (void) {return 1;} int main(int argc, char **argv) { yylex(); printf("Ligne_vide-WC : 1 0 1\n"); /*pour le \n non compté dans \n\n */ printf("CORPS-WC : %d %d %d \n",nbligne,nbmot,nbcar); return 0; }