4 美元从哪里来?
这是 Perl 的第一条规则:
grammar : GRAMPROG
{
PL_parser->expect = XSTATE;
}
remember stmtseq
{
newPROG(block_end($3,$4));
$$ = 0;
}
$4当右侧只有
可以工作吗?3
元素时
This is in the first rule of Perl:
grammar : GRAMPROG
{
PL_parser->expect = XSTATE;
}
remember stmtseq
{
newPROG(block_end($3,$4));
$ = 0;
}
How can $4
work when there're only 3
elements on the right side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嵌入操作(出现在规则中间的代码
{ PL_parser->expect = XSTATE; }
)算作一个元素。所以有4个元素。 $1 是终结符 GRAMPROG,$2 是嵌入操作,$3 是非终结符 remember,$4 是非终结符 stmtseq。 ($2 的值是在嵌入操作中分配给 $$ 的任何值。目前它是垃圾。)An embedded action (the code
{ PL_parser->expect = XSTATE; }
which occurs in the middle of the rule) counts as an element. So there are 4 elements. $1 is the terminal GRAMPROG, $2 is the embedded action, $3 is the nonterminal remember, and $4 is the nonterminal stmtseq. (The value of $2 is whatever value is assigned to $$ inside the embedded action. Currently it would be garbage.)在幕后,yacc 仅真正支持生产结束时的操作。因此,当您交错操作时
{ PL_parser->expect = XSTATE;在产生式的中间,yacc(或您正在使用的任何后代)提取操作并将其粘贴在空规则的末尾,如下所示:(
如果您的 yacc 变体支持转储详细语法并且如果这样做,您将看到许多 $$1、$$2 等操作规则。)
在这种情况下,交错操作实际上不会向
$$
分配任何内容,但如果它曾经,grammar
规则可以将值访问为$2
。Under the covers, yacc only really supports actions at the end of a production. So when you interleave an action
{ PL_parser->expect = XSTATE; }
in the middle of a production, yacc (or whatever descendent you're using) pulls out the action and sticks it at the end of an empty rule as such:(If your yacc variant support dumping the verbose grammar and you do that, you'll see a lot of $$1, $$2, etc. rules for actions.)
In this case the interleaved action doesn't actually assign anything to
$$
, but if it had, thegrammar
rule could have accessed the value as$2
.