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.

3
  • 1
    What did you set cVal to in the union? Commented Jul 6, 2017 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. Commented Jul 6, 2017 at 19:33
  • Possible duplicate of How to get string value of token in flex and bison? Commented Jul 6, 2017 at 19:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.