Error trying to use type from GNU GMP library as yylval type for Bison

I am trying to use a type mpz_tfrom the GMP library as a type for yylvalby including the following in a Bison file:

%define api.value.type {mpz_t}

I checked the generated parser and correctly created the string typedef mpz_t YYSTYPE, while YYSTYPEusing this to create yylval.

mpz_tis typdefed as typedef __mpz_struct mpz_t[1];in the GMP header file gmp.h. In turn, __mpz_structis typdefed as

typedef struct
{
    // struct members here - don't believe they're important
} __mpz_struct;

Bison works without errors, but whenever I try to create an executable, I get the following error:

calc.tab.c: In the function "yyparse:

calc.tab.c: 1148: 12: error: incompatible types when assigning type YYSTYPE from type 'Struct __mpz_struct *

* ++ yyvsp = yylval;

yyvsp YYSTYPE.

, ?

+4
1

, mpz_t typedef'ed :

typedef __mpz_struct mpz_t[1];

mpz_t :

mpz_t a, b;
mpz_init(b);
a = b;  /* Error: incompatible types when assigning to type ‘mpz_t’ */
        /* from type ‘struct __mpz_struct *’                        */

:

mpz_t a, b;
mpz_inits(a, b, 0);
mpz_set(a, b);   /* a is now a copy of b */

mpz_t - , gmp . . 1 .

Bison , YYSTYPE (. 2), , . , YYSTYPE , . , bison , %union.

gmp, , , . , , , , gmp ( , free - mpz_t)..

mpz_t , . , , ; , mpz_clear ed, .

( ) , mpz_t. bignum, . , .

mpz_t, , :

  • , (. 2). , , , :

    mpz_add(val_stack[top - 2], val_stack[top - 2], val_stack[top - 1]);
    --top;
    
  • , . .

  • , , , . , , .

1. GMP

gmp mpz_t ( ) 1 C- . , pass-by-reference . , , - , mpz_t. - , gmp .

Gmp . (, , .) , :

  • . , .

  • , . .

, , Java ++ . , :

  • . , . , , , .

  • . ++, .

.

  • , . Java Python; Java StringBuilder . ; , (sum += value;), sum .

  • , . , - .

Gmp . Bignums , C , , .

, - , - gmp . , gmp, , , union memcpy(), gmp , :

mpz_t.

, gmp bignum, realloc. , a b mpz_t, , , :

memcpy(a, b, sizeof(a));

b :

mpz_mul(b, b, b);  /* Set b to b squared */

, -

tmp = realloc(b->_mp_d, 2 * b->_mp_size);
if (tmp) b->_mp_d = tmp;

b , . b, , a, , realloc, , .

, b; . a , b: mpz_add(b, tmp1, tmp2); ( tmp1 / tmp2 , b.)

2. ,

Bison YYSTYPE ; , $$ . $$ = $1;. , , $1 $n , $$. , $1 $$, . ( $$ $1.)

+5

Source: https://habr.com/ru/post/1606902/


All Articles