%option nounput noinput %{ #include "proto-color.h" void echo(char *lex_cat) { fprintf(stdout,GREEN("[%s:%s]"), lex_cat, yytext); } void echo2(char *lex_cat) { fprintf(stdout,RED("[%s:%s]"), lex_cat, yytext); } int nb_v = 0, nb_mots = 0, nb_lignes = 0, nb_punct = 0; %} /* Accents : a partir des presents dans fichiers de test */ ACC_latin1 [\340\351\364] ACC_latin1bis [àéô] ACC_latin15 [\275]|{ACC_latin1} ACC_utf8 (\303\240|\303\251|\303\264|\305\223) ACC_utf8bis (à|é|ô|Å“) CAR [[:alpha:]]|{ACC_latin15}|{ACC_utf8} /*autre approche par negation ou separateur : */ CARbis [^ \n\t[:punct:]] MOT {CAR}* MOT_COMP {MOT}(-{MOT})* %% [vV]{MOT} {echo2("VMot"); nb_mots ++; nb_v ++;} {MOT} {echo("MOT"); nb_mots ++;} [[:punct:]] {nb_punct ++;} /* les - ou ' interne au mot ne sont pas compté */ \n nb_lignes++; ECHO; [ \t] . echo("UNK"); %% #include extern int errno; char **files; int file_cur, file_max; int open_next_file() { if (file_cur != 1 ) fclose(yyin); if (file_cur <= file_max){ if ( (yyin = fopen(files[file_cur],"r")) == 0 ) { perror(files[file_cur]); exit(1); } return 0; } else return 1; } int yywrap(void) { char * filename; if (file_max==0) filename="stdin"; else filename = files[file_cur]; printf("%s : nb_v = %d, nb_mots = %d, nb_punct = %d, nb_lignes = %d\n", filename , nb_v, nb_mots, nb_punct, nb_lignes); nb_v = 0; nb_mots = 0; nb_lignes = 0; nb_punct = 0; file_cur++; return open_next_file(); } int main (int argc, char *argv[]) { file_cur=1; file_max = argc-1; files = argv; if ( file_max > 0 ) open_next_file(); while (yylex()!=0) ; return 0; }