4 美元从哪里来?

发布于 2024-11-25 23:28:40 字数 430 浏览 0 评论 0原文

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘落散花 2024-12-02 23:28:40

嵌入操作(出现在规则中间的代码 { 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.)

找回味觉 2024-12-02 23:28:40

在幕后,yacc 仅真正支持生产结束时的操作。因此,当您交错操作时 { PL_parser->expect = XSTATE;在产生式的中间,yacc(或您正在使用的任何后代)提取操作并将其粘贴在空规则的末尾,如下所示:(

grammar: GRAMPROG $1 remember stmtmseq
    {
        newPROG(block_end($3, $4));
        $ = 0;
    }

$1:
   {
       PL_parser->expect = XSTATE;
   }

如果您的 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:

grammar: GRAMPROG $1 remember stmtmseq
    {
        newPROG(block_end($3, $4));
        $ = 0;
    }

$1:
   {
       PL_parser->expect = XSTATE;
   }

(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, the grammar rule could have accessed the value as $2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文