Flex Et Bison Exercice Calculatrice

2
Flex Flex Flex Flex et et et et Bison Bison Bison Bison exercice exercice exercice exercice : calculatrice Fichier Flex : %{ /* fichier dans lequel est défini NOMBRE */ #include "calc.tab.h" %} %% [0-9]+ { yylval = atoi(yytext); return NOMBRE;} [ \t] ; /* ignore les blancs et tabulations */ \n return 0; . return yytext[0]; %% _____________________________________________________________ ___ Fichier Bison : %{ #include<stdio.h> %} %token NOMBRE %start calcul %% calcul : expr {printf("Resultat = %d\n" , $1);} ; expr : expr '+' terme {$$ = $1 + $3;} | expr '-' terme {$$ = $1 - $3;} | terme {$$ = $1;} ;

Transcript of Flex Et Bison Exercice Calculatrice

Page 1: Flex Et Bison Exercice Calculatrice

FlexFlexFlexFlex etetetet BisonBisonBisonBison exerciceexerciceexerciceexercice : calculatrice

Fichier Flex :

%{

/* fichier dans lequel est défini NOMBRE */

#include "calc.tab.h"

%}

%%

[0-9]+ { yylval = atoi(yytext); return NOMBRE;}

[ \t] ; /* ignore les blancs et tabulations */

\n return 0;

. return yytext[0];

%%

________________________________________________________________

Fichier Bison :

%{

#include<stdio.h>

%}

%token NOMBRE

%start calcul

%%

calcul : expr {printf("Resultat = %d\n" , $1);}

;

expr : expr '+' terme {$$ = $1 + $3;}

| expr '-' terme {$$ = $1 - $3;}

| terme {$$ = $1;}

;

Page 2: Flex Et Bison Exercice Calculatrice

terme: terme '*' facteur {$$ = $1 * $3;}

| terme '/' facteur {$$ = $1 / $3;}

| facteur {$$ = $1;}

;

facteur: '(' expr ')' {$$ = $2;}

| '-' facteur {$$ = -$1;}

| NOMBRE {$$ = $1;}

;

%%

main ()

{

yyparse();

}

imaad
Texte écrit à la machine
by argoub el bachir