Je souhaite une très bonne Année 2007 à tous les membres du groupe. Que le seigneur vous guide dans toutes vos entreprises et vous procure succès!
Cher membre j’ai un peu de difficulté à construire un arbre syntaxique abstrait des expressions arithmétiques avec les outils YACC/LEX ou BISON/FLEX en précisant le type axiome et des différents tokens. Aidez un membre en détresse, merci d’avance.
voici:
******************************************************
le fichier yacc ou bison "arith.y"
*******************************************************
%{
#include <stdio.h>
#include <stdlib.h>
#include "lex.yy.c"
#include "arith.h" /* Contains definition of ‘symrec’. */
void yyerror (char const *);
%}
%union
{
double val; /* For returning numbers. */
symrec *tptr; /* For returning symbol-table pointers. */
}
/* Bison declarations. */
%token <val> NUM
%token <tptr> VARIABLE
%token PLUS MOINS DIV MULT
%token LPAR RPAR
%start arith
%% /* The grammar follows. */
arith : exp PLUS exp
| exp MOINS exp
| exp MULT exp
| exp DIV exp
| LPAR exp RPAR
;
exp : NUM { $$ = $1; }
| VARIABLE { $$ = $1->value.var; }
;
%%
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 ();
}
**********************************************************
le fichier "arith.h"
********************************************************
#include <stdio.h>
struct symrec
{ char *name; /* name of symbol */
union
{ double var; /* value of a VARIABLE */
} value;
struct symrec *next;
};
typedef struct symrec symrec;
}
*********************************************************
Le fichier lex ou Flex arith.l
*********************************************************
%{
#include <stdio.h>
#include "arith.tab.h"
%}
%%
"+" {return PLUS;}
"-" {return MOINS;}
"/" {return DIV;}
"*" {return MULT;}
\( {return LPAR;}
\) {return RPAR;}
[0-9]+ {return NUM;}
[a-z][a-z0-9]* {return VARIABLE}
%%
Partager