野牛意外标识符错误

bison unexpected identifier error

我收到一个 "unexpected identifier error",在 1-9,在线

string_op   | string_lit                { $$ = ; }

这是我的语法

%union 
{
    int intval;
    double dubval;
    char* strval;   
    obj object;
}

%token <intval> INTEGER 
%token <dubval> DOUBLE 
%token <strval> STRING_LITERAL

%type <object> number factor value term constant string_lit string_op element

%%
number      : INTEGER               { $$.type = 0;
                                      $$.ival = ; }
            | DOUBLE                { $$.type = 1;
                                      $$.dval = ; }
            ;

factor      : number                { $$ = ; }
            | '(' constant ')'      { $$ = ; } 
            ;

value       : factor                { $$ = ; }
            | value '^' factor      { doMath(&$$, , , '^'); }   
            ;

term        : value                 { $$ = ; }
            | term '*' value        { doMath(&$$, , , '*'); }
            | term '/' value        { doMath(&$$, , , '/'); }
            | term '%' value        { doMath(&$$, , , '%'); }
            ;

constant    : term                  { $$ = ; }
            | constant '+' term     { doMath(&$$, , , '+'); }
            | constant '-' term     { doMath(&$$, , , '-'); }
            ;

string_lit  : STRING LITERAL        { $$.type = 2; 
                                      $$.string = malloc(sizeof(strlen())+1);
                                      memcpy($$.string, , sizeof());        }
            ;

string_op   | string_lit                { $$ = ; }
            | string_op '+' string_lit  { concat(&$$, , ); }
            ;

variable    : string_op                 { $$ = ; }
            | constant                  { $$ = ; }
            | variable '+' string_op    { doMath(&$$, , , '+') };
            | variable '+' constant     { doMath(&$$, , , '+') };
            | variable '-' string_op    { doMath(&$$, , , '-') };
            | variable '-' constant     { doMath(&$$, , , '-') };
            | variable '*' string_op    { doMath(&$$, , , '*') };
            | variable '*' constant     { doMath(&$$, , , '*') };
            | variable '/' string_op    { doMath(&$$, , , '/') };
            | variable '/' constant     { doMath(&$$, , , '/') };
            | variable '%' string_op    { doMath(&$$, , , '%') };
            | variable '%' constant     { doMath(&$$, , , '%') };
            | variable '^' string_op    { doMath(&$$, , , '^') };
            | variable '^' constant     { doMath(&$$, , , '^') };
            | variable '>' string_op    { doMath(&$$, , , '>') };
            | variable '>' constant     { doMath(&$$, , , '>') };
            | variable '<' string_op    { doMath(&$$, , , '<') };
            | variable '<' constant     { doMath(&$$, , , '<') };
            | variable GT_EQ string_op  { doMath(&$$, , , '.') }; //>=
            | variable GT_EQ constant   { doMath(&$$, , , '.') }; 
            | variable LT_EQ string_op  { doMath(&$$, , , ',') }; //<=
            | variable LT_EQ constant   { doMath(&$$, , , ',') };
            ;
%%

我已经在“%type”行中声明了 "string_op"。我真的不知道该怎么做。

错误信息应该是 "Unexpected |",因为 string_op 之后的预期是 :.

bison 产生奇怪错误消息的原因是 bison lexer 将标识符与以下 : 组合成一个类型为 id_colon 而不是 identifier。它这样做是因为 ; 在规则的末尾是可选的,因此如果 : 是自己的令牌。 (讽刺的是,真的。)所以它期待一个 id_colon 并且意外地找到了一个 identifier.