0

这是我的 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) {...}; 

我收到一个错误:未知类型!如果能告诉我如何使这个结构对我的整个解析器可见,我将不胜感激。

4

1 回答 1

0

发生此错误通常是因为您拥有联合、令牌和类型:

%union {
    char *id;    
    long long integer;  
    struct Id Identifier;
    struct Commd Command;
    struct Commds *Commands;         
}
....
%type <Command> command
%type <Commands> commands

{% %}在大括号中的 C 代码之前。你需要把它放在{% %}. 基本上你是说你的终端或非终端有 type struct Commd,但 yacc 不知道是什么struct Commd,因为你将它包含在联合代码下。

你没有写完整的代码,所以我只能假设你已经完成了。如果是这样,这就是你的答案。

于 2016-01-21T18:27:48.917 回答