0

In my lex file I have:

[a-zA-Z][a-zA-Z0-9]*
{
    yylval.val = _strdup(yytext); // <- error here
    yylval.length = yylen;
    return id;
}

... for parsing text such as "myid2"

This is causing a compilation error:

error C2143: syntax error : missing ';' before '='

How to do this correctly so that I can pass on the id as a character string (char *) in the yacc file?

I am using win_flex and win_bison.

UPDATE: I put the statements on one line in the lex file:

[a-zA-Z][a-zA-Z0-9]* { yylval.val = _strdup(yytext); yylval.length = yylen; return id; }

Now I get the compilation errors:

error C2039: 'length' : is not a member of 'YYSTYPE'
error C2039: 'val' : is not a member of 'YYSTYPE'
error C2065: 'yylen' : undeclared identifier
gornvix
  • 3,154
  • 6
  • 35
  • 74
  • 2
    Are you sure you aren't missing any `;` in other nearby chunks of code? This particular piece looks ok. – Filipe Gonçalves Jan 14 '15 at 17:06
  • 1
    If the error occured in C compilation phase, you could try to look at the file generated by win_flex, to clearly know where C found it, and from them come back to the lex source. – Serge Ballesta Jan 14 '15 at 17:30
  • 1
    The statement(s) should begin on the same line as the pattern. Are you sure that you defined the *yystype* in your bison fie and include the header generated ? – Jean-Baptiste Yunès Jan 14 '15 at 17:40

1 Answers1

0

I found the solution to this problem. The member of yylval must correspond to the union declaration as follows, in the lex file:

[a-zA-Z][a-zA-Z0-9]* { yylval->str = _strdup(yytext); return id; }

...and in the yacc file:

%union
{
    char *str;
...
}
gornvix
  • 3,154
  • 6
  • 35
  • 74