Yacc 错误:字段声明的结构类型不完整

问题描述 投票:0回答:1

我正在使用 Yacc 和 Lex 构建一个解析器,并且我在共享头文件 (shared_vars.h) 中定义了几个结构。我已在主函数内的 C 文件 (shared_vars.c) 中初始化了这些结构。但是,当我尝试在 Yacc 文件(ex.yacc)中使用这些结构时,我遇到了与不完整类型相关的错误。

结构体在shared_vars.h中定义如下:

struct CtlInfo {
    // fields
};

struct Ctl_LobFile_spec {
    // fields
};

struct Ctl_pos_spec {
    // fields
};

struct Ctl_Scalar_spec {
    // fields
};

struct Ctl_filed_list {
    // fields
};

在 Yacc 文件(ex.yacc)中,我包含了必要的头文件并将结构添加到 %union 声明中:

%union {
    int ival;
    float fval;
    char* sval;
    struct CtlInfo ctlinfo;           // Forward declaration in header file
    struct Ctl_LobFile_spec lobfile_spec; // Forward declaration in header file
    struct Ctl_pos_spec pos_spec;     // Forward declaration in header file
    struct Ctl_Scalar_spec scalar_spec; // Forward declaration in header file
    struct Ctl_filed_list *field_list; // Note: This is a pointer to struct Ctl_filed_list
}
%type <pos_spec>

但是,在编译过程中,我收到以下错误:

ex.yacc:229:29: error: field has incomplete type 'struct Ctl_LobFile_spec'
    struct Ctl_LobFile_spec lobfile_spec;
                            ^
ex.yacc:229:12: note: forward declaration of 'struct Ctl_LobFile_spec'
    struct Ctl_LobFile_spec lobfile_spec;
           ^
ex.yacc:230:25: error: field has incomplete type 'struct Ctl_pos_spec'
    struct Ctl_pos_spec pos_spec;
                        ^
ex.yacc:230:12: note: forward declaration of 'struct Ctl_pos_spec'
    struct Ctl_pos_spec pos_spec;
           ^
ex.yacc:231:28: error: field has incomplete type 'struct Ctl_Scalar_spec'
    struct Ctl_Scalar_spec scalar_spec;
                           ^
ex.yacc:231:12: note: forward declaration of 'struct Ctl_Scalar_spec'
    struct Ctl_Scalar_spec scalar_spec;
           ^
3 errors generated.
c struct compiler-errors yacc
1个回答
0
投票

这篇帖子可能会有所帮助

将 %code 指令与“requires”一起使用

%code requires {
    struct CtlInfo {
    // fields
    };
}
...
...
© www.soinside.com 2019 - 2024. All rights reserved.