我试图做一个小exercice在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;
%%

<强> 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]; }
%%

<强>生成文件

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

你有什么不对任何想法? 感谢

编辑: 该错误消息是点击 flex calc_pol.l: calc_pol.l:18: règle non reconnue结果 第18行是与{reel}开头的行,并且该错误信息转换为英文为“未识别的规则”。

有帮助吗?

解决方案 2

在问题从|之间的空间排在entier规则

其他提示

我不想打破灵感闪现的快乐,这就是为什么只有提示:什么

之间的区别
1 2

12

在的 calc_pol.l

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

我会倾向于发现,你错过了“{}白朗”的动作......

编辑:随着越来越多的信息就出来了OP的问题....这里....

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

我会拿出最后一点在“entier”结束,因为它看起来是一个贪婪的比赛,因此看不到像“123.234” ......你觉得呢?

实数
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top