在Bison中使用$ n表示n

编程入门 行业动态 更新时间:2024-10-11 15:21:52
Bison中使用$ n表示n <0(Using $n for n < 0 in Bison)

在Bison中有没有办法检查当前的令牌堆栈大小?

我想使用$ nn作为负数来访问另一个规则的语义值,但前提是堆栈足够大。

谢谢。

Is there a way in Bison to check the current token stack size?

I'd like to use $n with n as a negative number to access a semantic value of another rule but only if the stack is large enough.

Thank you.

最满意答案

给出如下规则:

stmt: ID '=' DIGIT { $$ = $3; } ;

生成的代码片段是:

{ (yyval) = (yyvsp[(3) - (3)]); }

因此, yyvsp '数组'是答案的一部分。 进一步向上(常规 - 而不是GLR)生成的代码,你发现yyvsp实际上是一个指针,而不是一个数组。 例如:

yyvsp = yyvs + yysize - 1;

看起来yysize是你想要的价值; 不幸的是,它是一个块的本地变量,它在执行用户操作之前已经终止,因此它不能直接使用。 但是,您也可以找到以下代码:

yyvsp = yyvs;

看看代码,似乎yyvs是指向堆栈基础的指针(可以动态分配),而yyvsp是指向堆栈的指针。 这些变量在整个yyparse()函数中都是可见的yyparse()因此,特别是在用户操作中可见。并且您需要的答案是:

int nrules = yyvsp - yyvs;

点击这样的源代码并不是远程清理,但它确实能为您提供答案。

如果使用GLR语法,则必须检查Bison GLR语法是否具有相同含义的相同变量; 它可能不会。

Given a rule such as:

stmt: ID '=' DIGIT { $$ = $3; } ;

The generated code fragment is:

{ (yyval) = (yyvsp[(3) - (3)]); }

The yyvsp 'array', therefore, is part of the answer. Further up the (regular - not the GLR) generated code, you find that yyvsp is actually a pointer, not an array. For example:

yyvsp = yyvs + yysize - 1;

It looks like yysize is the value you want; unfortunately, though, it is a variable local to a block which has terminated before the user actions are executed, so it is not directly available. However, you can also find code with:

yyvsp = yyvs;

Looking at the code, it seems that yyvs is a pointer to the base of the stack (which can be dynamically allocated), and yyvsp is a pointer part way up the stack. Those variables are both visible throughout the yyparse() function (and therefore, in particular, are visible within user actions. And the answer you need is:

int nrules = yyvsp - yyvs;

It isn't remotely clean to poke at the source code like this, but it does get you an answer.

If you use a GLR grammar, you would have to examine whether a Bison GLR grammar has the same variables with the same meanings; it may not.

更多推荐

本文发布于:2023-08-07 16:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Bison

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!