为什么 ANTLR 要求标记所有替代品或不标注替代品?

编程入门 行业动态 更新时间:2024-10-14 14:20:52
本文介绍了为什么 ANTLR 要求标记所有替代品或不标注替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-20 06:59:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/980013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:替代品   或不   标记   ANTLR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!