Search code examples
compiler-errorsbisonflex-lexer

flex.l:17:8: error: invalid type argument of ‘->’ (have ‘YYSTYPE’ {aka ‘int’}) "int" {yylval->fn = 11; return TYPE; }


I'm tring to create a compiler in flex and bison, but when i try to compile my code I'm getting this type of error how can i resolve this problem??

flex.l:17:8: error: invalid type argument of ‘->’ (have ‘YYSTYPE’ {aka ‘int’}) "int" {yylval->fn = 11; return TYPE; }

this is flex file

%option header-file="flex.lex.h"
%{
  #include "bison.tab.h"
  #include "dichiarazioni.h"
 %}
 %%
 "global_variables" { return VAR; }
 "events"   { return EVENT; }
 "commands"             { return COMMAND; }
 "state"                    { return STATE; }
 "actions"              { return ACTION; }
 "char" {yylval->fn = 10; return TYPE; }
 "int"  {yylval->fn = 11; return TYPE; }
 "float"    { yylval->fn = 12; return TYPE; }
 "double" { yylval->fn = 13; return TYPE; }
 "void" { yylval->fn = 14; return TYPE; }
 "inc"                  { yylval->fn = 24; return OPERATORE; }
 "dec"                  { yylval->fn = 25; return OPERATORE; }
 "set"                  { yylval->fn = 26; return OPERATORE; } 
 print_schermo_message  { yylval->fn = 40; return PRINT; }
 [0-9]+                     { return NUMBER; }
 ([a-z]+[_a-z0-9]*)     { return ALPHA; }
 "+"                        { yylval->fn = 20; return OPERATORE; }
 "-"                        { yylval->fn = 21; return OPERATORE; }
 "*"                        { yylval->fn = 22; return OPERATORE; }
 "/"                     { yylval->fn = 23; return OPERATORE; }
 ">"            { yylval->fn = 1; return CMP; }
 "<"            { yylval->fn = 2; return CMP; }
 "!="           { yylval->fn = 3; return CMP; }
 "=="           { yylval->fn = 4; return CMP; }
 ">="           { yylval->fn = 5; return CMP; }
 "<="           { yylval->fn = 6; return CMP; }
 "&&"           { yylval->fn = 7; return CMP; }
 "||"           { yylval->fn = 8; return CMP; }
%%

Solution

  • The type of the semantic value (yylval) is normally defined in tbe grammar file using a %union declaration. Since you don't show your grammar file, it's impossible to be more precise about what's wrong but the fact that the compiler believes that yylval is an int suggests that you have not provided any declaration. (int is the default type.)

    In any case, unless you have requested a reentrant parser, yylval will normally be a union (or scalar) and not a pointer.

    Please see the bison manual for more details on how you declare and use semantic values.