Académique Documents
Professionnel Documents
Culture Documents
EXERCICE1 :
Question 1 :
Solution :
// compter le nombre de lignes, de mots et de caractres d'un fichier en
entre.
%{#include<stdio.h>
int nbreligne=0,nbremot=0,nbrechar=0; %}
%%
[^\t\n ]+ { nbremot++;nbrechar+=yyleng;}
\n
{ nbreligne++;nbrechar++;}
{ nbrechar++;}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file ){
printf("Ce fichier n'existe pas\n");
exit(-1) ;}
yyin=file ;
yylex();
printf("Ce fichier est contient :\n");
printf("- %d lignes \n",nbreligne);
printf("- %d mots \n",nbremot);
printf("- %d caracters \n",nbrechar);
fclose(file) ;}
return 0;
}
Question 2 :
Solution
// numrote les lignes dun fichier (hormis les lignes blanches).
%{#include<stdio.h> int i=1;%}
%%
^[\t ]*|
^\n
{}
^.*
{ printf("%d - %s",i++,yytext);}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1);}
yyin=file;
yylex();
fclose(file);}
return 0;}
Question 3 :
Solution
// imprime que les commentaires dun programmes. Ceux-ci sont compris
entre {}.
%{#include<stdio.h>%}
%%
"{"[^{}]+"}" { printf("%s\n",yytext);}
.|\n
{}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1);}
yyin=file ;
yylex();
fclose(file);}
return 0;
}
Question 4 :
Solution
//transforme un texte en remplaant le mot compilateur par interp si la ligne dbut
par a, par binterp si la ligne dbute par b et par cinterp! si la ligne dbute par c.
%{#include<stdio.h> int i; %}
%%
^[d-z]
{ ECHO;i=0;}
^a
{ ECHO;i=1;}
^b
{ ECHO;i=2;}
^c
{ ECHO;i=3;}
compilateur
{ if(i==0) ECHO;
if(i==1) printf("interp");
if(i==2) printf("binterp");
if(i==3) printf("cinterp!");}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1);}
yyin=file;
yylex() ;
fclose(file);}
return 0 ;}
Question 5 :
Solution
//supprime une suite despaces et les remplace par un seul espace
%{#include<stdio.h>%}
%%
[\t ]+ { printf(" ");}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1);}
yyin=file;
yylex();
fclose(file);}
return 0;}
Question 6 :
Solution
//convertit une chane en majuscule si elle est minuscules et
inversement.
%{#include<stdio.h>%}
%%
[a-z] { printf("%c",toupper(yytext[0]));}
[A-Z] { printf("%c",tolower(yytext[0]));}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1) ;}
yyin=file;
yylex();
fclose(file);}
return 0;}
EXERCICE2 :
Question 1 :
Solution
//reconnat une adresse lectronique simple.
%{#include<stdio.h>%}
%%
[a-zA-Z_0-9.]+@[a-zA-Z]+\.[a-zA-Z]+ {printf("%s\n",yytext);}
.|\n
{}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1) ;}
yyin=file ;
yylex();
fclose(file) ;}
return 0;}
Question 2 :
Solution
//reconnat les commentaires en langage C.
%{#include<stdio.h> int i; %}
%%
"/*"[^/**/]+"*/" | "//".*\n
{ printf("%s\n",yytext);}
.|\n
{}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1);}
yyin=file;
yylex();
fclose(file);}
return 0;}
Question 3 :
Solution
//reconnat les chanes contenant un nombre pair de a.
%{#include<stdio.h> int i,j=0;%}
%%
[a-zA-Z]+ { j=0;
for(i=0;i<yyleng;i++)
if(yytext[i]=='a') j++;
if(j%2==0) printf("%s\n",yytext); }
.|\n
{}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1) ;}
yyin=file;
yylex() ;
fclose(file) ;}
return 0;}
Question 4 :
Solution
//reconnat les chanes ne contenant pas les caractres x jusqu' z.
%{#include<stdio.h> int i,j;%}
%%
[a-zA-Z]+ {j=0; for(i=0;i<yyleng;i++){
if(yytext[i]=='x' ||yytext[i]=='y' || yytext[i]=='z') j++ ; }
if(j==0) printf("%s\n",yytext);}
.|\n
{}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n"); exit(-1);}
yyin=file;
yylex();
fclose(file);}
return 0;}
Question 5 :
Solution
//reconnat les chanes commenant par les lettres acc.
%{ #include<stdio.h> %}
%%
acc[a-zA-Z]* { printf("%s\n",yytext);}
.|\n
{}
%%
int main(int argc,char *argv[]){
if(argc>1){
FILE *file=fopen(argv[1],"r");
if(!file){
printf("Ce fichier n'existe pas\n");
exit(-1);}
yyin=file;
yylex();
fclose(file);}
return 0;}