0

There is a warning that I could not get rid of it from this code. And this is the code that I could provide :

%{
#include <stdlib.h>
#include <stdio.h>
#include "y.tab.h"
%}

%%

[1-9]+          {
                        yylval.iVal = atoi(yytext);
                        printf("NUMBER\n");
                        return NUMBER;
                }
[a-z]           {
                        printf("VARIABLE\n");
                        yylval.cVal = yytext;
                        return VARIABLE;
                }
[-()<>=+/*\n]   {
                        printf("OPERATOR\n");
                        return *yytext;
                 }
"^"             {
                        printf("POW\n");
                        return *yytext;
                }
[ \t]+          ;      // Skip whitespaces.
.               yyerror("Unknown character.\n");

%%

int yywrap(void)
{
     return 1;
}

I got the warning on this part : yylval.cVal = yytext;. How could it go wrong? Thanks in advance.

AmigaAbattoir
  • 632
  • 8
  • 21
Willze Fortner
  • 141
  • 2
  • 2
  • 12
  • 1
    What did you set cVal to in the union? – lostbard Jul 06 '17 at 19:33
  • What type is `yylval.cVal`? How were we supposed to guess that? The trouble is that the types of `yylval.cVal` and `yytext` are not compatible. Since `yytext` is either an array or a pointer (so on the RHS of an assignment, it is treated as a pointer either way), presumably `yylval.cVal` is a non-pointer type. Maybe you wanted `yylval.cVal = yytext[0];`? Or maybe you really need to duplicate the string and fix the type of `yylval.cVal`. – Jonathan Leffler Jul 06 '17 at 19:33
  • Possible duplicate of [How to get string value of token in flex and bison?](https://stackoverflow.com/questions/8632516/how-to-get-string-value-of-token-in-flex-and-bison) – r3mainer Jul 06 '17 at 19:35

1 Answers1

0

There's not really enough information(a) in the question to be certain but you're getting a message that normally involves assigning a pointer to an integral type, along with the following information:

regex field likely type based on other columns
[1-9]+ iVal int
[a-z] cVal char

That means the yytext variable (char *) is being directly assigned to cVal a (very likely) char variable.

The best way to fix this is simply get the first character from the memory pointed to by yytext, so that it is the correct type. That would be something like:

yylval.cVal = *yytext; // or yytext[0]

(a) The type of yytext can be inferred by the fact you're doing atoi() on it successfully. The type of yylval.cVal is not provided but is likely to be char given the iVal/cVal naming.

It's also very common practice to use yylval as a union which can contain various parameter types when parsing computer languages, as you appear to be doing here.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953