gpt4 book ai didi

c - yacc在reduction中丢失值

转载 作者:太空宇宙 更新时间:2023-11-04 02:01:42 24 4
gpt4 key购买 nike

我正在研究此语法以构建用于类型检查或类似功能的 SDD。我昨天花了很多时间研究数据结构和解析操作,但我总是遇到段错误。在我看来,YACC(bison) 在减少过程中正在失去值(value)。

因此我决定用更简单的操作构建一个更简单的语法。似乎值(value)在一次又一次的减少中丢失了,或者我做错了什么?词法分析器部分与本示例无关,因此我将其省略。

遵循语法及其操作以及结果与预期结果..

D:   T VAR SEMICOLON D              {
printf("processing D -> T var ; D\n");
printf("\tvalue of T is %f\n", $1);
}
|/*empty*/ {
printf("processing D -> empty\n");
}
;

T: B {
printf("processing B inside T\n");
printf("\tvalue of B is %f\n", $1);
}

C { printf("processing C inside T\n");
printf("processing T-> B C\n");
printf("\tvalue of B is %f\n", $1);
printf("\tvalue of C is %f\n", $<dbl>2);
$$ = $1 + $<dbl>2;

}
| RECORD '{' D '}' { printf("processing record { D }\n");}
;

B: INT { printf("processing B -> int\n");
$$ = 1;
}
| FLOAT { printf("processing B -> float\n");
$$ = 1;
}
;

C: /*empty*/ { printf("processing C -> empty\n");
printf("\tsetting C to be equal to 1\n");
$$=1;
}
| LBRACK NUM RBRACK C { int n = $2;
printf("processing C -> [%d] C\n", n);
double d = $4;
printf("\tprevious C value is %f\n", d);
double f = d+ 1;
printf("\tnew value of $$ is %f\n", f);
$$ = f;
}
;

这是像 int [12][3] ciao; 这样的输入的输出

processing B -> int
processing B inside T
value of B is 1.000000
processing C -> empty
setting C to be equal to 1
processing C -> [3] C
previous C value is 1.000000
new value of $$ is 2.000000
processing C -> [12] C
previous C value is 2.000000
new value of $$ is 3.000000
processing C inside T
processing T-> B C
value of B is 1.000000
value of C is 0.000000 (*)
processing D -> empty
processing D -> T var ; D
value of T is 1.000000 (*)

如您所见,在用 * 标记的 C 缩减中值(value)丢失了,我希望它长大,如下所示

processing B -> int
processing B inside T
value of B is 1.000000
processing C -> empty
setting C to be equal to 1
processing C -> [3] C
previous C value is 1.000000
new value of $$ is 2.000000
processing C -> [12] C
previous C value is 2.000000
new value of $$ is 3.000000
processing C inside T
processing T-> B C
value of B is 1.000000
value of C is 3.000000
processing D -> empty
processing D -> T var ; D
value of T is 4.000000

感谢任何提示以及达到范围的解释和建议,我是否遗漏了什么?

最佳答案

T 的产生式如下,已大大简化。

T: B { /* Mid Rule Action (MRA) */ } C { $$ = $1 + $2; }

T 的最终操作中,$2 指的是 MRA,因为 MRA 被计算在生产条款中。 (实际上,MRA 被替换为具有空 RHS 的非终端。)所以 C$3

由于 MRA 实际上没有设置值,$2 有点不确定,但 0 也不太可能。

Bison 手册引用:

Using Mid-Rule Actions:

The mid-rule action itself counts as one of the components of the rule. This makes a difference when there is another action later in the same rule (and usually there is another at the end): you have to count the actions along with the symbols when working out which number n to use in $n.

Mid-Rule Action Translation:指出“中间规则 Action 实际上转化为常规规则和 Action ”,然后提供了一些生成的空规则示例(及其内部名称,有助于理解 bison 调试输出。)

关于c - yacc在reduction中丢失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26571972/

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