Arbre syntaxique d'une expression arithmétique avec l'outil bison/flex
Bonjour à tous, je suis un débutant dans l'utilisation des outils bison/flex ou yacc/lex, et j'ai besoin de votre aide pour pouvoir génerer l'arbre syntaxique d'une expression arithmétique. voici mon fichier bison et flex:
<arith.y>
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| %{
#define YYSTYPE double
#include <math.h>
#include <stdio.h>
void yyerror (char const *);
%}
/* Bison declarations. */
%token NUM
%token PLUS
%token MINUS
%token TIMES
%token DIV
%token MOD
%token LPAR LPAR
%% /* The grammar follows. */
input : /* empty */
| input line
;
line : '\n'
| arithExpr '\n'
;
/* expression arithmetique*/
arithExpr:arithExpr PLUS terme
|arithExpr MINUS terme
|terme
;
terme : terme TIMES facteur
| terme DIV facteur
| terme MOD facteur
| facteur
;
facteur : LPAR arithExpr RPAR
| expr
;
expr:NUM
;
%%
void yyerror (char const *s)
{
fprintf (stderr, "%s\n", s);
}
int main (int argc, char * argV[])
{
if(argc > 1)
yyin = fopen(argV[1],"r");
return yyparse ();
} |
<arith.l>
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| %{
#define YYSTYPE int
#include <stdio.h>
#include <math.h>
#include "arith.tab.h"
extern int yyval;
%}
%%
"+" {return PLUS;}
"-" {return MINUS;}
"/" {return DIV;}
"*" {return TIMES;}
"%" {return MOD;}
\( {return LPAR;}
\) {return RPAR;}
[0-9][0-9]* {
yylval.Reel = atof(yytext);
return Num;}
%% |