Bison: where is the last $$ value stored in pure-push parser?

懵懂的女人 提交于 2019-12-11 10:18:05

问题


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 $$.



来源:https://stackoverflow.com/questions/48254587/bison-where-is-the-last-value-stored-in-pure-push-parser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!