[英]Bison grammar rule to match a group of tokens
Is there a way to match multiple tokens at once? 有没有办法一次匹配多个令牌? Right now one of my rules looks like this:
现在,我的规则之一如下所示:
rule:
T_A { $$ = some_common_behavior(); }
| T_B { $$ = some_common_behavior(); }
| T_C { $$ = some_common_behavior(); }
... // many more tokens
But it would be far more maintainable if a group of tokens could be matched at once: 但是,如果一组令牌可以一次匹配,它将更具可维护性:
rule:
T_A | T_B | T_C | ... { $$ = some_common_behavior(); }
PS: the some_common_behavior
call already handles current token, hence there is no need to repeat code for each token like in the first example. PS:
some_common_behavior
调用已经可以处理当前令牌,因此不需要像第一个示例中那样为每个令牌重复代码。 I just need a better way to declare this without all the repetition. 我只需要一个更好的方法来声明这一点,而无需所有重复。
Can't you just group the T_*
s into an expression and then use that expression in your rule
? 您不能只是将
T_*
分组为一个表达式,然后在您的rule
使用该表达式吗?
rule:
Ts { $$ = some_common_behaviour(); }
;
Ts:
T_A
| T_B
| T_C
| ...;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.