antlr2 返回多个值 [英] antlr2 return multiple values
问题描述
antlr2中如何让一个规则返回多个值.例如:
How to make a rule return multiple values in antlr2.For example:
declSpecifiers returns [int mods]
: ( storageClassSpecifier
| typeQualifier
| typeSpecifier)+
;
除了mods"之外,我还有一些其他信息要返回.我该怎么办?
I have some other information besides 'mods' to return.What should I do?
推荐答案
在 ANTLR v2.x 中,您只能返回单个值,这与 ANTLR v3.x 不同,在 ANTLR v3.x 中,多个返回值由包含多个返回的容器自动包装价值.
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 */ }
)+
;
我想您知道 ANTLR v2 已经很老了:如果可以,请迁移到 v3(我知道这并不总是一种选择……但仍然如此).
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).
这篇关于antlr2 返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!