Syntax Error in lex / flex

1

Because my flex / lex recognizer has a syntax error when I try to recognize the WRITE 1 text

%{

#include "gram.h"

int yyerror(const char *s);

%}



/* letras [A-Za-z]+ */


/* id     ({letras})({letras}|{digito})* */

%%

"ESCREVER" {return   ESCREVER; }                                
"TERMINAR" {return TERMINAR; }

[0-9]+     { yylval.num =atoi(yytext);
             return NUM; }


[A-Za-z0-9]* { yylval.str=strdup(yytext);
                return TEXTO;}


"/" | 
"-" |
"+" |
"*" |
"=" |
.          {return yytext[0];}
[ \n\t]    {  }


%%


int yywrap(){ return 1; }
    
asked by anonymous 12.01.2017 / 13:30

1 answer

1

In the phrase ESCREVER 1; the space ' ' is matching the rule

.   {return yytext[0];}

that is before the rule that processes spaces and picks up string with the same length. This returns the ' ' symbol that should cause grammatical error.

Tip 1: Set the space rule before the rule that includes .

Tip 2: Instead of

"-" |
"+" |
"*" |
"=" |
.          {return yytext[0];}

usa

[\-+*=]   {return yytext[0];}

by placing only the special symbols provided in the grammar

    
12.01.2017 / 18:17