Yacc, structure pointers in union

Asked by At

For some reason, the structure pointers in a union are seemingly not defined even though I include the .h beforehand.

%{
#include "agent.h"
.
.
.
%}
%union {int iValue; char sIndex; ASTnode *nPtr; litNode *litPtr; ruleNode *rulePtr; exprNode *expPtr; actNode *actPtr; conNode *conPtr};
.
.
.
%%

However, the function definitions that use such structure pointers under the #include "agent.h" work fine. Why do I get the error:

y.tab.h:58:15: error: unknown type name ‘ASTnode’
 typedef union {int iValue; char sIndex; ASTnode *nPtr; litNode *litPtr; ruleNode *rulePtr; exprNode *expPtr; actNode *actPtr; conNode *conPtr} YYSTYPE;
               ^
y.tab.h:58:15: error: unknown type name ‘litNode’
y.tab.h:58:15: error: unknown type name ‘ruleNode’
y.tab.h:58:15: error: unknown type name ‘exprNode’
y.tab.h:58:15: error: unknown type name ‘actNode’
y.tab.h:58:15: error: unknown type name ‘conNode’

even though I have included the .h file?

I have seen other questions regarding the issue but they have solved it by including the header file, but for my case, it still does not work.

Thanks.

2 Answers

1
Luis Colorado On Best Solutions

The compiler is complaining about an undefined type (ASTnode) which probably you have defined as a struct, but not with a typedef (in C++ a struct gets automatically a type defined with the same name, but that's not true in C) or that you have mispelled when using it in the %union sentence of yacc.

The problem has nothing to do with yacc. You are using a type that is not declared anywhere. Search for the definition of type ASTnode in your headers. Probably you lack a typedef somewhere.

The same applies to the types

  • litNode
  • ruleNode
  • exprNode
  • actNode
  • conNode

those types are application types, that you have to define before using them in the created union type. They are most probably defined in a .h file that you have to include at the beginning of each file where you include the generated y.tab.h and in the C part (the one surrounded by %{ and %}) to give the compiler access to those type definitions.

0
Huzo On

Seemingly, I have to include the .h file before the declaration of including y.tab.h. So, I have to also include the .h inside the Lex file, before calling include "y.tab.h"