我试图在野牛解析器中使用抽象语法树,所以我尝试使用%union
指令。语法文件如下所示:
%{
#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;
}
在当前状态下,我试图只使用结构,所以在文件中ast.h
我有以下声明:
#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
我使用的是 g++ 而不是 gcc,它应该可以工作(我在网上找到了类似的示例),但ast_node
在定义时似乎不知道,YYSTYPE
因为我收到了这个错误:
/shady_parser/shady.y:22:错误:ISO C++ 禁止声明没有类型的“ast_node”。/shady_parser/shady.y:22:错误:预期的“;” 在'*'标记之前./shady_parser/shady.l:在函数'int cyylex()'中:./shady_parser/shady.l:35:错误:'union YYSTYPE'没有名为'node'的成员。/shady_parser/shady .l:37: 错误:'union YYSTYPE' 没有名为'node' 的成员 ./shady_parser/shady.l:38: 错误:'union YYSTYPE' 没有名为'node' 的成员
为什么会发生这种情况?
那么是否可以将 ast_node 定义为一个类并使用指向它的指针而不是指向结构的指针?
在此先感谢,杰克