这是我的 yacc file.y 顶部
%code requires {
struct Id {
char *var;
};
struct Commds;
struct Commd {
struct Id lhs;
};
struct Commds {
struct Commd commd;
struct Commds *next;
};
}
我在我的代码中使用了这段代码%union
来为解析器定义新类型。
%union {
char *id;
long long integer;
struct Id Identifier;
struct Commd Command;
struct Commds *Commands;
}
....
%type <Command> command
%type <Commands> commands
$$
在从我的词法分析器评估标记的同时构建解析树时,我可以将它与 -dollars 属性一起使用。不幸的是,我想在%{ codes %}
部分的其他方法中使用在文件开头定义的结构。不幸的是,每当我这样定义函数时:
void add(struct Commd cmd) {...};
我收到一个错误:未知类型!如果能告诉我如何使这个结构对我的整个解析器可见,我将不胜感激。