user1039063
user1039063

Reputation: 211

bison's error: request for member ‘charTokens’ in something not a structure or union

I have this problem with bison/flex (I've seen other posts, but I don't define YYSTYPE anywhere, so that's not the issue here). I want to pass variables from the lexer to .y using %union. This is what I have

%{
#include "simple-expr.tab.h"
#include <math.h>
extern double vbltable[26];
extern int yyval;
%}

%%
([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) { yyval.integerID = atoi(yytext); return ID; }
\* { yyval.charTokens = yytext; return TIMES; }
\+ { yyval.charTokens = yytext; return PLUS; }
\( { yyval.charTokens = yytext; return LPAREN; }
\) { yyval.charTokens = yytext; return RPAREN; }
[ \t\n] ;
%%

and the yacc:

%{
%}

%union {
      int integerID;
      char* charTokens;
}

%token <charTokens> PLUS TIMES LPAREN RPAREN 
%token <integerID> ID

%%
e :  e PLUS t { printf("FROM THE yypars.y %c", PLUS); }
    | t
      ;
t : t TIMES f
    | f
      ;
f : LPAREN e RPAREN
    | ID
    ;

%%

These are the errors I'm getting:

simple-expr.lex:9:8: error: request for member ‘integerID’ in something not a structure or union simple-expr.lex:10:8: error: request for member ‘charTokens’ in something not a structure or union simple-expr.lex:11:8: error: request for member ‘charTokens’ in something not a structure or union simple-expr.lex:12:8: error: request for member ‘charTokens’ in something not a structure or union simple-expr.lex:13:8: error: request for member ‘charTokens’ in something not a structure or union make: * [simple-expr] Error 1

As I said before - I don't define YYSTYPE anywhere, so this shouldn't be a problems.

Upvotes: 0

Views: 669

Answers (1)

john
john

Reputation: 87972

The name of the variable to pass information from the lexer to the parser is yylval not yyval. It is automatically declared with the correct type in *.tab.h. So this should work

%{
#include "simple-expr.tab.h"
#include <math.h>
extern double vbltable[26];
%}

%%
([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) { yylval.integerID = atoi(yytext); return ID; }
\* { yylval.charTokens = yytext; return TIMES; }
\+ { yylval.charTokens = yytext; return PLUS; }
\( { yylval.charTokens = yytext; return LPAREN; }
\) { yylval.charTokens = yytext; return RPAREN; }
[ \t\n] ;
%%

Upvotes: 1

Related Questions