Bison 语法错误,意外标识符,应为类型。雅克。野牛

Bison 语法错误,意外标识符,应为类型。雅克。野牛,bison,yacc,Bison,Yacc,我正在做一个带和和减法运算的计算器 这是我的密码 Calc.y %{ #include %} %union{ double dval; } %token NUMERO %token SUMA RESTA %token ABRIR CERRAR %token END %left SUMA RESTA %left NEG %type Expresion %start Input %% Input: Line | Input Line ; Line: E

我正在做一个带和和减法运算的计算器

这是我的密码

Calc.y    
%{
#include
%}

%union{
    double dval;
}

%token NUMERO
%token SUMA RESTA
%token ABRIR CERRAR
%token END
%left SUMA RESTA
%left NEG

%type Expresion
%start Input

%%
Input:  Line
    | Input Line
    ;

Line:   END
    | Expresion END
        {
            printf("Resultado: %f\n",$1);
        }
    ;

Expresion:  NUMERO { $$=$1; }
        | Expresion SUMA Expresion { $$=$1+$3; }
        | Expresion RESTA Expresion { $$=$1-$3; }
        | RESTA Expresion %prec NEG { $$=-$2; }
        | ABRIR Expresion CERRAR { $$=$2; }
        ;
%%

int yyerror(char *s) { printf("%s\n",s); }
int main(void) { yyparse(); }
这就是错误所在
计算y:16.7-15:语法错误、意外标识符、预期类型指令
%type
的语法为

%type <TAG> NONTERMINAL...
您还必须声明
NUMERO
具有类型
;否则,bison将对该产品提出投诉:

Expresion:  NUMERO { $$=$1; }
因为
$1
只有在它所表示的对象具有值时才有意义,并且一旦您声明了
%union
,只有您为其提供类型的端子和非端子才具有值。因此,您应该真正指定:

%token <dval> NUMERO
%token NUMERO
有关更多信息,请参阅中的第3.8.4节、第3.8.2节和第3.8.5节

%token <dval> NUMERO