Search code examples
parsingbisonflex-lexer

Unrecognized rule in Flex lexer


In the process of making an XML parser :

As the title suggests I have documented the rules as shown in my code below , but flex seems to miss a specific one.

Error : Cmd Error Img

The line in question is :

{boolean}        {yylval.booleanval  = strdup(yytext); if(err==1){printf("\t\t\t\t\t\t");}; return BOOLEAN;}```

When clearly declared flex seems to disregard it, where for the other rules no such problem arises.

Flex Code :

    %option noyywrap
    %option yylineno

    string   [_a-zA-Z][_a-zA-Z0-9]*
    digit    [0-9]
    integer  {digit}+
    boolean  "True" | "False"
    text ({string}| )*

    %%
    .                          {printf("%s",yytext);}
    {boolean}                  {yylval.booleanval  = strdup(yytext); if(err==1){printf("\t\t\t\t\t\t");}; return BOOLEAN;}
    {integer}                  {return INT;}
    {string}                   {return STRING;}

    %%

Solution

  • Rereading the question, I think there is a terminology problem. The rule is

    {boolean}   {yylval.booleanval  = strdup(yytext); if(err==1){printf("\t\t\t\t\t\t");}; return BOOLEAN;}
    

    Like all rule, that rule consists of *pattern" and an action. The pattern {boolean} consists only of a macro expansion. Once the macro is expanded, the line can no longer be recognised as a rule because of stray whitespace in the macro's definition, as I explained in the original answer below:


    As indicated by the error message, the problem is the pattern in line 22 of your flex file, which contains a macro expansion of boolean:

    boolean  "True" | "False"
    

    Flex patterns may not contain unquoted whitespace, whether entered directly or through a macro.

    If you insist on using a macro, it could be:

    boolean  True|False
    

    Although nothing prevents you from inserting the pattern directly in the rule:

    True|False   {yylval.booleanval  = strdup(yytext); if(err==1){printf("\t\t\t\t\t\t");}; return BOOLEAN;}