ANTLR 和数组
我有关于使用 Java+ANTLR 组合实现数组的问题。 (我主要说的是java/c风格的数组)。
所以基本上我问的是如何实现这样的功能,如果已经有这样的例子,或者是否有人可以向我指出任何可能解决它的问题。
另一方面,我已经搜索了一些可能的解决方案。我看到的主要问题 用户可以创建各种维度的数组,如果他或她想要的话甚至可以疯狂(比如创建 5 维数组或更糟)。
虽然类似这样的语法相当简单,但
new ID (INT (',' INT)* )
后端确实涉及了一些。正如我所说,用户可以输入任意数量的维度,因此应该动态创建数组维度。 (至少在我看来,也许我把事情变得过于复杂了?)
搜索后,我确实发现了一些几乎可以完美解决这个问题的东西,这里是问题的链接:
当然可以,我的问题是,这是可行的例子,它有点(至少可以说)复杂?有更优雅的解决方案吗?
考虑到这一点,我想也许答案可能是以某种方式改变多维度的 变成更线性的结构?类似的东西有用吗?在stackoverflow上简单搜索一下就找到了很多解决方案 对此,例如:
值得朝那个方向搜索吗?
现在,最后,考虑到数组在许多语言中确实是常见的功能,我必须感到惊讶的是,在搜索 ANTLR 邮件列表后没有类似的问题,正如我之前所说,这让我相信我可能使事情过于复杂化? (除非我真的不擅长搜索?)我真的很感激反馈。
I have question relating to implementation of arrays with Java+ANTLR combo. (I'm mainly talking about java/c style arrays).
So basically I'm asking how do you implement such feature, if there is such example already available or if someone could point me to anything that may point to solve it.
On other hand, I've searched a bit how would possible solution be. Main problem that I see
is that user may create arrays of various dimensions, even go crazy if he or she wants (like creating 5 dimension arrays or worse).
While grammar for something like this is fairly simple, like
new ID (INT (',' INT)* )
back end really gets involved a bit. As I said, user may input any number of dimensions, so array dimensions should be dynamically created. (at least as I see it, maybe I'm over complicating things?)
After searching I did found something that pretty much solves this problem perfectly, here is link to the question:
Is it possible to dynamically build a multi-dimensional array in Java?
Of course, my question is, is this viable example, it is a bit (to say at least), complicated? Is there more elegant solution to it?
Having that in mind, I was thinking maybe answer might be in the grounds of somehow transforming multidimensions
into more linear structure ? Could something like that be useful ? Simple search on stackoverflow pointed many solutions
to this, like:
Algorithm to convert a multi-dimensional array to a one-dimensional array
Would it be worth to search in that direction ?
Now, at the end, having in mind that arrays are really common feature in many languages, I must find it surprising that after searching ANTLR mailing list there is no similar question, which as I previously said leads me to believe that I'm maybe over complicating things ? (Unless I really suck at search?) I would really appreciate feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话,你的语法对应于
new char 4,5,6,7
之类的东西,这有点奇怪。我希望您的意思确实是
new char[4,5,6,7]
但是从纯粹语法的角度来看,没有理由不将索引存储在数组中并让语义分析通过担心它。
Your syntax, if I'm not mistaken, corresponds to something like
new char 4,5,6,7
which is kind of strange. I expect that you really meant
new char[4,5,6,7]
However from a purely syntactic point of view, there's no reason not to just store the indices in an array and let the semantic analysis pass worry about it.