'Platformio compilation error in bison file

I'm using bison with flex combined with Arduino framework in Platformio. Compiling .y and .l files goes without a problem, but when I'm trying to make a Build in Platformio I got those errors:

error: cannot convert 'str*' to 'strn*' for argument '1' to 'ast* newstrref(strn*)'
error: cannot convert 'str*' to 'strn*' for argument '1' to 'ast* newstrasgn(strn*, ast*)'

below declarations and def's of those functions:

struct ast *newstrref(struct strn *a);
struct ast *newstrasgn(struct strn *, struct ast *);

struct ast *newstrref(struct strn *s)
{
   strref *a = (strref *)malloc(sizeof(strref));

   if(!a) {
      yyerror(OOF_MSG);
      exit(0);
          }

   a->nodetype = STRING_REF_TYPE;
   a->s = s;

   return (ast *)a;
}

struct ast *newstrasgn(struct strn *s, struct ast *v)
{
   strasgn *a = (strasgn *)malloc(sizeof(strasgn));

   if(!a) {
       yyerror(OOF_MSG);
       exit(0);
          }

   a->nodetype = STRING_ASGN_TYPE;
   a->s = s;
   a->v = v;

   return (ast *)a; 
 }

def's of structs:

struct strref {
   int nodetype;        
   struct strn *s;
  };

struct strasgn {
  int nodetype;         /* type = */
  struct strn *s;
  struct ast *v;        /* value */
 };
 struct strn {
     char *name;
     char *value; 
 };

Actually there is no str struct thus I have no idea where it came from. Also below lines in .y which invoke error:

strast: 
STRING                     { $$ = newstr($1); }
| NAMESTR                    { $$ = newstrref($1); }


Solution 1:[1]

Evidently, the cause of the error is in the part of the .y file which you chose not to share. So it's not going to be possible to say for sure what the problem is. But I guess you will find a typo in your %token declarations. [Note 1]

Note that in both C and C++ (and you seem to be compiling with C++, judging from the error message), it's sufficient to just mention struct str for it to leap into existence as an incomplete type. Moreover, it's legal (and common) to create a pointer to an incomplete type. So if somewhere in your code you write struct str* instead of struct strn*, that will not create any kind of error or warning -- until you try to use the incorrectly declared variable. That looks to me like what's going on here. But it's just a guess.

Writing in C, I prefer to always typedef my structs, using the same name for the tag and the typedef. So I would have written

typedef struct ast ast;
typedef struct strn strn;
typedef struct strasgn strasgn;
struct strasgn {
  int nodetype;     /* type = */
  strn *s;
  ast *v;           /* value */
 };

One of the reasons I like that style is that it gives me some protection from this sort of typo. Unlike struct typo, typing a typedef name incorrectly usually leads to an undefined symbol warning. But that's just me. YMMV. (And it doesn't help so much in C++, where the typedefs are redundant.)

(Actually I prefer to write typenames in CamelCase, but that's not relevant here.)


Notes

  1. Particularly when you don't know where the error is, posting a Minimal Reproducible Example is a much better strategy than posting a random excerpt of your code which might have little to do with the actual error. That's the general guideline for StackOverflow posts.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rici