在Bison中有没有办法检查当前的令牌堆栈大小?
我想使用$ n和n作为负数来访问另一个规则的语义值,但前提是堆栈足够大。
谢谢。
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.
更多推荐
发布评论