views:

134

answers:

3

Hi,

I am trying to do a little exercice in FLEX and BISON.

Here is the code I wrote :

calc_pol.y

%{
#define YYSTYPE double
#include "calc_pol.tab.h"
#include <math.h>
#include <stdlib.h>
%}
%start line
%token NOMBRE
%token FIN
%%
line: exp '\n' { printf("\t%.2lf\n", $1); };
exp: exp exp '+' { $$ = $1 + $2 ;}
     | exp exp '-' { $$ = $1 - $2 ;}
     | exp exp '*' { $$ = $1 * $2 ;}
     | exp exp '/' { $$ = $1 / $2 ;}
     | exp exp '^' { $$ = pow($1, $2) ;}
     | NOMBRE;
%%

calc_pol.l

%{
    #include "calc_pol.tab.h"
    #include <stdlib.h>
    #include <stdio.h>
    extern YYSTYPE yylval;
%}

blancs  [ \t]+

chiffre [0-9]
entier  [+-]?[1-9][0-9]* | 0
reel    {entier}('.'{entier})?

%%

{blancs} 
{reel}  { yylval = atof(yytext); return NOMBRE; }
\n      { return FIN; }
.       { return yytext[0]; }
%%

Makefile

all: calc_pol.tab.c lex.yy.c
        gcc -o calc_pol $< -ly -lfl -lm

calc_pol.tab.c: calc_pol.y
        bison -d calc_pol.y

lex.yy.c: calc_pol.l
        flex calc_pol.l

Do you have any idea of what's wrong ? Thanks

Edited: The error message is
flex calc_pol.l: calc_pol.l:18: règle non reconnue
Line 18 is the line beginning with {reel}, and the error message translates to English as "unrecognized rule".

A: 

in calc_pol.l

{blancs} { ; } <------- !!
{reel}  { yylval = atof(yytext); return NOMBRE; }
\n      { return FIN; }
.       { return yytext[0]; }

I would be inclined to spot that you're missing an action for the '{blancs}'...

Edit: As more information came out of the OP the problem....is here....

entier  [+-]?[1-9][0-9]+
reel    {entier}('.'{entier})?

I would take out the last bit at the end of 'entier' as it looks to be a greedy match and hence not see the real number like '123.234'...what do you think?

tommieb75
It is just to notice to flex that in case of blank, we don't want to do anything.The problem comes from the {reel} line.
Natim
0 is also an integer. I just don't want some integer like 0000 but this rule seams good for me. Anyway, it doesn't change the error.
Natim
@Natim: the ('.'{entier})? means zero or 1, so it should accept an integer....test cases would be -23, 23, 23.45, -123.34, 123.34....
tommieb75
The definition using entier after the . is wrong you are right. I changed like this `reel {entier}('.'[0-9]+)?`
Natim
+2  A: 

I don't want to break pleasure of flash of inspiration, that is why only hint: what the difference between

1 2

and

12
Dewfy
1 and 2 are two operand then 12 is only one. I don't get your point.
Natim
+1 because this should have been credit for the answer, not his OWN answer :)
chucknelson
+1  A: 

The problem came from the space between | in the entier rules

Natim