gpt4 book ai didi

Bison ,@1 和 $1 之间的差异

转载 作者:行者123 更新时间:2023-12-02 00:48:55 25 4
gpt4 key购买 nike

我正在编写 Pascal 编译器并且已经有了可用的语法。现在我想从语义分析开始,但实际上不明白它在 中是如何工作的野牛 .不知何故,我通过尝试每个选项编写了一段工作代码,我想知道@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 {}
;

还看到了 野牛文档,还是不明白。

最佳答案

在写 semantic action 时, $n引用semantic value@nlocation .

以下是来自 Bison documentation 的示例:

expr: expr '+' expr   { $$ = $1 + $3; } ;
$1引用左边表达式的值和 $3到正确表达式的值。

关于地点,在 documentation ,我们可以读到:

Like semantic values, locations can be reached in actions using a dedicated set of constructs. In the example above, the location of the whole grouping is @$, while the locations of the subexpressions are @1 and @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);
}
}

在此示例中,使用位置允许打印带有错误精确位置的详细错误消息。

关于 Bison ,@1 和 $1 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59225030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com