0

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.

Community
  • 1
  • 1
Te33Xaz
  • 23
  • 5
  • Ok, user types something like array [][]=new array[5][5].User declares two dimension array,and I want to put all array members in symbol table.Two nested for loops handle this nicely and I have support for two dimension arrays.(e.x.method in interpreter that cycles through indexes that I parsed and creates variables)But if user decides to declare n-dimension arrays, I can't nest for loops forever, so is my option to use variable arguments as in first link in example,or something else.Maybe better if I asked how to implement multidimensional arrays or along these lines? – Te33Xaz Aug 10 '11 at 20:52
  • Or, even simpler explanation,if someone removed arrays from java/c and I have to put it back, only solution I see so far is to parse indexes, and use code similar to first link to create all member variables. I'm wondering if there is simpler way to include support of arrays in a typical structural language ? – Te33Xaz Aug 10 '11 at 21:09

1 Answers1

0

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465