简体   繁体   中英

Java parser, dynamically declared n-dimensional array of arrays

I am currently trying to write a parser in Java for my program. I want the program to be able to call any function by name during runtime, as specified by the user. So the example syntax would look something like:

foo{boo{sth1,moo{sth2,sth3,sth4}},sth5},zoo{voo{sth6}}

where foo,boo,moo and etc are functions and sths are variables. Everything between the { and } is the parameter list for a function to call. So in this example moo is called with 3 variables as parameters and then passes it's return value as a parameter to the boo function(along with 2 other variables). The user should be able to nest this functionality an infinite number of times.

I've already written the function to call other functions by name, now I just need the parser to divide the input string properly. I wanted to do this by dividing the input string to "branches" of smaller strings. Each function would have it's own "branch" of strings which represent it's parameter's names. Then every branch would be attached to a higher branch and so on. So for example the branch of the moo function would consist of three strings "sth2","sth3","sth4" and be attached to the higher branch of the boo function, which would contain "sth1",String[] mooBranch. Also I want the Parse function to be a recursive function and return a branch of Strings.

Now, the only problem I have with this is that this requires dynamic declaration of n-dimensional array of arrays.In C++ you could use pointers for this, but Java has no pointer type. I do realise that every non-primitive type works kind of like a pointer to an object in Java, but something like this doesn't seem to work:

String[] branch = new String[x];
branch[0] = new String[y]; //Type mismatch: cannot convert from String[] to String

also please note that due to the recursive nature of my function I can't simply do something like

String[][] branch = new String[x][];
branch[0] = new String[y];

because it would only be enough for nesting twice(or a finite ammount of time with more []), and the user should be able to nest the functionality as many times as he wants.

You have to use a tree structure from what I understand, not a simple String array.

Example :

class Node {
    // Case of a parameter
    String parameterName;
    // Case of a function call
    String functionName;
    Node[] functionParameters;
}

You'll have to enhance this quick model to adjust your needs of course.

If you want to remain on the same path you are currently working on and, if you mean something like the old VB Redim Preserve() method which allows you to dynamically add to an array then you could try this hack which does work:

public static String[] RedimPreserve(String[] yourArray, int newSize) {
    String[] tmp = new String[newSize];
    if (yourArray.length != 0) {
        System.arraycopy(yourArray, 0, tmp, 0, Math.min(yourArray.length, tmp.length));
    }
    return tmp; 
}

And an example of how you could use it might be:

String[] myArray = {};   // Declared empty string array.

// Fill myArray[] with ASCII characters from ASCII 65 to ASCII 126...
int counter = 0;
for (int i = 65; i <= 126; i++) {
    myArray = RedimPreserve(myArray, counter + 1);
    myArray[counter] = Character.toString ((char) i);
    counter++;
}

// Output the new size of myArray[]...
System.out.println(myArray.length);

//Display myArray[] and see its elemental contents...
for (int i = 0; i < myArray.length; i++) {
    System.out.println(myArray[i]);
}

This is why ArrayLists are so nice. You don't need to go through this sort of thing. You can dynamically add to an ArrayList.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM