1
votes

I am making a calculator with the sum and subtraction operations

this is my code...

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(); }

And this is the error Calc.y:16.7-15: syntax error, unexpected identifier, expecting type

1

1 Answers

1
votes

The syntax of the %type directive is

%type <TAG> NONTERMINAL...

where TAG is one of the names declared in your %union directive. In your case, there is only one such name, so I assume you meant:

%type <dval> Expresion

You'll also have to declare that NUMERO has type <dval>; otherwise, bison will complain about this production:

Expresion:  NUMERO { $$=$1; }

because $1 is only meaningful if the object which it represents has a value, and once you declare a %union, the only terminals and non-terminals which have values are the ones for which you provide a type. So you should really specify:

%token <dval> NUMERO

For more information, see sections 3.8.4, 3.8.2, and 3.8.5 in the bison manual.