Bison: where is the last $$ value stored in pure-p

2019-08-02 17:10发布

问题:

I wrote a parser definition, which interfaces with python as an extension. I declared the following options, so that Bison generates a reentrant push parser:

%define api.pure full
%define api.push-pull push

In order to pass semantic values to python through it's C API, I declared the YYSTYPE to be PyObject* as follows:

%define api.value.type {PyObject*}

Now, the last ("accepting") rule has the following action:

full :  declarations data
  { $$ = Py_BuildValue("(s, O, O)", "full", $1, $2); }
;

The Py_BuildValue function here will create a tuple object containing a string and two other objects of an arbitrary type, and return a PyObject* pointer to it.

Given that I call the parser in the following way:

token_index = 0;
yypstate* ps = yypstate_new();

do{
    token_id = get_token_id(token_index);
    semval = semvals[token_index];
    state = yypush_parse(ps, token_id, &semval);
    token_index += 1;
} while (state == YYPUSH_MORE);

... how can I access the value generated by the full rule (i.e. its $$)?

I tried yylval and yyval, and it seems like they are not defined. I suspected that the pointer to the value may be stored somewhere in ps structure, but I can't find any documentation on it.

回答1:

The stack is deallocated at the end of the parse, so whatever was pushed as $$ will vanish. If you want to return a value you should provide an extra argument to the push parser which points to the place to store the result, rather than assigning it to $$.



标签: c bison