FLEX/BISON : Why my rule is not regonized?
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".I don't want to break pleasure of flash of inspiration, that is why only hint: what the difference between
1 2
and
12
The problem came from the space between |
in the entier
rules
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?
精彩评论