%{
#include <stdio.h>
%}
%option noyywrap
%option yylineno
%%
[0-9]+ { printf("NOMBRE(%s)\n", yytext); }
[a-zA-Z_][a-zA-Z0-9_]* { printf("IDENT(%s)\n", yytext); }
[-+*/=;] { printf("SYMBOLE(%s)\n", yytext); }
[ \t\n]+ { /* on ignore les blancs */ }
. { fprintf(stderr, "Erreur lexicale ligne %d :
'%s'\n", yylineno, yytext); }
%%
int main(void) {
yylex();
return 0;
}
%% %{
%} yytext
yylineno
flex lexer.l
gcc lex.yy.c -o lexer
echo "x = 12 + y;" | ./lexer
Exercices.
int char void if else while return
/* ... */ // ...
\n yyleng
Critère de réussite @
Étape 2 : flex + bison sur une calculatrice (2 jours)
La notion.grammaire
+ .y calc.tab.c