gpt4 book ai didi

error-handling - 故意不通过yacc规则

转载 作者:行者123 更新时间:2023-12-03 07:48:04 25 4
gpt4 key购买 nike

在yacc/bison语法中,有没有一种方法可以消除规则中的规则
总共,但没有错误(以及对yyerror的调用),因此其他选项仍会
有效(类似于flex中的“拒绝”)?

假设“num”具有整数值,则以下规则将定义
奇数和偶数产生的语法

rule :   num { if (($1 & 1) == 1) reject; } a b c 
| num { if (($1 & 1) == 0) reject; } d e ;

谢谢

最佳答案

您可以使用中间规则操作将 token 推送到解析堆栈,然后可以通过另一个规则对其进行评估,这将使您能够对解析结果进行程序选择。

http://www.gnu.org/software/bison/manual/html_node/Mid_002dRule-Actions.html

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.

The mid-rule action can also have a semantic value. The action can set its value with an assignment to $$, and actions later in the rule can refer to the value using $n. Since there is no symbol to name the action, there is no way to declare a data type for the value in advance, so you must use the ‘$<...>n’ construct to specify a data type each time you refer to this value.



因此,有可能这样做,但方式可能与flex完全不同。这样做可能有点骇人听闻,因为您实际上所做的是破坏语法的上下文无关性。

关于error-handling - 故意不通过yacc规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10601611/

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