Comment faire avec flex bison ??????
ziedzico
Messages postés
393
Date d'inscription
Statut
Membre
Dernière intervention
-
Tantor8 Messages postés 46 Date d'inscription Statut Membre Dernière intervention -
Tantor8 Messages postés 46 Date d'inscription Statut Membre Dernière intervention -
/* scanner for a toy Pascal-like language */
%{
/* need this for the call to atof() below */
#include <math.h>
%}
DIGIT [0-9]
ID [a-z][a-z0-9]*
%%
{DIGIT}+ {
printf( "An integer: %s (%d)\n", yytext,
atoi( yytext ) );
}
{DIGIT}+"."{DIGIT}* {
printf( "A float: %s (%g)\n", yytext,
atof( yytext ) );
}
if|then|begin|end|procedure|function {
printf( "A keyword: %s\n", yytext );
}
{ID} printf( "An identifier: %s\n", yytext );
"+"|"-"|"*"|"/" printf( "An operator: %s\n", yytext );
"{"[\^{}}\n]*"}" /* eat up one-line comments */
[ \t\n]+ /* eat up whitespace */
. printf( "Unrecognized character: %s\n", yytext );
%%
main( argc, argv )
int argc;
char **argv;
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
}
bonjour , merci de m'expliquer comment compiler ,executer sans erreur cet exemple
NB : que je vois un erreur de type : fin de fichier inatendue
%{
/* need this for the call to atof() below */
#include <math.h>
%}
DIGIT [0-9]
ID [a-z][a-z0-9]*
%%
{DIGIT}+ {
printf( "An integer: %s (%d)\n", yytext,
atoi( yytext ) );
}
{DIGIT}+"."{DIGIT}* {
printf( "A float: %s (%g)\n", yytext,
atof( yytext ) );
}
if|then|begin|end|procedure|function {
printf( "A keyword: %s\n", yytext );
}
{ID} printf( "An identifier: %s\n", yytext );
"+"|"-"|"*"|"/" printf( "An operator: %s\n", yytext );
"{"[\^{}}\n]*"}" /* eat up one-line comments */
[ \t\n]+ /* eat up whitespace */
. printf( "Unrecognized character: %s\n", yytext );
%%
main( argc, argv )
int argc;
char **argv;
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
}
bonjour , merci de m'expliquer comment compiler ,executer sans erreur cet exemple
NB : que je vois un erreur de type : fin de fichier inatendue
Même si cela est un peu tard cela aidera peut-être d'autres personnes dans le même cas.
Pour ma part je programme en flex et bison dans le cadre d'un projet, et cela sous ubuntu.
Donc une fois dans le répertoire où est ton fichier (fic.l par exemple) contenant ton code tu doit exécuter la commande suivante:
flex fic.l
Ce qui va te créer un fichier lex.yy.c
Puis tu execute avec:
gcc lex.yy.c -lfl
Cela pour exécuter ton fichier. Après si tu veux executer du flex et du bison tu doit faire les commandes suivantes:
bison -d fic.y
flex fic.l
gcc fic.tab.c lex.yy.c -ll -o executable
puis tu execute avec:
./executable
Voila bonne continuation.