Vous êtes sur la page 1sur 6

Correction serie flex compilation smi s5

YOUNES ADRAOUI

Exercice1

Exercice 2

%{

#include <stdio.h>

int prev_space = 1;

%}

%%

[[:space:]]+ {

if (!prev_space) {

printf(" ");

prev_space = 1;

[[:alpha:]]+ {

printf("%s", yytext);

prev_space = 0;

%%
int main(int argc, char *argv[]) {

yyin = fopen(argv[1], "r");

yylex();

fclose(yyin);

return 0;

Exercise 3

%{

#include <string.h>

%}

%%

[[:alpha:]]+ {

if (strlen(yytext) < 4) {

printf("%s ", yytext);

%%

int main(int argc, char *argv[]) {

yylex();

return 0;
}

%{

%}

%%

[[:alpha:]]+ {

if (yyleng < 4) {

printf("%s ", yytext);

%%

int main(int argc, char *argv[]) {

yylex();

return 0;

%{

%}

%%

[[:alpha:]]{1,3} {

printf("%s ", yytext);

%%
int main(int argc, char *argv[]) {

yylex();

return 0;

Execice 2

%{

#include <stdio.h>

int prev_space = 1;

%}

%%

[[:space:]]+ {

if (!prev_space) {

printf(" ");

prev_space = 1;

[[:alpha:]]+ {

printf("%s", yytext);

prev_space = 0;

%%

int main(int argc, char *argv[]) {

yyin = fopen(argv[1], "r");

yylex();

fclose(yyin);
return 0;

Exercice 4 : Ecrire un programme Flex qui extrait d’un fichier texte, le


mot le plus court, le mot le plus long et les mots qui sont des
palindromes. NB : un palindrome est un mot que l’on peut lire dans
les deux sens (de droite à gauche ou de gauche à droite), Ex :
lebel,elle,…etc

%{

#include <string.h>

#include <stdio.h>

#include <stdbool.h>

int min_length = -1;

int max_length = 0;

char min_word[100];

char max_word[100];

bool is_palindrome(const char *word) {

int length = strlen(word);

for (int i = 0; i < length / 2; i++) {

if (word[i] != word[length - i - 1]) {

return false;

return true;

%}
%%

[[:alpha:]]+ {

if (yyleng < min_length || min_length == -1) {

min_length = yyleng;

strcpy(min_word, yytext);

if (yyleng > max_length) {

max_length = yyleng;

strcpy(max_word, yytext);

if (is_palindrome(yytext)) {

printf("'%s' is a palindrome\n", yytext);

%%

int main(int argc, char *argv[]) {

yyin = fopen(argv[1], "r");

yylex();

fclose(yyin);

printf("Shortest word: %s\n", min_word);

printf("Longest word: %s\n", max_word);

return 0;

Vous aimerez peut-être aussi