为什么 ANTLR 要求标记所有替代品或不标注替代品? [英] Why does ANTLR require all or none alternatives be labeled?

查看:30
本文介绍了为什么 ANTLR 要求标记所有替代品或不标注替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ANTLR 的新手.我刚刚发现可以像这样在作品中标记每个替代品:

I'm new to ANTLR. I just discovered that it is possible to label each alternative in a production like so:

foo
    : a # aLabel
    | b # bLabel
    | // ...
    ;

但是,我发现必须标记所有替代方案或不标记替代方案是令人不快的.我最近只需要标记具有 20 多个分支的产品的 2 个替代方案,我最终标记了其他每个# stubLabel.是否有任何理由必须标记全部或不标记?

However, I find it unpleasant that all or none alternatives must be labeled. I needed to label just 2 alternatives of a production with 20+ branches recently, and I ended up labelling each of the others # stubLabel. Is there any reason why all or none have to be labeled?

推荐答案

一旦您添加标签,ANTLR4 将不再为该规则生成上下文类,而是为每个 alt 生成单独的上下文类.这不能混合(例如,具有整个规则的上下文,同时仅具有某些 alt 的上下文).一旦您开始使用标签并且不再生成规则上下文,您就必须为所有替代项生成上下文,否则会丢失某些内容.

As soon as you add a label ANTLR4 will no longer generate a context class for that rule but instead individual context classes for each alt. This cannot be mixed (e.g. having a context for the entire rule and at the same time contexts for only some of the alts). Once you start using labels and the rule context is no longer generated you have to generate contexts for all alts or something would be missing.

这篇关于为什么 ANTLR 要求标记所有替代品或不标注替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆