Nat*_*tim 2 calculator bison flex-lexer
我想在FLEX和BISON做一点练习.
这是我写的代码:
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;
%%
Run Code Online (Sandbox Code Playgroud)
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]; }
%%
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
你知道什么是错的吗?谢谢
编辑:错误消息是
flex calc_pol.l: calc_pol.l:18: règle non reconnue
第18行是以行开头的行{reel},错误消息转换为英语为"无法识别的规则".
我不想打破闪光灵感的快感,这就是为什么只提示:有什么区别
1 2
Run Code Online (Sandbox Code Playgroud)
和
12
Run Code Online (Sandbox Code Playgroud)