【问题标题】:‘YYSTYPE’ has no member named '--' for union type“YYSTYPE”没有名为“--”的联合类型成员
【发布时间】:2013-11-10 15:54:06
【问题描述】:

我已将 YYSTYPE 联合声明为

%union
{
        char* stringValue;
        union int_double_string* ids;
}

int_double_string 被声明为

union int_double_string
{
        short type;     //0:int 1:double 2:string
        int intValue;
        double doubleValue;
        char* stringValue;
};

一些令牌

%token <stringValue> KEY
%token <int_double_string> VALUE
%token <stringValue> COMMENT    
%type <stringValue> pair
%type <int_double_string> key_expr

但无论我在哪里使用令牌 VALUE,它都会给我这个常见错误。

‘YYSTYPE’ has no member named ‘int_double_string’

pair:
        KEY ws '=' ws VALUE     {
                char S5[15];
                addPair($1, $5);   //Error here and where-ever I use $5 in this function
                ...

虽然我已正确声明,但为什么会这样?我也在我的 lex 文件中使用了这个变量。那里没有显示错误。

lex 文件

{integer}       {
                yylval.ids = malloc(sizeof(union int_double_string));
                yylval.ids->type = 0;
                yylval.ids->intValue = atoi(yytext);
                return VALUE;
        }

我认为这与联合内部的联合概念有关。

怎么办?

【问题讨论】:

    标签: c parsing yacc lex


    【解决方案1】:
    ‘YYSTYPE’ has no member named ‘int_double_string’
    

    %type &lt;id&gt;%token &lt;id&gt; 中的 id 需要是 yyunion 中的字段。

    所以,定义为 int_double_string 类型的标记需要是类型 ids

    %token <int_double_string> VALUE
    %type <int_double_string> key_expr
    

    喜欢这个

    %token <ids> VALUE
    %type <ids> key_expr
    

    addPair 的第二个参数应该是 union int_double_string*

    在典型的 yacc 用法中,您将放置所有这些字段:

    short type;     //0:int 1:double 2:string
    int intValue;
    double doubleValue;
    char *stringVal;
    

    进入 yyunion 本身,并且在 yyunion 中没有 union 字段。我不是说你不能,但这是不寻常的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-28
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 2018-10-01
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多