0

I'm a little confused about how to specify my grammar member's type. I want to declare prog and decls as ASTNode. I'm gonna use these members for adding to a list or etc. But yacc can't recognize them as an ASTNode and I get type errors.

Here my tIdent,tCharConst,tIntConstant have some types but, how to give ASTNode type to my members.

%union{
  int ival;
  char cval;
  char *sval;
  struct ASTNode *nval;
}

%token <sval> tIdent
%token <cval> tCharConst
%token <ival> tIntConst

    prog          : decls ;
    decls         : /* empty */
                  | decls decl
                  ;
timrau
  • 22,578
  • 4
  • 51
  • 64
iva123
  • 3,395
  • 10
  • 47
  • 68

1 Answers1

2

At the very beginning of your .y file, you need something like

%{
struct ASTNode { ... };
%}

in order to declare the type of ASTNode. Or you might instead put it in a .h file:

%{
#include "astnode.h"
%}
%union { 
  ...
}
%term ...

and so on.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533