2
votes

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".

3
To answer the question, no I don't have any idea of what's wrong. Do you have any actual evidence that something is going wrong? Such as diagnostic output, or test results from calc_pol?David Thornley
flex calc_pol.l: calc_pol.l:18: règle non reconnueNatim
Which is line 18? In a comment below, you identify it as {reel} { yylval = atof(yytext); return NOMBRE; }; is that correct? Also, Google tells me that the error message translates to "unrecognized rule".David Thornley

3 Answers

6
votes

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

1 2

and

12
2
votes

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

0
votes

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?