野牛,@ 1和$ 1之间的差

问题描述 投票:3回答:1

我正在为Pascal编译器编程,并且已经有了有效的语法。现在,我想从语义分析开始,但实际上不了解它在bison中是如何工作的。我以某种方式通过尝试每个选项编写了一段工作代码,但我想知道@ 1和$ 1之间有什么区别。

uint_values_split_by_comma:
        UINT {
            pas::ic_label label = ctx->tab->new_label();
            ctx->tab->add_label_entry(@1, $1, label);
        }
        | uint_values_split_by_comma COMMA UINT {}
;

也看到了bison文档,但仍然不明白。

bison flex-lexer
1个回答
0
投票

[写入semantic action时,$nsemantic value@nlocation

这里是Bison documentation的示例:

expr: expr '+' expr   { $$ = $1 + $3; } ;

[$1表示左表达式的值,$3表示右表达式的值。

关于位置,在documentation中,我们可以阅读:

像语义值一样,可以使用一组专用的构造在操作中到达位置。在上面的示例中,整个分组的位置为@ $,而子表达式的位置为@ 1和@ 3。

以下是语义位置用法的示例:

exp:
  …
| exp '/' exp
    {
      @$.first_column = @1.first_column;
      @$.first_line = @1.first_line;
      @$.last_column = @3.last_column;
      @$.last_line = @3.last_line;
      if ($3)
        $$ = $1 / $3;
      else
        {
          $$ = 1;
          fprintf (stderr, "%d.%d-%d.%d: division by zero",
                   @3.first_line, @3.first_column,
                   @3.last_line, @3.last_column);
        }
    }

在此示例中,使用位置允许打印详细的错误消息以及错误的确切位置。

© www.soinside.com 2019 - 2024. All rights reserved.