野牛:纯推式解析器中存储的最后$$值在哪里? [英] Bison: where is the last $$ value stored in pure-push parser?

查看:77
本文介绍了野牛:纯推式解析器中存储的最后$$值在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个解析器定义,该定义以python为扩展接口.我声明了以下选项,以便Bison生成可重入的推式解析器:

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

为了通过其C API将语义值传递给python,我将YYSTYPE声明为PyObject*,如下所示:

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

此处的Py_BuildValue函数将创建一个包含字符串和两个其他任意类型对象的元组对象,并返回指向它的PyObject*指针.

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

...如何访问由full规则生成的值(即其$$)?

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

我尝试了yylvalyyval,似乎未定义它们. 我怀疑指向该值的指针可能存储在ps结构中的某个位置,但是我找不到关于它的任何文档.

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.

推荐答案

在解析的末尾释放了堆栈,因此按$$推送的任何内容都将消失.如果要返回一个值,则应为推式解析器提供一个额外的参数,该参数指向存储结果的位置,而不是将其分配给$$.

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

这篇关于野牛:纯推式解析器中存储的最后$$值在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆