i can't find a solution for the error unrecognized rule

bison flex-lexer

63 观看


3 作者的声誉

"lexicale.l", line 13: unrecognized rule
"lexicale.l", line 13: unrecognized rule

the line 13 is

{idf} {printf("indetificateur\n");}

the code is :

    #include "stdio.h"
    #include "stdlib.h"
int [0-9]+
float [0-9]+("."[0-9]+)*
bool 0 | 1
idf [A-Z] ( [0-9a-zA-Z] ( _ )? [0-9a-zA-Z] ) {0,11}
{int} {printf("int\n");}
{float} {printf("float\n");}
{bool} {printf("bool\n");}
{idf} {printf("indetificateur\n");}
.     {printf("erreur\n");}
作者: SiRine Fdl 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


167065 作者的声誉


(F)lex patterns cannot contain unquoted whitespace characters.

Both your definition bool and your definition idf contain unquoted whitespace. You should remove the whitespace from both definitions.

Because of a quirk in the way flex parses its own input file, the line number reported for syntax errors in pattern definitions is not very accurate. In particular, errors are reported when the definition is expanded, not when it is defined. Also, as in this case, the error is often reported on the line after the expansion of the incorrect definition.

On the whole, I personally find definitions unuseful; there is no obvious benefit in most cases (such as this one) and the oddities in the implementation can cause confusion and unexpected results.

If you are using flex, and you want to use whitespace in patterns, you can surround the pattern with (?x:…) to set the "eXtended syntax" flag:

idf (?x: [A-Z] ( [0-9a-zA-Z] ( _ )? [0-9a-zA-Z] ) {0,11} )

By the way, your bool pattern 0 | 1 could be more simply written as simply [01]. However, it will not recognize anything unless the rule is placed before the rule which matches int, because the int rule will definitely match 0 or 1, and it appears earlier in the file. (It's the order of rules that matters, not definitions. That's another possible confusion which results from unnecessary use of definitions.)

作者: rici 发布者: 27.12.2017 07:31