Syntax Error no lex/flex

Asked

Viewed 189 times

1

Because my flex/lex recognizer displayed a syntax error when trying to recognize the text WRITE 1;

%{

#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; }
  • 1

    Which error will return ?

  • Syntax Error , I already changed the grammar , but still the error persists

  • just viewing the grammar: the error message "syntax error ..." is probably issued by the parser (Yacc/Bison) that is calling this lexical parser. Guess: symbol ';' in the grammar of Yacc...

1 answer

1


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

.   {return yytext[0];}

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

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

Suggestion 2: instead of

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

uses

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

placing only the special symbols provided in the grammar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.