views:

466

answers:

1

Hello, I was trying to use an abstract syntax tree in a bison parser, so I tried to use %union directive. Grammar file looks like this:

%{


#include "compiler.h"
#include "ast.h"
#include "common.h"

static bool verbose = true;

extern "C"
{
  int cyylex(void);  
  void cyyerror(const char *s);
}  
%}

%union
{
    ast_node *node;
    unsigned char retn_type; 
}

At current state I was trying to use just structs so in file ast.h I have the following declaration:

#ifndef AST_H_
#define AST_H_

#include <string>
#include "common.h"

enum RETN_TYPE { E_VOID, E_FLOAT, E_VECTOR, E_POINT, E_COLOR, E_COLORW, E_BOOL};
enum AST_TYPE { AST_FLOAT, AST_INT, AST_ID, AST_FUNC };

struct ast_node
{
    u8 node_type;

    union
    { 
     float f;
     int i;
     u8 *id;

     struct
     {
      u8 *name;
      u8 retn_type;
     } func;
    };
};

#endif

I'm using g++ instead of gcc and it should work (I found similar examples over the web) but it seems that ast_node is not known when defining YYSTYPE because I get this error:

/shady_parser/shady.y:22: error: ISO C++ forbids declaration of 'ast_node' with no type ./shady_parser/shady.y:22: error: expected ';' before '*' token ./shady_parser/shady.l: In function 'int cyylex()': ./shady_parser/shady.l:35: error: 'union YYSTYPE' has no member named 'node' ./shady_parser/shady.l:37: error: 'union YYSTYPE' has no member named 'node' ./shady_parser/shady.l:38: error: 'union YYSTYPE' has no member named 'node'

Why this happens?

Then is it possible to define ast_node as a class and use a pointer to it instead that pointers to structs?

Thanks in advance, Jack

A: 

I think my answer to this question applies.

tim
actually the problem was that he wants union definition in C-style. I was using 'astnode* node' instead that 'struct astnode* node'. Then I've also been able to use a class..
Jack