tp-langages-formels/lexical/example.l

30 lines
511 B
Plaintext

%option noyywrap
%{
int lines = 1; // On compte la première ligne
%}
DIG [0-9]
HEX 0x[0-9A-Fa-f]+
%%
{HEX} { printf("hex(%d)", strtol(yytext, NULL, 16)); }
{DIG}+ { printf("int(%d)", atoi(yytext)); }
"if" { printf("IF"); }
"then" { printf("THEN"); }
"else" { printf("ELSE"); }
\n { lines++; }
. { /* Pour tous les autres caractères, on ne fait rien */ }
%%
int main (int argc, char **argv)
{
if (argc > 1) yyin = fopen(argv[1], "r");
yylex();
puts("");
printf("%d lignes\n", lines);
return 0;
}