antlr2 返回多个值

发布于 2024-12-21 03:19:15 字数 232 浏览 3 评论 0原文

如何让antlr2中的规则返回多个值。例如:

declSpecifiers returns [int mods]
        : ( storageClassSpecifier    
        | typeQualifier       
        | typeSpecifier)+
        ;

我除了'mods'之外还有一些其他信息要返回。我该怎么办?

How to make a rule return multiple values in antlr2.For example:

declSpecifiers returns [int mods]
        : ( storageClassSpecifier    
        | typeQualifier       
        | typeSpecifier)+
        ;

I have some other information besides 'mods' to return.What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2024-12-28 03:19:15

在 ANTLR v3.x 中,您可以通过将多个返回值列在括号中来包含它们。

declSpecifiers returns [int mods, Object otherInfo]
        : ( storageClassSpecifier    
        | typeQualifier       
        | typeSpecifier)+
        ;

生成的代码将使用您提供的名称返回一个生成的类,其中包含所有返回值的字段。

In ANTLR v3.x, you can include multiple return values by listing them in the brackets.

declSpecifiers returns [int mods, Object otherInfo]
        : ( storageClassSpecifier    
        | typeQualifier       
        | typeSpecifier)+
        ;

The generated code will return a generated class containing fields for all the return values, using the names you've provided.

祁梦 2024-12-28 03:19:15

在 ANTLR v2.x 中,您只能返回单个值,这与 ANTLR v3.x 不同,在 ANTLR v3.x 中,多个返回值会自动由包含多个返回值的容器包装。

您必须返回某种集合或自定义对象:

declSpecifiers returns [Map map]
{map = new HashMap();}
  : ( storageClassSpecifier { /* populate your map here */ }
    | typeQualifier         { /* populate your map here */ }
    | typeSpecifier         { /* populate your map here */ }
    )+
  ;

我想您知道 ANTLR v2 相当旧:如果可以,请迁移到 v3(我知道这并不总是一个选择......但仍然是)。

In ANTLR v2.x, you can only return a single value, unlike ANTLR v3.x where multiple return values are automatically wrapped by a container containing the multiple return values.

You'll have to return some sort of collection, or custom object:

declSpecifiers returns [Map map]
{map = new HashMap();}
  : ( storageClassSpecifier { /* populate your map here */ }
    | typeQualifier         { /* populate your map here */ }
    | typeSpecifier         { /* populate your map here */ }
    )+
  ;

I presume you know ANTLR v2 is rather old: if you can, migrate to v3 (I know that's not always an option... but still).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文