1

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

Bob Aman
  • 32,839
  • 9
  • 71
  • 95
Jack
  • 131,802
  • 30
  • 241
  • 343
  • If you are desperate, you can always use a pointer to void in the %union, then use static_cast (or something else) to get your ast_node pointer back. – Paul Oct 30 '09 at 23:34

0 Answers0